From 11a6bd890cc752ee7abd2890773054743433eae1 Mon Sep 17 00:00:00 2001 From: tamaina Date: Sun, 12 Jun 2022 19:28:13 +0900 Subject: [PATCH] fix: some fixes of multiple notification read (#8819) * fix: limit multiple notification read * fix * fix --- .../src/server/api/common/read-notification.ts | 2 ++ .../src/server/api/endpoints/notifications/read.ts | 6 +++++- packages/sw/src/scripts/notification-read.ts | 12 +++++++++++- 3 files changed, 18 insertions(+), 2 deletions(-) diff --git a/packages/backend/src/server/api/common/read-notification.ts b/packages/backend/src/server/api/common/read-notification.ts index 0dad35bcc..8c4ba41a3 100644 --- a/packages/backend/src/server/api/common/read-notification.ts +++ b/packages/backend/src/server/api/common/read-notification.ts @@ -9,6 +9,8 @@ export async function readNotification( userId: User['id'], notificationIds: Notification['id'][] ) { + if (notificationIds.length === 0) return; + // Update documents await Notifications.update({ id: In(notificationIds), diff --git a/packages/backend/src/server/api/endpoints/notifications/read.ts b/packages/backend/src/server/api/endpoints/notifications/read.ts index 65e96d486..e7839b246 100644 --- a/packages/backend/src/server/api/endpoints/notifications/read.ts +++ b/packages/backend/src/server/api/endpoints/notifications/read.ts @@ -34,7 +34,11 @@ export const paramDef = { { type: 'object', properties: { - notificationIds: { type: 'array', items: { type: 'string', format: 'misskey:id' } }, + notificationIds: { + type: 'array', + items: { type: 'string', format: 'misskey:id' }, + maxItems: 100, + }, }, required: ['notificationIds'], }, diff --git a/packages/sw/src/scripts/notification-read.ts b/packages/sw/src/scripts/notification-read.ts index 8433f902b..5c1de8908 100644 --- a/packages/sw/src/scripts/notification-read.ts +++ b/packages/sw/src/scripts/notification-read.ts @@ -37,12 +37,22 @@ class SwNotificationReadManager { account.queue.push(data.body.id as string); + if (account.queue.length >= 20) { + if (account.timeout) clearTimeout(account.timeout); + const notificationIds = account.queue; + account.queue = []; + await api('notifications/read', data.userId, { notificationIds }); + return; + } + // 最後の呼び出しから200ms待ってまとめて処理する if (account.timeout) clearTimeout(account.timeout); account.timeout = setTimeout(() => { account.timeout = null; - api('notifications/read', data.userId, { notificationIds: account.queue }); + const notificationIds = account.queue; + account.queue = []; + api('notifications/read', data.userId, { notificationIds }); }, 200); } }