simplify bulk updateUsers event to reduce code duplication

This commit is contained in:
Hazelnoot 2025-11-06 22:04:48 -05:00
parent 01bb4dc541
commit cdf1a5de8f

View file

@ -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<E extends 'usersUpdated'>(body: InternalEventTypes[E], _: E, isLocal: boolean): Promise<void> {
if (body.ids.length === 0) return;
private async onUserEvent<E extends 'userChangeSuspendedState' | 'userChangeDeletedState' | 'remoteUserUpdated' | 'localUserUpdated' | 'usersUpdated'>(body: InternalEventTypes[E], _: E, isLocal: boolean): Promise<void> {
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<E extends 'userChangeSuspendedState' | 'userChangeDeletedState' | 'remoteUserUpdated' | 'localUserUpdated'>(body: InternalEventTypes[E], _: E, isLocal: boolean): Promise<void> {
// 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);