]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/models/activitypub/actor.ts
Upgrade sequelize
[github/Chocobozzz/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,
6 BelongsTo,
7 Column,
8 CreatedAt,
9 DataType,
10 Default,
11 DefaultScope,
12 ForeignKey,
13 HasMany,
14 HasOne,
15 Is,
16 IsUUID,
17 Model,
18 Scopes,
19 Table,
20 UpdatedAt
21 } from 'sequelize-typescript'
22 import { ActivityPubActorType } from '../../../shared/models/activitypub'
23 import { Avatar } from '../../../shared/models/avatars/avatar.model'
24 import { activityPubContextify } from '../../helpers/activitypub'
25 import {
26 isActorFollowersCountValid,
27 isActorFollowingCountValid,
28 isActorPreferredUsernameValid,
29 isActorPrivateKeyValid,
30 isActorPublicKeyValid
31 } from '../../helpers/custom-validators/activitypub/actor'
32 import { isActivityPubUrlValid } from '../../helpers/custom-validators/activitypub/misc'
33 import { ACTIVITY_PUB, ACTIVITY_PUB_ACTOR_TYPES, CONSTRAINTS_FIELDS, WEBSERVER } from '../../initializers/constants'
34 import { AccountModel } from '../account/account'
35 import { AvatarModel } from '../avatar/avatar'
36 import { ServerModel } from '../server/server'
37 import { isOutdated, throwIfNotValid } from '../utils'
38 import { VideoChannelModel } from '../video/video-channel'
39 import { ActorFollowModel } from './actor-follow'
40 import { VideoModel } from '../video/video'
41
42 enum ScopeNames {
43 FULL = 'FULL'
44 }
45
46 export const unusedActorAttributesForAPI = [
47 'publicKey',
48 'privateKey',
49 'inboxUrl',
50 'outboxUrl',
51 'sharedInboxUrl',
52 'followersUrl',
53 'followingUrl',
54 'url',
55 'createdAt',
56 'updatedAt'
57 ]
58
59 @DefaultScope(() => ({
60 include: [
61 {
62 model: ServerModel,
63 required: false
64 },
65 {
66 model: AvatarModel,
67 required: false
68 }
69 ]
70 }))
71 @Scopes(() => ({
72 [ScopeNames.FULL]: {
73 include: [
74 {
75 model: AccountModel.unscoped(),
76 required: false
77 },
78 {
79 model: VideoChannelModel.unscoped(),
80 required: false,
81 include: [
82 {
83 model: AccountModel,
84 required: true
85 }
86 ]
87 },
88 {
89 model: ServerModel,
90 required: false
91 },
92 {
93 model: AvatarModel,
94 required: false
95 }
96 ]
97 }
98 }))
99 @Table({
100 tableName: 'actor',
101 indexes: [
102 {
103 fields: [ 'url' ],
104 unique: true
105 },
106 {
107 fields: [ 'preferredUsername', 'serverId' ],
108 unique: true
109 },
110 {
111 fields: [ 'inboxUrl', 'sharedInboxUrl' ]
112 },
113 {
114 fields: [ 'sharedInboxUrl' ]
115 },
116 {
117 fields: [ 'serverId' ]
118 },
119 {
120 fields: [ 'avatarId' ]
121 },
122 {
123 fields: [ 'uuid' ],
124 unique: true
125 },
126 {
127 fields: [ 'followersUrl' ]
128 }
129 ]
130 })
131 export class ActorModel extends Model<ActorModel> {
132
133 @AllowNull(false)
134 @Column(DataType.ENUM(...values(ACTIVITY_PUB_ACTOR_TYPES)))
135 type: ActivityPubActorType
136
137 @AllowNull(false)
138 @Default(DataType.UUIDV4)
139 @IsUUID(4)
140 @Column(DataType.UUID)
141 uuid: string
142
143 @AllowNull(false)
144 @Is('ActorPreferredUsername', value => throwIfNotValid(value, isActorPreferredUsernameValid, 'actor preferred username'))
145 @Column
146 preferredUsername: string
147
148 @AllowNull(false)
149 @Is('ActorUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'url'))
150 @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.URL.max))
151 url: string
152
153 @AllowNull(true)
154 @Is('ActorPublicKey', value => throwIfNotValid(value, isActorPublicKeyValid, 'public key', true))
155 @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.PUBLIC_KEY.max))
156 publicKey: string
157
158 @AllowNull(true)
159 @Is('ActorPublicKey', value => throwIfNotValid(value, isActorPrivateKeyValid, 'private key', true))
160 @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.PRIVATE_KEY.max))
161 privateKey: string
162
163 @AllowNull(false)
164 @Is('ActorFollowersCount', value => throwIfNotValid(value, isActorFollowersCountValid, 'followers count'))
165 @Column
166 followersCount: number
167
168 @AllowNull(false)
169 @Is('ActorFollowersCount', value => throwIfNotValid(value, isActorFollowingCountValid, 'following count'))
170 @Column
171 followingCount: number
172
173 @AllowNull(false)
174 @Is('ActorInboxUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'inbox url'))
175 @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.URL.max))
176 inboxUrl: string
177
178 @AllowNull(false)
179 @Is('ActorOutboxUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'outbox url'))
180 @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.URL.max))
181 outboxUrl: string
182
183 @AllowNull(false)
184 @Is('ActorSharedInboxUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'shared inbox url'))
185 @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.URL.max))
186 sharedInboxUrl: string
187
188 @AllowNull(false)
189 @Is('ActorFollowersUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'followers url'))
190 @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.URL.max))
191 followersUrl: string
192
193 @AllowNull(false)
194 @Is('ActorFollowingUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'following url'))
195 @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.URL.max))
196 followingUrl: string
197
198 @CreatedAt
199 createdAt: Date
200
201 @UpdatedAt
202 updatedAt: Date
203
204 @ForeignKey(() => AvatarModel)
205 @Column
206 avatarId: number
207
208 @BelongsTo(() => AvatarModel, {
209 foreignKey: {
210 allowNull: true
211 },
212 onDelete: 'set null',
213 hooks: true
214 })
215 Avatar: AvatarModel
216
217 @HasMany(() => ActorFollowModel, {
218 foreignKey: {
219 name: 'actorId',
220 allowNull: false
221 },
222 as: 'ActorFollowings',
223 onDelete: 'cascade'
224 })
225 ActorFollowing: ActorFollowModel[]
226
227 @HasMany(() => ActorFollowModel, {
228 foreignKey: {
229 name: 'targetActorId',
230 allowNull: false
231 },
232 as: 'ActorFollowers',
233 onDelete: 'cascade'
234 })
235 ActorFollowers: ActorFollowModel[]
236
237 @ForeignKey(() => ServerModel)
238 @Column
239 serverId: number
240
241 @BelongsTo(() => ServerModel, {
242 foreignKey: {
243 allowNull: true
244 },
245 onDelete: 'cascade'
246 })
247 Server: ServerModel
248
249 @HasOne(() => AccountModel, {
250 foreignKey: {
251 allowNull: true
252 },
253 onDelete: 'cascade',
254 hooks: true
255 })
256 Account: AccountModel
257
258 @HasOne(() => VideoChannelModel, {
259 foreignKey: {
260 allowNull: true
261 },
262 onDelete: 'cascade',
263 hooks: true
264 })
265 VideoChannel: VideoChannelModel
266
267 static load (id: number) {
268 return ActorModel.unscoped().findByPk(id)
269 }
270
271 static loadAccountActorByVideoId (videoId: number, transaction: Sequelize.Transaction) {
272 const query = {
273 include: [
274 {
275 attributes: [ 'id' ],
276 model: AccountModel.unscoped(),
277 required: true,
278 include: [
279 {
280 attributes: [ 'id' ],
281 model: VideoChannelModel.unscoped(),
282 required: true,
283 include: [
284 {
285 attributes: [ 'id' ],
286 model: VideoModel.unscoped(),
287 required: true,
288 where: {
289 id: videoId
290 }
291 }
292 ]
293 }
294 ]
295 }
296 ],
297 transaction
298 }
299
300 return ActorModel.unscoped().findOne(query)
301 }
302
303 static isActorUrlExist (url: string) {
304 const query = {
305 raw: true,
306 where: {
307 url
308 }
309 }
310
311 return ActorModel.unscoped().findOne(query)
312 .then(a => !!a)
313 }
314
315 static listByFollowersUrls (followersUrls: string[], transaction?: Sequelize.Transaction) {
316 const query = {
317 where: {
318 followersUrl: {
319 [ Sequelize.Op.in ]: followersUrls
320 }
321 },
322 transaction
323 }
324
325 return ActorModel.scope(ScopeNames.FULL).findAll(query)
326 }
327
328 static loadLocalByName (preferredUsername: string, transaction?: Sequelize.Transaction) {
329 const query = {
330 where: {
331 preferredUsername,
332 serverId: null
333 },
334 transaction
335 }
336
337 return ActorModel.scope(ScopeNames.FULL).findOne(query)
338 }
339
340 static loadByNameAndHost (preferredUsername: string, host: string) {
341 const query = {
342 where: {
343 preferredUsername
344 },
345 include: [
346 {
347 model: ServerModel,
348 required: true,
349 where: {
350 host
351 }
352 }
353 ]
354 }
355
356 return ActorModel.scope(ScopeNames.FULL).findOne(query)
357 }
358
359 static loadByUrl (url: string, transaction?: Sequelize.Transaction) {
360 const query = {
361 where: {
362 url
363 },
364 transaction,
365 include: [
366 {
367 attributes: [ 'id' ],
368 model: AccountModel.unscoped(),
369 required: false
370 },
371 {
372 attributes: [ 'id' ],
373 model: VideoChannelModel.unscoped(),
374 required: false
375 }
376 ]
377 }
378
379 return ActorModel.unscoped().findOne(query)
380 }
381
382 static loadByUrlAndPopulateAccountAndChannel (url: string, transaction?: Sequelize.Transaction) {
383 const query = {
384 where: {
385 url
386 },
387 transaction
388 }
389
390 return ActorModel.scope(ScopeNames.FULL).findOne(query)
391 }
392
393 static incrementFollows (id: number, column: 'followersCount' | 'followingCount', by: number) {
394 return ActorModel.increment(column, {
395 by,
396 where: {
397 id
398 }
399 })
400 }
401
402 toFormattedJSON () {
403 let avatar: Avatar = null
404 if (this.Avatar) {
405 avatar = this.Avatar.toFormattedJSON()
406 }
407
408 return {
409 id: this.id,
410 url: this.url,
411 uuid: this.uuid,
412 name: this.preferredUsername,
413 host: this.getHost(),
414 hostRedundancyAllowed: this.getRedundancyAllowed(),
415 followingCount: this.followingCount,
416 followersCount: this.followersCount,
417 avatar,
418 createdAt: this.createdAt,
419 updatedAt: this.updatedAt
420 }
421 }
422
423 toActivityPubObject (name: string, type: 'Account' | 'Application' | 'VideoChannel') {
424 let activityPubType
425 if (type === 'Account') {
426 activityPubType = 'Person' as 'Person'
427 } else if (type === 'Application') {
428 activityPubType = 'Application' as 'Application'
429 } else { // VideoChannel
430 activityPubType = 'Group' as 'Group'
431 }
432
433 let icon = undefined
434 if (this.avatarId) {
435 const extension = extname(this.Avatar.filename)
436 icon = {
437 type: 'Image',
438 mediaType: extension === '.png' ? 'image/png' : 'image/jpeg',
439 url: this.getAvatarUrl()
440 }
441 }
442
443 const json = {
444 type: activityPubType,
445 id: this.url,
446 following: this.getFollowingUrl(),
447 followers: this.getFollowersUrl(),
448 playlists: this.getPlaylistsUrl(),
449 inbox: this.inboxUrl,
450 outbox: this.outboxUrl,
451 preferredUsername: this.preferredUsername,
452 url: this.url,
453 name,
454 endpoints: {
455 sharedInbox: this.sharedInboxUrl
456 },
457 uuid: this.uuid,
458 publicKey: {
459 id: this.getPublicKeyUrl(),
460 owner: this.url,
461 publicKeyPem: this.publicKey
462 },
463 icon
464 }
465
466 return activityPubContextify(json)
467 }
468
469 getFollowerSharedInboxUrls (t: Sequelize.Transaction) {
470 const query = {
471 attributes: [ 'sharedInboxUrl' ],
472 include: [
473 {
474 attribute: [],
475 model: ActorFollowModel.unscoped(),
476 required: true,
477 as: 'ActorFollowing',
478 where: {
479 state: 'accepted',
480 targetActorId: this.id
481 }
482 }
483 ],
484 transaction: t
485 }
486
487 return ActorModel.findAll(query)
488 .then(accounts => accounts.map(a => a.sharedInboxUrl))
489 }
490
491 getFollowingUrl () {
492 return this.url + '/following'
493 }
494
495 getFollowersUrl () {
496 return this.url + '/followers'
497 }
498
499 getPlaylistsUrl () {
500 return this.url + '/playlists'
501 }
502
503 getPublicKeyUrl () {
504 return this.url + '#main-key'
505 }
506
507 isOwned () {
508 return this.serverId === null
509 }
510
511 getWebfingerUrl () {
512 return 'acct:' + this.preferredUsername + '@' + this.getHost()
513 }
514
515 getIdentifier () {
516 return this.Server ? `${this.preferredUsername}@${this.Server.host}` : this.preferredUsername
517 }
518
519 getHost () {
520 return this.Server ? this.Server.host : WEBSERVER.HOST
521 }
522
523 getRedundancyAllowed () {
524 return this.Server ? this.Server.redundancyAllowed : false
525 }
526
527 getAvatarUrl () {
528 if (!this.avatarId) return undefined
529
530 return WEBSERVER.URL + this.Avatar.getWebserverPath()
531 }
532
533 isOutdated () {
534 if (this.isOwned()) return false
535
536 return isOutdated(this, ACTIVITY_PUB.ACTOR_REFRESH_INTERVAL)
537 }
538 }