2021-11-11 10:02:25 -07:00
|
|
|
import { del, get, set } from '@/scripts/idb-proxy';
|
2020-12-18 18:55:52 -07:00
|
|
|
import { reactive } from 'vue';
|
2021-12-17 22:56:35 -07:00
|
|
|
import * as misskey from 'misskey-js';
|
2021-11-11 10:02:25 -07:00
|
|
|
import { apiUrl } from '@/config';
|
|
|
|
import { waiting, api, popup, popupMenu, success } from '@/os';
|
|
|
|
import { unisonReload, reloadChannel } from '@/scripts/unison-reload';
|
2021-09-18 11:23:12 -06:00
|
|
|
import { showSuspendedDialog } from './scripts/show-suspended-dialog';
|
2021-10-10 00:19:16 -06:00
|
|
|
import { i18n } from './i18n';
|
2020-12-18 18:55:52 -07:00
|
|
|
|
|
|
|
// TODO: 他のタブと永続化されたstateを同期
|
|
|
|
|
2021-12-17 22:56:35 -07:00
|
|
|
type Account = misskey.entities.MeDetailed;
|
2020-12-18 18:55:52 -07:00
|
|
|
|
|
|
|
const data = localStorage.getItem('account');
|
|
|
|
|
|
|
|
// TODO: 外部からはreadonlyに
|
|
|
|
export const $i = data ? reactive(JSON.parse(data) as Account) : null;
|
|
|
|
|
2021-08-20 04:38:16 -06:00
|
|
|
export async function signout() {
|
|
|
|
waiting();
|
2020-12-18 18:55:52 -07:00
|
|
|
localStorage.removeItem('account');
|
2021-08-20 04:38:16 -06:00
|
|
|
|
|
|
|
//#region Remove account
|
|
|
|
const accounts = await getAccounts();
|
|
|
|
accounts.splice(accounts.findIndex(x => x.id === $i.id), 1);
|
2021-09-04 03:09:53 -06:00
|
|
|
|
|
|
|
if (accounts.length > 0) await set('accounts', accounts);
|
|
|
|
else await del('accounts');
|
2021-08-20 04:38:16 -06:00
|
|
|
//#endregion
|
|
|
|
|
2021-09-04 03:09:53 -06:00
|
|
|
//#region Remove service worker registration
|
2021-08-20 20:51:46 -06:00
|
|
|
try {
|
2021-09-04 03:09:53 -06:00
|
|
|
if (navigator.serviceWorker.controller) {
|
|
|
|
const registration = await navigator.serviceWorker.ready;
|
|
|
|
const push = await registration.pushManager.getSubscription();
|
|
|
|
if (push) {
|
|
|
|
await fetch(`${apiUrl}/sw/unregister`, {
|
|
|
|
method: 'POST',
|
|
|
|
body: JSON.stringify({
|
|
|
|
i: $i.token,
|
|
|
|
endpoint: push.endpoint,
|
|
|
|
}),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (accounts.length === 0) {
|
|
|
|
await navigator.serviceWorker.getRegistrations()
|
|
|
|
.then(registrations => {
|
|
|
|
return Promise.all(registrations.map(registration => registration.unregister()));
|
|
|
|
});
|
|
|
|
}
|
2021-08-20 20:51:46 -06:00
|
|
|
} catch (e) {}
|
2021-08-20 04:38:16 -06:00
|
|
|
//#endregion
|
|
|
|
|
2020-12-18 18:55:52 -07:00
|
|
|
document.cookie = `igi=; path=/`;
|
2021-08-20 04:38:16 -06:00
|
|
|
|
|
|
|
if (accounts.length > 0) login(accounts[0].token);
|
2021-11-04 09:09:13 -06:00
|
|
|
else unisonReload('/');
|
2020-12-18 18:55:52 -07:00
|
|
|
}
|
|
|
|
|
2021-08-20 04:38:16 -06:00
|
|
|
export async function getAccounts(): Promise<{ id: Account['id'], token: Account['token'] }[]> {
|
|
|
|
return (await get('accounts')) || [];
|
2020-12-18 18:55:52 -07:00
|
|
|
}
|
|
|
|
|
2021-08-20 04:38:16 -06:00
|
|
|
export async function addAccount(id: Account['id'], token: Account['token']) {
|
|
|
|
const accounts = await getAccounts();
|
2020-12-18 18:55:52 -07:00
|
|
|
if (!accounts.some(x => x.id === id)) {
|
2021-08-20 04:38:16 -06:00
|
|
|
await set('accounts', accounts.concat([{ id, token }]));
|
2020-12-18 18:55:52 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function fetchAccount(token): Promise<Account> {
|
|
|
|
return new Promise((done, fail) => {
|
|
|
|
// Fetch user
|
|
|
|
fetch(`${apiUrl}/i`, {
|
|
|
|
method: 'POST',
|
|
|
|
body: JSON.stringify({
|
|
|
|
i: token
|
|
|
|
})
|
|
|
|
})
|
2021-09-18 11:23:12 -06:00
|
|
|
.then(res => res.json())
|
2020-12-18 18:55:52 -07:00
|
|
|
.then(res => {
|
2021-09-18 11:23:12 -06:00
|
|
|
if (res.error) {
|
|
|
|
if (res.error.id === 'a8c724b3-6e9c-4b46-b1a8-bc3ed6258370') {
|
|
|
|
showSuspendedDialog().then(() => {
|
|
|
|
signout();
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
signout();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
res.token = token;
|
|
|
|
done(res);
|
2020-12-18 18:55:52 -07:00
|
|
|
}
|
|
|
|
})
|
|
|
|
.catch(fail);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
export function updateAccount(data) {
|
|
|
|
for (const [key, value] of Object.entries(data)) {
|
|
|
|
$i[key] = value;
|
|
|
|
}
|
2021-02-11 00:06:19 -07:00
|
|
|
localStorage.setItem('account', JSON.stringify($i));
|
2020-12-18 18:55:52 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
export function refreshAccount() {
|
2021-08-20 04:38:16 -06:00
|
|
|
return fetchAccount($i.token).then(updateAccount);
|
2020-12-18 18:55:52 -07:00
|
|
|
}
|
|
|
|
|
2021-08-20 04:38:16 -06:00
|
|
|
export async function login(token: Account['token'], redirect?: string) {
|
2020-12-18 18:55:52 -07:00
|
|
|
waiting();
|
|
|
|
if (_DEV_) console.log('logging as token ', token);
|
|
|
|
const me = await fetchAccount(token);
|
|
|
|
localStorage.setItem('account', JSON.stringify(me));
|
2021-08-20 04:38:16 -06:00
|
|
|
await addAccount(me.id, token);
|
|
|
|
|
|
|
|
if (redirect) {
|
2021-11-04 09:09:13 -06:00
|
|
|
// 他のタブは再読み込みするだけ
|
|
|
|
reloadChannel.postMessage(null);
|
|
|
|
// このページはredirectで指定された先に移動
|
2021-08-20 04:38:16 -06:00
|
|
|
location.href = redirect;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-02-17 05:36:56 -07:00
|
|
|
unisonReload();
|
2020-12-18 18:55:52 -07:00
|
|
|
}
|
|
|
|
|
2021-10-10 00:19:16 -06:00
|
|
|
export async function openAccountMenu(ev: MouseEvent) {
|
|
|
|
function showSigninDialog() {
|
2021-11-11 10:02:25 -07:00
|
|
|
popup(import('@/components/signin-dialog.vue'), {}, {
|
2021-10-10 00:19:16 -06:00
|
|
|
done: res => {
|
|
|
|
addAccount(res.id, res.i);
|
|
|
|
success();
|
|
|
|
},
|
|
|
|
}, 'closed');
|
|
|
|
}
|
|
|
|
|
|
|
|
function createAccount() {
|
2021-11-11 10:02:25 -07:00
|
|
|
popup(import('@/components/signup-dialog.vue'), {}, {
|
2021-10-10 00:19:16 -06:00
|
|
|
done: res => {
|
|
|
|
addAccount(res.id, res.i);
|
|
|
|
switchAccountWithToken(res.i);
|
|
|
|
},
|
|
|
|
}, 'closed');
|
|
|
|
}
|
|
|
|
|
|
|
|
async function switchAccount(account: any) {
|
|
|
|
const storedAccounts = await getAccounts();
|
|
|
|
const token = storedAccounts.find(x => x.id === account.id).token;
|
|
|
|
switchAccountWithToken(token);
|
|
|
|
}
|
|
|
|
|
|
|
|
function switchAccountWithToken(token: string) {
|
|
|
|
login(token);
|
|
|
|
}
|
|
|
|
|
|
|
|
const storedAccounts = await getAccounts().then(accounts => accounts.filter(x => x.id !== $i.id));
|
|
|
|
const accountsPromise = api('users/show', { userIds: storedAccounts.map(x => x.id) });
|
|
|
|
|
|
|
|
const accountItemPromises = storedAccounts.map(a => new Promise(res => {
|
|
|
|
accountsPromise.then(accounts => {
|
|
|
|
const account = accounts.find(x => x.id === a.id);
|
|
|
|
if (account == null) return res(null);
|
|
|
|
res({
|
|
|
|
type: 'user',
|
|
|
|
user: account,
|
|
|
|
action: () => { switchAccount(account); }
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}));
|
|
|
|
|
|
|
|
popupMenu([...[{
|
|
|
|
type: 'link',
|
|
|
|
text: i18n.locale.profile,
|
|
|
|
to: `/@${ $i.username }`,
|
|
|
|
avatar: $i,
|
|
|
|
}, null, ...accountItemPromises, {
|
|
|
|
icon: 'fas fa-plus',
|
|
|
|
text: i18n.locale.addAccount,
|
|
|
|
action: () => {
|
|
|
|
popupMenu([{
|
|
|
|
text: i18n.locale.existingAccount,
|
|
|
|
action: () => { showSigninDialog(); },
|
|
|
|
}, {
|
|
|
|
text: i18n.locale.createAccount,
|
|
|
|
action: () => { createAccount(); },
|
|
|
|
}], ev.currentTarget || ev.target);
|
|
|
|
},
|
|
|
|
}, {
|
|
|
|
type: 'link',
|
|
|
|
icon: 'fas fa-users',
|
|
|
|
text: i18n.locale.manageAccounts,
|
|
|
|
to: `/settings/accounts`,
|
|
|
|
}]], ev.currentTarget || ev.target, {
|
|
|
|
align: 'left'
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-12-18 18:55:52 -07:00
|
|
|
// このファイルに書きたくないけどここに書かないと何故かVeturが認識しない
|
|
|
|
declare module '@vue/runtime-core' {
|
|
|
|
interface ComponentCustomProperties {
|
|
|
|
$i: typeof $i;
|
|
|
|
}
|
|
|
|
}
|