Add avatar max size limit
[oweals/peertube.git] / server / models / activitypub / actor.ts
1 import { values } from 'lodash'
2 import { extname } from 'path'
3 import * as Sequelize from 'sequelize'
4 import {
5   AllowNull, BelongsTo, Column, CreatedAt, DataType, Default, DefaultScope, ForeignKey, HasMany, HasOne, Is, IsUUID, Model, Scopes,
6   Table, UpdatedAt
7 } from 'sequelize-typescript'
8 import { ActivityPubActorType } from '../../../shared/models/activitypub'
9 import { Avatar } from '../../../shared/models/avatars/avatar.model'
10 import { activityPubContextify } from '../../helpers/activitypub'
11 import {
12   isActorFollowersCountValid, isActorFollowingCountValid, isActorPreferredUsernameValid, isActorPrivateKeyValid,
13   isActorPublicKeyValid
14 } from '../../helpers/custom-validators/activitypub/actor'
15 import { isActivityPubUrlValid } from '../../helpers/custom-validators/activitypub/misc'
16 import { ACTIVITY_PUB_ACTOR_TYPES, CONFIG, CONSTRAINTS_FIELDS } from '../../initializers'
17 import { AccountModel } from '../account/account'
18 import { AvatarModel } from '../avatar/avatar'
19 import { ServerModel } from '../server/server'
20 import { throwIfNotValid } from '../utils'
21 import { VideoChannelModel } from '../video/video-channel'
22 import { ActorFollowModel } from './actor-follow'
23
24 enum ScopeNames {
25   FULL = 'FULL'
26 }
27
28 @DefaultScope({
29   include: [
30     {
31       model: () => ServerModel,
32       required: false
33     },
34     {
35       model: () => AvatarModel,
36       required: false
37     }
38   ]
39 })
40 @Scopes({
41   [ScopeNames.FULL]: {
42     include: [
43       {
44         model: () => AccountModel,
45         required: false
46       },
47       {
48         model: () => VideoChannelModel,
49         required: false
50       },
51       {
52         model: () => ServerModel,
53         required: false
54       },
55       {
56         model: () => AvatarModel,
57         required: false
58       }
59     ]
60   }
61 })
62 @Table({
63   tableName: 'actor',
64   indexes: [
65     {
66       fields: [ 'preferredUsername', 'serverId' ],
67       unique: true
68     }
69   ]
70 })
71 export class ActorModel extends Model<ActorModel> {
72
73   @AllowNull(false)
74   @Column(DataType.ENUM(values(ACTIVITY_PUB_ACTOR_TYPES)))
75   type: ActivityPubActorType
76
77   @AllowNull(false)
78   @Default(DataType.UUIDV4)
79   @IsUUID(4)
80   @Column(DataType.UUID)
81   uuid: string
82
83   @AllowNull(false)
84   @Is('ActorPreferredUsername', value => throwIfNotValid(value, isActorPreferredUsernameValid, 'actor preferred username'))
85   @Column
86   preferredUsername: string
87
88   @AllowNull(false)
89   @Is('ActorUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'url'))
90   @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.URL.max))
91   url: string
92
93   @AllowNull(true)
94   @Is('ActorPublicKey', value => throwIfNotValid(value, isActorPublicKeyValid, 'public key'))
95   @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.PUBLIC_KEY.max))
96   publicKey: string
97
98   @AllowNull(true)
99   @Is('ActorPublicKey', value => throwIfNotValid(value, isActorPrivateKeyValid, 'private key'))
100   @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.PRIVATE_KEY.max))
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'))
115   @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.URL.max))
116   inboxUrl: string
117
118   @AllowNull(false)
119   @Is('ActorOutboxUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'outbox url'))
120   @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.URL.max))
121   outboxUrl: string
122
123   @AllowNull(false)
124   @Is('ActorSharedInboxUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'shared inbox url'))
125   @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.URL.max))
126   sharedInboxUrl: string
127
128   @AllowNull(false)
129   @Is('ActorFollowersUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'followers url'))
130   @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.URL.max))
131   followersUrl: string
132
133   @AllowNull(false)
134   @Is('ActorFollowingUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'following url'))
135   @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.URL.max))
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     },
152     onDelete: 'set null'
153   })
154   Avatar: AvatarModel
155
156   @HasMany(() => ActorFollowModel, {
157     foreignKey: {
158       name: 'actorId',
159       allowNull: false
160     },
161     onDelete: 'cascade'
162   })
163   AccountFollowing: ActorFollowModel[]
164
165   @HasMany(() => ActorFollowModel, {
166     foreignKey: {
167       name: 'targetActorId',
168       allowNull: false
169     },
170     as: 'followers',
171     onDelete: 'cascade'
172   })
173   AccountFollowers: ActorFollowModel[]
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
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
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
217     return ActorModel.scope(ScopeNames.FULL).findAll(query)
218   }
219
220   static loadLocalByName (preferredUsername: string) {
221     const query = {
222       where: {
223         preferredUsername,
224         serverId: null
225       }
226     }
227
228     return ActorModel.scope(ScopeNames.FULL).findOne(query)
229   }
230
231   static loadByNameAndHost (preferredUsername: string, host: string) {
232     const query = {
233       where: {
234         preferredUsername
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)
259   }
260
261   toFormattedJSON () {
262     let avatar: Avatar = null
263     if (this.Avatar) {
264       avatar = this.Avatar.toFormattedJSON()
265     }
266
267     let score: number
268     if (this.Server) {
269       score = this.Server.score
270     }
271
272     return {
273       id: this.id,
274       uuid: this.uuid,
275       host: this.getHost(),
276       score,
277       followingCount: this.followingCount,
278       followersCount: this.followersCount,
279       avatar
280     }
281   }
282
283   toActivityPubObject (name: string, type: 'Account' | 'Application' | 'VideoChannel') {
284     let activityPubType
285     if (type === 'Account') {
286       activityPubType = 'Person' as 'Person'
287     } else if (type === 'Application') {
288       activityPubType = 'Application' as 'Application'
289     } else { // VideoChannel
290       activityPubType = 'Group' as 'Group'
291     }
292
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
303     const json = {
304       type: activityPubType,
305       id: this.url,
306       following: this.getFollowingUrl(),
307       followers: this.getFollowersUrl(),
308       inbox: this.inboxUrl,
309       outbox: this.outboxUrl,
310       preferredUsername: this.preferredUsername,
311       url: this.url,
312       name,
313       endpoints: {
314         sharedInbox: this.sharedInboxUrl
315       },
316       uuid: this.uuid,
317       publicKey: {
318         id: this.getPublicKeyUrl(),
319         owner: this.url,
320         publicKeyPem: this.publicKey
321       },
322       icon
323     }
324
325     return activityPubContextify(json)
326   }
327
328   getFollowerSharedInboxUrls (t: Sequelize.Transaction) {
329     const query = {
330       attributes: [ 'sharedInboxUrl' ],
331       include: [
332         {
333           model: ActorFollowModel,
334           required: true,
335           as: 'followers',
336           where: {
337             targetActorId: this.id
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   }
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   }
371
372   getAvatarUrl () {
373     if (!this.avatarId) return undefined
374
375     return CONFIG.WEBSERVER.URL + this.Avatar.getWebserverPath
376   }
377 }