From cdf1a5de8f0d03b9984502c3e95dcbd3ab728a5f Mon Sep 17 00:00:00 2001 From: Hazelnoot Date: Thu, 6 Nov 2025 22:04:48 -0500 Subject: [PATCH] simplify bulk updateUsers event to reduce code duplication --- packages/backend/src/core/CacheService.ts | 72 +++++++---------------- 1 file changed, 21 insertions(+), 51 deletions(-) diff --git a/packages/backend/src/core/CacheService.ts b/packages/backend/src/core/CacheService.ts index 82952a38c7..e144960ea3 100644 --- a/packages/backend/src/core/CacheService.ts +++ b/packages/backend/src/core/CacheService.ts @@ -356,7 +356,7 @@ export class CacheService implements OnApplicationShutdown { // TODO bulk fetcher }); - this.internalEventService.on('usersUpdated', this.onBulkUserEvent); + this.internalEventService.on('usersUpdated', this.onUserEvent); this.internalEventService.on('userChangeSuspendedState', this.onUserEvent); this.internalEventService.on('userChangeDeletedState', this.onUserEvent); this.internalEventService.on('remoteUserUpdated', this.onUserEvent); @@ -377,75 +377,45 @@ export class CacheService implements OnApplicationShutdown { } @bindThis - private async onBulkUserEvent(body: InternalEventTypes[E], _: E, isLocal: boolean): Promise { - if (body.ids.length === 0) return; + private async onUserEvent(body: InternalEventTypes[E], _: E, isLocal: boolean): Promise { + const ids = 'ids' in body ? body.ids : [body.id]; + if (ids.length === 0) return; // Update quantum caches if (isLocal) { // Contains IDs of all lists where this user is a member. const userListMemberships = this.listUserMembershipsCache .entries() - .filter(e => body.ids.some(id => e[1].has(id))) + .filter(e => ids.some(id => e[1].has(id))) .map(e => e[0]) .toArray(); await Promise.all([ - this.userProfileCache.deleteMany(body.ids), - this.userMutingsCache.deleteMany(body.ids), - this.userMutedCache.deleteMany(body.ids), - this.userBlockingCache.deleteMany(body.ids), - this.userBlockedCache.deleteMany(body.ids), - this.renoteMutingsCache.deleteMany(body.ids), - this.userFollowingsCache.deleteMany(body.ids), - this.userFollowersCache.deleteMany(body.ids), - this.hibernatedUserCache.deleteMany(body.ids), - this.threadMutingsCache.deleteMany(body.ids), - this.noteMutingsCache.deleteMany(body.ids), - this.userListMembershipsCache.deleteMany(body.ids), + this.userProfileCache.deleteMany(ids), + this.userMutingsCache.deleteMany(ids), + this.userBlockingCache.deleteMany(ids), + this.userBlockedCache.deleteMany(ids), + this.renoteMutingsCache.deleteMany(ids), + this.userFollowingsCache.deleteMany(ids), + this.userFollowersCache.deleteMany(ids), + this.hibernatedUserCache.deleteMany(ids), + this.threadMutingsCache.deleteMany(ids), + this.noteMutingsCache.deleteMany(ids), + this.userListMembershipsCache.deleteMany(ids), this.listUserMembershipsCache.deleteMany(userListMemberships), ]); } // Update other caches - const users = await this.usersRepository.findBy({ id: In(body.ids) }); - for (const id of body.ids) { + const users = await this.usersRepository.findBy({ + id: ids.length === 1 ? ids[0] : In(ids), + }); + for (const id of ids) { const user = users.find(u => u.id === id); this.updateMkUserCaches({ id }, user ?? null); } } - @bindThis - private async onUserEvent(body: InternalEventTypes[E], _: E, isLocal: boolean): Promise { - // Update quantum caches - if (isLocal) { - // Contains IDs of all lists where this user is a member. - const userListMemberships = this.listUserMembershipsCache - .entries() - .filter(e => e[1].has(body.id)) - .map(e => e[0]) - .toArray(); - - await Promise.all([ - this.userProfileCache.delete(body.id), - this.userMutingsCache.delete(body.id), - this.userBlockingCache.delete(body.id), - this.userBlockedCache.delete(body.id), - this.renoteMutingsCache.delete(body.id), - this.userFollowingsCache.delete(body.id), - this.userFollowersCache.delete(body.id), - this.hibernatedUserCache.delete(body.id), - this.threadMutingsCache.delete(body.id), - this.noteMutingsCache.delete(body.id), - this.userListMembershipsCache.delete(body.id), - this.listUserMembershipsCache.deleteMany(userListMemberships), - ]); - } - - // Update other caches - const user = await this.usersRepository.findOneBy({ id: body.id }); - this.updateMkUserCaches(body, user); - } - // This is here purely to help git line up MK's original code with our changes @bindThis private updateMkUserCaches(body: { id: string }, user: MiUser | null): void { @@ -711,7 +681,7 @@ export class CacheService implements OnApplicationShutdown { @bindThis public dispose(): void { - this.internalEventService.off('usersUpdated', this.onBulkUserEvent); + this.internalEventService.off('usersUpdated', this.onUserEvent); this.internalEventService.off('userChangeSuspendedState', this.onUserEvent); this.internalEventService.off('userChangeDeletedState', this.onUserEvent); this.internalEventService.off('remoteUserUpdated', this.onUserEvent);