From ad1bacc2e5f94926efe7d6441fb70549cf4a89e5 Mon Sep 17 00:00:00 2001 From: Namekuji Date: Mon, 17 Apr 2023 13:32:55 -0400 Subject: [PATCH] remove unnecessary joins --- .../backend/src/core/AccountMoveService.ts | 33 ++++++++----------- .../src/core/activitypub/ApInboxService.ts | 6 ++-- 2 files changed, 17 insertions(+), 22 deletions(-) diff --git a/packages/backend/src/core/AccountMoveService.ts b/packages/backend/src/core/AccountMoveService.ts index 6d12f682f1..55874908ee 100644 --- a/packages/backend/src/core/AccountMoveService.ts +++ b/packages/backend/src/core/AccountMoveService.ts @@ -141,24 +141,22 @@ export class AccountMoveService { // follow the new account and unfollow the old one const proxy = await this.proxyAccountService.fetch(); - const followings = await this.followingsRepository.find({ - relations: { - follower: true, - }, - where: { + const followings = await this.followingsRepository.findBy({ followeeId: src.id, followerHost: IsNull(), // follower is local followerId: proxy ? Not(proxy.id) : undefined, - }, }); const followJobs: RelationshipJobData[] = []; for (const following of followings) { - if (!following.follower) continue; - followJobs.push({ from: { id: following.follower.id }, to: { id: dst.id } }); + followJobs.push({ from: { id: following.followerId }, to: { id: dst.id } }); } // Decrease following count instead of unfollowing. - await this.adjustFollowingCounts(followJobs.map(job => job.from.id), src); + try { + await this.adjustFollowingCounts(followJobs.map(job => job.from.id), src); + } catch { + /* skip if any error happens */ + } // Should be queued because this can cause a number of follow per one move. this.queueService.createFollowJob(followJobs); @@ -168,19 +166,14 @@ export class AccountMoveService { public async copyBlocking(src: ThinUser, dst: ThinUser): Promise { // Followers shouldn't overlap with blockers, but the destination account, different from the blockee (i.e., old account), may have followed the local user before moving. // So block the destination account here. - const blockings = await this.blockingsRepository.find({ // FIXME: might be expensive - relations: { - blocker: true - }, - where: { - blockeeId: src.id - } - }); + const srcBlockings = await this.blockingsRepository.findBy({ blockeeId: src.id }); + const dstBlockings = await this.blockingsRepository.findBy({ blockeeId: dst.id }); + const blockerIds = dstBlockings.map(blocking => blocking.blockerId); // reblock the destination account const blockJobs: RelationshipJobData[] = []; - for (const blocking of blockings) { - if (!blocking.blocker) continue; - blockJobs.push({ from: { id: blocking.blocker.id }, to: { id: dst.id } }); + for (const blocking of srcBlockings) { + if (blockerIds.includes(blocking.blockerId)) continue; // skip if already blocked + blockJobs.push({ from: { id: blocking.blockerId }, to: { id: dst.id } }); } // no need to unblock the old account because it may be still functional this.queueService.createBlockJob(blockJobs); diff --git a/packages/backend/src/core/activitypub/ApInboxService.ts b/packages/backend/src/core/activitypub/ApInboxService.ts index 60539e58ee..9df6ffad7c 100644 --- a/packages/backend/src/core/activitypub/ApInboxService.ts +++ b/packages/backend/src/core/activitypub/ApInboxService.ts @@ -765,8 +765,10 @@ export class ApInboxService { } // add target uri to movedToUri in order to indicate that the user has moved - await this.usersRepository.update(oldAccount.id, { movedToUri: targetUri }); - oldAccount.movedToUri = targetUri; + if (oldAccount.movedToUri !== targetUri) { + await this.usersRepository.update(oldAccount.id, { movedToUri: targetUri }); + oldAccount.movedToUri = targetUri; + } // Move! await this.accountMoveService.move(oldAccount, newAccount);