From a1f3b212fe29c57b6519e0ae2e573799e4fef469 Mon Sep 17 00:00:00 2001 From: Johann150 Date: Sat, 12 Nov 2022 18:18:24 +0100 Subject: [PATCH 1/5] client: translate comments --- packages/client/src/scripts/initialize-sw.ts | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/packages/client/src/scripts/initialize-sw.ts b/packages/client/src/scripts/initialize-sw.ts index 30b4ae895..7d3b265bc 100644 --- a/packages/client/src/scripts/initialize-sw.ts +++ b/packages/client/src/scripts/initialize-sw.ts @@ -33,14 +33,14 @@ export async function initializeSw() { }) // When subscribe failed .catch(async (err: Error) => { - // 通知が許可されていなかったとき + // when notifications were not authorized if (err.name === 'NotAllowedError') { return; } - - // 違うapplicationServerKey (または gcm_sender_id)のサブスクリプションが - // 既に存在していることが原因でエラーになった可能性があるので、 - // そのサブスクリプションを解除しておく + + // The error may have been caused by the fact that a subscription to a + // different applicationServerKey (or gcm_sender_id) already exists, so + // unsubscribe to it. const subscription = await registration.pushManager.getSubscription(); if (subscription) subscription.unsubscribe(); }); From c34bebdf46e379de5285eafeb6d4306ecfc691ad Mon Sep 17 00:00:00 2001 From: Johann150 Date: Sat, 12 Nov 2022 18:23:18 +0100 Subject: [PATCH 2/5] service worker: also show notifications if client is connected --- packages/sw/src/sw.ts | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/packages/sw/src/sw.ts b/packages/sw/src/sw.ts index dfe5094e2..b328bcad3 100644 --- a/packages/sw/src/sw.ts +++ b/packages/sw/src/sw.ts @@ -24,19 +24,13 @@ self.addEventListener('activate', ev => { }); self.addEventListener('push', ev => { - // クライアント取得 - ev.waitUntil(self.clients.matchAll({ - includeUncontrolled: true, - type: 'window' - }).then(async (clients: readonly WindowClient[]) => { + ev.waitUntil((async () => { const data: pushNotificationDataMap[K] = ev.data?.json(); switch (data.type) { // case 'driveFileCreated': case 'notification': case 'unreadMessagingMessage': - // クライアントがあったらストリームに接続しているということなので通知しない - if (clients.length != 0) return; return createNotification(data); case 'readAllNotifications': for (const n of await self.registration.getNotifications()) { @@ -67,7 +61,7 @@ self.addEventListener('push', ev => { } break; } - })); + })()); }); self.addEventListener('notificationclick', (ev: ServiceWorkerGlobalScopeEventMap['notificationclick']) => { From 1e8e551ee3a6712561ab1d890640155a46959aee Mon Sep 17 00:00:00 2001 From: Johann150 Date: Sat, 12 Nov 2022 18:27:44 +0100 Subject: [PATCH 3/5] service worker: refactor message event handler It is now possible for the client to trigger notifications "manually" if push notifications are not configured on the server. --- packages/client/src/scripts/initialize-sw.ts | 2 +- packages/sw/src/sw.ts | 22 ++++++-------------- 2 files changed, 7 insertions(+), 17 deletions(-) diff --git a/packages/client/src/scripts/initialize-sw.ts b/packages/client/src/scripts/initialize-sw.ts index 7d3b265bc..584a023ef 100644 --- a/packages/client/src/scripts/initialize-sw.ts +++ b/packages/client/src/scripts/initialize-sw.ts @@ -9,7 +9,7 @@ export async function initializeSw() { navigator.serviceWorker.register('/sw.js', { scope: '/', type: 'classic' }); navigator.serviceWorker.ready.then(registration => { registration.active?.postMessage({ - msg: 'initialize', + type: 'initialize', lang, }); diff --git a/packages/sw/src/sw.ts b/packages/sw/src/sw.ts index b328bcad3..9712279ed 100644 --- a/packages/sw/src/sw.ts +++ b/packages/sw/src/sw.ts @@ -161,24 +161,14 @@ self.addEventListener('notificationclose', { ev.waitUntil((async () => { - switch (ev.data) { - case 'clear': - // Cache Storage全削除 - await caches.keys() - .then(cacheNames => Promise.all( - cacheNames.map(name => caches.delete(name)) - )); - return; // TODO - } - if (typeof ev.data === 'object') { - // E.g. '[object Array]' → 'array' - const otype = Object.prototype.toString.call(ev.data).slice(8, -1).toLowerCase(); - - if (otype === 'object') { - if (ev.data.msg === 'initialize') { + switch (ev.data.type) { + case 'initialize': swLang.setLang(ev.data.lang); - } + break; + case 'notification': + createNotification(ev.data); + break; } } })()); From 8a807db02e088e0f5575d3da234301b5a646bd74 Mon Sep 17 00:00:00 2001 From: Johann150 Date: Sat, 12 Nov 2022 18:48:25 +0100 Subject: [PATCH 4/5] client: pass along notifications if push notifs disabled --- packages/client/src/ui/_common_/common.vue | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/packages/client/src/ui/_common_/common.vue b/packages/client/src/ui/_common_/common.vue index 0eb9dedb1..3cec9b388 100644 --- a/packages/client/src/ui/_common_/common.vue +++ b/packages/client/src/ui/_common_/common.vue @@ -19,6 +19,7 @@ - -