2023-07-27 05:31:52 +00:00
|
|
|
|
/*
|
|
|
|
|
* SPDX-FileCopyrightText: syuilo and other misskey contributors
|
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
*/
|
|
|
|
|
|
2023-03-09 05:27:16 +00:00
|
|
|
|
import { defineAsyncComponent, reactive, ref } from 'vue';
|
2023-09-04 04:33:38 +00:00
|
|
|
|
import * as Misskey from 'misskey-js';
|
2023-09-19 07:37:43 +00:00
|
|
|
|
import { showSuspendedDialog } from '@/scripts/show-suspended-dialog.js';
|
|
|
|
|
import { i18n } from '@/i18n.js';
|
|
|
|
|
import { miLocalStorage } from '@/local-storage.js';
|
|
|
|
|
import { MenuButton } from '@/types/menu.js';
|
|
|
|
|
import { del, get, set } from '@/scripts/idb-proxy.js';
|
|
|
|
|
import { apiUrl } from '@/config.js';
|
2024-01-04 09:32:46 +00:00
|
|
|
|
import { waiting, popup, popupMenu, success, alert } from '@/os.js';
|
|
|
|
|
import { misskeyApi } from '@/scripts/misskey-api.js';
|
2023-09-19 07:37:43 +00:00
|
|
|
|
import { unisonReload, reloadChannel } from '@/scripts/unison-reload.js';
|
2020-12-19 01:55:52 +00:00
|
|
|
|
|
|
|
|
|
// TODO: 他のタブと永続化されたstateを同期
|
|
|
|
|
|
2023-12-14 03:26:02 +00:00
|
|
|
|
type Account = Misskey.entities.MeDetailed & { token: string };
|
2020-12-19 01:55:52 +00:00
|
|
|
|
|
2023-01-07 01:13:02 +00:00
|
|
|
|
const accountData = miLocalStorage.getItem('account');
|
2020-12-19 01:55:52 +00:00
|
|
|
|
|
|
|
|
|
// TODO: 外部からはreadonlyに
|
2022-05-26 13:53:09 +00:00
|
|
|
|
export const $i = accountData ? reactive(JSON.parse(accountData) as Account) : null;
|
2020-12-19 01:55:52 +00:00
|
|
|
|
|
2024-01-04 06:20:23 +00:00
|
|
|
|
export const iAmModerator = $i != null && ($i.isAdmin === true || $i.isModerator === true);
|
2022-07-02 06:12:11 +00:00
|
|
|
|
export const iAmAdmin = $i != null && $i.isAdmin;
|
2022-01-18 12:30:17 +00:00
|
|
|
|
|
2024-01-04 06:30:40 +00:00
|
|
|
|
export function signinRequired() {
|
|
|
|
|
if ($i == null) throw new Error('signin required');
|
|
|
|
|
return $i;
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-21 04:14:55 +00:00
|
|
|
|
export let notesCount = $i == null ? 0 : $i.notesCount;
|
|
|
|
|
export function incNotesCount() {
|
|
|
|
|
notesCount++;
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-20 10:38:16 +00:00
|
|
|
|
export async function signout() {
|
2023-03-09 05:27:16 +00:00
|
|
|
|
if (!$i) return;
|
|
|
|
|
|
2021-08-20 10:38:16 +00:00
|
|
|
|
waiting();
|
2023-01-07 01:13:02 +00:00
|
|
|
|
miLocalStorage.removeItem('account');
|
2022-06-20 08:38:49 +00:00
|
|
|
|
await removeAccount($i.id);
|
2022-06-24 12:16:05 +00:00
|
|
|
|
const accounts = await getAccounts();
|
|
|
|
|
|
2021-09-04 09:09:53 +00:00
|
|
|
|
//#region Remove service worker registration
|
2021-08-21 02:51:46 +00:00
|
|
|
|
try {
|
2021-09-04 09:09:53 +00:00
|
|
|
|
if (navigator.serviceWorker.controller) {
|
|
|
|
|
const registration = await navigator.serviceWorker.ready;
|
|
|
|
|
const push = await registration.pushManager.getSubscription();
|
|
|
|
|
if (push) {
|
2022-12-03 10:42:05 +00:00
|
|
|
|
await window.fetch(`${apiUrl}/sw/unregister`, {
|
2021-09-04 09:09:53 +00:00
|
|
|
|
method: 'POST',
|
|
|
|
|
body: JSON.stringify({
|
|
|
|
|
i: $i.token,
|
|
|
|
|
endpoint: push.endpoint,
|
|
|
|
|
}),
|
2022-12-03 10:42:05 +00:00
|
|
|
|
headers: {
|
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
|
},
|
2021-09-04 09:09:53 +00:00
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (accounts.length === 0) {
|
|
|
|
|
await navigator.serviceWorker.getRegistrations()
|
|
|
|
|
.then(registrations => {
|
|
|
|
|
return Promise.all(registrations.map(registration => registration.unregister()));
|
|
|
|
|
});
|
|
|
|
|
}
|
2022-05-26 13:53:09 +00:00
|
|
|
|
} catch (err) {}
|
2021-08-20 10:38:16 +00:00
|
|
|
|
//#endregion
|
|
|
|
|
|
|
|
|
|
if (accounts.length > 0) login(accounts[0].token);
|
2021-11-04 15:09:13 +00:00
|
|
|
|
else unisonReload('/');
|
2020-12-19 01:55:52 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-08-20 10:38:16 +00:00
|
|
|
|
export async function getAccounts(): Promise<{ id: Account['id'], token: Account['token'] }[]> {
|
|
|
|
|
return (await get('accounts')) || [];
|
2020-12-19 01:55:52 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-08-20 10:38:16 +00:00
|
|
|
|
export async function addAccount(id: Account['id'], token: Account['token']) {
|
|
|
|
|
const accounts = await getAccounts();
|
2020-12-19 01:55:52 +00:00
|
|
|
|
if (!accounts.some(x => x.id === id)) {
|
2021-08-20 10:38:16 +00:00
|
|
|
|
await set('accounts', accounts.concat([{ id, token }]));
|
2020-12-19 01:55:52 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-09 05:27:16 +00:00
|
|
|
|
export async function removeAccount(idOrToken: Account['id']) {
|
2022-06-20 08:38:49 +00:00
|
|
|
|
const accounts = await getAccounts();
|
2023-03-09 05:27:16 +00:00
|
|
|
|
const i = accounts.findIndex(x => x.id === idOrToken || x.token === idOrToken);
|
|
|
|
|
if (i !== -1) accounts.splice(i, 1);
|
2022-06-20 08:38:49 +00:00
|
|
|
|
|
2023-03-09 05:27:16 +00:00
|
|
|
|
if (accounts.length > 0) {
|
|
|
|
|
await set('accounts', accounts);
|
|
|
|
|
} else {
|
|
|
|
|
await del('accounts');
|
|
|
|
|
}
|
2022-06-20 08:38:49 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-03-09 05:27:16 +00:00
|
|
|
|
function fetchAccount(token: string, id?: string, forceShowDialog?: boolean): Promise<Account> {
|
2020-12-19 01:55:52 +00:00
|
|
|
|
return new Promise((done, fail) => {
|
2022-12-03 10:42:05 +00:00
|
|
|
|
window.fetch(`${apiUrl}/i`, {
|
2020-12-19 01:55:52 +00:00
|
|
|
|
method: 'POST',
|
|
|
|
|
body: JSON.stringify({
|
2022-06-20 08:38:49 +00:00
|
|
|
|
i: token,
|
|
|
|
|
}),
|
2022-12-03 10:42:05 +00:00
|
|
|
|
headers: {
|
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
|
},
|
2020-12-19 01:55:52 +00:00
|
|
|
|
})
|
2023-05-18 11:17:32 +00:00
|
|
|
|
.then(res => new Promise<Account | { error: Record<string, any> }>((done2, fail2) => {
|
|
|
|
|
if (res.status >= 500 && res.status < 600) {
|
2023-08-13 11:12:29 +00:00
|
|
|
|
// サーバーエラー(5xx)の場合をrejectとする
|
|
|
|
|
// (認証エラーなど4xxはresolve)
|
2023-05-18 11:17:32 +00:00
|
|
|
|
return fail2(res);
|
|
|
|
|
}
|
|
|
|
|
res.json().then(done2, fail2);
|
|
|
|
|
}))
|
|
|
|
|
.then(async res => {
|
|
|
|
|
if (res.error) {
|
|
|
|
|
if (res.error.id === 'a8c724b3-6e9c-4b46-b1a8-bc3ed6258370') {
|
2023-07-31 10:14:20 +00:00
|
|
|
|
// SUSPENDED
|
2023-05-18 11:17:32 +00:00
|
|
|
|
if (forceShowDialog || $i && (token === $i.token || id === $i.id)) {
|
|
|
|
|
await showSuspendedDialog();
|
|
|
|
|
}
|
|
|
|
|
} else if (res.error.id === 'e5b3b9f0-2b8f-4b9f-9c1f-8c5c1b2e1b1a') {
|
2023-07-31 10:14:20 +00:00
|
|
|
|
// USER_IS_DELETED
|
|
|
|
|
// アカウントが削除されている
|
2023-05-18 11:17:32 +00:00
|
|
|
|
if (forceShowDialog || $i && (token === $i.token || id === $i.id)) {
|
|
|
|
|
await alert({
|
|
|
|
|
type: 'error',
|
|
|
|
|
title: i18n.ts.accountDeleted,
|
|
|
|
|
text: i18n.ts.accountDeletedDescription,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
} else if (res.error.id === 'b0a7f5f8-dc2f-4171-b91f-de88ad238e14') {
|
2023-07-31 10:14:20 +00:00
|
|
|
|
// AUTHENTICATION_FAILED
|
|
|
|
|
// トークンが無効化されていたりアカウントが削除されたりしている
|
2023-05-18 11:17:32 +00:00
|
|
|
|
if (forceShowDialog || $i && (token === $i.token || id === $i.id)) {
|
|
|
|
|
await alert({
|
|
|
|
|
type: 'error',
|
|
|
|
|
title: i18n.ts.tokenRevoked,
|
|
|
|
|
text: i18n.ts.tokenRevokedDescription,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
} else {
|
2023-03-09 05:27:16 +00:00
|
|
|
|
await alert({
|
2022-12-03 10:42:05 +00:00
|
|
|
|
type: 'error',
|
2023-05-18 11:17:32 +00:00
|
|
|
|
title: i18n.ts.failedToFetchAccountInformation,
|
|
|
|
|
text: JSON.stringify(res.error),
|
2022-12-03 10:42:05 +00:00
|
|
|
|
});
|
|
|
|
|
}
|
2023-05-18 11:17:32 +00:00
|
|
|
|
|
|
|
|
|
// rejectかつ理由がtrueの場合、削除対象であることを示す
|
|
|
|
|
fail(true);
|
2021-09-18 17:23:12 +00:00
|
|
|
|
} else {
|
2023-05-18 11:17:32 +00:00
|
|
|
|
(res as Account).token = token;
|
|
|
|
|
done(res as Account);
|
2021-09-18 17:23:12 +00:00
|
|
|
|
}
|
2023-05-18 11:17:32 +00:00
|
|
|
|
})
|
|
|
|
|
.catch(fail);
|
2020-12-19 01:55:52 +00:00
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-09 05:27:16 +00:00
|
|
|
|
export function updateAccount(accountData: Partial<Account>) {
|
|
|
|
|
if (!$i) return;
|
2022-05-26 13:53:09 +00:00
|
|
|
|
for (const [key, value] of Object.entries(accountData)) {
|
2020-12-19 01:55:52 +00:00
|
|
|
|
$i[key] = value;
|
|
|
|
|
}
|
2023-01-07 01:13:02 +00:00
|
|
|
|
miLocalStorage.setItem('account', JSON.stringify($i));
|
2020-12-19 01:55:52 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-03-09 05:27:16 +00:00
|
|
|
|
export async function refreshAccount() {
|
|
|
|
|
if (!$i) return;
|
|
|
|
|
return fetchAccount($i.token, $i.id)
|
|
|
|
|
.then(updateAccount, reason => {
|
|
|
|
|
if (reason === true) return signout();
|
|
|
|
|
return;
|
|
|
|
|
});
|
2020-12-19 01:55:52 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-08-20 10:38:16 +00:00
|
|
|
|
export async function login(token: Account['token'], redirect?: string) {
|
2023-03-09 05:27:16 +00:00
|
|
|
|
const showing = ref(true);
|
|
|
|
|
popup(defineAsyncComponent(() => import('@/components/MkWaitingDialog.vue')), {
|
|
|
|
|
success: false,
|
|
|
|
|
showing: showing,
|
|
|
|
|
}, {}, 'closed');
|
2020-12-19 01:55:52 +00:00
|
|
|
|
if (_DEV_) console.log('logging as token ', token);
|
2023-03-09 05:27:16 +00:00
|
|
|
|
const me = await fetchAccount(token, undefined, true)
|
|
|
|
|
.catch(reason => {
|
|
|
|
|
if (reason === true) {
|
|
|
|
|
// 削除対象の場合
|
|
|
|
|
removeAccount(token);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
showing.value = false;
|
|
|
|
|
throw reason;
|
|
|
|
|
});
|
2023-01-07 01:13:02 +00:00
|
|
|
|
miLocalStorage.setItem('account', JSON.stringify(me));
|
2022-03-19 10:08:55 +00:00
|
|
|
|
document.cookie = `token=${token}; path=/; max-age=31536000`; // bull dashboardの認証とかで使う
|
2021-08-20 10:38:16 +00:00
|
|
|
|
await addAccount(me.id, token);
|
|
|
|
|
|
|
|
|
|
if (redirect) {
|
2021-11-04 15:09:13 +00:00
|
|
|
|
// 他のタブは再読み込みするだけ
|
|
|
|
|
reloadChannel.postMessage(null);
|
|
|
|
|
// このページはredirectで指定された先に移動
|
2021-08-20 10:38:16 +00:00
|
|
|
|
location.href = redirect;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-17 12:36:56 +00:00
|
|
|
|
unisonReload();
|
2020-12-19 01:55:52 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-01-21 11:17:31 +00:00
|
|
|
|
export async function openAccountMenu(opts: {
|
|
|
|
|
includeCurrentAccount?: boolean;
|
|
|
|
|
withExtraOperation: boolean;
|
2023-09-04 04:33:38 +00:00
|
|
|
|
active?: Misskey.entities.UserDetailed['id'];
|
|
|
|
|
onChoose?: (account: Misskey.entities.UserDetailed) => void;
|
2022-01-21 11:17:31 +00:00
|
|
|
|
}, ev: MouseEvent) {
|
2023-03-09 05:27:16 +00:00
|
|
|
|
if (!$i) return;
|
|
|
|
|
|
2021-10-10 06:19:16 +00:00
|
|
|
|
function showSigninDialog() {
|
2022-08-30 15:24:33 +00:00
|
|
|
|
popup(defineAsyncComponent(() => import('@/components/MkSigninDialog.vue')), {}, {
|
2021-10-10 06:19:16 +00:00
|
|
|
|
done: res => {
|
|
|
|
|
addAccount(res.id, res.i);
|
|
|
|
|
success();
|
|
|
|
|
},
|
|
|
|
|
}, 'closed');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function createAccount() {
|
2022-08-30 15:24:33 +00:00
|
|
|
|
popup(defineAsyncComponent(() => import('@/components/MkSignupDialog.vue')), {}, {
|
2021-10-10 06:19:16 +00:00
|
|
|
|
done: res => {
|
|
|
|
|
addAccount(res.id, res.i);
|
|
|
|
|
switchAccountWithToken(res.i);
|
|
|
|
|
},
|
|
|
|
|
}, 'closed');
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-04 04:33:38 +00:00
|
|
|
|
async function switchAccount(account: Misskey.entities.UserDetailed) {
|
2021-10-10 06:19:16 +00:00
|
|
|
|
const storedAccounts = await getAccounts();
|
2023-03-09 05:27:16 +00:00
|
|
|
|
const found = storedAccounts.find(x => x.id === account.id);
|
|
|
|
|
if (found == null) return;
|
|
|
|
|
switchAccountWithToken(found.token);
|
2021-10-10 06:19:16 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function switchAccountWithToken(token: string) {
|
|
|
|
|
login(token);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const storedAccounts = await getAccounts().then(accounts => accounts.filter(x => x.id !== $i.id));
|
2024-01-04 09:32:46 +00:00
|
|
|
|
const accountsPromise = misskeyApi('users/show', { userIds: storedAccounts.map(x => x.id) });
|
2021-10-10 06:19:16 +00:00
|
|
|
|
|
2023-09-04 04:33:38 +00:00
|
|
|
|
function createItem(account: Misskey.entities.UserDetailed) {
|
2022-01-21 11:17:31 +00:00
|
|
|
|
return {
|
2023-03-09 05:27:16 +00:00
|
|
|
|
type: 'user' as const,
|
2022-01-21 11:17:31 +00:00
|
|
|
|
user: account,
|
|
|
|
|
active: opts.active != null ? opts.active === account.id : false,
|
|
|
|
|
action: () => {
|
|
|
|
|
if (opts.onChoose) {
|
|
|
|
|
opts.onChoose(account);
|
|
|
|
|
} else {
|
|
|
|
|
switchAccount(account);
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-09 05:27:16 +00:00
|
|
|
|
const accountItemPromises = storedAccounts.map(a => new Promise<ReturnType<typeof createItem> | MenuButton>(res => {
|
2021-10-10 06:19:16 +00:00
|
|
|
|
accountsPromise.then(accounts => {
|
|
|
|
|
const account = accounts.find(x => x.id === a.id);
|
2023-03-09 05:27:16 +00:00
|
|
|
|
if (account == null) return res({
|
|
|
|
|
type: 'button' as const,
|
|
|
|
|
text: a.id,
|
|
|
|
|
action: () => {
|
|
|
|
|
switchAccountWithToken(a.token);
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
2022-01-21 11:17:31 +00:00
|
|
|
|
res(createItem(account));
|
2021-10-10 06:19:16 +00:00
|
|
|
|
});
|
|
|
|
|
}));
|
|
|
|
|
|
2022-01-21 11:17:31 +00:00
|
|
|
|
if (opts.withExtraOperation) {
|
|
|
|
|
popupMenu([...[{
|
2023-03-09 05:27:16 +00:00
|
|
|
|
type: 'link' as const,
|
2022-01-28 02:39:49 +00:00
|
|
|
|
text: i18n.ts.profile,
|
2022-01-21 11:17:31 +00:00
|
|
|
|
to: `/@${ $i.username }`,
|
|
|
|
|
avatar: $i,
|
2023-12-13 07:56:19 +00:00
|
|
|
|
}, { type: 'divider' }, ...(opts.includeCurrentAccount ? [createItem($i)] : []), ...accountItemPromises, {
|
2023-03-09 05:27:16 +00:00
|
|
|
|
type: 'parent' as const,
|
2023-09-30 19:53:52 +00:00
|
|
|
|
icon: 'ph-plus ph-bold ph-lg',
|
2022-01-28 02:39:49 +00:00
|
|
|
|
text: i18n.ts.addAccount,
|
2022-07-17 14:18:05 +00:00
|
|
|
|
children: [{
|
|
|
|
|
text: i18n.ts.existingAccount,
|
|
|
|
|
action: () => { showSigninDialog(); },
|
|
|
|
|
}, {
|
|
|
|
|
text: i18n.ts.createAccount,
|
|
|
|
|
action: () => { createAccount(); },
|
|
|
|
|
}],
|
2022-01-21 11:17:31 +00:00
|
|
|
|
}, {
|
2023-03-09 05:27:16 +00:00
|
|
|
|
type: 'link' as const,
|
2023-09-30 22:53:01 +00:00
|
|
|
|
icon: 'ph-users ph-bold ph-lg',
|
2022-01-28 02:39:49 +00:00
|
|
|
|
text: i18n.ts.manageAccounts,
|
2022-06-20 08:38:49 +00:00
|
|
|
|
to: '/settings/accounts',
|
2023-09-21 23:12:29 +00:00
|
|
|
|
}, {
|
|
|
|
|
type: 'button' as const,
|
2023-09-30 19:53:52 +00:00
|
|
|
|
icon: 'ph-power ph-bold ph-lg',
|
2023-09-21 23:12:29 +00:00
|
|
|
|
text: i18n.ts.logout,
|
|
|
|
|
action: () => { signout(); },
|
2022-01-28 02:53:12 +00:00
|
|
|
|
}]], ev.currentTarget ?? ev.target, {
|
2022-06-20 08:38:49 +00:00
|
|
|
|
align: 'left',
|
2022-01-21 11:17:31 +00:00
|
|
|
|
});
|
|
|
|
|
} else {
|
2022-01-28 02:53:12 +00:00
|
|
|
|
popupMenu([...(opts.includeCurrentAccount ? [createItem($i)] : []), ...accountItemPromises], ev.currentTarget ?? ev.target, {
|
2022-06-20 08:38:49 +00:00
|
|
|
|
align: 'left',
|
2022-01-21 11:17:31 +00:00
|
|
|
|
});
|
|
|
|
|
}
|
2021-10-10 06:19:16 +00:00
|
|
|
|
}
|
2023-05-18 11:17:32 +00:00
|
|
|
|
|
|
|
|
|
if (_DEV_) {
|
|
|
|
|
(window as any).$i = $i;
|
|
|
|
|
}
|