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;
import { get, set } from 'idb-keyval';
import { I18n } from '@/scripts/i18n';
import { I18n } from '../../misc/i18n';
class SwLang {
public cacheName = `mk-cache-${_VERSION_}`;

View File

@ -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);
}
}

View File

@ -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のオプションから投稿フォームを開く

View File

@ -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));
}
});

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 = {
type: 'order';