2023-07-27 07:31:52 +02:00
|
|
|
/*
|
|
|
|
* SPDX-FileCopyrightText: syuilo and other misskey contributors
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
2023-09-19 09:37:43 +02:00
|
|
|
import { api, post } from '@/os.js';
|
|
|
|
import { $i, login } from '@/account.js';
|
|
|
|
import { getAccountFromId } from '@/scripts/get-account-from-id.js';
|
|
|
|
import { mainRouter } from '@/router.js';
|
|
|
|
import { deepClone } from '@/scripts/clone.js';
|
2022-04-30 14:52:07 +02:00
|
|
|
|
|
|
|
export function swInject() {
|
2023-04-11 07:11:39 +02:00
|
|
|
navigator.serviceWorker.addEventListener('message', async ev => {
|
2022-04-30 14:52:07 +02:00
|
|
|
if (_DEV_) {
|
|
|
|
console.log('sw msg', ev.data);
|
|
|
|
}
|
|
|
|
|
2022-05-26 15:53:09 +02:00
|
|
|
if (ev.data.type !== 'order') return;
|
2022-04-30 14:52:07 +02:00
|
|
|
|
2023-04-11 07:11:39 +02:00
|
|
|
if (ev.data.loginId && ev.data.loginId !== $i?.id) {
|
2022-05-26 15:53:09 +02:00
|
|
|
return getAccountFromId(ev.data.loginId).then(account => {
|
2022-04-30 14:52:07 +02:00
|
|
|
if (!account) return;
|
2022-05-26 15:53:09 +02:00
|
|
|
return login(account.token, ev.data.url);
|
2022-04-30 14:52:07 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-05-26 15:53:09 +02:00
|
|
|
switch (ev.data.order) {
|
2023-04-11 07:11:39 +02:00
|
|
|
case 'post': {
|
|
|
|
const props = deepClone(ev.data.options);
|
|
|
|
// プッシュ通知から来たreply,renoteはtruncateBodyが通されているため、
|
|
|
|
// 完全なノートを取得しなおす
|
|
|
|
if (props.reply) {
|
|
|
|
props.reply = await api('notes/show', { noteId: props.reply.id });
|
|
|
|
}
|
|
|
|
if (props.renote) {
|
|
|
|
props.renote = await api('notes/show', { noteId: props.renote.id });
|
|
|
|
}
|
|
|
|
return post(props);
|
|
|
|
}
|
2022-04-30 14:52:07 +02:00
|
|
|
case 'push':
|
2022-06-20 10:38:49 +02:00
|
|
|
if (mainRouter.currentRoute.value.path === ev.data.url) {
|
2022-04-30 14:52:07 +02:00
|
|
|
return window.scroll({ top: 0, behavior: 'smooth' });
|
|
|
|
}
|
2022-06-20 10:38:49 +02:00
|
|
|
return mainRouter.push(ev.data.url);
|
2022-04-30 14:52:07 +02:00
|
|
|
default:
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|