From 8979e779da7c3cb4f815282fc9e95b520e782366 Mon Sep 17 00:00:00 2001 From: Johann150 Date: Sat, 12 Nov 2022 15:09:50 +0100 Subject: [PATCH] server: optimise follower inboxes query Use the distinct query thingy so we don't have to make the Set work so hard. This is also uniform code with the "everyone" above so should hopefully be easier to understand. --- .../src/remote/activitypub/deliver-manager.ts | 30 +++++++------------ 1 file changed, 11 insertions(+), 19 deletions(-) diff --git a/packages/backend/src/remote/activitypub/deliver-manager.ts b/packages/backend/src/remote/activitypub/deliver-manager.ts index 55054bf35..c706968df 100644 --- a/packages/backend/src/remote/activitypub/deliver-manager.ts +++ b/packages/backend/src/remote/activitypub/deliver-manager.ts @@ -119,26 +119,18 @@ export default class DeliverManager { if (this.recipes.some(r => isFollowers(r))) { // followers deliver - // TODO: SELECT DISTINCT ON ("followerSharedInbox") "followerSharedInbox" みたいな問い合わせにすればよりパフォーマンス向上できそう - // ただ、sharedInboxがnullなリモートユーザーも稀におり、その対応ができなさそう? - const followers = await Followings.find({ - where: { - followeeId: this.actor.id, - followerHost: Not(IsNull()), - }, - select: { - followerSharedInbox: true, - followerInbox: true, - }, - }) as { - followerSharedInbox: string | null; - followerInbox: string; - }[]; + const followers = await Followings.createQueryBuilder('followings') + // return either the shared inbox (if available) or the individual inbox + .select('COALESCE(followings.followerSharedInbox, followings.followerInbox)', 'inbox') + // so we don't have to make our inboxes Set work as hard + .distinct(true) + // ...for the specific actors followers + .where('followings.followeeId = :actorId', { actorId: this.actor.id }) + // don't deliver to ourselves + .andWhere('followings.followerHost IS NOT NULL') + .getRawMany(); - for (const following of followers) { - const inbox = following.followerSharedInbox || following.followerInbox; - inboxes.add(inbox); - } + followers.forEach(({ inbox }) => inboxes.add(inbox)); } this.recipes.filter((recipe): recipe is IDirectRecipe =>