From ed33ac714126e3c1a5c3d7a84e7bda368b53a20f Mon Sep 17 00:00:00 2001 From: taichanne30 Date: Thu, 22 Feb 2024 01:25:16 +0900 Subject: [PATCH] Refactor: less sql calls --- .../entities/NotificationEntityService.ts | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/packages/backend/src/core/entities/NotificationEntityService.ts b/packages/backend/src/core/entities/NotificationEntityService.ts index e2a6ab6e8e..7bf908ec33 100644 --- a/packages/backend/src/core/entities/NotificationEntityService.ts +++ b/packages/backend/src/core/entities/NotificationEntityService.ts @@ -279,11 +279,13 @@ export class NotificationEntityService implements OnModuleInit { notification: T, userIdsWhoMeMuting: Set, userMutedInstances: Set, + notifiers: MiUser[], ): Promise { if (!('notifierId' in notification)) return true; if (userIdsWhoMeMuting.has(notification.notifierId)) return false; - const notifier = await this.usersRepository.findOneBy({ id: notification.notifierId }); + const notifier = notifiers.find(x => x.id === notification.notifierId) ?? null; + if (notifier === null) return false; if (notifier.host && userMutedInstances.has(notifier.host)) return false; @@ -307,7 +309,13 @@ export class NotificationEntityService implements OnModuleInit { this.cacheService.userProfileCache.fetch(meId).then(p => new Set(p.mutedInstances)), ]); - return this.#validateNotifier(notification, userIdsWhoMeMuting, userMutedInstances); + const notifiers = (await Promise.all([notification] + .map(x => 'notifierId' in x ? x.notifierId : null) + .filter(isNotNull) + .map(async id => await this.usersRepository.findOneBy({ id })))) + .filter(isNotNull); + + return this.#validateNotifier(notification, userIdsWhoMeMuting, userMutedInstances, notifiers); } /** @@ -325,8 +333,13 @@ export class NotificationEntityService implements OnModuleInit { this.cacheService.userProfileCache.fetch(meId).then(p => new Set(p.mutedInstances)), ]); + const notifierIds = notifications.map(notification => 'notifierId' in notification ? notification.notifierId : null).filter(isNotNull); + const notifiers = notifierIds.length > 0 ? await this.usersRepository.find({ + where: { id: In(notifierIds) }, + }) : []; + const filteredNotifications = ((await Promise.all(notifications.map(async (notification) => { - const isValid = await this.#validateNotifier(notification, userIdsWhoMeMuting, userMutedInstances); + const isValid = await this.#validateNotifier(notification, userIdsWhoMeMuting, userMutedInstances, notifiers); return isValid ? notification : null; }))) as [T|null] ).filter(isNotNull);