From e2443d8dc502bfa708f5c74e4389cbf9d0e419c9 Mon Sep 17 00:00:00 2001 From: tamaina Date: Wed, 21 Feb 2024 18:08:27 +0000 Subject: [PATCH] refactor --- .../entities/NotificationEntityService.ts | 88 ++++++++----------- .../api/endpoints/i/notifications-grouped.ts | 11 +-- .../server/api/endpoints/i/notifications.ts | 11 +-- 3 files changed, 39 insertions(+), 71 deletions(-) diff --git a/packages/backend/src/core/entities/NotificationEntityService.ts b/packages/backend/src/core/entities/NotificationEntityService.ts index 7bf908ec33..f5df3d9431 100644 --- a/packages/backend/src/core/entities/NotificationEntityService.ts +++ b/packages/backend/src/core/entities/NotificationEntityService.ts @@ -16,6 +16,8 @@ import { bindThis } from '@/decorators.js'; import { isNotNull } from '@/misc/is-not-null.js'; import { FilterUnionByProperty, groupedNotificationTypes } from '@/types.js'; import { CacheService } from '@/core/CacheService.js'; +import { NoteReadService } from '@/core/NoteReadService.js'; +import { trackPromise } from '@/misc/promise-tracker.js'; import { RoleEntityService } from './RoleEntityService.js'; import type { OnModuleInit } from '@nestjs/common'; import type { UserEntityService } from './UserEntityService.js'; @@ -42,6 +44,7 @@ export class NotificationEntityService implements OnModuleInit { private followRequestsRepository: FollowRequestsRepository, private cacheService: CacheService, + private noteReadService: NoteReadService, //private userEntityService: UserEntityService, //private noteEntityService: NoteEntityService, @@ -82,9 +85,7 @@ export class NotificationEntityService implements OnModuleInit { }) ) : undefined; // if the note has been deleted, don't show this notification - if (needsNote && !noteIfNeed) { - return null; - } + if (needsNote && !noteIfNeed) return null; const needsUser = 'notifierId' in notification; const userIfNeed = needsUser ? ( @@ -93,9 +94,7 @@ export class NotificationEntityService implements OnModuleInit { : this.userEntityService.pack(notification.notifierId, { id: meId }) ) : undefined; // if the user has been deleted, don't show this notification - if (needsUser && !userIfNeed) { - return null; - } + if (needsUser && !userIfNeed) return null; // #region Grouped notifications if (notification.type === 'reaction:grouped') { @@ -178,6 +177,7 @@ export class NotificationEntityService implements OnModuleInit { async #packManyInternal ( notifications: T[], meId: MiUser['id'], + markNotesAsRead = false, ): Promise { if (notifications.length === 0) return []; @@ -218,39 +218,29 @@ export class NotificationEntityService implements OnModuleInit { validNotifications = validNotifications.filter(x => (x.type !== 'receiveFollowRequest') || reqs.some(r => r.followerId === x.notifierId)); } - return (await Promise.all(validNotifications.map(x => this.packGrouped(x, meId, { checkValidNotifier: false }, { - packedNotes, - packedUsers, - })))).filter(isNotNull); + const packPromises = validNotifications.map(x => { + return this.pack( + x, + meId, + { checkValidNotifier: false }, + { packedNotes, packedUsers }, + ); + }); + + if (markNotesAsRead) { + try { + trackPromise(this.noteReadService.read(meId, notes)); + } catch (e) { + // console.error('error thrown by NoteReadService.read', e); + } + } + + return (await Promise.all(packPromises)).filter(isNotNull); } @bindThis public async pack( - src: MiNotification, - meId: MiUser['id'], - // eslint-disable-next-line @typescript-eslint/ban-types - options: { - checkValidNotifier?: boolean; - }, - hint?: { - packedNotes: Map>; - packedUsers: Map>; - }, - ): Promise | null> { - return this.#packInternal(src, meId, options, hint); - } - - @bindThis - public async packMany( - notifications: MiNotification[], - meId: MiUser['id'], - ): Promise { - return await this.#packManyInternal(notifications, meId); - } - - @bindThis - public async packGrouped( - src: MiGroupedNotification, + src: MiNotification | MiGroupedNotification, meId: MiUser['id'], // eslint-disable-next-line @typescript-eslint/ban-types options: { @@ -264,12 +254,22 @@ export class NotificationEntityService implements OnModuleInit { return await this.#packInternal(src, meId, options, hint); } + @bindThis + public async packMany( + notifications: MiNotification[], + meId: MiUser['id'], + markNotesAsRead = false, + ): Promise { + return await this.#packManyInternal(notifications, meId, markNotesAsRead); + } + @bindThis public async packGroupedMany( notifications: MiGroupedNotification[], meId: MiUser['id'], + markNotesAsRead = false, ) : Promise { - return await this.#packManyInternal(notifications, meId); + return await this.#packManyInternal(notifications, meId, markNotesAsRead); } /** @@ -301,21 +301,7 @@ export class NotificationEntityService implements OnModuleInit { notification: T, meId: MiUser['id'], ) : Promise { - const [ - userIdsWhoMeMuting, - userMutedInstances, - ] = await Promise.all([ - this.cacheService.userMutingsCache.fetch(meId), - this.cacheService.userProfileCache.fetch(meId).then(p => new Set(p.mutedInstances)), - ]); - - 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); + return (await this.#filterValidNotifier([notification], meId)).length === 1; } /** diff --git a/packages/backend/src/server/api/endpoints/i/notifications-grouped.ts b/packages/backend/src/server/api/endpoints/i/notifications-grouped.ts index 715927e991..ff1d3c43e2 100644 --- a/packages/backend/src/server/api/endpoints/i/notifications-grouped.ts +++ b/packages/backend/src/server/api/endpoints/i/notifications-grouped.ts @@ -163,16 +163,7 @@ export default class extends Endpoint { // eslint- groupedNotifications = groupedNotifications.slice(0, ps.limit); - const noteIds = groupedNotifications - .filter((notification): notification is FilterUnionByProperty => ['mention', 'reply', 'quote'].includes(notification.type)) - .map(notification => notification.noteId!); - - if (noteIds.length > 0) { - const notes = await this.notesRepository.findBy({ id: In(noteIds) }); - this.noteReadService.read(me.id, notes); - } - - return await this.notificationEntityService.packGroupedMany(groupedNotifications, me.id); + return await this.notificationEntityService.packGroupedMany(groupedNotifications, me.id, true); }); } } diff --git a/packages/backend/src/server/api/endpoints/i/notifications.ts b/packages/backend/src/server/api/endpoints/i/notifications.ts index 52b6749e3f..efefbc2865 100644 --- a/packages/backend/src/server/api/endpoints/i/notifications.ts +++ b/packages/backend/src/server/api/endpoints/i/notifications.ts @@ -112,16 +112,7 @@ export default class extends Endpoint { // eslint- this.notificationService.readAllNotification(me.id); } - const noteIds = notifications - .filter((notification): notification is FilterUnionByProperty => ['mention', 'reply', 'quote'].includes(notification.type)) - .map(notification => notification.noteId); - - if (noteIds.length > 0) { - const notes = await this.notesRepository.findBy({ id: In(noteIds) }); - this.noteReadService.read(me.id, notes); - } - - return await this.notificationEntityService.packMany(notifications, me.id); + return await this.notificationEntityService.packMany(notifications, me.id, true); }); } }