summaryrefslogtreecommitdiff
path: root/packages/frontend/src/server-context.ts
blob: aa44a1029076e5bcd7f03fd21443c124e15b46d2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/*
 * SPDX-FileCopyrightText: syuilo and misskey-project
 * SPDX-License-Identifier: AGPL-3.0-only
 */
import * as Misskey from 'misskey-js';
import { $i } from '@/account.js';

const providedContextEl = document.getElementById('misskey_clientCtx');

export type ServerContext = {
	clip?: Misskey.entities.Clip;
	note?: Misskey.entities.Note;
	user?: Misskey.entities.UserLite;
} | null;

export const serverContext: ServerContext = (providedContextEl && providedContextEl.textContent) ? JSON.parse(providedContextEl.textContent) : null;

export function getServerContext<K extends keyof NonNullable<ServerContext>>(entity: K): Required<Pick<NonNullable<ServerContext>, K>> | null {
	// contextは非ログイン状態の情報しかないためログイン時は利用できない
	if ($i) return null;

	return serverContext ? (serverContext[entity] ?? null) : null;
}