2023-01-12 21:40:33 -07:00
|
|
|
import { defineAsyncComponent, reactive } from "vue";
|
2023-07-02 16:18:30 -06:00
|
|
|
import * as misskey from "firefish-js";
|
2023-01-12 21:40:33 -07:00
|
|
|
import { i18n } from "./i18n";
|
|
|
|
import { del, get, set } from "@/scripts/idb-proxy";
|
|
|
|
import { apiUrl } from "@/config";
|
|
|
|
import { waiting, api, popup, popupMenu, success, alert } from "@/os";
|
|
|
|
import { unisonReload, reloadChannel } from "@/scripts/unison-reload";
|
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
|
|
|
|
2023-01-12 21:40:33 -07:00
|
|
|
const accountData = localStorage.getItem("account");
|
2020-12-18 18:55:52 -07:00
|
|
|
|
|
|
|
// TODO: 外部からはreadonlyに
|
2023-01-12 21:40:33 -07:00
|
|
|
export const $i = accountData
|
|
|
|
? reactive(JSON.parse(accountData) as Account)
|
|
|
|
: null;
|
2020-12-18 18:55:52 -07:00
|
|
|
|
2022-01-18 05:30:17 -07:00
|
|
|
export const iAmModerator = $i != null && ($i.isAdmin || $i.isModerator);
|
2023-01-12 21:40:33 -07:00
|
|
|
export const iAmAdmin = $i?.isAdmin;
|
2022-01-18 05:30:17 -07:00
|
|
|
|
2021-08-20 04:38:16 -06:00
|
|
|
export async function signout() {
|
|
|
|
waiting();
|
2023-01-12 21:40:33 -07:00
|
|
|
localStorage.removeItem("account");
|
2021-08-20 04:38:16 -06:00
|
|
|
|
2022-06-20 02:38:49 -06:00
|
|
|
await removeAccount($i.id);
|
2021-08-20 04:38:16 -06:00
|
|
|
|
2022-06-24 06:16:05 -06:00
|
|
|
const accounts = await getAccounts();
|
|
|
|
|
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`, {
|
2023-01-12 21:40:33 -07:00
|
|
|
method: "POST",
|
2021-09-04 03:09:53 -06:00
|
|
|
body: JSON.stringify({
|
|
|
|
i: $i.token,
|
|
|
|
endpoint: push.endpoint,
|
|
|
|
}),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (accounts.length === 0) {
|
2023-01-12 21:40:33 -07:00
|
|
|
await navigator.serviceWorker.getRegistrations().then((registrations) => {
|
|
|
|
return Promise.all(
|
|
|
|
registrations.map((registration) => registration.unregister()),
|
|
|
|
);
|
|
|
|
});
|
2021-09-04 03:09:53 -06:00
|
|
|
}
|
2022-05-26 07:53:09 -06:00
|
|
|
} catch (err) {}
|
2021-08-20 04:38:16 -06:00
|
|
|
//#endregion
|
|
|
|
|
2023-01-12 21:40:33 -07:00
|
|
|
document.cookie = "igi=; path=/";
|
2021-08-20 04:38:16 -06:00
|
|
|
|
|
|
|
if (accounts.length > 0) login(accounts[0].token);
|
2023-01-12 21:40:33 -07:00
|
|
|
else unisonReload("/");
|
2020-12-18 18:55:52 -07:00
|
|
|
}
|
|
|
|
|
2023-01-12 21:40:33 -07:00
|
|
|
export async function getAccounts(): Promise<
|
|
|
|
{ id: Account["id"]; token: Account["token"] }[]
|
|
|
|
> {
|
|
|
|
return (await get("accounts")) || [];
|
2020-12-18 18:55:52 -07:00
|
|
|
}
|
|
|
|
|
2023-01-12 21:40:33 -07:00
|
|
|
export async function addAccount(id: Account["id"], token: Account["token"]) {
|
2021-08-20 04:38:16 -06:00
|
|
|
const accounts = await getAccounts();
|
2023-01-12 21:40:33 -07:00
|
|
|
if (!accounts.some((x) => x.id === id)) {
|
|
|
|
await set("accounts", accounts.concat([{ id, token }]));
|
2020-12-18 18:55:52 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-12 21:40:33 -07:00
|
|
|
export async function removeAccount(id: Account["id"]) {
|
2022-06-20 02:38:49 -06:00
|
|
|
const accounts = await getAccounts();
|
2023-01-12 21:40:33 -07:00
|
|
|
accounts.splice(
|
|
|
|
accounts.findIndex((x) => x.id === id),
|
|
|
|
1,
|
|
|
|
);
|
2022-06-20 02:38:49 -06:00
|
|
|
|
2023-01-12 21:40:33 -07:00
|
|
|
if (accounts.length > 0) await set("accounts", accounts);
|
|
|
|
else await del("accounts");
|
2022-06-20 02:38:49 -06:00
|
|
|
}
|
|
|
|
|
2022-04-11 07:50:53 -06:00
|
|
|
function fetchAccount(token: string): Promise<Account> {
|
2020-12-18 18:55:52 -07:00
|
|
|
return new Promise((done, fail) => {
|
|
|
|
// Fetch user
|
|
|
|
fetch(`${apiUrl}/i`, {
|
2023-01-12 21:40:33 -07:00
|
|
|
method: "POST",
|
2020-12-18 18:55:52 -07:00
|
|
|
body: JSON.stringify({
|
2022-06-20 02:38:49 -06:00
|
|
|
i: token,
|
|
|
|
}),
|
2020-12-18 18:55:52 -07:00
|
|
|
})
|
2023-01-12 21:40:33 -07: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 11:23:12 -06:00
|
|
|
} else {
|
2023-01-12 21:40:33 -07:00
|
|
|
res.token = token;
|
|
|
|
done(res);
|
2021-09-18 11:23:12 -06:00
|
|
|
}
|
2023-01-12 21:40:33 -07:00
|
|
|
})
|
|
|
|
.catch(fail);
|
2020-12-18 18:55:52 -07:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-05-26 07:53:09 -06:00
|
|
|
export function updateAccount(accountData) {
|
|
|
|
for (const [key, value] of Object.entries(accountData)) {
|
2020-12-18 18:55:52 -07:00
|
|
|
$i[key] = value;
|
|
|
|
}
|
2023-01-12 21:40:33 -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
|
|
|
}
|
|
|
|
|
2023-01-12 21:40:33 -07:00
|
|
|
export async function login(token: Account["token"], redirect?: string) {
|
2020-12-18 18:55:52 -07:00
|
|
|
waiting();
|
2023-01-12 21:40:33 -07:00
|
|
|
if (_DEV_) console.log("logging as token ", token);
|
2020-12-18 18:55:52 -07:00
|
|
|
const me = await fetchAccount(token);
|
2023-01-12 21:40:33 -07:00
|
|
|
localStorage.setItem("account", JSON.stringify(me));
|
2022-03-19 04:08:55 -06:00
|
|
|
document.cookie = `token=${token}; path=/; max-age=31536000`; // bull dashboardの認証とかで使う
|
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
|
|
|
}
|
|
|
|
|
2023-01-12 21:40:33 -07: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 00:19:16 -06:00
|
|
|
function showSigninDialog() {
|
2023-01-12 21:40:33 -07:00
|
|
|
popup(
|
|
|
|
defineAsyncComponent(() => import("@/components/MkSigninDialog.vue")),
|
|
|
|
{},
|
|
|
|
{
|
|
|
|
done: (res) => {
|
|
|
|
addAccount(res.id, res.i);
|
2023-07-06 18:44:32 -06:00
|
|
|
switchAccountWithToken(res.i);
|
2023-01-12 21:40:33 -07:00
|
|
|
},
|
2021-10-10 00:19:16 -06:00
|
|
|
},
|
2023-01-12 21:40:33 -07:00
|
|
|
"closed",
|
|
|
|
);
|
2021-10-10 00:19:16 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
function createAccount() {
|
2023-01-12 21:40:33 -07:00
|
|
|
popup(
|
|
|
|
defineAsyncComponent(() => import("@/components/MkSignupDialog.vue")),
|
|
|
|
{},
|
|
|
|
{
|
|
|
|
done: (res) => {
|
|
|
|
addAccount(res.id, res.i);
|
|
|
|
switchAccountWithToken(res.i);
|
|
|
|
},
|
2021-10-10 00:19:16 -06:00
|
|
|
},
|
2023-01-12 21:40:33 -07:00
|
|
|
"closed",
|
|
|
|
);
|
2021-10-10 00:19:16 -06:00
|
|
|
}
|
|
|
|
|
2022-01-21 04:17:31 -07:00
|
|
|
async function switchAccount(account: misskey.entities.UserDetailed) {
|
2021-10-10 00:19:16 -06:00
|
|
|
const storedAccounts = await getAccounts();
|
2023-01-12 21:40:33 -07:00
|
|
|
const token = storedAccounts.find((x) => x.id === account.id).token;
|
2021-10-10 00:19:16 -06:00
|
|
|
switchAccountWithToken(token);
|
|
|
|
}
|
|
|
|
|
|
|
|
function switchAccountWithToken(token: string) {
|
|
|
|
login(token);
|
|
|
|
}
|
|
|
|
|
2023-01-12 21:40:33 -07:00
|
|
|
const storedAccounts = await getAccounts().then((accounts) =>
|
|
|
|
accounts.filter((x) => x.id !== $i.id),
|
|
|
|
);
|
|
|
|
const accountsPromise = api("users/show", {
|
|
|
|
userIds: storedAccounts.map((x) => x.id),
|
|
|
|
});
|
2021-10-10 00:19:16 -06:00
|
|
|
|
2022-01-21 04:17:31 -07:00
|
|
|
function createItem(account: misskey.entities.UserDetailed) {
|
|
|
|
return {
|
2023-01-12 21:40:33 -07:00
|
|
|
type: "user",
|
2022-01-21 04:17:31 -07:00
|
|
|
user: account,
|
|
|
|
active: opts.active != null ? opts.active === account.id : false,
|
|
|
|
action: () => {
|
|
|
|
if (opts.onChoose) {
|
|
|
|
opts.onChoose(account);
|
|
|
|
} else {
|
|
|
|
switchAccount(account);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2023-01-12 21:40:33 -07: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);
|
|
|
|
res(createItem(account));
|
|
|
|
});
|
|
|
|
}),
|
|
|
|
);
|
2021-10-10 00:19:16 -06:00
|
|
|
|
2022-01-21 04:17:31 -07:00
|
|
|
if (opts.withExtraOperation) {
|
2023-01-12 21:40:33 -07:00
|
|
|
popupMenu(
|
|
|
|
[
|
|
|
|
...[
|
|
|
|
{
|
|
|
|
type: "link",
|
|
|
|
text: i18n.ts.profile,
|
|
|
|
to: `/@${$i.username}`,
|
|
|
|
avatar: $i,
|
|
|
|
},
|
|
|
|
null,
|
|
|
|
...(opts.includeCurrentAccount ? [createItem($i)] : []),
|
|
|
|
...accountItemPromises,
|
|
|
|
{
|
|
|
|
type: "parent",
|
2023-03-11 14:01:04 -07:00
|
|
|
icon: "ph-plus ph-bold ph-lg",
|
2023-01-12 21:40:33 -07:00
|
|
|
text: i18n.ts.addAccount,
|
|
|
|
children: [
|
|
|
|
{
|
|
|
|
text: i18n.ts.existingAccount,
|
|
|
|
action: () => {
|
|
|
|
showSigninDialog();
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
text: i18n.ts.createAccount,
|
|
|
|
action: () => {
|
|
|
|
createAccount();
|
|
|
|
},
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
{
|
|
|
|
type: "link",
|
2023-03-11 14:01:04 -07:00
|
|
|
icon: "ph-users ph-bold ph-lg",
|
2023-01-12 21:40:33 -07:00
|
|
|
text: i18n.ts.manageAccounts,
|
|
|
|
to: "/settings/accounts",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
type: "button",
|
2023-03-11 14:01:04 -07:00
|
|
|
icon: "ph-sign-out ph-bold ph-lg",
|
2023-01-12 21:40:33 -07:00
|
|
|
text: i18n.ts.logout,
|
|
|
|
action: () => {
|
|
|
|
signout();
|
|
|
|
},
|
|
|
|
},
|
|
|
|
],
|
|
|
|
],
|
|
|
|
ev.currentTarget ?? ev.target,
|
|
|
|
{
|
|
|
|
align: "left",
|
|
|
|
},
|
|
|
|
);
|
2022-01-21 04:17:31 -07:00
|
|
|
} else {
|
2023-01-12 21:40:33 -07:00
|
|
|
popupMenu(
|
|
|
|
[
|
|
|
|
...(opts.includeCurrentAccount ? [createItem($i)] : []),
|
|
|
|
...accountItemPromises,
|
|
|
|
],
|
|
|
|
ev.currentTarget ?? ev.target,
|
|
|
|
{
|
|
|
|
align: "left",
|
|
|
|
},
|
|
|
|
);
|
2022-01-21 04:17:31 -07:00
|
|
|
}
|
2021-10-10 00:19:16 -06:00
|
|
|
}
|