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