From 1d44a27505c8852b5ecaeaa13a90a25535b6ce36 Mon Sep 17 00:00:00 2001 From: Hazelnoot Date: Wed, 25 Jun 2025 14:05:40 -0400 Subject: [PATCH] implement followingCountDelta and followersCountDelta in updateUserQueue --- packages/backend/src/core/CollapsedQueueService.ts | 7 +++++++ packages/backend/src/core/UserFollowingService.ts | 12 ++++-------- 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/packages/backend/src/core/CollapsedQueueService.ts b/packages/backend/src/core/CollapsedQueueService.ts index 9af084b55d..22a56a2346 100644 --- a/packages/backend/src/core/CollapsedQueueService.ts +++ b/packages/backend/src/core/CollapsedQueueService.ts @@ -29,6 +29,8 @@ export type UpdateInstanceJob = { export type UpdateUserJob = { updatedAt?: Date, notesCountDelta?: number, + followingCountDelta?: number, + followersCountDelta?: number, }; export type UpdateNoteJob = { @@ -97,10 +99,14 @@ export class CollapsedQueueService implements OnApplicationShutdown { (oldJob, newJob) => ({ updatedAt: maxDate(oldJob.updatedAt, newJob.updatedAt), notesCountDelta: (oldJob.notesCountDelta ?? 0) + (newJob.notesCountDelta ?? 0), + followingCountDelta: (oldJob.followingCountDelta ?? 0) + (newJob.followingCountDelta ?? 0), + followersCountDelta: (oldJob.followersCountDelta ?? 0) + (newJob.followersCountDelta ?? 0), }), (id, job) => this.usersRepository.update({ id }, { updatedAt: job.updatedAt, notesCount: job.notesCountDelta ? () => `"notesCount" + ${job.notesCountDelta}` : undefined, + followingCount: job.followingCountDelta ? () => `"followingCount" + ${job.followingCountDelta}` : undefined, + followersCount: job.followersCountDelta ? () => `"followersCount" + ${job.followersCountDelta}` : undefined, }), { onError: this.onQueueError, @@ -170,6 +176,7 @@ export class CollapsedQueueService implements OnApplicationShutdown { async onApplicationShutdown() { // TODO note/user delete events + // TODO remove updated events this.internalEventService.off('localUserUpdated', this.onUserUpdated); this.internalEventService.off('remoteUserUpdated', this.onUserUpdated); diff --git a/packages/backend/src/core/UserFollowingService.ts b/packages/backend/src/core/UserFollowingService.ts index 49c3539c1f..9a58d2d1fe 100644 --- a/packages/backend/src/core/UserFollowingService.ts +++ b/packages/backend/src/core/UserFollowingService.ts @@ -288,10 +288,8 @@ export class UserFollowingService implements OnModuleInit { // Neither followee nor follower has moved. if (!followeeUser.movedToUri && !followerUser.movedToUri) { //#region Increment counts - await Promise.all([ - this.usersRepository.increment({ id: follower.id }, 'followingCount', 1), - this.usersRepository.increment({ id: followee.id }, 'followersCount', 1), - ]); + this.collapsedQueueService.updateUserQueue.enqueue(follower.id, { followingCountDelta: 1 }); + this.collapsedQueueService.updateUserQueue.enqueue(followee.id, { followersCountDelta: 1 }); //#endregion //#region Update instance stats @@ -400,10 +398,8 @@ export class UserFollowingService implements OnModuleInit { // Neither followee nor follower has moved. if (!follower.movedToUri && !followee.movedToUri) { //#region Decrement following / followers counts - await Promise.all([ - this.usersRepository.decrement({ id: follower.id }, 'followingCount', 1), - this.usersRepository.decrement({ id: followee.id }, 'followersCount', 1), - ]); + this.collapsedQueueService.updateUserQueue.enqueue(follower.id, { followingCountDelta: -1 }); + this.collapsedQueueService.updateUserQueue.enqueue(followee.id, { followersCountDelta: -1 }); //#endregion //#region Update instance stats