2022-04-30 14:52:07 +02:00
|
|
|
|
/*
|
|
|
|
|
* Operations
|
|
|
|
|
* 各種操作
|
|
|
|
|
*/
|
|
|
|
|
declare var self: ServiceWorkerGlobalScope;
|
|
|
|
|
|
2023-01-13 05:40:33 +01:00
|
|
|
|
import * as Misskey from "calckey-js";
|
|
|
|
|
import { SwMessage, swMessageOrderType } from "@/types";
|
|
|
|
|
import { acct as getAcct } from "@/filters/user";
|
|
|
|
|
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,
|
|
|
|
|
fetch: (...args) => fetch(...args),
|
|
|
|
|
});
|
2022-04-30 14:52:07 +02:00
|
|
|
|
|
2023-01-13 05:40:33 +01:00
|
|
|
|
export async function api<E extends keyof Misskey.Endpoints>(
|
|
|
|
|
endpoint: E,
|
|
|
|
|
userId: string,
|
|
|
|
|
options?: Misskey.Endpoints[E]["req"],
|
|
|
|
|
) {
|
2022-04-30 14:52:07 +02:00
|
|
|
|
const account = await getAccountFromId(userId);
|
|
|
|
|
if (!account) return;
|
|
|
|
|
|
|
|
|
|
return cli.request(endpoint, options, account.token);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// rendered acctからユーザーを開く
|
|
|
|
|
export function openUser(acct: string, loginId: string) {
|
2023-01-13 05:40:33 +01:00
|
|
|
|
return openClient("push", `/@${acct}`, loginId, { acct });
|
2022-04-30 14:52:07 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// noteIdからノートを開く
|
|
|
|
|
export function openNote(noteId: string, loginId: string) {
|
2023-01-13 05:40:33 +01:00
|
|
|
|
return openClient("push", `/notes/${noteId}`, loginId, { noteId });
|
2022-04-30 14:52:07 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function openChat(body: any, loginId: string) {
|
|
|
|
|
if (body.groupId === null) {
|
2023-01-13 05:40:33 +01:00
|
|
|
|
return openClient("push", `/my/messaging/${getAcct(body.user)}`, loginId, {
|
|
|
|
|
body,
|
|
|
|
|
});
|
2022-04-30 14:52:07 +02:00
|
|
|
|
} else {
|
2023-01-13 05:40:33 +01:00
|
|
|
|
return openClient("push", `/my/messaging/group/${body.groupId}`, loginId, {
|
|
|
|
|
body,
|
|
|
|
|
});
|
2022-04-30 14:52:07 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// post-formのオプションから投稿フォームを開く
|
|
|
|
|
export async function openPost(options: any, loginId: string) {
|
|
|
|
|
// クエリを作成しておく
|
2023-01-13 05:40:33 +01:00
|
|
|
|
let url = "/share?";
|
2022-04-30 14:52:07 +02:00
|
|
|
|
if (options.initialText) url += `text=${options.initialText}&`;
|
|
|
|
|
if (options.reply) url += `replyId=${options.reply.id}&`;
|
|
|
|
|
if (options.renote) url += `renoteId=${options.renote.id}&`;
|
|
|
|
|
|
2023-01-13 05:40:33 +01:00
|
|
|
|
return openClient("post", url, loginId, { options });
|
2022-04-30 14:52:07 +02:00
|
|
|
|
}
|
|
|
|
|
|
2023-01-13 05:40:33 +01:00
|
|
|
|
export async function openClient(
|
|
|
|
|
order: swMessageOrderType,
|
|
|
|
|
url: string,
|
|
|
|
|
loginId: string,
|
|
|
|
|
query: any = {},
|
|
|
|
|
) {
|
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,
|
|
|
|
|
} as SwMessage);
|
2022-04-30 14:52:07 +02:00
|
|
|
|
return client;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return self.clients.openWindow(getUrlWithLoginId(url, loginId));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function findClient() {
|
|
|
|
|
const clients = await self.clients.matchAll({
|
2023-01-13 05:40:33 +01:00
|
|
|
|
type: "window",
|
2022-04-30 14:52:07 +02:00
|
|
|
|
});
|
|
|
|
|
for (const c of clients) {
|
2023-01-13 05:40:33 +01:00
|
|
|
|
if (c.url.indexOf("?zen") < 0) return c;
|
2022-04-30 14:52:07 +02:00
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|