]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/models/actor/actor.ts
Prefer using Object.values
[github/Chocobozzz/PeerTube.git] / server / models / actor / actor.ts
1 import { literal, Op, QueryTypes, Transaction } from 'sequelize'
2 import {
3 AllowNull,
4 BelongsTo,
5 Column,
6 CreatedAt,
7 DataType,
8 DefaultScope,
9 ForeignKey,
10 HasMany,
11 HasOne,
12 Is,
13 Model,
14 Scopes,
15 Table,
16 UpdatedAt
17 } from 'sequelize-typescript'
18 import { activityPubContextify } from '@server/lib/activitypub/context'
19 import { getBiggestActorImage } from '@server/lib/actor-image'
20 import { ModelCache } from '@server/models/model-cache'
21 import { getLowercaseExtension } from '@shared/core-utils'
22 import { ActivityIconObject, ActivityPubActorType, ActorImageType } from '@shared/models'
23 import { AttributesOnly } from '@shared/typescript-utils'
24 import {
25 isActorFollowersCountValid,
26 isActorFollowingCountValid,
27 isActorPreferredUsernameValid,
28 isActorPrivateKeyValid,
29 isActorPublicKeyValid
30 } from '../../helpers/custom-validators/activitypub/actor'
31 import { isActivityPubUrlValid } from '../../helpers/custom-validators/activitypub/misc'
32 import {
33 ACTIVITY_PUB,
34 ACTIVITY_PUB_ACTOR_TYPES,
35 CONSTRAINTS_FIELDS,
36 MIMETYPES,
37 SERVER_ACTOR_NAME,
38 WEBSERVER
39 } from '../../initializers/constants'
40 import {
41 MActor,
42 MActorAccountChannelId,
43 MActorAPAccount,
44 MActorAPChannel,
45 MActorFollowersUrl,
46 MActorFormattable,
47 MActorFull,
48 MActorHost,
49 MActorId,
50 MActorServer,
51 MActorSummaryFormattable,
52 MActorUrl,
53 MActorWithInboxes
54 } from '../../types/models'
55 import { AccountModel } from '../account/account'
56 import { getServerActor } from '../application/application'
57 import { ServerModel } from '../server/server'
58 import { isOutdated, throwIfNotValid } from '../utils'
59 import { VideoModel } from '../video/video'
60 import { VideoChannelModel } from '../video/video-channel'
61 import { ActorFollowModel } from './actor-follow'
62 import { ActorImageModel } from './actor-image'
63
64 enum ScopeNames {
65 FULL = 'FULL'
66 }
67
68 export const unusedActorAttributesForAPI = [
69 'publicKey',
70 'privateKey',
71 'inboxUrl',
72 'outboxUrl',
73 'sharedInboxUrl',
74 'followersUrl',
75 'followingUrl'
76 ]
77
78 @DefaultScope(() => ({
79 include: [
80 {
81 model: ServerModel,
82 required: false
83 },
84 {
85 model: ActorImageModel,
86 as: 'Avatars',
87 required: false
88 }
89 ]
90 }))
91 @Scopes(() => ({
92 [ScopeNames.FULL]: {
93 include: [
94 {
95 model: AccountModel.unscoped(),
96 required: false
97 },
98 {
99 model: VideoChannelModel.unscoped(),
100 required: false,
101 include: [
102 {
103 model: AccountModel,
104 required: true
105 }
106 ]
107 },
108 {
109 model: ServerModel,
110 required: false
111 },
112 {
113 model: ActorImageModel,
114 as: 'Avatars',
115 required: false
116 },
117 {
118 model: ActorImageModel,
119 as: 'Banners',
120 required: false
121 }
122 ]
123 }
124 }))
125 @Table({
126 tableName: 'actor',
127 indexes: [
128 {
129 fields: [ 'url' ],
130 unique: true
131 },
132 {
133 fields: [ 'preferredUsername', 'serverId' ],
134 unique: true,
135 where: {
136 serverId: {
137 [Op.ne]: null
138 }
139 }
140 },
141 {
142 fields: [ 'preferredUsername' ],
143 unique: true,
144 where: {
145 serverId: null
146 }
147 },
148 {
149 fields: [ 'inboxUrl', 'sharedInboxUrl' ]
150 },
151 {
152 fields: [ 'sharedInboxUrl' ]
153 },
154 {
155 fields: [ 'serverId' ]
156 },
157 {
158 fields: [ 'followersUrl' ]
159 }
160 ]
161 })
162 export class ActorModel extends Model<Partial<AttributesOnly<ActorModel>>> {
163
164 @AllowNull(false)
165 @Column(DataType.ENUM(...Object.values(ACTIVITY_PUB_ACTOR_TYPES)))
166 type: ActivityPubActorType
167
168 @AllowNull(false)
169 @Is('ActorPreferredUsername', value => throwIfNotValid(value, isActorPreferredUsernameValid, 'actor preferred username'))
170 @Column
171 preferredUsername: string
172
173 @AllowNull(false)
174 @Is('ActorUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'url'))
175 @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.URL.max))
176 url: string
177
178 @AllowNull(true)
179 @Is('ActorPublicKey', value => throwIfNotValid(value, isActorPublicKeyValid, 'public key', true))
180 @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.PUBLIC_KEY.max))
181 publicKey: string
182
183 @AllowNull(true)
184 @Is('ActorPublicKey', value => throwIfNotValid(value, isActorPrivateKeyValid, 'private key', true))
185 @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.PRIVATE_KEY.max))
186 privateKey: string
187
188 @AllowNull(false)
189 @Is('ActorFollowersCount', value => throwIfNotValid(value, isActorFollowersCountValid, 'followers count'))
190 @Column
191 followersCount: number
192
193 @AllowNull(false)
194 @Is('ActorFollowersCount', value => throwIfNotValid(value, isActorFollowingCountValid, 'following count'))
195 @Column
196 followingCount: number
197
198 @AllowNull(false)
199 @Is('ActorInboxUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'inbox url'))
200 @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.URL.max))
201 inboxUrl: string
202
203 @AllowNull(true)
204 @Is('ActorOutboxUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'outbox url', true))
205 @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.URL.max))
206 outboxUrl: string
207
208 @AllowNull(true)
209 @Is('ActorSharedInboxUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'shared inbox url', true))
210 @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.URL.max))
211 sharedInboxUrl: string
212
213 @AllowNull(true)
214 @Is('ActorFollowersUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'followers url', true))
215 @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.URL.max))
216 followersUrl: string
217
218 @AllowNull(true)
219 @Is('ActorFollowingUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'following url', true))
220 @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.URL.max))
221 followingUrl: string
222
223 @AllowNull(true)
224 @Column
225 remoteCreatedAt: Date
226
227 @CreatedAt
228 createdAt: Date
229
230 @UpdatedAt
231 updatedAt: Date
232
233 @HasMany(() => ActorImageModel, {
234 as: 'Avatars',
235 onDelete: 'cascade',
236 hooks: true,
237 foreignKey: {
238 allowNull: false
239 },
240 scope: {
241 type: ActorImageType.AVATAR
242 }
243 })
244 Avatars: ActorImageModel[]
245
246 @HasMany(() => ActorImageModel, {
247 as: 'Banners',
248 onDelete: 'cascade',
249 hooks: true,
250 foreignKey: {
251 allowNull: false
252 },
253 scope: {
254 type: ActorImageType.BANNER
255 }
256 })
257 Banners: ActorImageModel[]
258
259 @HasMany(() => ActorFollowModel, {
260 foreignKey: {
261 name: 'actorId',
262 allowNull: false
263 },
264 as: 'ActorFollowings',
265 onDelete: 'cascade'
266 })
267 ActorFollowing: ActorFollowModel[]
268
269 @HasMany(() => ActorFollowModel, {
270 foreignKey: {
271 name: 'targetActorId',
272 allowNull: false
273 },
274 as: 'ActorFollowers',
275 onDelete: 'cascade'
276 })
277 ActorFollowers: ActorFollowModel[]
278
279 @ForeignKey(() => ServerModel)
280 @Column
281 serverId: number
282
283 @BelongsTo(() => ServerModel, {
284 foreignKey: {
285 allowNull: true
286 },
287 onDelete: 'cascade'
288 })
289 Server: ServerModel
290
291 @HasOne(() => AccountModel, {
292 foreignKey: {
293 allowNull: true
294 },
295 onDelete: 'cascade',
296 hooks: true
297 })
298 Account: AccountModel
299
300 @HasOne(() => VideoChannelModel, {
301 foreignKey: {
302 allowNull: true
303 },
304 onDelete: 'cascade',
305 hooks: true
306 })
307 VideoChannel: VideoChannelModel
308
309 static async load (id: number): Promise<MActor> {
310 const actorServer = await getServerActor()
311 if (id === actorServer.id) return actorServer
312
313 return ActorModel.unscoped().findByPk(id)
314 }
315
316 static loadFull (id: number): Promise<MActorFull> {
317 return ActorModel.scope(ScopeNames.FULL).findByPk(id)
318 }
319
320 static loadAccountActorFollowerUrlByVideoId (videoId: number, transaction: Transaction) {
321 const query = `SELECT "actor"."id" AS "id", "actor"."followersUrl" AS "followersUrl" ` +
322 `FROM "actor" ` +
323 `INNER JOIN "account" ON "actor"."id" = "account"."actorId" ` +
324 `INNER JOIN "videoChannel" ON "videoChannel"."accountId" = "account"."id" ` +
325 `INNER JOIN "video" ON "video"."channelId" = "videoChannel"."id" AND "video"."id" = :videoId`
326
327 const options = {
328 type: QueryTypes.SELECT as QueryTypes.SELECT,
329 replacements: { videoId },
330 plain: true as true,
331 transaction
332 }
333
334 return ActorModel.sequelize.query<MActorId & MActorFollowersUrl>(query, options)
335 }
336
337 static listByFollowersUrls (followersUrls: string[], transaction?: Transaction): Promise<MActorFull[]> {
338 const query = {
339 where: {
340 followersUrl: {
341 [Op.in]: followersUrls
342 }
343 },
344 transaction
345 }
346
347 return ActorModel.scope(ScopeNames.FULL).findAll(query)
348 }
349
350 static loadLocalByName (preferredUsername: string, transaction?: Transaction): Promise<MActorFull> {
351 const fun = () => {
352 const query = {
353 where: {
354 preferredUsername,
355 serverId: null
356 },
357 transaction
358 }
359
360 return ActorModel.scope(ScopeNames.FULL).findOne(query)
361 }
362
363 return ModelCache.Instance.doCache({
364 cacheType: 'local-actor-name',
365 key: preferredUsername,
366 // The server actor never change, so we can easily cache it
367 whitelist: () => preferredUsername === SERVER_ACTOR_NAME,
368 fun
369 })
370 }
371
372 static loadLocalUrlByName (preferredUsername: string, transaction?: Transaction): Promise<MActorUrl> {
373 const fun = () => {
374 const query = {
375 attributes: [ 'url' ],
376 where: {
377 preferredUsername,
378 serverId: null
379 },
380 transaction
381 }
382
383 return ActorModel.unscoped().findOne(query)
384 }
385
386 return ModelCache.Instance.doCache({
387 cacheType: 'local-actor-name',
388 key: preferredUsername,
389 // The server actor never change, so we can easily cache it
390 whitelist: () => preferredUsername === SERVER_ACTOR_NAME,
391 fun
392 })
393 }
394
395 static loadByNameAndHost (preferredUsername: string, host: string): Promise<MActorFull> {
396 const query = {
397 where: {
398 preferredUsername
399 },
400 include: [
401 {
402 model: ServerModel,
403 required: true,
404 where: {
405 host
406 }
407 }
408 ]
409 }
410
411 return ActorModel.scope(ScopeNames.FULL).findOne(query)
412 }
413
414 static loadByUrl (url: string, transaction?: Transaction): Promise<MActorAccountChannelId> {
415 const query = {
416 where: {
417 url
418 },
419 transaction,
420 include: [
421 {
422 attributes: [ 'id' ],
423 model: AccountModel.unscoped(),
424 required: false
425 },
426 {
427 attributes: [ 'id' ],
428 model: VideoChannelModel.unscoped(),
429 required: false
430 }
431 ]
432 }
433
434 return ActorModel.unscoped().findOne(query)
435 }
436
437 static loadByUrlAndPopulateAccountAndChannel (url: string, transaction?: Transaction): Promise<MActorFull> {
438 const query = {
439 where: {
440 url
441 },
442 transaction
443 }
444
445 return ActorModel.scope(ScopeNames.FULL).findOne(query)
446 }
447
448 static rebuildFollowsCount (ofId: number, type: 'followers' | 'following', transaction?: Transaction) {
449 const sanitizedOfId = parseInt(ofId + '', 10)
450 const where = { id: sanitizedOfId }
451
452 let columnToUpdate: string
453 let columnOfCount: string
454
455 if (type === 'followers') {
456 columnToUpdate = 'followersCount'
457 columnOfCount = 'targetActorId'
458 } else {
459 columnToUpdate = 'followingCount'
460 columnOfCount = 'actorId'
461 }
462
463 return ActorModel.update({
464 [columnToUpdate]: literal(`(SELECT COUNT(*) FROM "actorFollow" WHERE "${columnOfCount}" = ${sanitizedOfId} AND "state" = 'accepted')`)
465 }, { where, transaction })
466 }
467
468 static loadAccountActorByVideoId (videoId: number, transaction: Transaction): Promise<MActor> {
469 const query = {
470 include: [
471 {
472 attributes: [ 'id' ],
473 model: AccountModel.unscoped(),
474 required: true,
475 include: [
476 {
477 attributes: [ 'id', 'accountId' ],
478 model: VideoChannelModel.unscoped(),
479 required: true,
480 include: [
481 {
482 attributes: [ 'id', 'channelId' ],
483 model: VideoModel.unscoped(),
484 where: {
485 id: videoId
486 }
487 }
488 ]
489 }
490 ]
491 }
492 ],
493 transaction
494 }
495
496 return ActorModel.unscoped().findOne(query)
497 }
498
499 getSharedInbox (this: MActorWithInboxes) {
500 return this.sharedInboxUrl || this.inboxUrl
501 }
502
503 toFormattedSummaryJSON (this: MActorSummaryFormattable) {
504 return {
505 url: this.url,
506 name: this.preferredUsername,
507 host: this.getHost(),
508 avatars: (this.Avatars || []).map(a => a.toFormattedJSON()),
509
510 // TODO: remove, deprecated in 4.2
511 avatar: this.hasImage(ActorImageType.AVATAR)
512 ? this.Avatars[0].toFormattedJSON()
513 : undefined
514 }
515 }
516
517 toFormattedJSON (this: MActorFormattable) {
518 return {
519 ...this.toFormattedSummaryJSON(),
520
521 id: this.id,
522 hostRedundancyAllowed: this.getRedundancyAllowed(),
523 followingCount: this.followingCount,
524 followersCount: this.followersCount,
525 createdAt: this.getCreatedAt(),
526
527 banners: (this.Banners || []).map(b => b.toFormattedJSON()),
528
529 // TODO: remove, deprecated in 4.2
530 banner: this.hasImage(ActorImageType.BANNER)
531 ? this.Banners[0].toFormattedJSON()
532 : undefined
533 }
534 }
535
536 toActivityPubObject (this: MActorAPChannel | MActorAPAccount, name: string) {
537 let icon: ActivityIconObject
538 let icons: ActivityIconObject[]
539 let image: ActivityIconObject
540
541 if (this.hasImage(ActorImageType.AVATAR)) {
542 icon = getBiggestActorImage(this.Avatars).toActivityPubObject()
543 icons = this.Avatars.map(a => a.toActivityPubObject())
544 }
545
546 if (this.hasImage(ActorImageType.BANNER)) {
547 const banner = getBiggestActorImage((this as MActorAPChannel).Banners)
548 const extension = getLowercaseExtension(banner.filename)
549
550 image = {
551 type: 'Image',
552 mediaType: MIMETYPES.IMAGE.EXT_MIMETYPE[extension],
553 height: banner.height,
554 width: banner.width,
555 url: ActorImageModel.getImageUrl(banner)
556 }
557 }
558
559 const json = {
560 type: this.type,
561 id: this.url,
562 following: this.getFollowingUrl(),
563 followers: this.getFollowersUrl(),
564 playlists: this.getPlaylistsUrl(),
565 inbox: this.inboxUrl,
566 outbox: this.outboxUrl,
567 preferredUsername: this.preferredUsername,
568 url: this.url,
569 name,
570 endpoints: {
571 sharedInbox: this.sharedInboxUrl
572 },
573 publicKey: {
574 id: this.getPublicKeyUrl(),
575 owner: this.url,
576 publicKeyPem: this.publicKey
577 },
578 published: this.getCreatedAt().toISOString(),
579
580 icon,
581 icons,
582
583 image
584 }
585
586 return activityPubContextify(json, 'Actor')
587 }
588
589 getFollowerSharedInboxUrls (t: Transaction) {
590 const query = {
591 attributes: [ 'sharedInboxUrl' ],
592 include: [
593 {
594 attribute: [],
595 model: ActorFollowModel.unscoped(),
596 required: true,
597 as: 'ActorFollowing',
598 where: {
599 state: 'accepted',
600 targetActorId: this.id
601 }
602 }
603 ],
604 transaction: t
605 }
606
607 return ActorModel.findAll(query)
608 .then(accounts => accounts.map(a => a.sharedInboxUrl))
609 }
610
611 getFollowingUrl () {
612 return this.url + '/following'
613 }
614
615 getFollowersUrl () {
616 return this.url + '/followers'
617 }
618
619 getPlaylistsUrl () {
620 return this.url + '/playlists'
621 }
622
623 getPublicKeyUrl () {
624 return this.url + '#main-key'
625 }
626
627 isOwned () {
628 return this.serverId === null
629 }
630
631 getWebfingerUrl (this: MActorServer) {
632 return 'acct:' + this.preferredUsername + '@' + this.getHost()
633 }
634
635 getIdentifier () {
636 return this.Server ? `${this.preferredUsername}@${this.Server.host}` : this.preferredUsername
637 }
638
639 getHost (this: MActorHost) {
640 return this.Server ? this.Server.host : WEBSERVER.HOST
641 }
642
643 getRedundancyAllowed () {
644 return this.Server ? this.Server.redundancyAllowed : false
645 }
646
647 hasImage (type: ActorImageType) {
648 const images = type === ActorImageType.AVATAR
649 ? this.Avatars
650 : this.Banners
651
652 return Array.isArray(images) && images.length !== 0
653 }
654
655 isOutdated () {
656 if (this.isOwned()) return false
657
658 return isOutdated(this, ACTIVITY_PUB.ACTOR_REFRESH_INTERVAL)
659 }
660
661 getCreatedAt (this: MActorAPChannel | MActorAPAccount | MActorFormattable) {
662 return this.remoteCreatedAt || this.createdAt
663 }
664 }