2022-05-01 15:51:07 +02:00
|
|
|
import { defineAsyncComponent, reactive } from 'vue';
|
2021-12-18 06:56:35 +01:00
|
|
|
import * as misskey from 'misskey-js';
|
2022-06-20 10:38:49 +02:00
|
|
|
import { showSuspendedDialog } from './scripts/show-suspended-dialog';
|
|
|
|
import { i18n } from './i18n';
|
2023-01-21 05:14:55 +01:00
|
|
|
import { miLocalStorage } from './local-storage';
|
2022-06-20 10:38:49 +02:00
|
|
|
import { del, get, set } from '@/scripts/idb-proxy';
|
2021-11-11 18:02:25 +01:00
|
|
|
import { apiUrl } from '@/config';
|
2022-03-24 18:43:48 +01:00
|
|
|
import { waiting, api, popup, popupMenu, success, alert } from '@/os';
|
2021-11-11 18:02:25 +01:00
|
|
|
import { unisonReload, reloadChannel } from '@/scripts/unison-reload';
|
2020-12-19 02:55:52 +01:00
|
|
|
|
|
|
|
// TODO: 他のタブと永続化されたstateを同期
|
|
|
|
|
2021-12-18 06:56:35 +01:00
|
|
|
type Account = misskey.entities.MeDetailed;
|
2020-12-19 02:55:52 +01:00
|
|
|
|
2023-01-07 02:13:02 +01:00
|
|
|
const accountData = miLocalStorage.getItem('account');
|
2020-12-19 02:55:52 +01:00
|
|
|
|
|
|
|
// TODO: 外部からはreadonlyに
|
2022-05-26 15:53:09 +02:00
|
|
|
export const $i = accountData ? reactive(JSON.parse(accountData) as Account) : null;
|
2020-12-19 02:55:52 +01:00
|
|
|
|
2022-01-18 13:30:17 +01:00
|
|
|
export const iAmModerator = $i != null && ($i.isAdmin || $i.isModerator);
|
2022-07-02 08:12:11 +02:00
|
|
|
export const iAmAdmin = $i != null && $i.isAdmin;
|
2022-01-18 13:30:17 +01:00
|
|
|
|
2023-01-21 05:14:55 +01:00
|
|
|
export let notesCount = $i == null ? 0 : $i.notesCount;
|
|
|
|
export function incNotesCount() {
|
|
|
|
notesCount++;
|
|
|
|
}
|
|
|
|
|
2021-08-20 12:38:16 +02:00
|
|
|
export async function signout() {
|
|
|
|
waiting();
|
2023-01-07 02:13:02 +01:00
|
|
|
miLocalStorage.removeItem('account');
|
2021-08-20 12:38:16 +02:00
|
|
|
|
2022-06-20 10:38:49 +02:00
|
|
|
await removeAccount($i.id);
|
2021-08-20 12:38:16 +02:00
|
|
|
|
2022-06-24 14:16:05 +02:00
|
|
|
const accounts = await getAccounts();
|
|
|
|
|
2021-09-04 11:09:53 +02:00
|
|
|
//#region Remove service worker registration
|
2021-08-21 04:51:46 +02:00
|
|
|
try {
|
2021-09-04 11:09:53 +02:00
|
|
|
if (navigator.serviceWorker.controller) {
|
|
|
|
const registration = await navigator.serviceWorker.ready;
|
|
|
|
const push = await registration.pushManager.getSubscription();
|
|
|
|
if (push) {
|
2022-12-03 11:42:05 +01:00
|
|
|
await window.fetch(`${apiUrl}/sw/unregister`, {
|
2021-09-04 11:09:53 +02:00
|
|
|
method: 'POST',
|
|
|
|
body: JSON.stringify({
|
|
|
|
i: $i.token,
|
|
|
|
endpoint: push.endpoint,
|
|
|
|
}),
|
2022-12-03 11:42:05 +01:00
|
|
|
headers: {
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
},
|
2021-09-04 11:09:53 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (accounts.length === 0) {
|
|
|
|
await navigator.serviceWorker.getRegistrations()
|
|
|
|
.then(registrations => {
|
|
|
|
return Promise.all(registrations.map(registration => registration.unregister()));
|
|
|
|
});
|
|
|
|
}
|
2022-05-26 15:53:09 +02:00
|
|
|
} catch (err) {}
|
2021-08-20 12:38:16 +02:00
|
|
|
//#endregion
|
|
|
|
|
|
|
|
if (accounts.length > 0) login(accounts[0].token);
|
2021-11-04 16:09:13 +01:00
|
|
|
else unisonReload('/');
|
2020-12-19 02:55:52 +01:00
|
|
|
}
|
|
|
|
|
2021-08-20 12:38:16 +02:00
|
|
|
export async function getAccounts(): Promise<{ id: Account['id'], token: Account['token'] }[]> {
|
|
|
|
return (await get('accounts')) || [];
|
2020-12-19 02:55:52 +01:00
|
|
|
}
|
|
|
|
|
2021-08-20 12:38:16 +02:00
|
|
|
export async function addAccount(id: Account['id'], token: Account['token']) {
|
|
|
|
const accounts = await getAccounts();
|
2020-12-19 02:55:52 +01:00
|
|
|
if (!accounts.some(x => x.id === id)) {
|
2021-08-20 12:38:16 +02:00
|
|
|
await set('accounts', accounts.concat([{ id, token }]));
|
2020-12-19 02:55:52 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-20 10:38:49 +02:00
|
|
|
export async function removeAccount(id: Account['id']) {
|
|
|
|
const accounts = await getAccounts();
|
|
|
|
accounts.splice(accounts.findIndex(x => x.id === id), 1);
|
|
|
|
|
|
|
|
if (accounts.length > 0) await set('accounts', accounts);
|
|
|
|
else await del('accounts');
|
|
|
|
}
|
|
|
|
|
2022-04-11 15:50:53 +02:00
|
|
|
function fetchAccount(token: string): Promise<Account> {
|
2020-12-19 02:55:52 +01:00
|
|
|
return new Promise((done, fail) => {
|
|
|
|
// Fetch user
|
2022-12-03 11:42:05 +01:00
|
|
|
window.fetch(`${apiUrl}/i`, {
|
2020-12-19 02:55:52 +01:00
|
|
|
method: 'POST',
|
|
|
|
body: JSON.stringify({
|
2022-06-20 10:38:49 +02:00
|
|
|
i: token,
|
|
|
|
}),
|
2022-12-03 11:42:05 +01:00
|
|
|
headers: {
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
},
|
2020-12-19 02:55:52 +01:00
|
|
|
})
|
2022-12-03 11:42:05 +01:00
|
|
|
.then(res => res.json())
|
|
|
|
.then(res => {
|
|
|
|
if (res.error) {
|
|
|
|
if (res.error.id === 'a8c724b3-6e9c-4b46-b1a8-bc3ed6258370') {
|
|
|
|
showSuspendedDialog().then(() => {
|
|
|
|
signout();
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
alert({
|
|
|
|
type: 'error',
|
|
|
|
title: i18n.ts.failedToFetchAccountInformation,
|
|
|
|
text: JSON.stringify(res.error),
|
|
|
|
});
|
|
|
|
}
|
2021-09-18 19:23:12 +02:00
|
|
|
} else {
|
2022-12-03 11:42:05 +01:00
|
|
|
res.token = token;
|
|
|
|
done(res);
|
2021-09-18 19:23:12 +02:00
|
|
|
}
|
2022-12-03 11:42:05 +01:00
|
|
|
})
|
|
|
|
.catch(fail);
|
2020-12-19 02:55:52 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-05-26 15:53:09 +02:00
|
|
|
export function updateAccount(accountData) {
|
|
|
|
for (const [key, value] of Object.entries(accountData)) {
|
2020-12-19 02:55:52 +01:00
|
|
|
$i[key] = value;
|
|
|
|
}
|
2023-01-07 02:13:02 +01:00
|
|
|
miLocalStorage.setItem('account', JSON.stringify($i));
|
2020-12-19 02:55:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
export function refreshAccount() {
|
2021-08-20 12:38:16 +02:00
|
|
|
return fetchAccount($i.token).then(updateAccount);
|
2020-12-19 02:55:52 +01:00
|
|
|
}
|
|
|
|
|
2021-08-20 12:38:16 +02:00
|
|
|
export async function login(token: Account['token'], redirect?: string) {
|
2020-12-19 02:55:52 +01:00
|
|
|
waiting();
|
|
|
|
if (_DEV_) console.log('logging as token ', token);
|
|
|
|
const me = await fetchAccount(token);
|
2023-01-07 02:13:02 +01:00
|
|
|
miLocalStorage.setItem('account', JSON.stringify(me));
|
2022-03-19 11:08:55 +01:00
|
|
|
document.cookie = `token=${token}; path=/; max-age=31536000`; // bull dashboardの認証とかで使う
|
2021-08-20 12:38:16 +02:00
|
|
|
await addAccount(me.id, token);
|
|
|
|
|
|
|
|
if (redirect) {
|
2021-11-04 16:09:13 +01:00
|
|
|
// 他のタブは再読み込みするだけ
|
|
|
|
reloadChannel.postMessage(null);
|
|
|
|
// このページはredirectで指定された先に移動
|
2021-08-20 12:38:16 +02:00
|
|
|
location.href = redirect;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-02-17 13:36:56 +01:00
|
|
|
unisonReload();
|
2020-12-19 02:55:52 +01:00
|
|
|
}
|
|
|
|
|
2022-01-21 12:17:31 +01:00
|
|
|
export async function openAccountMenu(opts: {
|
|
|
|
includeCurrentAccount?: boolean;
|
|
|
|
withExtraOperation: boolean;
|
|
|
|
active?: misskey.entities.UserDetailed['id'];
|
|
|
|
onChoose?: (account: misskey.entities.UserDetailed) => void;
|
|
|
|
}, ev: MouseEvent) {
|
2021-10-10 08:19:16 +02:00
|
|
|
function showSigninDialog() {
|
2022-08-30 17:24:33 +02:00
|
|
|
popup(defineAsyncComponent(() => import('@/components/MkSigninDialog.vue')), {}, {
|
2021-10-10 08:19:16 +02:00
|
|
|
done: res => {
|
|
|
|
addAccount(res.id, res.i);
|
|
|
|
success();
|
|
|
|
},
|
|
|
|
}, 'closed');
|
|
|
|
}
|
|
|
|
|
|
|
|
function createAccount() {
|
2022-08-30 17:24:33 +02:00
|
|
|
popup(defineAsyncComponent(() => import('@/components/MkSignupDialog.vue')), {}, {
|
2021-10-10 08:19:16 +02:00
|
|
|
done: res => {
|
|
|
|
addAccount(res.id, res.i);
|
|
|
|
switchAccountWithToken(res.i);
|
|
|
|
},
|
|
|
|
}, 'closed');
|
|
|
|
}
|
|
|
|
|
2022-01-21 12:17:31 +01:00
|
|
|
async function switchAccount(account: misskey.entities.UserDetailed) {
|
2021-10-10 08:19:16 +02:00
|
|
|
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) });
|
|
|
|
|
2022-01-21 12:17:31 +01:00
|
|
|
function createItem(account: misskey.entities.UserDetailed) {
|
|
|
|
return {
|
|
|
|
type: 'user',
|
|
|
|
user: account,
|
|
|
|
active: opts.active != null ? opts.active === account.id : false,
|
|
|
|
action: () => {
|
|
|
|
if (opts.onChoose) {
|
|
|
|
opts.onChoose(account);
|
|
|
|
} else {
|
|
|
|
switchAccount(account);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-10-10 08:19:16 +02:00
|
|
|
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);
|
2022-01-21 12:17:31 +01:00
|
|
|
res(createItem(account));
|
2021-10-10 08:19:16 +02:00
|
|
|
});
|
|
|
|
}));
|
|
|
|
|
2022-01-21 12:17:31 +01:00
|
|
|
if (opts.withExtraOperation) {
|
|
|
|
popupMenu([...[{
|
|
|
|
type: 'link',
|
2022-01-28 03:39:49 +01:00
|
|
|
text: i18n.ts.profile,
|
2022-01-21 12:17:31 +01:00
|
|
|
to: `/@${ $i.username }`,
|
|
|
|
avatar: $i,
|
|
|
|
}, null, ...(opts.includeCurrentAccount ? [createItem($i)] : []), ...accountItemPromises, {
|
2022-07-17 16:18:05 +02:00
|
|
|
type: 'parent',
|
2022-12-19 11:01:30 +01:00
|
|
|
icon: 'ti ti-plus',
|
2022-01-28 03:39:49 +01:00
|
|
|
text: i18n.ts.addAccount,
|
2022-07-17 16:18:05 +02:00
|
|
|
children: [{
|
|
|
|
text: i18n.ts.existingAccount,
|
|
|
|
action: () => { showSigninDialog(); },
|
|
|
|
}, {
|
|
|
|
text: i18n.ts.createAccount,
|
|
|
|
action: () => { createAccount(); },
|
|
|
|
}],
|
2022-01-21 12:17:31 +01:00
|
|
|
}, {
|
|
|
|
type: 'link',
|
2022-12-19 11:01:30 +01:00
|
|
|
icon: 'ti ti-users',
|
2022-01-28 03:39:49 +01:00
|
|
|
text: i18n.ts.manageAccounts,
|
2022-06-20 10:38:49 +02:00
|
|
|
to: '/settings/accounts',
|
2022-01-28 03:53:12 +01:00
|
|
|
}]], ev.currentTarget ?? ev.target, {
|
2022-06-20 10:38:49 +02:00
|
|
|
align: 'left',
|
2022-01-21 12:17:31 +01:00
|
|
|
});
|
|
|
|
} else {
|
2022-01-28 03:53:12 +01:00
|
|
|
popupMenu([...(opts.includeCurrentAccount ? [createItem($i)] : []), ...accountItemPromises], ev.currentTarget ?? ev.target, {
|
2022-06-20 10:38:49 +02:00
|
|
|
align: 'left',
|
2022-01-21 12:17:31 +01:00
|
|
|
});
|
|
|
|
}
|
2021-10-10 08:19:16 +02:00
|
|
|
}
|