From e6411857ad782615927589403abbb106ab329686 Mon Sep 17 00:00:00 2001 From: tamaina Date: Sun, 14 Feb 2021 23:06:47 +0900 Subject: [PATCH] wip --- src/client/sw/lang.ts | 2 +- src/client/sw/notification-read.ts | 12 +++++------- src/client/sw/open-client.ts | 4 ++-- src/client/sw/sw.ts | 30 ++++++------------------------ src/client/sw/types.ts | 12 ++++++------ 5 files changed, 20 insertions(+), 40 deletions(-) diff --git a/src/client/sw/lang.ts b/src/client/sw/lang.ts index 2d05404ef9..1b3f1ad620 100644 --- a/src/client/sw/lang.ts +++ b/src/client/sw/lang.ts @@ -4,7 +4,7 @@ declare var self: ServiceWorkerGlobalScope; import { get, set } from 'idb-keyval'; -import { I18n } from '@/scripts/i18n'; +import { I18n } from '../../misc/i18n'; class SwLang { public cacheName = `mk-cache-${_VERSION_}`; diff --git a/src/client/sw/notification-read.ts b/src/client/sw/notification-read.ts index bf0245fbf9..4f9664a8fe 100644 --- a/src/client/sw/notification-read.ts +++ b/src/client/sw/notification-read.ts @@ -15,14 +15,14 @@ class SwNotificationRead { private accounts: Accounts = {}; public async construct() { - const accounts = await get('accounts') as { i: string, id: string }[]; - if (accounts) Error('Account is not recorded'); + const accounts = await get('accounts') as { token: string, id: string }[]; + if (!accounts) Error('Account is not recorded'); this.accounts = accounts.reduce((acc, e) => { acc[e.id] = { queue: [], timeout: null, - token: e.i, + token: e.token, }; return acc; }, {} as Accounts); @@ -38,7 +38,7 @@ class SwNotificationRead { account.queue.push(data.body.id); - // 最後の呼び出しから100ms待ってまとめて処理する + // 最後の呼び出しから200ms待ってまとめて処理する if (account.timeout) clearTimeout(account.timeout); account.timeout = setTimeout(() => { account.timeout = null; @@ -50,10 +50,8 @@ class SwNotificationRead { i: account.token, notificationIds: account.queue }) - }).then(res => { - self.registration.showNotification('notificationread', { body: `${account.queue}, ${res.ok}` }); }); - }, 100); + }, 200); } } diff --git a/src/client/sw/open-client.ts b/src/client/sw/open-client.ts index 89f921e7d4..5ef1cee434 100644 --- a/src/client/sw/open-client.ts +++ b/src/client/sw/open-client.ts @@ -7,9 +7,9 @@ declare var self: ServiceWorkerGlobalScope; import { SwMessage, swMessageOrderType } from './types'; -// acctからユーザーを開く +// rendered acctからユーザーを開く export async function openUser(acct: string, loginId: string) { - open('push-user', { acct }, `${origin}/${acct}?loginId=${loginId}`, loginId) + open('push-user', { acct }, `${origin}/@${acct}?loginId=${loginId}`, loginId) } // post-formのオプションから投稿フォームを開く diff --git a/src/client/sw/sw.ts b/src/client/sw/sw.ts index 7e309c699b..8570c30987 100644 --- a/src/client/sw/sw.ts +++ b/src/client/sw/sw.ts @@ -7,6 +7,8 @@ import { createNotification } from '@/sw/create-notification'; import { swLang } from '@/sw/lang'; import { swNotificationRead } from '@/sw/notification-read'; import { pushNotificationData } from '../../types'; +import { openUser } from './open-client'; +import renderAcct from '../../misc/acct/render'; //#region Lifecycle: Install self.addEventListener('install', ev => { @@ -46,8 +48,6 @@ self.addEventListener('push', ev => { const data: pushNotificationData = ev.data?.json(); - console.log('push', data) - switch (data.type) { // case 'driveFileCreated': case 'notification': @@ -73,53 +73,35 @@ self.addEventListener('push', ev => { //#region Notification self.addEventListener('notificationclick', async ev => { const { action, notification } = ev; - console.log('click', action, notification) const data: pushNotificationData = notification.data; - const { origin } = location; - const client = self.clients.matchAll({ - includeUncontrolled: true, - type: 'window' - }).then(clients => { - for (const client of clients) { - client.postMessage(notification.data); - if ('focus' in client) (client as any).focus() - console.log('postMessage', client) - } - }); - - const suffix = `?loginId=${data.userId}`; switch (action) { case 'showUser': switch (data.body.type) { case 'reaction': - await self.clients.openWindow(`${origin}/users/${data.body.user.id}${suffix}`); - break; + return openUser(renderAcct(data.body.user), data.userId); default: if ('note' in data.body) { - await self.clients.openWindow(`${origin}/users/${data.body.note.user.id}${suffix}`); + return openUser(renderAcct(data.body.data.user), data.userId); } } break; default: } - notification.close(); + // notification.close(); }); self.addEventListener('notificationclose', ev => { const { notification } = ev; - console.log('close', notification) - - if (notification.title !== 'notificationclose') { + if (!notification.title.startsWith('notification')) { self.registration.showNotification('notificationclose', { body: `${notification?.data?.body?.id}` }); } const data: pushNotificationData = notification.data; if (data.type === 'notification') { - console.log('close', data); swNotificationRead.then(that => that.read(data)); } }); diff --git a/src/client/sw/types.ts b/src/client/sw/types.ts index 6e8b7abfab..a3594d8a2e 100644 --- a/src/client/sw/types.ts +++ b/src/client/sw/types.ts @@ -1,9 +1,9 @@ -export type swMessageOrderType = 'post' | 'push-user' | 'push-note' | 'push-messaging-room' +export type swMessageOrderType = 'post' | 'push-user' | 'push-note' | 'push-messaging-room'; export type SwMessage = { - type: 'order'; - order: swMessageOrderType; - loginId: string; - url: string; - [x: string]: any; + type: 'order'; + order: swMessageOrderType; + loginId: string; + url: string; + [x: string]: any; };