2023-07-27 07:31:52 +02:00
|
|
|
/*
|
2024-02-13 16:59:27 +01:00
|
|
|
* SPDX-FileCopyrightText: syuilo and misskey-project
|
2023-07-27 07:31:52 +02:00
|
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
2024-01-04 10:32:46 +01:00
|
|
|
import { post } from '@/os.js';
|
|
|
|
import { misskeyApi } from '@/scripts/misskey-api.js';
|
2023-09-19 09:37:43 +02:00
|
|
|
import { $i, login } from '@/account.js';
|
|
|
|
import { getAccountFromId } from '@/scripts/get-account-from-id.js';
|
|
|
|
import { deepClone } from '@/scripts/clone.js';
|
2024-01-30 13:07:34 +01:00
|
|
|
import { mainRouter } from '@/router/main.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) {
|
2024-01-04 10:32:46 +01:00
|
|
|
props.reply = await misskeyApi('notes/show', { noteId: props.reply.id });
|
2023-04-11 07:11:39 +02:00
|
|
|
}
|
|
|
|
if (props.renote) {
|
2024-01-04 10:32:46 +01:00
|
|
|
props.renote = await misskeyApi('notes/show', { noteId: props.renote.id });
|
2023-04-11 07:11:39 +02:00
|
|
|
}
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|