Refactor: 判定部分を共通化

This commit is contained in:
taichan 2024-02-20 15:36:30 +09:00
parent ff7f7c8835
commit f5ae6630bd
No known key found for this signature in database
GPG Key ID: CCE28623AFA24E9C
1 changed files with 26 additions and 22 deletions

View File

@ -297,20 +297,14 @@ export class NotificationEntityService implements OnModuleInit {
} }
/** /**
* notifierが存在するか * notifierが存在するかvalidator
*/ */
async #isValidNotifier <T extends MiNotification | MiGroupedNotification> ( async #validateNotifier <T extends MiNotification | MiGroupedNotification> (
notification: T, notification: T,
meId: MiUser['id'], meId: MiUser['id'],
userIdsWhoMeMuting: Set<MiUser['id']>,
userMutedInstances: Set<string>,
): Promise<boolean> { ): Promise<boolean> {
const [
userIdsWhoMeMuting,
userMutedInstances,
] = await Promise.all([
this.cacheService.userMutingsCache.fetch(meId),
this.cacheService.userProfileCache.fetch(meId).then(p => new Set(p.mutedInstances)),
]);
if (!('notifierId' in notification)) return true; if (!('notifierId' in notification)) return true;
if (userIdsWhoMeMuting.has(notification.notifierId)) return false; if (userIdsWhoMeMuting.has(notification.notifierId)) return false;
@ -324,7 +318,25 @@ export class NotificationEntityService implements OnModuleInit {
} }
/** /**
* notifierが存在するか * notifierが存在するか
*/
async #isValidNotifier <T extends MiNotification | MiGroupedNotification> (
notification: T,
meId: MiUser['id'],
) : Promise<boolean> {
const [
userIdsWhoMeMuting,
userMutedInstances,
] = await Promise.all([
this.cacheService.userMutingsCache.fetch(meId),
this.cacheService.userProfileCache.fetch(meId).then(p => new Set(p.mutedInstances)),
]);
return this.#validateNotifier(notification, meId, userIdsWhoMeMuting, userMutedInstances);
}
/**
* notifierが存在するか
*/ */
async #filterValidNotifier <T extends MiNotification | MiGroupedNotification> ( async #filterValidNotifier <T extends MiNotification | MiGroupedNotification> (
notifications: T[], notifications: T[],
@ -339,16 +351,8 @@ export class NotificationEntityService implements OnModuleInit {
]); ]);
const filteredNotifications = ((await Promise.all(notifications.map(async (notification) => { const filteredNotifications = ((await Promise.all(notifications.map(async (notification) => {
if (!('notifierId' in notification)) return notification; const isValid = await this.#validateNotifier(notification, meId, userIdsWhoMeMuting, userMutedInstances);
if (userIdsWhoMeMuting.has(notification.notifierId)) return null; return isValid ? notification : null;
const notifier = await this.usersRepository.findOneBy({ id: notification.notifierId });
if (notifier === null) return null;
if (notifier.host && userMutedInstances.has(notifier.host)) return null;
if (notifier.isSuspended) return null;
return notification;
}))) as [T|null] ).filter((notification): notification is T => notification !== null); }))) as [T|null] ).filter((notification): notification is T => notification !== null);
return filteredNotifications; return filteredNotifications;