diff options
Diffstat (limited to 'server/models/activitypub/actor.ts')
-rw-r--r-- | server/models/activitypub/actor.ts | 101 |
1 files changed, 77 insertions, 24 deletions
diff --git a/server/models/activitypub/actor.ts b/server/models/activitypub/actor.ts index 007647ced..e547d2c0c 100644 --- a/server/models/activitypub/actor.ts +++ b/server/models/activitypub/actor.ts | |||
@@ -16,7 +16,7 @@ import { | |||
16 | Table, | 16 | Table, |
17 | UpdatedAt | 17 | UpdatedAt |
18 | } from 'sequelize-typescript' | 18 | } from 'sequelize-typescript' |
19 | import { ActivityPubActorType } from '../../../shared/models/activitypub' | 19 | import { ActivityIconObject, ActivityPubActorType } from '../../../shared/models/activitypub' |
20 | import { Avatar } from '../../../shared/models/avatars/avatar.model' | 20 | import { Avatar } from '../../../shared/models/avatars/avatar.model' |
21 | import { activityPubContextify } from '../../helpers/activitypub' | 21 | import { activityPubContextify } from '../../helpers/activitypub' |
22 | import { | 22 | import { |
@@ -43,11 +43,12 @@ import { | |||
43 | MActorFull, | 43 | MActorFull, |
44 | MActorHost, | 44 | MActorHost, |
45 | MActorServer, | 45 | MActorServer, |
46 | MActorSummaryFormattable, | 46 | MActorSummaryFormattable, MActorUrl, |
47 | MActorWithInboxes | 47 | MActorWithInboxes |
48 | } from '../../typings/models' | 48 | } from '../../typings/models' |
49 | import * as Bluebird from 'bluebird' | 49 | import * as Bluebird from 'bluebird' |
50 | import { Op, Transaction, literal } from 'sequelize' | 50 | import { Op, Transaction, literal } from 'sequelize' |
51 | import { ModelCache } from '@server/models/model-cache' | ||
51 | 52 | ||
52 | enum ScopeNames { | 53 | enum ScopeNames { |
53 | FULL = 'FULL' | 54 | FULL = 'FULL' |
@@ -276,8 +277,6 @@ export class ActorModel extends Model<ActorModel> { | |||
276 | }) | 277 | }) |
277 | VideoChannel: VideoChannelModel | 278 | VideoChannel: VideoChannelModel |
278 | 279 | ||
279 | private static cache: { [ id: string ]: any } = {} | ||
280 | |||
281 | static load (id: number): Bluebird<MActor> { | 280 | static load (id: number): Bluebird<MActor> { |
282 | return ActorModel.unscoped().findByPk(id) | 281 | return ActorModel.unscoped().findByPk(id) |
283 | } | 282 | } |
@@ -334,7 +333,7 @@ export class ActorModel extends Model<ActorModel> { | |||
334 | const query = { | 333 | const query = { |
335 | where: { | 334 | where: { |
336 | followersUrl: { | 335 | followersUrl: { |
337 | [ Op.in ]: followersUrls | 336 | [Op.in]: followersUrls |
338 | } | 337 | } |
339 | }, | 338 | }, |
340 | transaction | 339 | transaction |
@@ -344,28 +343,50 @@ export class ActorModel extends Model<ActorModel> { | |||
344 | } | 343 | } |
345 | 344 | ||
346 | static loadLocalByName (preferredUsername: string, transaction?: Transaction): Bluebird<MActorFull> { | 345 | static loadLocalByName (preferredUsername: string, transaction?: Transaction): Bluebird<MActorFull> { |
347 | // The server actor never change, so we can easily cache it | 346 | const fun = () => { |
348 | if (preferredUsername === SERVER_ACTOR_NAME && ActorModel.cache[preferredUsername]) { | 347 | const query = { |
349 | return Bluebird.resolve(ActorModel.cache[preferredUsername]) | 348 | where: { |
350 | } | 349 | preferredUsername, |
350 | serverId: null | ||
351 | }, | ||
352 | transaction | ||
353 | } | ||
351 | 354 | ||
352 | const query = { | 355 | return ActorModel.scope(ScopeNames.FULL) |
353 | where: { | 356 | .findOne(query) |
354 | preferredUsername, | ||
355 | serverId: null | ||
356 | }, | ||
357 | transaction | ||
358 | } | 357 | } |
359 | 358 | ||
360 | return ActorModel.scope(ScopeNames.FULL) | 359 | return ModelCache.Instance.doCache({ |
361 | .findOne(query) | 360 | cacheType: 'local-actor-name', |
362 | .then(actor => { | 361 | key: preferredUsername, |
363 | if (preferredUsername === SERVER_ACTOR_NAME) { | 362 | // The server actor never change, so we can easily cache it |
364 | ActorModel.cache[ preferredUsername ] = actor | 363 | whitelist: () => preferredUsername === SERVER_ACTOR_NAME, |
365 | } | 364 | fun |
365 | }) | ||
366 | } | ||
366 | 367 | ||
367 | return actor | 368 | static loadLocalUrlByName (preferredUsername: string, transaction?: Transaction): Bluebird<MActorUrl> { |
368 | }) | 369 | const fun = () => { |
370 | const query = { | ||
371 | attributes: [ 'url' ], | ||
372 | where: { | ||
373 | preferredUsername, | ||
374 | serverId: null | ||
375 | }, | ||
376 | transaction | ||
377 | } | ||
378 | |||
379 | return ActorModel.unscoped() | ||
380 | .findOne(query) | ||
381 | } | ||
382 | |||
383 | return ModelCache.Instance.doCache({ | ||
384 | cacheType: 'local-actor-name', | ||
385 | key: preferredUsername, | ||
386 | // The server actor never change, so we can easily cache it | ||
387 | whitelist: () => preferredUsername === SERVER_ACTOR_NAME, | ||
388 | fun | ||
389 | }) | ||
369 | } | 390 | } |
370 | 391 | ||
371 | static loadByNameAndHost (preferredUsername: string, host: string): Bluebird<MActorFull> { | 392 | static loadByNameAndHost (preferredUsername: string, host: string): Bluebird<MActorFull> { |
@@ -441,6 +462,36 @@ export class ActorModel extends Model<ActorModel> { | |||
441 | }, { where, transaction }) | 462 | }, { where, transaction }) |
442 | } | 463 | } |
443 | 464 | ||
465 | static loadAccountActorByVideoId (videoId: number): Bluebird<MActor> { | ||
466 | const query = { | ||
467 | include: [ | ||
468 | { | ||
469 | attributes: [ 'id' ], | ||
470 | model: AccountModel.unscoped(), | ||
471 | required: true, | ||
472 | include: [ | ||
473 | { | ||
474 | attributes: [ 'id', 'accountId' ], | ||
475 | model: VideoChannelModel.unscoped(), | ||
476 | required: true, | ||
477 | include: [ | ||
478 | { | ||
479 | attributes: [ 'id', 'channelId' ], | ||
480 | model: VideoModel.unscoped(), | ||
481 | where: { | ||
482 | id: videoId | ||
483 | } | ||
484 | } | ||
485 | ] | ||
486 | } | ||
487 | ] | ||
488 | } | ||
489 | ] | ||
490 | } | ||
491 | |||
492 | return ActorModel.unscoped().findOne(query) | ||
493 | } | ||
494 | |||
444 | getSharedInbox (this: MActorWithInboxes) { | 495 | getSharedInbox (this: MActorWithInboxes) { |
445 | return this.sharedInboxUrl || this.inboxUrl | 496 | return this.sharedInboxUrl || this.inboxUrl |
446 | } | 497 | } |
@@ -473,9 +524,11 @@ export class ActorModel extends Model<ActorModel> { | |||
473 | } | 524 | } |
474 | 525 | ||
475 | toActivityPubObject (this: MActorAP, name: string) { | 526 | toActivityPubObject (this: MActorAP, name: string) { |
476 | let icon = undefined | 527 | let icon: ActivityIconObject |
528 | |||
477 | if (this.avatarId) { | 529 | if (this.avatarId) { |
478 | const extension = extname(this.Avatar.filename) | 530 | const extension = extname(this.Avatar.filename) |
531 | |||
479 | icon = { | 532 | icon = { |
480 | type: 'Image', | 533 | type: 'Image', |
481 | mediaType: extension === '.png' ? 'image/png' : 'image/jpeg', | 534 | mediaType: extension === '.png' ? 'image/png' : 'image/jpeg', |