This commit is contained in:
tamaina 2021-02-14 23:06:47 +09:00
parent 2f151cc3d2
commit e6411857ad
5 changed files with 20 additions and 40 deletions

View File

@ -4,7 +4,7 @@
declare var self: ServiceWorkerGlobalScope; declare var self: ServiceWorkerGlobalScope;
import { get, set } from 'idb-keyval'; import { get, set } from 'idb-keyval';
import { I18n } from '@/scripts/i18n'; import { I18n } from '../../misc/i18n';
class SwLang { class SwLang {
public cacheName = `mk-cache-${_VERSION_}`; public cacheName = `mk-cache-${_VERSION_}`;

View File

@ -15,14 +15,14 @@ class SwNotificationRead {
private accounts: Accounts = {}; private accounts: Accounts = {};
public async construct() { public async construct() {
const accounts = await get('accounts') as { i: string, id: string }[]; const accounts = await get('accounts') as { token: string, id: string }[];
if (accounts) Error('Account is not recorded'); if (!accounts) Error('Account is not recorded');
this.accounts = accounts.reduce((acc, e) => { this.accounts = accounts.reduce((acc, e) => {
acc[e.id] = { acc[e.id] = {
queue: [], queue: [],
timeout: null, timeout: null,
token: e.i, token: e.token,
}; };
return acc; return acc;
}, {} as Accounts); }, {} as Accounts);
@ -38,7 +38,7 @@ class SwNotificationRead {
account.queue.push(data.body.id); account.queue.push(data.body.id);
// 最後の呼び出しから100ms待ってまとめて処理する // 最後の呼び出しから200ms待ってまとめて処理する
if (account.timeout) clearTimeout(account.timeout); if (account.timeout) clearTimeout(account.timeout);
account.timeout = setTimeout(() => { account.timeout = setTimeout(() => {
account.timeout = null; account.timeout = null;
@ -50,10 +50,8 @@ class SwNotificationRead {
i: account.token, i: account.token,
notificationIds: account.queue notificationIds: account.queue
}) })
}).then(res => {
self.registration.showNotification('notificationread', { body: `${account.queue}, ${res.ok}` });
}); });
}, 100); }, 200);
} }
} }

View File

@ -7,9 +7,9 @@ declare var self: ServiceWorkerGlobalScope;
import { SwMessage, swMessageOrderType } from './types'; import { SwMessage, swMessageOrderType } from './types';
// acctからユーザーを開く // rendered acctからユーザーを開く
export async function openUser(acct: string, loginId: string) { 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のオプションから投稿フォームを開く // post-formのオプションから投稿フォームを開く

View File

@ -7,6 +7,8 @@ import { createNotification } from '@/sw/create-notification';
import { swLang } from '@/sw/lang'; import { swLang } from '@/sw/lang';
import { swNotificationRead } from '@/sw/notification-read'; import { swNotificationRead } from '@/sw/notification-read';
import { pushNotificationData } from '../../types'; import { pushNotificationData } from '../../types';
import { openUser } from './open-client';
import renderAcct from '../../misc/acct/render';
//#region Lifecycle: Install //#region Lifecycle: Install
self.addEventListener('install', ev => { self.addEventListener('install', ev => {
@ -46,8 +48,6 @@ self.addEventListener('push', ev => {
const data: pushNotificationData = ev.data?.json(); const data: pushNotificationData = ev.data?.json();
console.log('push', data)
switch (data.type) { switch (data.type) {
// case 'driveFileCreated': // case 'driveFileCreated':
case 'notification': case 'notification':
@ -73,53 +73,35 @@ self.addEventListener('push', ev => {
//#region Notification //#region Notification
self.addEventListener('notificationclick', async ev => { self.addEventListener('notificationclick', async ev => {
const { action, notification } = ev; const { action, notification } = ev;
console.log('click', action, notification)
const data: pushNotificationData = notification.data; 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) { switch (action) {
case 'showUser': case 'showUser':
switch (data.body.type) { switch (data.body.type) {
case 'reaction': case 'reaction':
await self.clients.openWindow(`${origin}/users/${data.body.user.id}${suffix}`); return openUser(renderAcct(data.body.user), data.userId);
break;
default: default:
if ('note' in data.body) { 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; break;
default: default:
} }
notification.close(); // notification.close();
}); });
self.addEventListener('notificationclose', ev => { self.addEventListener('notificationclose', ev => {
const { notification } = ev; const { notification } = ev;
console.log('close', notification) if (!notification.title.startsWith('notification')) {
if (notification.title !== 'notificationclose') {
self.registration.showNotification('notificationclose', { body: `${notification?.data?.body?.id}` }); self.registration.showNotification('notificationclose', { body: `${notification?.data?.body?.id}` });
} }
const data: pushNotificationData = notification.data; const data: pushNotificationData = notification.data;
if (data.type === 'notification') { if (data.type === 'notification') {
console.log('close', data);
swNotificationRead.then(that => that.read(data)); swNotificationRead.then(that => that.read(data));
} }
}); });

View File

@ -1,4 +1,4 @@
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 = { export type SwMessage = {
type: 'order'; type: 'order';