diff options
author | Chocobozzz <me@florianbigard.com> | 2021-05-27 16:12:41 +0200 |
---|---|---|
committer | Chocobozzz <me@florianbigard.com> | 2021-05-27 16:12:41 +0200 |
commit | 8f608a4cb22ab232cfab20665050764b38bac9c7 (patch) | |
tree | 6a6785aae79bf5939ad7b7a50a1bd8031268d2b4 /server/models/actor/actor.ts | |
parent | 030ccfce59a8cb8f2fee6ea8dd363ba635c5c5c2 (diff) | |
parent | c215e627b575d2c4085ccb222f4ca8d0237b7552 (diff) | |
download | PeerTube-8f608a4cb22ab232cfab20665050764b38bac9c7.tar.gz PeerTube-8f608a4cb22ab232cfab20665050764b38bac9c7.tar.zst PeerTube-8f608a4cb22ab232cfab20665050764b38bac9c7.zip |
Merge branch 'develop' into shorter-URLs-channels-accounts
Diffstat (limited to 'server/models/actor/actor.ts')
-rw-r--r-- | server/models/actor/actor.ts | 700 |
1 files changed, 700 insertions, 0 deletions
diff --git a/server/models/actor/actor.ts b/server/models/actor/actor.ts new file mode 100644 index 000000000..65c53f8f8 --- /dev/null +++ b/server/models/actor/actor.ts | |||
@@ -0,0 +1,700 @@ | |||
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 { AttributesOnly } from '@shared/core-utils' | ||
22 | import { ActivityIconObject, ActivityPubActorType } from '../../../shared/models/activitypub' | ||
23 | import { ActorImage } from '../../../shared/models/actors/actor-image.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 { | ||
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: 'Avatar', | ||
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: 'Avatar', | ||
113 | required: false | ||
114 | }, | ||
115 | { | ||
116 | model: ActorImageModel, | ||
117 | as: 'Banner', | ||
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: [ 'avatarId' ] | ||
157 | }, | ||
158 | { | ||
159 | fields: [ 'followersUrl' ] | ||
160 | } | ||
161 | ] | ||
162 | }) | ||
163 | export class ActorModel extends Model<Partial<AttributesOnly<ActorModel>>> { | ||
164 | |||
165 | @AllowNull(false) | ||
166 | @Column(DataType.ENUM(...values(ACTIVITY_PUB_ACTOR_TYPES))) | ||
167 | type: ActivityPubActorType | ||
168 | |||
169 | @AllowNull(false) | ||
170 | @Is('ActorPreferredUsername', value => throwIfNotValid(value, isActorPreferredUsernameValid, 'actor preferred username')) | ||
171 | @Column | ||
172 | preferredUsername: string | ||
173 | |||
174 | @AllowNull(false) | ||
175 | @Is('ActorUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'url')) | ||
176 | @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.URL.max)) | ||
177 | url: string | ||
178 | |||
179 | @AllowNull(true) | ||
180 | @Is('ActorPublicKey', value => throwIfNotValid(value, isActorPublicKeyValid, 'public key', true)) | ||
181 | @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.PUBLIC_KEY.max)) | ||
182 | publicKey: string | ||
183 | |||
184 | @AllowNull(true) | ||
185 | @Is('ActorPublicKey', value => throwIfNotValid(value, isActorPrivateKeyValid, 'private key', true)) | ||
186 | @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.PRIVATE_KEY.max)) | ||
187 | privateKey: string | ||
188 | |||
189 | @AllowNull(false) | ||
190 | @Is('ActorFollowersCount', value => throwIfNotValid(value, isActorFollowersCountValid, 'followers count')) | ||
191 | @Column | ||
192 | followersCount: number | ||
193 | |||
194 | @AllowNull(false) | ||
195 | @Is('ActorFollowersCount', value => throwIfNotValid(value, isActorFollowingCountValid, 'following count')) | ||
196 | @Column | ||
197 | followingCount: number | ||
198 | |||
199 | @AllowNull(false) | ||
200 | @Is('ActorInboxUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'inbox url')) | ||
201 | @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.URL.max)) | ||
202 | inboxUrl: string | ||
203 | |||
204 | @AllowNull(true) | ||
205 | @Is('ActorOutboxUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'outbox url', true)) | ||
206 | @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.URL.max)) | ||
207 | outboxUrl: string | ||
208 | |||
209 | @AllowNull(true) | ||
210 | @Is('ActorSharedInboxUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'shared inbox url', true)) | ||
211 | @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.URL.max)) | ||
212 | sharedInboxUrl: string | ||
213 | |||
214 | @AllowNull(true) | ||
215 | @Is('ActorFollowersUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'followers url', true)) | ||
216 | @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.URL.max)) | ||
217 | followersUrl: string | ||
218 | |||
219 | @AllowNull(true) | ||
220 | @Is('ActorFollowingUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'following url', true)) | ||
221 | @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.URL.max)) | ||
222 | followingUrl: string | ||
223 | |||
224 | @AllowNull(true) | ||
225 | @Column | ||
226 | remoteCreatedAt: Date | ||
227 | |||
228 | @CreatedAt | ||
229 | createdAt: Date | ||
230 | |||
231 | @UpdatedAt | ||
232 | updatedAt: Date | ||
233 | |||
234 | @ForeignKey(() => ActorImageModel) | ||
235 | @Column | ||
236 | avatarId: number | ||
237 | |||
238 | @ForeignKey(() => ActorImageModel) | ||
239 | @Column | ||
240 | bannerId: number | ||
241 | |||
242 | @BelongsTo(() => ActorImageModel, { | ||
243 | foreignKey: { | ||
244 | name: 'avatarId', | ||
245 | allowNull: true | ||
246 | }, | ||
247 | as: 'Avatar', | ||
248 | onDelete: 'set null', | ||
249 | hooks: true | ||
250 | }) | ||
251 | Avatar: ActorImageModel | ||
252 | |||
253 | @BelongsTo(() => ActorImageModel, { | ||
254 | foreignKey: { | ||
255 | name: 'bannerId', | ||
256 | allowNull: true | ||
257 | }, | ||
258 | as: 'Banner', | ||
259 | onDelete: 'set null', | ||
260 | hooks: true | ||
261 | }) | ||
262 | Banner: ActorImageModel | ||
263 | |||
264 | @HasMany(() => ActorFollowModel, { | ||
265 | foreignKey: { | ||
266 | name: 'actorId', | ||
267 | allowNull: false | ||
268 | }, | ||
269 | as: 'ActorFollowings', | ||
270 | onDelete: 'cascade' | ||
271 | }) | ||
272 | ActorFollowing: ActorFollowModel[] | ||
273 | |||
274 | @HasMany(() => ActorFollowModel, { | ||
275 | foreignKey: { | ||
276 | name: 'targetActorId', | ||
277 | allowNull: false | ||
278 | }, | ||
279 | as: 'ActorFollowers', | ||
280 | onDelete: 'cascade' | ||
281 | }) | ||
282 | ActorFollowers: ActorFollowModel[] | ||
283 | |||
284 | @ForeignKey(() => ServerModel) | ||
285 | @Column | ||
286 | serverId: number | ||
287 | |||
288 | @BelongsTo(() => ServerModel, { | ||
289 | foreignKey: { | ||
290 | allowNull: true | ||
291 | }, | ||
292 | onDelete: 'cascade' | ||
293 | }) | ||
294 | Server: ServerModel | ||
295 | |||
296 | @HasOne(() => AccountModel, { | ||
297 | foreignKey: { | ||
298 | allowNull: true | ||
299 | }, | ||
300 | onDelete: 'cascade', | ||
301 | hooks: true | ||
302 | }) | ||
303 | Account: AccountModel | ||
304 | |||
305 | @HasOne(() => VideoChannelModel, { | ||
306 | foreignKey: { | ||
307 | allowNull: true | ||
308 | }, | ||
309 | onDelete: 'cascade', | ||
310 | hooks: true | ||
311 | }) | ||
312 | VideoChannel: VideoChannelModel | ||
313 | |||
314 | static load (id: number): Promise<MActor> { | ||
315 | return ActorModel.unscoped().findByPk(id) | ||
316 | } | ||
317 | |||
318 | static loadFull (id: number): Promise<MActorFull> { | ||
319 | return ActorModel.scope(ScopeNames.FULL).findByPk(id) | ||
320 | } | ||
321 | |||
322 | static loadFromAccountByVideoId (videoId: number, transaction: Transaction): Promise<MActor> { | ||
323 | const query = { | ||
324 | include: [ | ||
325 | { | ||
326 | attributes: [ 'id' ], | ||
327 | model: AccountModel.unscoped(), | ||
328 | required: true, | ||
329 | include: [ | ||
330 | { | ||
331 | attributes: [ 'id' ], | ||
332 | model: VideoChannelModel.unscoped(), | ||
333 | required: true, | ||
334 | include: [ | ||
335 | { | ||
336 | attributes: [ 'id' ], | ||
337 | model: VideoModel.unscoped(), | ||
338 | required: true, | ||
339 | where: { | ||
340 | id: videoId | ||
341 | } | ||
342 | } | ||
343 | ] | ||
344 | } | ||
345 | ] | ||
346 | } | ||
347 | ], | ||
348 | transaction | ||
349 | } | ||
350 | |||
351 | return ActorModel.unscoped().findOne(query) | ||
352 | } | ||
353 | |||
354 | static isActorUrlExist (url: string) { | ||
355 | const query = { | ||
356 | raw: true, | ||
357 | where: { | ||
358 | url | ||
359 | } | ||
360 | } | ||
361 | |||
362 | return ActorModel.unscoped().findOne(query) | ||
363 | .then(a => !!a) | ||
364 | } | ||
365 | |||
366 | static listByFollowersUrls (followersUrls: string[], transaction?: Transaction): Promise<MActorFull[]> { | ||
367 | const query = { | ||
368 | where: { | ||
369 | followersUrl: { | ||
370 | [Op.in]: followersUrls | ||
371 | } | ||
372 | }, | ||
373 | transaction | ||
374 | } | ||
375 | |||
376 | return ActorModel.scope(ScopeNames.FULL).findAll(query) | ||
377 | } | ||
378 | |||
379 | static loadLocalByName (preferredUsername: string, transaction?: Transaction): Promise<MActorFull> { | ||
380 | const fun = () => { | ||
381 | const query = { | ||
382 | where: { | ||
383 | preferredUsername, | ||
384 | serverId: null | ||
385 | }, | ||
386 | transaction | ||
387 | } | ||
388 | |||
389 | return ActorModel.scope(ScopeNames.FULL) | ||
390 | .findOne(query) | ||
391 | } | ||
392 | |||
393 | return ModelCache.Instance.doCache({ | ||
394 | cacheType: 'local-actor-name', | ||
395 | key: preferredUsername, | ||
396 | // The server actor never change, so we can easily cache it | ||
397 | whitelist: () => preferredUsername === SERVER_ACTOR_NAME, | ||
398 | fun | ||
399 | }) | ||
400 | } | ||
401 | |||
402 | static loadLocalUrlByName (preferredUsername: string, transaction?: Transaction): Promise<MActorUrl> { | ||
403 | const fun = () => { | ||
404 | const query = { | ||
405 | attributes: [ 'url' ], | ||
406 | where: { | ||
407 | preferredUsername, | ||
408 | serverId: null | ||
409 | }, | ||
410 | transaction | ||
411 | } | ||
412 | |||
413 | return ActorModel.unscoped() | ||
414 | .findOne(query) | ||
415 | } | ||
416 | |||
417 | return ModelCache.Instance.doCache({ | ||
418 | cacheType: 'local-actor-name', | ||
419 | key: preferredUsername, | ||
420 | // The server actor never change, so we can easily cache it | ||
421 | whitelist: () => preferredUsername === SERVER_ACTOR_NAME, | ||
422 | fun | ||
423 | }) | ||
424 | } | ||
425 | |||
426 | static loadByNameAndHost (preferredUsername: string, host: string): Promise<MActorFull> { | ||
427 | const query = { | ||
428 | where: { | ||
429 | preferredUsername | ||
430 | }, | ||
431 | include: [ | ||
432 | { | ||
433 | model: ServerModel, | ||
434 | required: true, | ||
435 | where: { | ||
436 | host | ||
437 | } | ||
438 | } | ||
439 | ] | ||
440 | } | ||
441 | |||
442 | return ActorModel.scope(ScopeNames.FULL).findOne(query) | ||
443 | } | ||
444 | |||
445 | static loadByUrl (url: string, transaction?: Transaction): Promise<MActorAccountChannelId> { | ||
446 | const query = { | ||
447 | where: { | ||
448 | url | ||
449 | }, | ||
450 | transaction, | ||
451 | include: [ | ||
452 | { | ||
453 | attributes: [ 'id' ], | ||
454 | model: AccountModel.unscoped(), | ||
455 | required: false | ||
456 | }, | ||
457 | { | ||
458 | attributes: [ 'id' ], | ||
459 | model: VideoChannelModel.unscoped(), | ||
460 | required: false | ||
461 | } | ||
462 | ] | ||
463 | } | ||
464 | |||
465 | return ActorModel.unscoped().findOne(query) | ||
466 | } | ||
467 | |||
468 | static loadByUrlAndPopulateAccountAndChannel (url: string, transaction?: Transaction): Promise<MActorFull> { | ||
469 | const query = { | ||
470 | where: { | ||
471 | url | ||
472 | }, | ||
473 | transaction | ||
474 | } | ||
475 | |||
476 | return ActorModel.scope(ScopeNames.FULL).findOne(query) | ||
477 | } | ||
478 | |||
479 | static rebuildFollowsCount (ofId: number, type: 'followers' | 'following', transaction?: Transaction) { | ||
480 | const sanitizedOfId = parseInt(ofId + '', 10) | ||
481 | const where = { id: sanitizedOfId } | ||
482 | |||
483 | let columnToUpdate: string | ||
484 | let columnOfCount: string | ||
485 | |||
486 | if (type === 'followers') { | ||
487 | columnToUpdate = 'followersCount' | ||
488 | columnOfCount = 'targetActorId' | ||
489 | } else { | ||
490 | columnToUpdate = 'followingCount' | ||
491 | columnOfCount = 'actorId' | ||
492 | } | ||
493 | |||
494 | return ActorModel.update({ | ||
495 | [columnToUpdate]: literal(`(SELECT COUNT(*) FROM "actorFollow" WHERE "${columnOfCount}" = ${sanitizedOfId})`) | ||
496 | }, { where, transaction }) | ||
497 | } | ||
498 | |||
499 | static loadAccountActorByVideoId (videoId: number): Promise<MActor> { | ||
500 | const query = { | ||
501 | include: [ | ||
502 | { | ||
503 | attributes: [ 'id' ], | ||
504 | model: AccountModel.unscoped(), | ||
505 | required: true, | ||
506 | include: [ | ||
507 | { | ||
508 | attributes: [ 'id', 'accountId' ], | ||
509 | model: VideoChannelModel.unscoped(), | ||
510 | required: true, | ||
511 | include: [ | ||
512 | { | ||
513 | attributes: [ 'id', 'channelId' ], | ||
514 | model: VideoModel.unscoped(), | ||
515 | where: { | ||
516 | id: videoId | ||
517 | } | ||
518 | } | ||
519 | ] | ||
520 | } | ||
521 | ] | ||
522 | } | ||
523 | ] | ||
524 | } | ||
525 | |||
526 | return ActorModel.unscoped().findOne(query) | ||
527 | } | ||
528 | |||
529 | getSharedInbox (this: MActorWithInboxes) { | ||
530 | return this.sharedInboxUrl || this.inboxUrl | ||
531 | } | ||
532 | |||
533 | toFormattedSummaryJSON (this: MActorSummaryFormattable) { | ||
534 | let avatar: ActorImage = null | ||
535 | if (this.Avatar) { | ||
536 | avatar = this.Avatar.toFormattedJSON() | ||
537 | } | ||
538 | |||
539 | return { | ||
540 | url: this.url, | ||
541 | name: this.preferredUsername, | ||
542 | host: this.getHost(), | ||
543 | avatar | ||
544 | } | ||
545 | } | ||
546 | |||
547 | toFormattedJSON (this: MActorFormattable) { | ||
548 | const base = this.toFormattedSummaryJSON() | ||
549 | |||
550 | let banner: ActorImage = null | ||
551 | if (this.Banner) { | ||
552 | banner = this.Banner.toFormattedJSON() | ||
553 | } | ||
554 | |||
555 | return Object.assign(base, { | ||
556 | id: this.id, | ||
557 | hostRedundancyAllowed: this.getRedundancyAllowed(), | ||
558 | followingCount: this.followingCount, | ||
559 | followersCount: this.followersCount, | ||
560 | banner, | ||
561 | createdAt: this.getCreatedAt() | ||
562 | }) | ||
563 | } | ||
564 | |||
565 | toActivityPubObject (this: MActorAPChannel | MActorAPAccount, name: string) { | ||
566 | let icon: ActivityIconObject | ||
567 | let image: ActivityIconObject | ||
568 | |||
569 | if (this.avatarId) { | ||
570 | const extension = extname(this.Avatar.filename) | ||
571 | |||
572 | icon = { | ||
573 | type: 'Image', | ||
574 | mediaType: MIMETYPES.IMAGE.EXT_MIMETYPE[extension], | ||
575 | height: this.Avatar.height, | ||
576 | width: this.Avatar.width, | ||
577 | url: this.getAvatarUrl() | ||
578 | } | ||
579 | } | ||
580 | |||
581 | if (this.bannerId) { | ||
582 | const banner = (this as MActorAPChannel).Banner | ||
583 | const extension = extname(banner.filename) | ||
584 | |||
585 | image = { | ||
586 | type: 'Image', | ||
587 | mediaType: MIMETYPES.IMAGE.EXT_MIMETYPE[extension], | ||
588 | height: banner.height, | ||
589 | width: banner.width, | ||
590 | url: this.getBannerUrl() | ||
591 | } | ||
592 | } | ||
593 | |||
594 | const json = { | ||
595 | type: this.type, | ||
596 | id: this.url, | ||
597 | following: this.getFollowingUrl(), | ||
598 | followers: this.getFollowersUrl(), | ||
599 | playlists: this.getPlaylistsUrl(), | ||
600 | inbox: this.inboxUrl, | ||
601 | outbox: this.outboxUrl, | ||
602 | preferredUsername: this.preferredUsername, | ||
603 | url: this.url, | ||
604 | name, | ||
605 | endpoints: { | ||
606 | sharedInbox: this.sharedInboxUrl | ||
607 | }, | ||
608 | publicKey: { | ||
609 | id: this.getPublicKeyUrl(), | ||
610 | owner: this.url, | ||
611 | publicKeyPem: this.publicKey | ||
612 | }, | ||
613 | published: this.getCreatedAt().toISOString(), | ||
614 | icon, | ||
615 | image | ||
616 | } | ||
617 | |||
618 | return activityPubContextify(json) | ||
619 | } | ||
620 | |||
621 | getFollowerSharedInboxUrls (t: Transaction) { | ||
622 | const query = { | ||
623 | attributes: [ 'sharedInboxUrl' ], | ||
624 | include: [ | ||
625 | { | ||
626 | attribute: [], | ||
627 | model: ActorFollowModel.unscoped(), | ||
628 | required: true, | ||
629 | as: 'ActorFollowing', | ||
630 | where: { | ||
631 | state: 'accepted', | ||
632 | targetActorId: this.id | ||
633 | } | ||
634 | } | ||
635 | ], | ||
636 | transaction: t | ||
637 | } | ||
638 | |||
639 | return ActorModel.findAll(query) | ||
640 | .then(accounts => accounts.map(a => a.sharedInboxUrl)) | ||
641 | } | ||
642 | |||
643 | getFollowingUrl () { | ||
644 | return this.url + '/following' | ||
645 | } | ||
646 | |||
647 | getFollowersUrl () { | ||
648 | return this.url + '/followers' | ||
649 | } | ||
650 | |||
651 | getPlaylistsUrl () { | ||
652 | return this.url + '/playlists' | ||
653 | } | ||
654 | |||
655 | getPublicKeyUrl () { | ||
656 | return this.url + '#main-key' | ||
657 | } | ||
658 | |||
659 | isOwned () { | ||
660 | return this.serverId === null | ||
661 | } | ||
662 | |||
663 | getWebfingerUrl (this: MActorServer) { | ||
664 | return 'acct:' + this.preferredUsername + '@' + this.getHost() | ||
665 | } | ||
666 | |||
667 | getIdentifier () { | ||
668 | return this.Server ? `${this.preferredUsername}@${this.Server.host}` : this.preferredUsername | ||
669 | } | ||
670 | |||
671 | getHost (this: MActorHost) { | ||
672 | return this.Server ? this.Server.host : WEBSERVER.HOST | ||
673 | } | ||
674 | |||
675 | getRedundancyAllowed () { | ||
676 | return this.Server ? this.Server.redundancyAllowed : false | ||
677 | } | ||
678 | |||
679 | getAvatarUrl () { | ||
680 | if (!this.avatarId) return undefined | ||
681 | |||
682 | return WEBSERVER.URL + this.Avatar.getStaticPath() | ||
683 | } | ||
684 | |||
685 | getBannerUrl () { | ||
686 | if (!this.bannerId) return undefined | ||
687 | |||
688 | return WEBSERVER.URL + this.Banner.getStaticPath() | ||
689 | } | ||
690 | |||
691 | isOutdated () { | ||
692 | if (this.isOwned()) return false | ||
693 | |||
694 | return isOutdated(this, ACTIVITY_PUB.ACTOR_REFRESH_INTERVAL) | ||
695 | } | ||
696 | |||
697 | getCreatedAt (this: MActorAPChannel | MActorAPAccount | MActorFormattable) { | ||
698 | return this.remoteCreatedAt || this.createdAt | ||
699 | } | ||
700 | } | ||