mizzkey/src/client/sw/sw.ts

156 lines
3.8 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 01:54:07 +09:00
import { get } from 'idb-keyval';
import { swNotification } from '@/sw/notification';
import { swLang } from '@/sw/lang';
2021-01-21 21:17:22 +09:00
2021-01-23 00:56:06 +09:00
//#region Variables
2021-02-10 01:54:07 +09:00
// const cacheName = `mk-cache-${_VERSION_}`;
const apiUrl = `${location.origin}/api/`;
2021-01-23 00:56:06 +09:00
//#endregion
//#region Lifecycle: Install
2017-11-27 22:00:48 +09:00
self.addEventListener('install', ev => {
2021-02-09 20:09:45 +09:00
// Nothing to do
});
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-02-07 10:46:26 +09:00
// TODO: 消せるかも ref. https://github.com/syuilo/misskey/pull/7108#issuecomment-774573666
2021-01-23 00:56:06 +09:00
//#region When: Fetching
self.addEventListener('fetch', ev => {
if (ev.request.method !== 'GET' || ev.request.url.startsWith(apiUrl)) return;
ev.respondWith(
caches.match(ev.request)
.then(response => {
return response || fetch(ev.request);
})
2021-02-09 20:09:45 +09:00
.catch(() => new Response('SW cathces error while fetching. You may not be connected to the Internet, or the server may be down.', { status: 200, statusText: 'OK SW' }))
);
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({
2017-11-21 03:40:09 +09:00
includeUncontrolled: true
}).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-01-28 03:24:32 +09:00
const data = ev.data?.json();
2017-11-21 07:06:36 +09:00
2021-02-10 01:54:07 +09:00
switch (data.type) {
case 'notification':
case 'driveFileCreated':
case 'unreadMessagingMessage':
return swNotification.append(data);
case 'readAllNotifications':
for (const n of await self.registration.getNotifications()) {
n.close();
}
break;
case 'readNotifications':
for (const n of await self.registration.getNotifications()) {
if (data.notificationIds.includes(n.data.body.id)) n.close();
}
break;
}
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
self.addEventListener('notificationclick', ev => {
const { action, notification } = ev;
const { data } = notification;
const { origin } = location;
2021-02-10 01:54:07 +09:00
const suffix = `?loginId=${data.userId}`;
2021-01-28 03:24:32 +09:00
switch (action) {
case 'showUser':
switch (data.body.type) {
case 'reaction':
2021-02-10 01:54:07 +09:00
self.clients.openWindow(`${origin}/users/${data.body.user.id}${suffix}`);
2021-01-28 03:24:32 +09:00
break;
default:
if ('note' in data.body) {
2021-02-10 01:54:07 +09:00
self.clients.openWindow(`${origin}/users/${data.body.note.user.id}${suffix}`);
2021-01-28 03:24:32 +09:00
}
}
break;
default:
}
notification.close();
});
self.addEventListener('notificationclose', async ev => {
const { notification } = ev;
2021-02-10 01:54:07 +09:00
if (notification.title !== 'notificationclose') {
self.registration.showNotification('notificationclose', { body: `${notification.data.id}` });
}
2021-01-28 03:49:27 +09:00
const { data } = notification;
2021-01-28 03:24:32 +09:00
if (data.isNotification) {
const { origin } = location;
const accounts = await get('accounts');
const account = accounts.find(i => i.id === data.userId);
if (!account) return;
2021-02-10 01:54:07 +09:00
if (data.type === 'notification') {
fetch(`${origin}/api/notifications/read`, {
method: 'POST',
body: JSON.stringify({
i: account.token,
notificationIds: [data.body.id]
})
});
}
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-01-21 21:17:22 +09:00
self.addEventListener('message', ev => {
switch(ev.data) {
case 'clear':
return; // TODO
default:
break;
}
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