mizzkey/src/client/sw/sw.ts

217 lines
5.7 KiB
TypeScript
Raw Normal View History

2017-11-21 03:40:09 +09:00
/**
* Service Worker
*/
declare var self: ServiceWorkerGlobalScope;
2017-11-21 03:40:09 +09:00
2021-02-10 22:19:09 +09:00
import { createNotification } from '@/sw/create-notification';
2021-02-10 01:54:07 +09:00
import { swLang } from '@/sw/lang';
2021-02-10 22:19:09 +09:00
import { swNotificationRead } from '@/sw/notification-read';
import { pushNotificationData } from '../../types';
2021-02-15 06:05:18 +09:00
import * as ope from './operations';
2021-02-14 23:06:47 +09:00
import renderAcct from '../../misc/acct/render';
2021-01-21 21:17:22 +09:00
2021-01-23 00:56:06 +09:00
//#region Lifecycle: Install
2017-11-27 22:00:48 +09:00
self.addEventListener('install', ev => {
ev.waitUntil(self.skipWaiting());
});
2021-01-23 00:56:06 +09:00
//#endregion
2021-01-23 00:56:06 +09:00
//#region Lifecycle: Activate
self.addEventListener('activate', ev => {
ev.waitUntil(
caches.keys()
.then(cacheNames => Promise.all(
cacheNames
2021-02-10 01:54:07 +09:00
.filter((v) => v !== swLang.cacheName)
.map(name => caches.delete(name))
))
.then(() => self.clients.claim())
);
});
2021-01-23 00:56:06 +09:00
//#endregion
2021-01-23 00:56:06 +09:00
//#region When: Fetching
self.addEventListener('fetch', ev => {
2021-03-03 13:56:00 +09:00
ev.respondWith(
fetch(ev.request)
2021-03-03 14:48:06 +09:00
.catch(() => new Response(`Offline. Service Worker @${_VERSION_}`, { status: 200 }))
2021-03-03 13:56:00 +09:00
);
2017-11-27 22:00:48 +09:00
});
2021-01-23 00:56:06 +09:00
//#endregion
2017-11-27 22:00:48 +09:00
2021-01-23 00:56:06 +09:00
//#region When: Caught Notification
2017-11-21 03:40:09 +09:00
self.addEventListener('push', ev => {
2021-02-17 02:20:45 +09:00
setTimeout(async () => {
for (const n of await self.registration.getNotifications({ tag: 'user_visible_auto_notification' })) {
n.close();
}
}, 5000);
2017-11-21 03:40:09 +09:00
// クライアント取得
2017-11-21 07:06:36 +09:00
ev.waitUntil(self.clients.matchAll({
includeUncontrolled: true,
type: 'window'
}).then(async clients => {
2021-01-28 03:24:32 +09:00
// // クライアントがあったらストリームに接続しているということなので通知しない
// if (clients.length != 0) return;
2017-11-21 03:40:09 +09:00
2021-02-10 22:19:09 +09:00
const data: pushNotificationData = ev.data?.json();
2017-11-21 07:06:36 +09:00
2021-02-10 01:54:07 +09:00
switch (data.type) {
2021-02-10 22:19:09 +09:00
// case 'driveFileCreated':
2021-02-13 01:28:20 +09:00
case 'notification':
2021-02-10 01:54:07 +09:00
case 'unreadMessagingMessage':
2021-02-10 22:19:09 +09:00
return createNotification(data);
2021-02-10 01:54:07 +09:00
case 'readAllNotifications':
for (const n of await self.registration.getNotifications()) {
2021-02-15 06:05:18 +09:00
if (n.data.type === 'notification') n.close();
}
break;
case 'readAllMessagingMessages':
for (const n of await self.registration.getNotifications()) {
if (n.data.type === 'unreadMessagingMessage') n.close();
2021-02-10 01:54:07 +09:00
}
break;
case 'readNotifications':
2021-02-15 06:05:18 +09:00
for (const n of await self.registration.getNotifications()) {
if (data.body.notificationIds?.includes(n.data.body.id)) {
n.close();
2021-02-10 22:30:02 +09:00
}
2021-02-10 01:54:07 +09:00
}
break;
2021-02-15 06:05:18 +09:00
case 'readAllMessagingMessagesOfARoom':
for (const n of await self.registration.getNotifications()) {
if (n.data.type === 'unreadMessagingMessage'
&& ('userId' in data.body
? data.body.userId === n.data.body.userId
: data.body.groupId === n.data.body.groupId)
) {
n.close();
}
}
break;
2021-02-10 01:54:07 +09:00
}
2017-11-21 07:06:36 +09:00
}));
2017-11-21 03:40:09 +09:00
});
2021-01-23 00:56:06 +09:00
//#endregion
2021-01-21 21:17:22 +09:00
2021-01-28 03:24:32 +09:00
//#region Notification
2021-02-15 06:05:18 +09:00
self.addEventListener('notificationclick', ev => {
ev.waitUntil((async () => {
if (_DEV_) {
console.log('notificationclick', ev.action, ev.notification.data);
}
2021-01-28 03:24:32 +09:00
const { action, notification } = ev;
2021-02-10 22:19:09 +09:00
const data: pushNotificationData = notification.data;
2021-02-15 06:05:18 +09:00
const { type, userId: id, body } = data;
let client: WindowClient | null = null;
let close = true;
2021-02-10 01:54:07 +09:00
2021-01-28 03:24:32 +09:00
switch (action) {
2021-02-15 06:05:18 +09:00
case 'follow':
client = await ope.api('following/create', id, { userId: body.userId });
break;
2021-01-28 03:24:32 +09:00
case 'showUser':
2021-02-15 06:05:18 +09:00
client = await ope.openUser(renderAcct(body.user), id);
if (body.type !== 'renote') close = false;
break;
case 'reply':
client = await ope.openPost({ reply: body.note }, id);
break;
case 'renote':
await ope.api('notes/create', id, { renoteId: body.note.id });
break;
case 'accept':
if (body.type === 'receiveFollowRequest') {
await ope.api('following/requests/accept', id, { userId: body.userId });
} else if (body.type === 'groupInvited') {
await ope.api('users/groups/invitations/accept', id, { invitationId: body.invitation.id });
}
break;
case 'reject':
if (body.type === 'receiveFollowRequest') {
await ope.api('following/requests/reject', id, { userId: body.userId });
} else if (body.type === 'groupInvited') {
await ope.api('users/groups/invitations/reject', id, { invitationId: body.invitation.id });
}
break;
case 'showFollowRequests':
client = await ope.openClient('push', '/my/follow-requests', id);
break;
default:
if (type === 'unreadMessagingMessage') {
client = await ope.openChat(body, id);
break;
}
2021-01-28 03:24:32 +09:00
2021-02-15 06:05:18 +09:00
switch (body.type) {
case 'receiveFollowRequest':
client = await ope.openClient('push', '/my/follow-requests', id);
break;
case 'groupInvited':
client = await ope.openClient('push', '/my/groups', id);
break;
case 'reaction':
client = await ope.openNote(body.note.id, id);
break;
2021-01-28 03:24:32 +09:00
default:
2021-02-15 06:05:18 +09:00
if ('note' in body) {
client = await ope.openNote(body.note.id, id);
break;
}
if ('user' in body) {
client = await ope.openUser(renderAcct(body.data.user), id);
break;
2021-01-28 03:24:32 +09:00
}
}
}
2021-02-15 06:05:18 +09:00
if (client) {
client.focus();
}
if (type === 'notification') {
swNotificationRead.then(that => that.read(data));
}
if (close) {
notification.close();
}
2021-02-15 06:17:21 +09:00
})());
2021-01-28 03:24:32 +09:00
});
2021-02-10 22:19:09 +09:00
self.addEventListener('notificationclose', ev => {
2021-02-15 06:05:18 +09:00
const data: pushNotificationData = ev.notification.data;
2021-01-28 03:24:32 +09:00
2021-02-10 22:19:09 +09:00
if (data.type === 'notification') {
swNotificationRead.then(that => that.read(data));
2021-01-28 03:24:32 +09:00
}
});
//#endregion
2021-01-23 00:56:06 +09:00
//#region When: Caught a message from the client
2021-02-15 06:05:18 +09:00
self.addEventListener('message', async ev => {
2021-02-10 02:03:05 +09:00
switch (ev.data) {
2021-01-21 21:17:22 +09:00
case 'clear':
2021-02-15 06:05:18 +09:00
// Cache Storage全削除
await caches.keys()
.then(cacheNames => Promise.all(
cacheNames.map(name => caches.delete(name))
2021-02-15 06:17:21 +09:00
));
2021-01-21 21:17:22 +09:00
return; // TODO
}
if (typeof ev.data === 'object') {
2021-01-24 18:17:42 +09:00
// E.g. '[object Array]' → 'array'
2021-01-21 21:17:22 +09:00
const otype = Object.prototype.toString.call(ev.data).slice(8, -1).toLowerCase();
if (otype === 'object') {
if (ev.data.msg === 'initialize') {
2021-02-10 01:54:07 +09:00
swLang.setLang(ev.data.lang);
2021-01-21 21:17:22 +09:00
}
}
}
});
2021-01-23 00:56:06 +09:00
//#endregion