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