misskey/src/client/sw/operations.ts

78 lines
2.3 KiB
TypeScript
Raw Normal View History

2021-02-15 06:05:18 +09:00
/*
* Operations
*
*/
declare var self: ServiceWorkerGlobalScope;
import { SwMessage, swMessageOrderType } from './types';
2021-07-17 22:00:48 +09:00
import { getAcct } from '@/misc/acct';
2021-03-23 21:41:22 +09:00
import { getAccountFromId } from '@client/scripts/get-account-from-id';
import { appendLoginId } from '@client/scripts/login-id';
2021-02-15 06:05:18 +09:00
export async function api(endpoint: string, userId: string, options: any = {}) {
2021-02-15 06:17:21 +09:00
const account = await getAccountFromId(userId);
if (!account) return;
2021-02-15 06:05:18 +09:00
2021-02-15 06:17:21 +09:00
return fetch(`${origin}/api/${endpoint}`, {
method: 'POST',
body: JSON.stringify({
i: account.token,
...options
}),
credentials: 'omit',
cache: 'no-cache',
}).then(async res => {
if (!res.ok) Error(`Error while fetching: ${await res.text()}`);
2021-02-15 06:05:18 +09:00
2021-02-15 06:17:21 +09:00
if (res.status === 200) return res.json();
return;
});
2021-02-15 06:05:18 +09:00
}
// rendered acctからユーザーを開く
export function openUser(acct: string, loginId: string) {
2021-02-15 06:17:21 +09:00
return openClient('push', `/@${acct}`, loginId, { acct });
2021-02-15 06:05:18 +09:00
}
// noteIdからートを開く
export function openNote(noteId: string, loginId: string) {
2021-02-15 06:17:21 +09:00
return openClient('push', `/notes/${noteId}`, loginId, { noteId });
2021-02-15 06:05:18 +09:00
}
export async function openChat(body: any, loginId: string) {
2021-02-15 06:17:21 +09:00
if (body.groupId === null) {
2021-07-17 22:00:48 +09:00
return openClient('push', `/my/messaging/${getAcct(body.user)}`, loginId, { body });
2021-02-15 06:17:21 +09:00
} else {
return openClient('push', `/my/messaging/group/${body.groupId}`, loginId, { body });
}
2021-02-15 06:05:18 +09:00
}
// post-formのオプションから投稿フォームを開く
export async function openPost(options: any, loginId: string) {
2021-02-15 06:17:21 +09:00
// クエリを作成しておく
let url = `/share?`;
if (options.initialText) url += `text=${options.initialText}&`;
if (options.reply) url += `replyId=${options.reply.id}&`;
if (options.renote) url += `renoteId=${options.renote.id}&`;
2021-02-15 06:05:18 +09:00
2021-02-15 06:17:21 +09:00
return openClient('post', url, loginId, { options });
2021-02-15 06:05:18 +09:00
}
export async function openClient(order: swMessageOrderType, url: string, loginId: string, query: any = {}) {
2021-02-15 06:17:21 +09:00
const client = await self.clients.matchAll({
2021-02-15 06:05:18 +09:00
type: 'window'
2021-02-17 02:20:45 +09:00
}).then(clients => {
for (const c of clients) {
if (c.url.indexOf('?zen') < 0) return c;
}
return null;
});
2021-02-15 06:05:18 +09:00
2021-02-15 06:17:21 +09:00
if (client) {
client.postMessage({ type: 'order', ...query, order, loginId, url } as SwMessage);
return client;
}
2021-02-15 06:05:18 +09:00
2021-02-15 06:17:21 +09:00
return self.clients.openWindow(appendLoginId(url, loginId));
2021-02-15 06:05:18 +09:00
}