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