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