2022-04-30 12:52:07 +00:00
|
|
|
|
/*
|
|
|
|
|
* Operations
|
|
|
|
|
* 各種操作
|
|
|
|
|
*/
|
|
|
|
|
import * as Misskey from 'misskey-js';
|
2023-04-10 12:10:06 +00:00
|
|
|
|
import { SwMessage, SwMessageOrderType } from '@/types';
|
2022-04-30 12:52:07 +00:00
|
|
|
|
import { getAccountFromId } from '@/scripts/get-account-from-id';
|
|
|
|
|
import { getUrlWithLoginId } from '@/scripts/login-id';
|
|
|
|
|
|
|
|
|
|
export const cli = new Misskey.api.APIClient({ origin, fetch: (...args) => fetch(...args) });
|
|
|
|
|
|
|
|
|
|
export async function api<E extends keyof Misskey.Endpoints>(endpoint: E, userId: string, options?: Misskey.Endpoints[E]['req']) {
|
|
|
|
|
const account = await getAccountFromId(userId);
|
|
|
|
|
if (!account) return;
|
|
|
|
|
|
|
|
|
|
return cli.request(endpoint, options, account.token);
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-11 05:11:39 +00:00
|
|
|
|
// 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 12:52:07 +00:00
|
|
|
|
// rendered acctからユーザーを開く
|
2023-04-11 05:11:39 +00:00
|
|
|
|
export function openUser(acct: string, loginId?: string) {
|
2022-04-30 12:52:07 +00:00
|
|
|
|
return openClient('push', `/@${acct}`, loginId, { acct });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// noteIdからノートを開く
|
2023-04-11 05:11:39 +00:00
|
|
|
|
export function openNote(noteId: string, loginId?: string) {
|
2022-04-30 12:52:07 +00:00
|
|
|
|
return openClient('push', `/notes/${noteId}`, loginId, { noteId });
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-18 10:50:02 +00:00
|
|
|
|
// noteIdからノートを開く
|
|
|
|
|
export function openAntenna(antennaId: string, loginId: string) {
|
|
|
|
|
return openClient('push', `/timeline/antenna/${antennaId}`, loginId, { antennaId });
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-30 12:52:07 +00:00
|
|
|
|
// post-formのオプションから投稿フォームを開く
|
2023-04-11 05:11:39 +00:00
|
|
|
|
export async function openPost(options: any, loginId?: string) {
|
2022-04-30 12:52:07 +00:00
|
|
|
|
// クエリを作成しておく
|
2023-02-15 04:37:18 +00:00
|
|
|
|
let url = '/share?';
|
2022-04-30 12:52:07 +00:00
|
|
|
|
if (options.initialText) url += `text=${options.initialText}&`;
|
|
|
|
|
if (options.reply) url += `replyId=${options.reply.id}&`;
|
|
|
|
|
if (options.renote) url += `renoteId=${options.renote.id}&`;
|
|
|
|
|
|
|
|
|
|
return openClient('post', url, loginId, { options });
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-11 05:11:39 +00:00
|
|
|
|
export async function openClient(order: SwMessageOrderType, url: string, loginId?: string, query: any = {}) {
|
2022-04-30 12:52:07 +00:00
|
|
|
|
const client = await findClient();
|
|
|
|
|
|
|
|
|
|
if (client) {
|
|
|
|
|
client.postMessage({ type: 'order', ...query, order, loginId, url } as SwMessage);
|
|
|
|
|
return client;
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-11 05:11:39 +00:00
|
|
|
|
return globalThis.clients.openWindow(loginId ? getUrlWithLoginId(url, loginId) : url);
|
2022-04-30 12:52:07 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function findClient() {
|
2023-02-18 05:16:34 +00:00
|
|
|
|
const clients = await globalThis.clients.matchAll({
|
2023-02-15 04:37:18 +00:00
|
|
|
|
type: 'window',
|
2022-04-30 12:52:07 +00:00
|
|
|
|
});
|
|
|
|
|
for (const c of clients) {
|
2023-04-11 05:11:39 +00:00
|
|
|
|
if (!(new URL(c.url)).searchParams.has('zen')) return c;
|
2022-04-30 12:52:07 +00:00
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|