2022-04-30 05:52:07 -07:00
|
|
|
import { createEmptyNotification, createNotification } from '@/scripts/create-notification';
|
|
|
|
import { swLang } from '@/scripts/lang';
|
|
|
|
import { swNotificationRead } from '@/scripts/notification-read';
|
|
|
|
import { pushNotificationDataMap } from '@/types';
|
|
|
|
import * as swos from '@/scripts/operations';
|
|
|
|
import { acct as getAcct } from '@/filters/user';
|
|
|
|
|
2023-02-12 07:31:37 -08:00
|
|
|
globalThis.addEventListener('install', ev => {
|
2023-02-17 21:16:34 -08:00
|
|
|
//ev.waitUntil(globalThis.skipWaiting());
|
2022-04-30 05:52:07 -07:00
|
|
|
});
|
|
|
|
|
2023-02-12 07:31:37 -08:00
|
|
|
globalThis.addEventListener('activate', ev => {
|
2022-04-30 05:52:07 -07:00
|
|
|
ev.waitUntil(
|
|
|
|
caches.keys()
|
|
|
|
.then(cacheNames => Promise.all(
|
|
|
|
cacheNames
|
|
|
|
.filter((v) => v !== swLang.cacheName)
|
2023-02-14 20:06:06 -08:00
|
|
|
.map(name => caches.delete(name)),
|
2022-04-30 05:52:07 -07:00
|
|
|
))
|
2023-02-17 21:16:34 -08:00
|
|
|
.then(() => globalThis.clients.claim()),
|
2022-04-30 05:52:07 -07:00
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2023-02-12 07:31:37 -08:00
|
|
|
globalThis.addEventListener('fetch', ev => {
|
2022-09-10 03:25:32 -07:00
|
|
|
let isHTMLRequest = false;
|
|
|
|
if (ev.request.headers.get('sec-fetch-dest') === 'document') {
|
|
|
|
isHTMLRequest = true;
|
|
|
|
} else if (ev.request.headers.get('accept')?.includes('/html')) {
|
|
|
|
isHTMLRequest = true;
|
|
|
|
} else if (ev.request.url.endsWith('/')) {
|
|
|
|
isHTMLRequest = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!isHTMLRequest) return;
|
2022-04-30 05:52:07 -07:00
|
|
|
ev.respondWith(
|
|
|
|
fetch(ev.request)
|
2023-02-14 20:06:06 -08:00
|
|
|
.catch(() => new Response(`Offline. Service Worker @${_VERSION_}`, { status: 200 })),
|
2022-04-30 05:52:07 -07:00
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2023-02-12 07:31:37 -08:00
|
|
|
globalThis.addEventListener('push', ev => {
|
2022-04-30 05:52:07 -07:00
|
|
|
// クライアント取得
|
2023-02-17 21:16:34 -08:00
|
|
|
ev.waitUntil(globalThis.clients.matchAll({
|
2022-04-30 05:52:07 -07:00
|
|
|
includeUncontrolled: true,
|
2023-02-14 20:06:06 -08:00
|
|
|
type: 'window',
|
2023-02-12 07:31:37 -08:00
|
|
|
}).then(async (clients: readonly WindowClient[]) => {
|
|
|
|
const data: pushNotificationDataMap[keyof pushNotificationDataMap] = ev.data?.json();
|
2022-04-30 05:52:07 -07:00
|
|
|
|
|
|
|
switch (data.type) {
|
|
|
|
// case 'driveFileCreated':
|
|
|
|
case 'notification':
|
2022-12-18 02:50:02 -08:00
|
|
|
case 'unreadAntennaNote':
|
2022-07-09 23:15:21 -07:00
|
|
|
// 1日以上経過している場合は無視
|
|
|
|
if ((new Date()).getTime() - data.dateTime > 1000 * 60 * 60 * 24) break;
|
|
|
|
|
2022-04-30 05:52:07 -07:00
|
|
|
return createNotification(data);
|
|
|
|
case 'readAllNotifications':
|
2023-02-17 21:16:34 -08:00
|
|
|
for (const n of await globalThis.registration.getNotifications()) {
|
2022-04-30 05:52:07 -07:00
|
|
|
if (n?.data?.type === 'notification') n.close();
|
|
|
|
}
|
|
|
|
break;
|
2022-12-18 02:50:02 -08:00
|
|
|
case 'readAllAntennas':
|
2023-02-17 21:16:34 -08:00
|
|
|
for (const n of await globalThis.registration.getNotifications()) {
|
2022-12-18 02:50:02 -08:00
|
|
|
if (n?.data?.type === 'unreadAntennaNote') n.close();
|
|
|
|
}
|
|
|
|
break;
|
2022-04-30 05:52:07 -07:00
|
|
|
case 'readNotifications':
|
2023-02-17 21:16:34 -08:00
|
|
|
for (const n of await globalThis.registration.getNotifications()) {
|
2023-02-14 20:06:06 -08:00
|
|
|
if (data.body.notificationIds.includes(n.data.body.id)) {
|
2022-04-30 05:52:07 -07:00
|
|
|
n.close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
2022-12-18 02:50:02 -08:00
|
|
|
case 'readAntenna':
|
2023-02-17 21:16:34 -08:00
|
|
|
for (const n of await globalThis.registration.getNotifications()) {
|
2023-02-14 20:06:06 -08:00
|
|
|
if (n?.data?.type === 'unreadAntennaNote' && data.body.antennaId === n.data.body.antenna.id) {
|
2022-12-18 02:50:02 -08:00
|
|
|
n.close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
2022-04-30 05:52:07 -07:00
|
|
|
}
|
|
|
|
|
2023-02-18 00:48:20 -08:00
|
|
|
await createEmptyNotification();
|
|
|
|
return;
|
2022-04-30 05:52:07 -07:00
|
|
|
}));
|
|
|
|
});
|
|
|
|
|
2023-02-12 07:31:37 -08:00
|
|
|
globalThis.addEventListener('notificationclick', (ev: ServiceWorkerGlobalScopeEventMap['notificationclick']) => {
|
2022-04-30 05:52:07 -07:00
|
|
|
ev.waitUntil((async () => {
|
|
|
|
if (_DEV_) {
|
|
|
|
console.log('notificationclick', ev.action, ev.notification.data);
|
|
|
|
}
|
2023-02-12 07:31:37 -08:00
|
|
|
|
2022-04-30 05:52:07 -07:00
|
|
|
const { action, notification } = ev;
|
2023-02-12 07:31:37 -08:00
|
|
|
const data: pushNotificationDataMap[keyof pushNotificationDataMap] = notification.data;
|
2022-12-18 02:50:02 -08:00
|
|
|
const { userId: loginId } = data;
|
2022-04-30 05:52:07 -07:00
|
|
|
let client: WindowClient | null = null;
|
2023-02-12 07:31:37 -08:00
|
|
|
|
2022-04-30 05:52:07 -07:00
|
|
|
switch (data.type) {
|
|
|
|
case 'notification':
|
|
|
|
switch (action) {
|
|
|
|
case 'follow':
|
2022-12-18 02:50:02 -08:00
|
|
|
if ('userId' in data.body) await swos.api('following/create', loginId, { userId: data.body.userId });
|
2022-04-30 05:52:07 -07:00
|
|
|
break;
|
|
|
|
case 'showUser':
|
2022-12-18 02:50:02 -08:00
|
|
|
if ('user' in data.body) client = await swos.openUser(getAcct(data.body.user), loginId);
|
2022-04-30 05:52:07 -07:00
|
|
|
break;
|
|
|
|
case 'reply':
|
2022-12-18 02:50:02 -08:00
|
|
|
if ('note' in data.body) client = await swos.openPost({ reply: data.body.note }, loginId);
|
2022-04-30 05:52:07 -07:00
|
|
|
break;
|
|
|
|
case 'renote':
|
2022-12-18 02:50:02 -08:00
|
|
|
if ('note' in data.body) await swos.api('notes/create', loginId, { renoteId: data.body.note.id });
|
2022-04-30 05:52:07 -07:00
|
|
|
break;
|
|
|
|
case 'accept':
|
|
|
|
switch (data.body.type) {
|
|
|
|
case 'receiveFollowRequest':
|
2022-12-18 02:50:02 -08:00
|
|
|
await swos.api('following/requests/accept', loginId, { userId: data.body.userId });
|
2022-04-30 05:52:07 -07:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'reject':
|
|
|
|
switch (data.body.type) {
|
|
|
|
case 'receiveFollowRequest':
|
2022-12-18 02:50:02 -08:00
|
|
|
await swos.api('following/requests/reject', loginId, { userId: data.body.userId });
|
2022-04-30 05:52:07 -07:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'showFollowRequests':
|
2022-12-18 02:50:02 -08:00
|
|
|
client = await swos.openClient('push', '/my/follow-requests', loginId);
|
2022-04-30 05:52:07 -07:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
switch (data.body.type) {
|
|
|
|
case 'receiveFollowRequest':
|
2022-12-18 02:50:02 -08:00
|
|
|
client = await swos.openClient('push', '/my/follow-requests', loginId);
|
2022-04-30 05:52:07 -07:00
|
|
|
break;
|
|
|
|
case 'reaction':
|
2022-12-18 02:50:02 -08:00
|
|
|
client = await swos.openNote(data.body.note.id, loginId);
|
2022-04-30 05:52:07 -07:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
if ('note' in data.body) {
|
2022-12-18 02:50:02 -08:00
|
|
|
client = await swos.openNote(data.body.note.id, loginId);
|
2022-04-30 05:52:07 -07:00
|
|
|
} else if ('user' in data.body) {
|
2022-12-18 02:50:02 -08:00
|
|
|
client = await swos.openUser(getAcct(data.body.user), loginId);
|
2022-04-30 05:52:07 -07:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
2022-12-18 02:50:02 -08:00
|
|
|
case 'unreadAntennaNote':
|
|
|
|
client = await swos.openAntenna(data.body.antenna.id, loginId);
|
2022-04-30 05:52:07 -07:00
|
|
|
}
|
2023-02-12 07:31:37 -08:00
|
|
|
|
2022-04-30 05:52:07 -07:00
|
|
|
if (client) {
|
|
|
|
client.focus();
|
|
|
|
}
|
|
|
|
if (data.type === 'notification') {
|
|
|
|
swNotificationRead.then(that => that.read(data));
|
|
|
|
}
|
2023-02-12 07:31:37 -08:00
|
|
|
|
2022-04-30 05:52:07 -07:00
|
|
|
notification.close();
|
|
|
|
})());
|
|
|
|
});
|
|
|
|
|
2023-02-12 07:31:37 -08:00
|
|
|
globalThis.addEventListener('notificationclose', (ev: ServiceWorkerGlobalScopeEventMap['notificationclose']) => {
|
|
|
|
const data: pushNotificationDataMap[keyof pushNotificationDataMap] = ev.notification.data;
|
2022-04-30 05:52:07 -07:00
|
|
|
|
|
|
|
if (data.type === 'notification') {
|
|
|
|
swNotificationRead.then(that => that.read(data));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2023-02-12 07:31:37 -08:00
|
|
|
globalThis.addEventListener('message', (ev: ServiceWorkerGlobalScopeEventMap['message']) => {
|
2022-04-30 05:52:07 -07:00
|
|
|
ev.waitUntil((async () => {
|
|
|
|
switch (ev.data) {
|
|
|
|
case 'clear':
|
|
|
|
// Cache Storage全削除
|
|
|
|
await caches.keys()
|
|
|
|
.then(cacheNames => Promise.all(
|
2023-02-14 20:06:06 -08:00
|
|
|
cacheNames.map(name => caches.delete(name)),
|
2022-04-30 05:52:07 -07:00
|
|
|
));
|
|
|
|
return; // TODO
|
|
|
|
}
|
2023-02-12 07:31:37 -08:00
|
|
|
|
2022-04-30 05:52:07 -07:00
|
|
|
if (typeof ev.data === 'object') {
|
|
|
|
// E.g. '[object Array]' → 'array'
|
|
|
|
const otype = Object.prototype.toString.call(ev.data).slice(8, -1).toLowerCase();
|
2023-02-12 07:31:37 -08:00
|
|
|
|
2022-04-30 05:52:07 -07:00
|
|
|
if (otype === 'object') {
|
|
|
|
if (ev.data.msg === 'initialize') {
|
|
|
|
swLang.setLang(ev.data.lang);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})());
|
|
|
|
});
|