]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/models/activitypub/actor.ts
Add avatar max size limit
[github/Chocobozzz/PeerTube.git] / server / models / activitypub / actor.ts
CommitLineData
50d6de9c 1import { values } from 'lodash'
47564bbe 2import { extname } from 'path'
fadf619a
C
3import * as Sequelize from 'sequelize'
4import {
e12a0092
C
5 AllowNull, BelongsTo, Column, CreatedAt, DataType, Default, DefaultScope, ForeignKey, HasMany, HasOne, Is, IsUUID, Model, Scopes,
6 Table, UpdatedAt
fadf619a 7} from 'sequelize-typescript'
50d6de9c 8import { ActivityPubActorType } from '../../../shared/models/activitypub'
fadf619a 9import { Avatar } from '../../../shared/models/avatars/avatar.model'
da854ddd 10import { activityPubContextify } from '../../helpers/activitypub'
fadf619a 11import {
da854ddd
C
12 isActorFollowersCountValid, isActorFollowingCountValid, isActorPreferredUsernameValid, isActorPrivateKeyValid,
13 isActorPublicKeyValid
14} from '../../helpers/custom-validators/activitypub/actor'
15import { isActivityPubUrlValid } from '../../helpers/custom-validators/activitypub/misc'
47564bbe 16import { ACTIVITY_PUB_ACTOR_TYPES, CONFIG, CONSTRAINTS_FIELDS } from '../../initializers'
50d6de9c 17import { AccountModel } from '../account/account'
fadf619a
C
18import { AvatarModel } from '../avatar/avatar'
19import { ServerModel } from '../server/server'
20import { throwIfNotValid } from '../utils'
50d6de9c
C
21import { VideoChannelModel } from '../video/video-channel'
22import { ActorFollowModel } from './actor-follow'
fadf619a 23
50d6de9c
C
24enum ScopeNames {
25 FULL = 'FULL'
26}
27
ce33ee01
C
28@DefaultScope({
29 include: [
30 {
31 model: () => ServerModel,
32 required: false
c5911fd3
C
33 },
34 {
35 model: () => AvatarModel,
36 required: false
ce33ee01
C
37 }
38 ]
39})
50d6de9c
C
40@Scopes({
41 [ScopeNames.FULL]: {
42 include: [
43 {
44 model: () => AccountModel,
45 required: false
46 },
47 {
48 model: () => VideoChannelModel,
49 required: false
ce33ee01
C
50 },
51 {
52 model: () => ServerModel,
53 required: false
c5911fd3
C
54 },
55 {
56 model: () => AvatarModel,
57 required: false
50d6de9c
C
58 }
59 ]
60 }
61})
fadf619a 62@Table({
50d6de9c
C
63 tableName: 'actor',
64 indexes: [
65 {
e12a0092 66 fields: [ 'preferredUsername', 'serverId' ],
50d6de9c
C
67 unique: true
68 }
69 ]
fadf619a
C
70})
71export class ActorModel extends Model<ActorModel> {
72
50d6de9c
C
73 @AllowNull(false)
74 @Column(DataType.ENUM(values(ACTIVITY_PUB_ACTOR_TYPES)))
75 type: ActivityPubActorType
76
fadf619a
C
77 @AllowNull(false)
78 @Default(DataType.UUIDV4)
79 @IsUUID(4)
80 @Column(DataType.UUID)
81 uuid: string
82
83 @AllowNull(false)
e12a0092 84 @Is('ActorPreferredUsername', value => throwIfNotValid(value, isActorPreferredUsernameValid, 'actor preferred username'))
fadf619a 85 @Column
e12a0092 86 preferredUsername: string
fadf619a
C
87
88 @AllowNull(false)
89 @Is('ActorUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'url'))
01de67b9 90 @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.URL.max))
fadf619a
C
91 url: string
92
93 @AllowNull(true)
94 @Is('ActorPublicKey', value => throwIfNotValid(value, isActorPublicKeyValid, 'public key'))
01de67b9 95 @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.PUBLIC_KEY.max))
fadf619a
C
96 publicKey: string
97
98 @AllowNull(true)
99 @Is('ActorPublicKey', value => throwIfNotValid(value, isActorPrivateKeyValid, 'private key'))
01de67b9 100 @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.PRIVATE_KEY.max))
fadf619a
C
101 privateKey: string
102
103 @AllowNull(false)
104 @Is('ActorFollowersCount', value => throwIfNotValid(value, isActorFollowersCountValid, 'followers count'))
105 @Column
106 followersCount: number
107
108 @AllowNull(false)
109 @Is('ActorFollowersCount', value => throwIfNotValid(value, isActorFollowingCountValid, 'following count'))
110 @Column
111 followingCount: number
112
113 @AllowNull(false)
114 @Is('ActorInboxUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'inbox url'))
01de67b9 115 @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.URL.max))
fadf619a
C
116 inboxUrl: string
117
118 @AllowNull(false)
119 @Is('ActorOutboxUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'outbox url'))
01de67b9 120 @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.URL.max))
fadf619a
C
121 outboxUrl: string
122
123 @AllowNull(false)
124 @Is('ActorSharedInboxUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'shared inbox url'))
01de67b9 125 @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.URL.max))
fadf619a
C
126 sharedInboxUrl: string
127
128 @AllowNull(false)
129 @Is('ActorFollowersUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'followers url'))
01de67b9 130 @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.URL.max))
fadf619a
C
131 followersUrl: string
132
133 @AllowNull(false)
134 @Is('ActorFollowingUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'following url'))
01de67b9 135 @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.URL.max))
fadf619a
C
136 followingUrl: string
137
138 @CreatedAt
139 createdAt: Date
140
141 @UpdatedAt
142 updatedAt: Date
143
144 @ForeignKey(() => AvatarModel)
145 @Column
146 avatarId: number
147
148 @BelongsTo(() => AvatarModel, {
149 foreignKey: {
150 allowNull: true
151 },
c5911fd3 152 onDelete: 'set null'
fadf619a
C
153 })
154 Avatar: AvatarModel
155
50d6de9c 156 @HasMany(() => ActorFollowModel, {
fadf619a 157 foreignKey: {
50d6de9c 158 name: 'actorId',
fadf619a
C
159 allowNull: false
160 },
161 onDelete: 'cascade'
162 })
50d6de9c 163 AccountFollowing: ActorFollowModel[]
fadf619a 164
50d6de9c 165 @HasMany(() => ActorFollowModel, {
fadf619a 166 foreignKey: {
50d6de9c 167 name: 'targetActorId',
fadf619a
C
168 allowNull: false
169 },
170 as: 'followers',
171 onDelete: 'cascade'
172 })
50d6de9c 173 AccountFollowers: ActorFollowModel[]
fadf619a
C
174
175 @ForeignKey(() => ServerModel)
176 @Column
177 serverId: number
178
179 @BelongsTo(() => ServerModel, {
180 foreignKey: {
181 allowNull: true
182 },
183 onDelete: 'cascade'
184 })
185 Server: ServerModel
186
50d6de9c
C
187 @HasOne(() => AccountModel, {
188 foreignKey: {
189 allowNull: true
190 },
191 onDelete: 'cascade'
192 })
193 Account: AccountModel
194
195 @HasOne(() => VideoChannelModel, {
196 foreignKey: {
197 allowNull: true
198 },
199 onDelete: 'cascade'
200 })
201 VideoChannel: VideoChannelModel
202
203 static load (id: number) {
204 return ActorModel.scope(ScopeNames.FULL).findById(id)
205 }
206
fadf619a
C
207 static listByFollowersUrls (followersUrls: string[], transaction?: Sequelize.Transaction) {
208 const query = {
209 where: {
210 followersUrl: {
211 [ Sequelize.Op.in ]: followersUrls
212 }
213 },
214 transaction
215 }
216
50d6de9c
C
217 return ActorModel.scope(ScopeNames.FULL).findAll(query)
218 }
219
e12a0092 220 static loadLocalByName (preferredUsername: string) {
50d6de9c
C
221 const query = {
222 where: {
e12a0092 223 preferredUsername,
50d6de9c
C
224 serverId: null
225 }
226 }
227
228 return ActorModel.scope(ScopeNames.FULL).findOne(query)
229 }
230
e12a0092 231 static loadByNameAndHost (preferredUsername: string, host: string) {
50d6de9c
C
232 const query = {
233 where: {
e12a0092 234 preferredUsername
50d6de9c
C
235 },
236 include: [
237 {
238 model: ServerModel,
239 required: true,
240 where: {
241 host
242 }
243 }
244 ]
245 }
246
247 return ActorModel.scope(ScopeNames.FULL).findOne(query)
248 }
249
250 static loadByUrl (url: string, transaction?: Sequelize.Transaction) {
251 const query = {
252 where: {
253 url
254 },
255 transaction
256 }
257
258 return ActorModel.scope(ScopeNames.FULL).findOne(query)
fadf619a
C
259 }
260
261 toFormattedJSON () {
262 let avatar: Avatar = null
263 if (this.Avatar) {
c5911fd3 264 avatar = this.Avatar.toFormattedJSON()
fadf619a
C
265 }
266
fadf619a
C
267 let score: number
268 if (this.Server) {
fadf619a
C
269 score = this.Server.score
270 }
271
272 return {
273 id: this.id,
50d6de9c 274 uuid: this.uuid,
e12a0092 275 host: this.getHost(),
fadf619a
C
276 score,
277 followingCount: this.followingCount,
278 followersCount: this.followersCount,
279 avatar
280 }
281 }
282
e12a0092 283 toActivityPubObject (name: string, type: 'Account' | 'Application' | 'VideoChannel') {
fadf619a
C
284 let activityPubType
285 if (type === 'Account') {
50d6de9c
C
286 activityPubType = 'Person' as 'Person'
287 } else if (type === 'Application') {
288 activityPubType = 'Application' as 'Application'
fadf619a 289 } else { // VideoChannel
50d6de9c 290 activityPubType = 'Group' as 'Group'
fadf619a
C
291 }
292
c5911fd3
C
293 let icon = undefined
294 if (this.avatarId) {
295 const extension = extname(this.Avatar.filename)
296 icon = {
297 type: 'Image',
298 mediaType: extension === '.png' ? 'image/png' : 'image/jpeg',
299 url: this.getAvatarUrl()
300 }
301 }
302
fadf619a 303 const json = {
50d6de9c 304 type: activityPubType,
fadf619a
C
305 id: this.url,
306 following: this.getFollowingUrl(),
307 followers: this.getFollowersUrl(),
308 inbox: this.inboxUrl,
309 outbox: this.outboxUrl,
e12a0092 310 preferredUsername: this.preferredUsername,
fadf619a 311 url: this.url,
e12a0092 312 name,
fadf619a
C
313 endpoints: {
314 sharedInbox: this.sharedInboxUrl
315 },
50d6de9c 316 uuid: this.uuid,
fadf619a
C
317 publicKey: {
318 id: this.getPublicKeyUrl(),
319 owner: this.url,
320 publicKeyPem: this.publicKey
c5911fd3
C
321 },
322 icon
fadf619a
C
323 }
324
325 return activityPubContextify(json)
326 }
327
328 getFollowerSharedInboxUrls (t: Sequelize.Transaction) {
329 const query = {
330 attributes: [ 'sharedInboxUrl' ],
331 include: [
332 {
50d6de9c 333 model: ActorFollowModel,
fadf619a
C
334 required: true,
335 as: 'followers',
336 where: {
50d6de9c 337 targetActorId: this.id
fadf619a
C
338 }
339 }
340 ],
341 transaction: t
342 }
343
344 return ActorModel.findAll(query)
345 .then(accounts => accounts.map(a => a.sharedInboxUrl))
346 }
347
348 getFollowingUrl () {
349 return this.url + '/following'
350 }
351
352 getFollowersUrl () {
353 return this.url + '/followers'
354 }
355
356 getPublicKeyUrl () {
357 return this.url + '#main-key'
358 }
359
360 isOwned () {
361 return this.serverId === null
362 }
e12a0092
C
363
364 getWebfingerUrl () {
365 return 'acct:' + this.preferredUsername + '@' + this.getHost()
366 }
367
368 getHost () {
369 return this.Server ? this.Server.host : CONFIG.WEBSERVER.HOST
370 }
c5911fd3
C
371
372 getAvatarUrl () {
373 if (!this.avatarId) return undefined
374
375 return CONFIG.WEBSERVER.URL + this.Avatar.getWebserverPath
376 }
fadf619a 377}