2022-04-30 14:52:07 +02:00
|
|
|
|
/*
|
|
|
|
|
* Operations
|
|
|
|
|
* 各種操作
|
|
|
|
|
*/
|
2023-01-13 05:40:33 +01:00
|
|
|
|
import * as Misskey from "calckey-js";
|
2023-04-11 18:07:24 +02:00
|
|
|
|
import type { SwMessage, SwMessageOrderType } from "@/types";
|
2023-01-13 05:40:33 +01:00
|
|
|
|
import { getAccountFromId } from "@/scripts/get-account-from-id";
|
|
|
|
|
import { getUrlWithLoginId } from "@/scripts/login-id";
|
2022-04-30 14:52:07 +02:00
|
|
|
|
|
2023-01-13 05:40:33 +01:00
|
|
|
|
export const cli = new Misskey.api.APIClient({
|
|
|
|
|
origin,
|
2023-04-11 18:07:24 +02:00
|
|
|
|
fetch: (...args): Promise<Response> => fetch(...args),
|
2023-01-13 05:40:33 +01:00
|
|
|
|
});
|
2022-04-30 14:52:07 +02:00
|
|
|
|
|
2023-04-11 18:07:24 +02:00
|
|
|
|
export async function api<
|
|
|
|
|
E extends keyof Misskey.Endpoints,
|
|
|
|
|
O extends Misskey.Endpoints[E]["req"],
|
|
|
|
|
>(
|
2023-01-13 05:40:33 +01:00
|
|
|
|
endpoint: E,
|
2023-04-11 18:07:24 +02:00
|
|
|
|
userId?: string,
|
|
|
|
|
options?: O,
|
|
|
|
|
): Promise<void | ReturnType<typeof cli.request<E, O>>> {
|
|
|
|
|
let account: { token: string; id: string } | void;
|
|
|
|
|
|
|
|
|
|
if (userId) {
|
|
|
|
|
account = await getAccountFromId(userId);
|
|
|
|
|
if (!account) return;
|
|
|
|
|
}
|
2022-04-30 14:52:07 +02:00
|
|
|
|
|
2023-04-11 18:07:24 +02:00
|
|
|
|
return cli.request(endpoint, options, account?.token);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// mark-all-as-read送出を1秒間隔に制限する
|
|
|
|
|
const readBlockingStatus = new Map<string, boolean>();
|
|
|
|
|
export function sendMarkAllAsRead(
|
|
|
|
|
userId: string,
|
|
|
|
|
): Promise<null | undefined | void> {
|
|
|
|
|
if (readBlockingStatus.get(userId)) return Promise.resolve();
|
|
|
|
|
readBlockingStatus.set(userId, true);
|
|
|
|
|
return new Promise((resolve) => {
|
|
|
|
|
setTimeout(() => {
|
|
|
|
|
readBlockingStatus.set(userId, false);
|
|
|
|
|
api("notifications/mark-all-as-read", userId).then(resolve, resolve);
|
|
|
|
|
}, 1000);
|
|
|
|
|
});
|
2022-04-30 14:52:07 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// rendered acctからユーザーを開く
|
2023-04-11 18:07:24 +02:00
|
|
|
|
export function openUser(
|
|
|
|
|
acct: string,
|
|
|
|
|
loginId?: string,
|
|
|
|
|
): ReturnType<typeof openClient> {
|
2023-01-13 05:40:33 +01:00
|
|
|
|
return openClient("push", `/@${acct}`, loginId, { acct });
|
2022-04-30 14:52:07 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// noteIdからノートを開く
|
2023-04-11 18:07:24 +02:00
|
|
|
|
export function openNote(
|
|
|
|
|
noteId: string,
|
|
|
|
|
loginId?: string,
|
|
|
|
|
): ReturnType<typeof openClient> {
|
2023-01-13 05:40:33 +01:00
|
|
|
|
return openClient("push", `/notes/${noteId}`, loginId, { noteId });
|
2022-04-30 14:52:07 +02:00
|
|
|
|
}
|
|
|
|
|
|
2023-04-11 18:07:24 +02:00
|
|
|
|
// noteIdからノートを開く
|
|
|
|
|
export function openAntenna(
|
|
|
|
|
antennaId: string,
|
|
|
|
|
loginId: string,
|
|
|
|
|
): ReturnType<typeof openClient> {
|
|
|
|
|
return openClient("push", `/timeline/antenna/${antennaId}`, loginId, {
|
|
|
|
|
antennaId,
|
|
|
|
|
});
|
2022-04-30 14:52:07 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// post-formのオプションから投稿フォームを開く
|
2023-04-11 18:07:24 +02:00
|
|
|
|
export async function openPost(
|
|
|
|
|
options: {
|
|
|
|
|
initialText?: string;
|
|
|
|
|
reply?: Misskey.entities.Note;
|
|
|
|
|
renote?: Misskey.entities.Note;
|
|
|
|
|
},
|
|
|
|
|
loginId?: string,
|
|
|
|
|
): ReturnType<typeof openClient> {
|
2022-04-30 14:52:07 +02:00
|
|
|
|
// クエリを作成しておく
|
2023-04-11 18:07:24 +02:00
|
|
|
|
const url = "/share";
|
|
|
|
|
const query = new URLSearchParams();
|
|
|
|
|
if (options.initialText) query.set("text", options.initialText);
|
|
|
|
|
if (options.reply) query.set("replyId", options.reply.id);
|
|
|
|
|
if (options.renote) query.set("renoteId", options.renote.id);
|
2022-04-30 14:52:07 +02:00
|
|
|
|
|
2023-04-11 18:07:24 +02:00
|
|
|
|
return openClient("post", `${url}?${query}`, loginId, { options });
|
2022-04-30 14:52:07 +02:00
|
|
|
|
}
|
|
|
|
|
|
2023-01-13 05:40:33 +01:00
|
|
|
|
export async function openClient(
|
2023-04-11 18:07:24 +02:00
|
|
|
|
order: SwMessageOrderType,
|
2023-01-13 05:40:33 +01:00
|
|
|
|
url: string,
|
2023-04-11 18:07:24 +02:00
|
|
|
|
loginId?: string,
|
|
|
|
|
query: Record<string, SwMessage[string]> = {},
|
|
|
|
|
): Promise<WindowClient | null> {
|
2022-04-30 14:52:07 +02:00
|
|
|
|
const client = await findClient();
|
|
|
|
|
|
|
|
|
|
if (client) {
|
2023-01-13 05:40:33 +01:00
|
|
|
|
client.postMessage({
|
|
|
|
|
type: "order",
|
|
|
|
|
...query,
|
|
|
|
|
order,
|
|
|
|
|
loginId,
|
|
|
|
|
url,
|
2023-04-11 18:07:24 +02:00
|
|
|
|
} satisfies SwMessage);
|
2022-04-30 14:52:07 +02:00
|
|
|
|
return client;
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-11 18:07:24 +02:00
|
|
|
|
return self.clients.openWindow(getUrlWithLoginId(url, loginId!));
|
2022-04-30 14:52:07 +02:00
|
|
|
|
}
|
|
|
|
|
|
2023-04-11 18:07:24 +02:00
|
|
|
|
export async function findClient(): Promise<WindowClient | null> {
|
|
|
|
|
const clients = await globalThis.clients.matchAll({
|
2023-01-13 05:40:33 +01:00
|
|
|
|
type: "window",
|
2022-04-30 14:52:07 +02:00
|
|
|
|
});
|
2023-04-11 18:07:24 +02:00
|
|
|
|
return clients.find((c) => !new URL(c.url).searchParams.has("zen")) ?? null;
|
2022-04-30 14:52:07 +02:00
|
|
|
|
}
|