From a461625b31457034258315beeb7b56c98daa9780 Mon Sep 17 00:00:00 2001 From: tamaina Date: Sun, 10 Jul 2022 15:15:21 +0900 Subject: [PATCH] enhance(sw): If receiving a push notification issued more than a day, ignore it. (#8980) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * enhance(sw): ignore old push notification * :v: * 半日 * !== * 1日 --- packages/backend/src/services/push-notification.ts | 1 + packages/sw/src/sw.ts | 6 +++++- packages/sw/src/types.ts | 1 + 3 files changed, 7 insertions(+), 1 deletion(-) diff --git a/packages/backend/src/services/push-notification.ts b/packages/backend/src/services/push-notification.ts index 5c3bafbb3..393a23d05 100644 --- a/packages/backend/src/services/push-notification.ts +++ b/packages/backend/src/services/push-notification.ts @@ -64,6 +64,7 @@ export async function pushNotification(u type, body: type === 'notification' ? truncateNotification(body as Packed<'Notification'>) : body, userId, + dateTime: (new Date()).getTime(), }), { proxy: config.proxy, }).catch((err: any) => { diff --git a/packages/sw/src/sw.ts b/packages/sw/src/sw.ts index 0ba6a6e4a..872692f90 100644 --- a/packages/sw/src/sw.ts +++ b/packages/sw/src/sw.ts @@ -42,8 +42,12 @@ self.addEventListener('push', ev => { // case 'driveFileCreated': case 'notification': case 'unreadMessagingMessage': + // 1日以上経過している場合は無視 + if ((new Date()).getTime() - data.dateTime > 1000 * 60 * 60 * 24) break; + // クライアントがあったらストリームに接続しているということなので通知しない - if (clients.length != 0) return; + if (clients.length !== 0) break; + return createNotification(data); case 'readAllNotifications': for (const n of await self.registration.getNotifications()) { diff --git a/packages/sw/src/types.ts b/packages/sw/src/types.ts index 6aa3726ea..0404e21e5 100644 --- a/packages/sw/src/types.ts +++ b/packages/sw/src/types.ts @@ -24,6 +24,7 @@ export type pushNotificationData type: K; body: pushNotificationDataSourceMap[K]; userId: string; + dateTime: number; }; export type pushNotificationDataMap = {