2023-07-27 14:31:52 +09:00
|
|
|
/*
|
|
|
|
* SPDX-FileCopyrightText: syuilo and other misskey contributors
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
2023-09-19 16:37:43 +09: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 21:52:07 +09:00
|
|
|
|
|
|
|
export function swInject() {
|
2023-04-11 14:11:39 +09:00
|
|
|
navigator.serviceWorker.addEventListener('message', async ev => {
|
2022-04-30 21:52:07 +09:00
|
|
|
if (_DEV_) {
|
|
|
|
console.log('sw msg', ev.data);
|
|
|
|
}
|
|
|
|
|
2022-05-26 22:53:09 +09:00
|
|
|
if (ev.data.type !== 'order') return;
|
2022-04-30 21:52:07 +09:00
|
|
|
|
2023-04-11 14:11:39 +09:00
|
|
|
if (ev.data.loginId && ev.data.loginId !== $i?.id) {
|
2022-05-26 22:53:09 +09:00
|
|
|
return getAccountFromId(ev.data.loginId).then(account => {
|
2022-04-30 21:52:07 +09:00
|
|
|
if (!account) return;
|
2022-05-26 22:53:09 +09:00
|
|
|
return login(account.token, ev.data.url);
|
2022-04-30 21:52:07 +09:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-05-26 22:53:09 +09:00
|
|
|
switch (ev.data.order) {
|
2023-04-11 14:11:39 +09: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 21:52:07 +09:00
|
|
|
case 'push':
|
2022-06-20 17:38:49 +09:00
|
|
|
if (mainRouter.currentRoute.value.path === ev.data.url) {
|
2022-04-30 21:52:07 +09:00
|
|
|
return window.scroll({ top: 0, behavior: 'smooth' });
|
|
|
|
}
|
2022-06-20 17:38:49 +09:00
|
|
|
return mainRouter.push(ev.data.url);
|
2022-04-30 21:52:07 +09:00
|
|
|
default:
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|