From 8c883653c959fff5de4ddc24d8ee64f59230f880 Mon Sep 17 00:00:00 2001 From: tamaina Date: Sat, 18 Feb 2023 17:48:20 +0900 Subject: [PATCH] =?UTF-8?q?fix/enhance(sw):=20=E3=83=97=E3=83=83=E3=82=B7?= =?UTF-8?q?=E3=83=A5=E9=80=9A=E7=9F=A5=20(=E3=83=90=E3=83=83=E3=82=AF?= =?UTF-8?q?=E3=82=B0=E3=83=A9=E3=82=A6=E3=83=B3=E3=83=89=E3=81=A7=E9=96=8B?= =?UTF-8?q?=E3=81=84=E3=81=A6=E3=81=84=E3=82=8B=E5=A0=B4=E5=90=88=E3=82=82?= =?UTF-8?q?=E9=80=9A=E7=9F=A5,=20=E3=83=AA=E3=82=A2=E3=82=AF=E3=82=B7?= =?UTF-8?q?=E3=83=A7=E3=83=B3=E9=80=9A=E7=9F=A5=E3=81=AF=E3=83=8E=E3=83=BC?= =?UTF-8?q?=E3=83=88=E3=81=AB=E3=81=A4=E3=81=8D1=E3=81=A4=E3=81=AB)=20(#99?= =?UTF-8?q?77)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fix(sw): クライアントがあってもpush notificationを無視しない 「プッシュ通知を更新しました」の原因になるため * enhance(sw): リアクション通知は1つのノートにつき1つしか表示しない Safari対応で、通知tagは能動的に閉じるように * revert closeNotificationsByTags --- .../sw/src/scripts/create-notification.ts | 26 +++++++++++-------- packages/sw/src/sw.ts | 6 ++--- 2 files changed, 17 insertions(+), 15 deletions(-) diff --git a/packages/sw/src/scripts/create-notification.ts b/packages/sw/src/scripts/create-notification.ts index e45c3f504..14f96816a 100644 --- a/packages/sw/src/scripts/create-notification.ts +++ b/packages/sw/src/scripts/create-notification.ts @@ -10,6 +10,12 @@ import { getAccountFromId } from '@/scripts/get-account-from-id'; import { char2fileName } from '@/scripts/twemoji-base'; import * as url from '@/scripts/url'; +const closeNotificationsByTags = async (tags: string[]) => { + for (const n of (await Promise.all(tags.map(tag => globalThis.registration.getNotifications({ tag })))).flat()) { + n.close(); + } +} + const iconUrl = (name: badgeNames) => `/static-assets/tabler-badges/${name}.png`; /* How to add a new badge: * 1. Find the icon and download png from https://tabler-icons.io/ @@ -161,6 +167,7 @@ async function composeNotification(data: pushNotificationDataMap[keyof pushNotif badge = iconUrl('plus'); } + const tag = `reaction:${data.body.note.id}`; return [`${reaction} ${getUserName(data.body.user)}`, { body: data.body.note.text ?? '', icon: data.body.user.avatarUrl, @@ -176,10 +183,11 @@ async function composeNotification(data: pushNotificationDataMap[keyof pushNotif } case 'pollEnded': + const tag = `poll:${data.body.note.id}`; return [t('_notification.pollEnded'), { body: data.body.note.text || '', badge: iconUrl('chart-arrows'), - tag: `poll:${data.body.note.id}`, + tag, data, }]; @@ -220,11 +228,12 @@ async function composeNotification(data: pushNotificationDataMap[keyof pushNotif return null; } case 'unreadAntennaNote': + const tag = `antenna:${data.body.antenna.id}`; return [t('_notification.unreadAntennaNote', { name: data.body.antenna.name }), { body: `${getUserName(data.body.note.user)}: ${data.body.note.text ?? ''}`, icon: data.body.note.user.avatarUrl, badge: iconUrl('antenna'), - tag: `antenna:${data.body.antenna.id}`, + tag, data, renotify: true, }]; @@ -248,16 +257,11 @@ export async function createEmptyNotification() { }, ); - res(); - setTimeout(async () => { - for (const n of - [ - ...(await globalThis.registration.getNotifications({ tag: 'user_visible_auto_notification' })), - ...(await globalThis.registration.getNotifications({ tag: 'read_notification' })), - ] - ) { - n.close(); + try { + await closeNotificationsByTags(['user_visible_auto_notification', 'read_notification']); + } finally { + res(); } }, 1000); }); diff --git a/packages/sw/src/sw.ts b/packages/sw/src/sw.ts index f4d768547..6f4c48735 100644 --- a/packages/sw/src/sw.ts +++ b/packages/sw/src/sw.ts @@ -53,9 +53,6 @@ globalThis.addEventListener('push', ev => { // 1日以上経過している場合は無視 if ((new Date()).getTime() - data.dateTime > 1000 * 60 * 60 * 24) break; - // クライアントがあったらストリームに接続しているということなので通知しない - if (clients.length !== 0) break; - return createNotification(data); case 'readAllNotifications': for (const n of await globalThis.registration.getNotifications()) { @@ -83,7 +80,8 @@ globalThis.addEventListener('push', ev => { break; } - return createEmptyNotification(); + await createEmptyNotification(); + return; })); });