2023-07-26 22:31:52 -07:00
|
|
|
/*
|
2024-02-13 07:59:27 -08:00
|
|
|
* SPDX-FileCopyrightText: syuilo and misskey-project
|
2023-07-26 22:31:52 -07:00
|
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
2023-04-11 09:07:24 -07:00
|
|
|
import { get } from 'idb-keyval';
|
2023-09-03 21:33:38 -07:00
|
|
|
import * as Misskey from 'misskey-js';
|
2023-09-28 16:04:53 -07:00
|
|
|
import type { PushNotificationDataMap } from '@/types.js';
|
2024-09-09 04:57:36 -07:00
|
|
|
import type { I18n } from '@@/js/i18n.js';
|
2024-08-29 18:58:11 -07:00
|
|
|
import type { Locale } from '../../../locales/index.js';
|
2023-09-19 00:37:43 -07:00
|
|
|
import { createEmptyNotification, createNotification } from '@/scripts/create-notification.js';
|
|
|
|
import { swLang } from '@/scripts/lang.js';
|
|
|
|
import * as swos from '@/scripts/operations.js';
|
2022-04-30 05:52:07 -07:00
|
|
|
|
2023-04-11 09:07:24 -07:00
|
|
|
globalThis.addEventListener('install', () => {
|
|
|
|
// 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
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2024-01-20 18:50:05 -08:00
|
|
|
async function offlineContentHTML() {
|
|
|
|
const i18n = await (swLang.i18n ?? swLang.fetchLocale()) as Partial<I18n<Locale>>;
|
|
|
|
const messages = {
|
2024-08-29 18:58:11 -07:00
|
|
|
title: i18n.ts?._offlineScreen.title ?? 'Offline - Could not connect to server',
|
|
|
|
header: i18n.ts?._offlineScreen.header ?? 'Could not connect to server',
|
2024-01-20 18:50:05 -08:00
|
|
|
reload: i18n.ts?.reload ?? 'Reload',
|
|
|
|
};
|
|
|
|
|
|
|
|
return `<!DOCTYPE html><html lang="ja"><head><meta charset="UTF-8"><meta content="width=device-width,initial-scale=1"name="viewport"><title>${messages.title}</title><style>body{background-color:#0c1210;color:#dee7e4;font-family:Hiragino Maru Gothic Pro,BIZ UDGothic,Roboto,HelveticaNeue,Arial,sans-serif;line-height:1.35;display:flex;flex-direction:column;align-items:center;justify-content:center;min-height:100vh;margin:0;padding:24px;box-sizing:border-box}.icon{max-width:120px;width:100%;height:auto;margin-bottom:20px;}.message{text-align:center;font-size:20px;font-weight:700;margin-bottom:20px}.version{text-align:center;font-size:90%;margin-bottom:20px}button{padding:7px 14px;min-width:100px;font-weight:700;font-family:Hiragino Maru Gothic Pro,BIZ UDGothic,Roboto,HelveticaNeue,Arial,sans-serif;line-height:1.35;border-radius:99rem;background-color:#b4e900;color:#192320;border:none;cursor:pointer;-webkit-tap-highlight-color:transparent}button:hover{background-color:#c6ff03}</style></head><body><svg class="icon"fill="none"height="24"stroke="currentColor"stroke-linecap="round"stroke-linejoin="round"stroke-width="2"viewBox="0 0 24 24"width="24"xmlns="http://www.w3.org/2000/svg"><path d="M0 0h24v24H0z"fill="none"stroke="none"/><path d="M9.58 5.548c.24 -.11 .492 -.207 .752 -.286c1.88 -.572 3.956 -.193 5.444 1c1.488 1.19 2.162 3.007 1.77 4.769h.99c1.913 0 3.464 1.56 3.464 3.486c0 .957 -.383 1.824 -1.003 2.454m-2.997 1.033h-11.343c-2.572 -.004 -4.657 -2.011 -4.657 -4.487c0 -2.475 2.085 -4.482 4.657 -4.482c.13 -.582 .37 -1.128 .7 -1.62"/><path d="M3 3l18 18"/></svg><div class="message">${messages.header}</div><div class="version">v${_VERSION_}</div><button onclick="reloadPage()">${messages.reload}</button><script>function reloadPage(){location.reload(!0)}</script></body></html>`;
|
2023-07-11 02:24:10 -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)
|
2024-01-20 18:50:05 -08:00
|
|
|
.catch(async () => {
|
|
|
|
const html = await offlineContentHTML();
|
|
|
|
return new Response(html, {
|
2023-07-11 02:24:10 -07:00
|
|
|
status: 200,
|
|
|
|
headers: {
|
|
|
|
'content-type': 'text/html',
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}),
|
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-04-11 09:07:24 -07:00
|
|
|
}).then(async () => {
|
2023-04-10 05:10:06 -07:00
|
|
|
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日以上経過している場合は無視
|
2024-04-07 05:14:13 -07:00
|
|
|
if (Date.now() - data.dateTime > 1000 * 60 * 60 * 24) break;
|
2022-07-09 23:15:21 -07:00
|
|
|
|
2022-04-30 05:52:07 -07:00
|
|
|
return createNotification(data);
|
2023-04-10 22:11:39 -07:00
|
|
|
case 'readAllNotifications':
|
|
|
|
await globalThis.registration.getNotifications()
|
2023-04-17 23:01:18 -07:00
|
|
|
.then(notifications => notifications.forEach(n => n.tag !== 'read_notification' && n.close()));
|
2023-04-10 22:11:39 -07:00
|
|
|
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']) => {
|
2023-04-11 09:07:24 -07:00
|
|
|
ev.waitUntil((async (): Promise<void> => {
|
2022-04-30 05:52:07 -07:00
|
|
|
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-04-10 22:11:39 -07: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':
|
2023-09-03 21:33:38 -07:00
|
|
|
if ('user' in data.body) client = await swos.openUser(Misskey.acct.toString(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;
|
2024-02-20 07:10:41 -08:00
|
|
|
case 'edited':
|
|
|
|
if ('note' in data.body) client = await swos.openPost({ reply: data.body.note }, loginId);
|
|
|
|
break;
|
2022-04-30 05:52:07 -07:00
|
|
|
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) {
|
2023-09-03 21:33:38 -07:00
|
|
|
client = await swos.openUser(Misskey.acct.toString(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);
|
2023-04-10 22:11:39 -07:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
switch (action) {
|
|
|
|
case 'markAllAsRead':
|
|
|
|
await globalThis.registration.getNotifications()
|
2023-04-17 23:01:18 -07:00
|
|
|
.then(notifications => notifications.forEach(n => n.tag !== 'read_notification' && n.close()));
|
2024-08-29 23:53:04 -07:00
|
|
|
await get<Pick<Misskey.entities.SignupResponse, 'id' | 'token'>[]>('accounts').then(accounts => {
|
|
|
|
return Promise.all((accounts ?? []).map(async account => {
|
2023-04-10 22:11:39 -07:00
|
|
|
await swos.sendMarkAllAsRead(account.id);
|
|
|
|
}));
|
|
|
|
});
|
|
|
|
break;
|
|
|
|
case 'settings':
|
|
|
|
client = await swos.openClient('push', '/settings/notifications', loginId);
|
|
|
|
break;
|
|
|
|
}
|
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') {
|
2023-04-10 22:11:39 -07:00
|
|
|
await swos.sendMarkAllAsRead(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
|
|
|
notification.close();
|
|
|
|
})());
|
|
|
|
});
|
|
|
|
|
2023-02-12 07:31:37 -08:00
|
|
|
globalThis.addEventListener('notificationclose', (ev: ServiceWorkerGlobalScopeEventMap['notificationclose']) => {
|
2023-04-10 05:10:06 -07:00
|
|
|
const data: PushNotificationDataMap[keyof PushNotificationDataMap] = ev.notification.data;
|
2022-04-30 05:52:07 -07:00
|
|
|
|
2023-04-11 09:07:24 -07:00
|
|
|
ev.waitUntil((async (): Promise<void> => {
|
2023-04-10 22:11:39 -07:00
|
|
|
if (data.type === 'notification') {
|
|
|
|
await swos.sendMarkAllAsRead(data.userId);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
})());
|
2022-04-30 05:52:07 -07:00
|
|
|
});
|
|
|
|
|
2023-02-12 07:31:37 -08:00
|
|
|
globalThis.addEventListener('message', (ev: ServiceWorkerGlobalScopeEventMap['message']) => {
|
2023-04-11 09:07:24 -07:00
|
|
|
ev.waitUntil((async (): Promise<void> => {
|
2022-04-30 05:52:07 -07:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})());
|
|
|
|
});
|