mizzkey/src/client/sw/sw.ts

219 lines
6.1 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-03-29 21:36:53 +09:00
import { createEmptyNotification, createNotification } from '@client/sw/create-notification';
import { swLang } from '@client/sw/lang';
import { swNotificationRead } from '@client/sw/notification-read';
2021-09-21 01:55:36 +09:00
import { pushNotificationDataMap } from '@client/sw/types';
import * as swos from './operations';
2021-07-17 22:00:48 +09:00
import { getAcct } from '@/misc/acct';
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 => {
// クライアント取得
2017-11-21 07:06:36 +09:00
ev.waitUntil(self.clients.matchAll({
includeUncontrolled: true,
type: 'window'
2021-09-21 01:55:36 +09:00
}).then(async <K extends keyof pushNotificationDataMap>(clients: readonly WindowClient[]) => {
2021-01-28 03:24:32 +09:00
// // クライアントがあったらストリームに接続しているということなので通知しない
// if (clients.length != 0) return;
2017-11-21 03:40:09 +09:00
2021-09-21 01:55:36 +09:00
const data: pushNotificationDataMap[K] = 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-03-29 21:36:53 +09:00
2021-02-10 01:54:07 +09:00
case 'readAllNotifications':
for (const n of await self.registration.getNotifications()) {
2021-04-24 01:37:29 +09:00
if (n?.data?.type === 'notification') n.close();
2021-02-15 06:05:18 +09:00
}
break;
case 'readAllMessagingMessages':
for (const n of await self.registration.getNotifications()) {
2021-04-24 01:37:29 +09:00
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()) {
2021-04-24 01:37:29 +09:00
if (data.body?.notificationIds?.includes(n.data.body.id)) {
2021-02-15 06:05:18 +09:00
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
}
2021-03-29 21:36:53 +09:00
createEmptyNotification();
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-09-21 01:55:36 +09:00
self.addEventListener('notificationclick', <K extends keyof pushNotificationDataMap>(ev: NotificationEvent) => {
2021-02-15 06:05:18 +09:00
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-09-21 01:55:36 +09:00
const data: pushNotificationDataMap[K] = notification.data;
const { userId: id } = data;
2021-02-15 06:05:18 +09:00
let client: WindowClient | null = null;
2021-02-10 01:54:07 +09:00
2021-09-21 01:55:36 +09:00
switch (data.type) {
case 'notification':
switch (action) {
case 'follow':
2021-09-21 01:58:05 +09:00
if ('userId' in data.body)await swos.api('following/create', id, { userId: data.body.userId });
2021-02-15 06:05:18 +09:00
break;
2021-09-21 01:55:36 +09:00
case 'showUser':
if ('user' in data.body) client = await swos.openUser(getAcct(data.body.user), id);
2021-02-15 06:05:18 +09:00
break;
2021-09-21 01:55:36 +09:00
case 'reply':
if ('note' in data.body) client = await swos.openPost({ reply: data.body.note }, id);
2021-02-15 06:05:18 +09:00
break;
2021-09-21 01:55:36 +09:00
case 'renote':
if ('note' in data.body) await swos.api('notes/create', id, { renoteId: data.body.note.id });
break;
case 'accept':
switch (data.body.type) {
case 'receiveFollowRequest':
await swos.api('following/requests/accept', id, { userId: data.body.userId });
break;
case 'groupInvited':
await swos.api('users/groups/invitations/accept', id, { invitationId: data.body.invitation.id });
break;
}
break;
case 'reject':
switch (data.body.type) {
case 'receiveFollowRequest':
await swos.api('following/requests/reject', id, { userId: data.body.userId });
break;
case 'groupInvited':
await swos.api('users/groups/invitations/reject', id, { invitationId: data.body.invitation.id });
break;
2021-02-15 06:05:18 +09:00
}
2021-09-21 01:55:36 +09:00
break;
case 'showFollowRequests':
client = await swos.openClient('push', '/my/follow-requests', id);
break;
default:
switch (data.body.type) {
case 'receiveFollowRequest':
client = await swos.openClient('push', '/my/follow-requests', id);
break;
case 'groupInvited':
client = await swos.openClient('push', '/my/groups', id);
break;
case 'reaction':
client = await swos.openNote(data.body.note.id, id);
break;
default:
if ('note' in data.body) {
client = await swos.openNote(data.body.note.id, id);
break;
}
if ('user' in data.body) {
client = await swos.openUser(getAcct(data.body.user), id);
break;
}
2021-01-28 03:24:32 +09:00
}
}
2021-09-21 01:55:36 +09:00
break;
case 'unreadMessagingMessage':
client = await swos.openChat(data.body, id);
break;
2021-01-28 03:24:32 +09:00
}
2021-02-15 06:05:18 +09:00
if (client) {
client.focus();
}
2021-09-21 01:55:36 +09:00
if (data.type === 'notification') {
2021-02-15 06:05:18 +09:00
swNotificationRead.then(that => that.read(data));
}
2021-09-21 01:55:36 +09:00
notification.close();
2021-02-15 06:05:18 +09:00
2021-02-15 06:17:21 +09:00
})());
2021-01-28 03:24:32 +09:00
});
2021-09-21 01:55:36 +09:00
self.addEventListener('notificationclose', <K extends keyof pushNotificationDataMap>(ev: NotificationEvent) => {
const data: pushNotificationDataMap[K] = 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