From 54e740594bc2eacd8026b5d2d6cfdfc06416a65b Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Thu, 18 Jan 2018 14:59:27 +0100 Subject: Avoid too many requests and fetching outbox --- server/models/activitypub/actor.ts | 51 ++++++++++++++++++++++++++++++++++---- 1 file changed, 46 insertions(+), 5 deletions(-) (limited to 'server/models/activitypub/actor.ts') diff --git a/server/models/activitypub/actor.ts b/server/models/activitypub/actor.ts index 408d4df23..269149a31 100644 --- a/server/models/activitypub/actor.ts +++ b/server/models/activitypub/actor.ts @@ -167,17 +167,17 @@ export class ActorModel extends Model { }, onDelete: 'cascade' }) - AccountFollowing: ActorFollowModel[] + ActorFollowing: ActorFollowModel[] @HasMany(() => ActorFollowModel, { foreignKey: { name: 'targetActorId', allowNull: false }, - as: 'followers', + as: 'ActorFollowers', onDelete: 'cascade' }) - AccountFollowers: ActorFollowModel[] + ActorFollowers: ActorFollowModel[] @ForeignKey(() => ServerModel) @Column @@ -277,6 +277,45 @@ export class ActorModel extends Model { }) } + static async getActorsFollowerSharedInboxUrls (actors: ActorModel[], t: Sequelize.Transaction) { + const query = { + // attribute: [], + where: { + id: { + [Sequelize.Op.in]: actors.map(a => a.id) + } + }, + include: [ + { + // attributes: [ ], + model: ActorFollowModel.unscoped(), + required: true, + as: 'ActorFollowers', + where: { + state: 'accepted' + }, + include: [ + { + attributes: [ 'sharedInboxUrl' ], + model: ActorModel.unscoped(), + as: 'ActorFollower', + required: true + } + ] + } + ], + transaction: t + } + + const hash: { [ id: number ]: string[] } = {} + const res = await ActorModel.findAll(query) + for (const actor of res) { + hash[actor.id] = actor.ActorFollowers.map(follow => follow.ActorFollower.sharedInboxUrl) + } + + return hash + } + toFormattedJSON () { let avatar: Avatar = null if (this.Avatar) { @@ -347,10 +386,12 @@ export class ActorModel extends Model { attributes: [ 'sharedInboxUrl' ], include: [ { - model: ActorFollowModel, + attribute: [], + model: ActorFollowModel.unscoped(), required: true, - as: 'followers', + as: 'ActorFollowers', where: { + state: 'accepted', targetActorId: this.id } } -- cgit v1.2.3