atagen

malice

public
Star0
Watch0
06339d2de37a2adbd7c9697702da5360fad61c36
2
ssh://brewery@pls.lobotomise.me:33/atagen/malice.git

Blame: web/src/lib/api.ts

973f8fbdaf80atagen2026-09-28 09:10 UTC1
// Backend-agnostic API surface. Every call forwards to whichever transport
973f8fbdaf80atagen2026-09-28 09:10 UTC2
// this build was compiled for, hosted (fetch + SSE) or on-device (worker).
249e439c27e5atagen2026-10-25 13:26 UTC3
import { parseEventContextPage, parseEventContextSession } from './types';
249e439c27e5atagen2026-10-25 13:26 UTC4
import type { EventContextPage, EventContextSession, MemberSummary, RoomSummary } from './types';
05a48e9e7942atagen2026-10-24 09:32 UTC5
import type { Emote } from './emotes';
668ab5c30531atagen2026-10-25 03:13 UTC6
import type { MentionKey, MentionsBoundary, MentionsPage, SavedMentionContext } from './mentions-model';
668ab5c30531atagen2026-10-25 03:13 UTC7
export { mentionKey, parseMentionsPage, parseSavedMentionContext, parseMentionsUpdate } from './mentions-model';
668ab5c30531atagen2026-10-25 03:13 UTC8
export type { MentionKey, MentionReason, MentionCaptureState, MentionEntry, MentionCoverage, MentionGap, MentionsBoundary, MentionsPage, SavedMentionContext, MentionsUpdate } from './mentions-model';
6d6a4c34bdc1atagen2026-10-24 06:51 UTC9
import { formatBody } from './markdown';
cf23dbf4e77catagen2026-10-23 06:36 UTC10
import { mentionedUserIds, type Mention } from './mentions';
973f8fbdaf80atagen2026-09-28 09:10 UTC11
import { HttpTransport } from './transport-http';
249e439c27e5atagen2026-10-25 13:26 UTC12
import { parseSearchPage, parseSearchSession, scopeWire, type SearchPage, type SearchScope, type SearchSession } from './search-model';
7cca1d5628d1atagen2026-10-24 07:36 UTC13
import type { CommandRequest, SendKind, SessionInfo, Transport, VerifyAction } from './transport';
c51e1fe62291atagen2026-10-25 07:58 UTC14
export type { HistoryRequest } from './transport';
973f8fbdaf80atagen2026-09-28 09:10 UTC15
import { createDeviceTransport } from 'malice:device-transport';
973f8fbdaf80atagen2026-09-28 09:10 UTC16
ba90983cec2eatagen2026-10-25 10:51 UTC17
import { ApiError } from './transport';
20fe182f3204atagen2026-10-26 05:50 UTC18
import { isRecord } from './guards';
973f8fbdaf80atagen2026-09-28 09:10 UTC19
export { ApiError } from './transport';
973f8fbdaf80atagen2026-09-28 09:10 UTC20
export type { SessionInfo, SoftLogout, VerifyAction } from './transport';
973f8fbdaf80atagen2026-09-28 09:10 UTC21
973f8fbdaf80atagen2026-09-28 09:10 UTC22
function createTransport(): Transport {
973f8fbdaf80atagen2026-09-28 09:10 UTC23
  if (import.meta.env.VITE_MALICE_TARGET === 'device') return createDeviceTransport();
973f8fbdaf80atagen2026-09-28 09:10 UTC24
  return new HttpTransport();
973f8fbdaf80atagen2026-09-28 09:10 UTC25
}
973f8fbdaf80atagen2026-09-28 09:10 UTC26
973f8fbdaf80atagen2026-09-28 09:10 UTC27
const transport: Transport = createTransport();
973f8fbdaf80atagen2026-09-28 09:10 UTC28
973f8fbdaf80atagen2026-09-28 09:10 UTC29
export function getSession(): Promise<SessionInfo> {
973f8fbdaf80atagen2026-09-28 09:10 UTC30
  return transport.getSession();
973f8fbdaf80atagen2026-09-28 09:10 UTC31
}
973f8fbdaf80atagen2026-09-28 09:10 UTC32
973f8fbdaf80atagen2026-09-28 09:10 UTC33
export function login(homeserver: string, username: string, password: string): Promise<{ user_id: string }> {
973f8fbdaf80atagen2026-09-28 09:10 UTC34
  return transport.login(homeserver, username, password);
973f8fbdaf80atagen2026-09-28 09:10 UTC35
}
973f8fbdaf80atagen2026-09-28 09:10 UTC36
973f8fbdaf80atagen2026-09-28 09:10 UTC37
export function logout(): Promise<void> {
973f8fbdaf80atagen2026-09-28 09:10 UTC38
  return transport.logout();
973f8fbdaf80atagen2026-09-28 09:10 UTC39
}
973f8fbdaf80atagen2026-09-28 09:10 UTC40
973f8fbdaf80atagen2026-09-28 09:10 UTC41
export function getRooms(): Promise<{ rooms: RoomSummary[] }> {
973f8fbdaf80atagen2026-09-28 09:10 UTC42
  return transport.getRooms();
973f8fbdaf80atagen2026-09-28 09:10 UTC43
}
973f8fbdaf80atagen2026-09-28 09:10 UTC44
973f8fbdaf80atagen2026-09-28 09:10 UTC45
export const openRoom = (room_id: string) => transport.openRoom(room_id);
973f8fbdaf80atagen2026-09-28 09:10 UTC46
export const closeRoom = (room_id: string) => transport.closeRoom(room_id);
6d6a4c34bdc1atagen2026-10-24 06:51 UTC47
export const sendMessage = (
6d6a4c34bdc1atagen2026-10-24 06:51 UTC48
  room_id: string,
6d6a4c34bdc1atagen2026-10-24 06:51 UTC49
  body: string,
6d6a4c34bdc1atagen2026-10-24 06:51 UTC50
  reply_to: string | null = null,
6d6a4c34bdc1atagen2026-10-24 06:51 UTC51
  mentions: Mention[] = [],
05a48e9e7942atagen2026-10-24 09:32 UTC52
  kind: SendKind = 'text',
7ebc2472a9e3atagen2026-10-25 09:53 UTC53
  emotes: Emote[] = [],
7ebc2472a9e3atagen2026-10-25 09:53 UTC54
  thread_root: string | null = null
7ebc2472a9e3atagen2026-10-25 09:53 UTC55
) => transport.sendMessage(room_id, body, formatBody(body, mentions, true, emotes), reply_to, mentionedUserIds(mentions), kind, thread_root);
e4c64c36f105atagen2026-10-24 07:09 UTC56
export const runCommand = (request: CommandRequest) => transport.command(request);
e4c64c36f105atagen2026-10-24 07:09 UTC57
export const uploadAvatar = (file: File) => transport.uploadAvatar(file);
7cca1d5628d1atagen2026-10-24 07:36 UTC58
export const uploadRoomAvatar = (room_id: string, file: File) => transport.uploadRoomAvatar(room_id, file);
d095131eec0eatagen2026-10-24 08:39 UTC59
export const uploadMedia = (file: File) => transport.uploadMedia(file);
7ebc2472a9e3atagen2026-10-25 09:53 UTC60
export const paginateRoom = (room_id: string, thread_root: string | null = null) => transport.paginateRoom(room_id, thread_root);
249e439c27e5atagen2026-10-25 13:26 UTC61
export async function eventContext(room_id: string, event_id: string, limit = 20): Promise<EventContextSession> {
249e439c27e5atagen2026-10-25 13:26 UTC62
  const parsed = parseEventContextSession(
249e439c27e5atagen2026-10-25 13:26 UTC63
    await transport.command({ op: 'event_context', room_id, event_id, before_limit: limit, after_limit: limit })
249e439c27e5atagen2026-10-25 13:26 UTC64
  );
249e439c27e5atagen2026-10-25 13:26 UTC65
  if (!parsed) throw new ApiError(502, 'Invalid event context response');
249e439c27e5atagen2026-10-25 13:26 UTC66
  return parsed;
249e439c27e5atagen2026-10-25 13:26 UTC67
}
249e439c27e5atagen2026-10-25 13:26 UTC68
async function eventContextPage(op: 'event_context_before' | 'event_context_after', context_id: string, token: string, limit: number): Promise<EventContextPage> {
249e439c27e5atagen2026-10-25 13:26 UTC69
  const parsed = parseEventContextPage(await transport.command({ op, context_id, token, limit }));
249e439c27e5atagen2026-10-25 13:26 UTC70
  if (!parsed) throw new ApiError(502, 'Invalid event context page response');
249e439c27e5atagen2026-10-25 13:26 UTC71
  return parsed;
249e439c27e5atagen2026-10-25 13:26 UTC72
}
249e439c27e5atagen2026-10-25 13:26 UTC73
export const eventContextBefore = (context_id: string, token: string, limit = 20): Promise<EventContextPage> =>
249e439c27e5atagen2026-10-25 13:26 UTC74
  eventContextPage('event_context_before', context_id, token, limit);
249e439c27e5atagen2026-10-25 13:26 UTC75
export const eventContextAfter = (context_id: string, token: string, limit = 20): Promise<EventContextPage> =>
249e439c27e5atagen2026-10-25 13:26 UTC76
  eventContextPage('event_context_after', context_id, token, limit);
249e439c27e5atagen2026-10-25 13:26 UTC77
export async function closeEventContext(context_id: string): Promise<void> {
249e439c27e5atagen2026-10-25 13:26 UTC78
  await transport.command({ op: 'close_event_context', context_id });
249e439c27e5atagen2026-10-25 13:26 UTC79
}
668ab5c30531atagen2026-10-25 03:13 UTC80
export const mentionsPage = (cursor: string | null = null, limit = 50): Promise<MentionsPage> => transport.mentionsPage(cursor, limit);
668ab5c30531atagen2026-10-25 03:13 UTC81
export const mentionContext = (room_id: string, event_id: string): Promise<SavedMentionContext> => transport.mentionContext(room_id, event_id);
668ab5c30531atagen2026-10-25 03:13 UTC82
export const retryMentionContext = (room_id: string, event_id: string): Promise<SavedMentionContext> => transport.retryMentionContext(room_id, event_id);
668ab5c30531atagen2026-10-25 03:13 UTC83
export const setMentionsRead = (keys: MentionKey[], read: boolean) => transport.setMentionsRead(keys, read);
668ab5c30531atagen2026-10-25 03:13 UTC84
export const markMentionsReadThrough = (boundary: MentionsBoundary) => transport.markMentionsReadThrough(boundary);
668ab5c30531atagen2026-10-25 03:13 UTC85
export const recoverMentions = () => transport.recoverMentions();
249e439c27e5atagen2026-10-25 13:26 UTC86
export async function searchMessages(query: string, scope: SearchScope, limit = 30): Promise<SearchSession> {
249e439c27e5atagen2026-10-25 13:26 UTC87
  const session = parseSearchSession(await transport.command({ op: 'search', query, scope: scopeWire(scope), limit }));
249e439c27e5atagen2026-10-25 13:26 UTC88
  if (!session) throw new ApiError(502, 'Invalid search response');
249e439c27e5atagen2026-10-25 13:26 UTC89
  return session;
249e439c27e5atagen2026-10-25 13:26 UTC90
}
249e439c27e5atagen2026-10-25 13:26 UTC91
export async function searchMore(search_id: string, cursor: string, limit = 30): Promise<SearchPage> {
249e439c27e5atagen2026-10-25 13:26 UTC92
  const page = parseSearchPage(await transport.command({ op: 'search_more', search_id, cursor, limit }));
ba90983cec2eatagen2026-10-25 10:51 UTC93
  if (!page) throw new ApiError(502, 'Invalid search response');
ba90983cec2eatagen2026-10-25 10:51 UTC94
  return page;
ba90983cec2eatagen2026-10-25 10:51 UTC95
}
249e439c27e5atagen2026-10-25 13:26 UTC96
export async function closeSearch(search_id: string): Promise<void> {
249e439c27e5atagen2026-10-25 13:26 UTC97
  await transport.command({ op: 'close_search', search_id });
249e439c27e5atagen2026-10-25 13:26 UTC98
}
249e439c27e5atagen2026-10-25 13:26 UTC99
export async function retryRecentHistory(room_id: string): Promise<void> {
249e439c27e5atagen2026-10-25 13:26 UTC100
  await transport.command({ op: 'retry_recent_history', room_id });
249e439c27e5atagen2026-10-25 13:26 UTC101
}
249e439c27e5atagen2026-10-25 13:26 UTC102
export async function fillHistoryGap(room_id: string, gap_id: string): Promise<void> {
249e439c27e5atagen2026-10-25 13:26 UTC103
  await transport.command({ op: 'fill_history_gap', room_id, gap_id });
249e439c27e5atagen2026-10-25 13:26 UTC104
}
249e439c27e5atagen2026-10-25 13:26 UTC105
export async function openThreadList(room_id: string): Promise<void> {
249e439c27e5atagen2026-10-25 13:26 UTC106
  await transport.command({ op: 'open_thread_list', room_id });
249e439c27e5atagen2026-10-25 13:26 UTC107
}
249e439c27e5atagen2026-10-25 13:26 UTC108
export async function paginateThreadList(room_id: string, limit = 20): Promise<void> {
249e439c27e5atagen2026-10-25 13:26 UTC109
  await transport.command({ op: 'paginate_thread_list', room_id, limit });
249e439c27e5atagen2026-10-25 13:26 UTC110
}
249e439c27e5atagen2026-10-25 13:26 UTC111
export async function closeThreadList(room_id: string): Promise<void> {
249e439c27e5atagen2026-10-25 13:26 UTC112
  await transport.command({ op: 'close_thread_list', room_id });
249e439c27e5atagen2026-10-25 13:26 UTC113
}
973f8fbdaf80atagen2026-09-28 09:10 UTC114
export const setTyping = (room_id: string, typing: boolean) => transport.setTyping(room_id, typing);
544a170ead2catagen2026-10-20 11:47 UTC115
export const getAccountData = (eventType: string) => transport.getAccountData(eventType);
ecb8598b38afatagen2026-10-22 09:19 UTC116
export const getUrlPreview = (room_id: string, url: string) => transport.getUrlPreview(room_id, url);
544a170ead2catagen2026-10-20 11:47 UTC117
export const setAccountData = (eventType: string, content: Record<string, unknown>) =>
544a170ead2catagen2026-10-20 11:47 UTC118
  transport.setAccountData(eventType, content);
973f8fbdaf80atagen2026-09-28 09:10 UTC119
973f8fbdaf80atagen2026-09-28 09:10 UTC120
export function joinRoom(room: string): Promise<{ room_id: string }> {
973f8fbdaf80atagen2026-09-28 09:10 UTC121
  return transport.joinRoom(room);
973f8fbdaf80atagen2026-09-28 09:10 UTC122
}
973f8fbdaf80atagen2026-09-28 09:10 UTC123
973f8fbdaf80atagen2026-09-28 09:10 UTC124
export function createRoom(name: string, topic?: string): Promise<{ room_id: string }> {
973f8fbdaf80atagen2026-09-28 09:10 UTC125
  return transport.createRoom(name, topic);
973f8fbdaf80atagen2026-09-28 09:10 UTC126
}
973f8fbdaf80atagen2026-09-28 09:10 UTC127
973f8fbdaf80atagen2026-09-28 09:10 UTC128
export const leaveRoom = (room_id: string) => transport.leaveRoom(room_id);
7ebc2472a9e3atagen2026-10-25 09:53 UTC129
export const reactToMessage = (room_id: string, event_id: string, key: string, shortcode?: string, thread_root: string | null = null) =>
7ebc2472a9e3atagen2026-10-25 09:53 UTC130
  transport.reactToMessage(room_id, event_id, key, shortcode, thread_root);
7ebc2472a9e3atagen2026-10-25 09:53 UTC131
export const editMessage = (room_id: string, event_id: string, body: string, mentions: Mention[] = [], emotes: Emote[] = [], thread_root: string | null = null) =>
7ebc2472a9e3atagen2026-10-25 09:53 UTC132
  transport.editMessage(room_id, event_id, body, formatBody(body, mentions, true, emotes), mentionedUserIds(mentions), thread_root);
7ebc2472a9e3atagen2026-10-25 09:53 UTC133
export const markRead = (room_id: string, event_id: string, thread_root: string | null = null) => transport.markRead(room_id, event_id, thread_root);
973f8fbdaf80atagen2026-09-28 09:10 UTC134
973f8fbdaf80atagen2026-09-28 09:10 UTC135
export function getMembers(room_id: string): Promise<{ members: MemberSummary[] }> {
973f8fbdaf80atagen2026-09-28 09:10 UTC136
  return transport.getMembers(room_id);
973f8fbdaf80atagen2026-09-28 09:10 UTC137
}
973f8fbdaf80atagen2026-09-28 09:10 UTC138
973f8fbdaf80atagen2026-09-28 09:10 UTC139
/** Same URL shape in both builds: the hosted server and the device service worker both answer it. */
7643577c76bbatagen2026-10-25 07:37 UTC140
export function mediaUrl(mxc: string | null, size = 256): string | null {
973f8fbdaf80atagen2026-09-28 09:10 UTC141
  if (!mxc) return null;
7643577c76bbatagen2026-10-25 07:37 UTC142
  return `/api/media?mxc=${encodeURIComponent(mxc)}&size=${Math.max(16, Math.min(512, Math.round(size)))}`;
973f8fbdaf80atagen2026-09-28 09:10 UTC143
}
973f8fbdaf80atagen2026-09-28 09:10 UTC144
973f8fbdaf80atagen2026-09-28 09:10 UTC145
export function messageMediaUrl(mediaId: string, size: 'Thumbnail' | 'Full'): string {
973f8fbdaf80atagen2026-09-28 09:10 UTC146
  return `/api/message-media?media_id=${encodeURIComponent(mediaId)}&size=${size}`;
973f8fbdaf80atagen2026-09-28 09:10 UTC147
}
973f8fbdaf80atagen2026-09-28 09:10 UTC148
973f8fbdaf80atagen2026-09-28 09:10 UTC149
export function getProfile(): Promise<{ display_name: string | null; avatar_url: string | null }> {
973f8fbdaf80atagen2026-09-28 09:10 UTC150
  return transport.getProfile();
973f8fbdaf80atagen2026-09-28 09:10 UTC151
}
973f8fbdaf80atagen2026-09-28 09:10 UTC152
973f8fbdaf80atagen2026-09-28 09:10 UTC153
export function getSpaceChildren(room_id: string): Promise<{ room_ids: string[] }> {
973f8fbdaf80atagen2026-09-28 09:10 UTC154
  return transport.getSpaceChildren(room_id);
973f8fbdaf80atagen2026-09-28 09:10 UTC155
}
973f8fbdaf80atagen2026-09-28 09:10 UTC156
973f8fbdaf80atagen2026-09-28 09:10 UTC157
export function getEncryption(): Promise<{ state: unknown; verification: unknown }> {
973f8fbdaf80atagen2026-09-28 09:10 UTC158
  return transport.getEncryption();
973f8fbdaf80atagen2026-09-28 09:10 UTC159
}
973f8fbdaf80atagen2026-09-28 09:10 UTC160
973f8fbdaf80atagen2026-09-28 09:10 UTC161
export function recoverEncryption(recovery_key: string): Promise<void> {
973f8fbdaf80atagen2026-09-28 09:10 UTC162
  return transport.recoverEncryption(recovery_key);
973f8fbdaf80atagen2026-09-28 09:10 UTC163
}
973f8fbdaf80atagen2026-09-28 09:10 UTC164
4929dcd34937atagen2026-10-26 04:43 UTC165
export type KeyBackupAction = 'enable_key_backup' | 'disable_key_backup';
4929dcd34937atagen2026-10-26 04:43 UTC166
4929dcd34937atagen2026-10-26 04:43 UTC167
export async function runKeyBackup(op: KeyBackupAction, deleteFromServer = false): Promise<void> {
4929dcd34937atagen2026-10-26 04:43 UTC168
  await transport.command(op === 'disable_key_backup' ? { op, delete_from_server: deleteFromServer } : { op });
4929dcd34937atagen2026-10-26 04:43 UTC169
}
4929dcd34937atagen2026-10-26 04:43 UTC170
20fe182f3204atagen2026-10-26 05:50 UTC171
export async function setupRecovery(op: 'enable_recovery' | 'reset_recovery_key', passphrase: string | null): Promise<string> {
20fe182f3204atagen2026-10-26 05:50 UTC172
  const reply = await transport.command({ op, passphrase });
20fe182f3204atagen2026-10-26 05:50 UTC173
  const key = isRecord(reply) ? reply['recovery_key'] : null;
20fe182f3204atagen2026-10-26 05:50 UTC174
  if (typeof key !== 'string' || !key) throw new Error('No recovery key returned');
20fe182f3204atagen2026-10-26 05:50 UTC175
  return key;
20fe182f3204atagen2026-10-26 05:50 UTC176
}
20fe182f3204atagen2026-10-26 05:50 UTC177
20fe182f3204atagen2026-10-26 05:50 UTC178
export const isBackupExists = (e: unknown): boolean => e instanceof ApiError && e.kind === 'key_backup_exists';
20fe182f3204atagen2026-10-26 05:50 UTC179
20fe182f3204atagen2026-10-26 05:50 UTC180
export async function disableRecovery(): Promise<void> {
20fe182f3204atagen2026-10-26 05:50 UTC181
  await transport.command({ op: 'disable_recovery' });
20fe182f3204atagen2026-10-26 05:50 UTC182
}
20fe182f3204atagen2026-10-26 05:50 UTC183
20fe182f3204atagen2026-10-26 05:50 UTC184
export async function verifyRecoveryKey(recovery_key: string): Promise<boolean> {
20fe182f3204atagen2026-10-26 05:50 UTC185
  const reply = await transport.command({ op: 'verify_recovery_key', recovery_key });
20fe182f3204atagen2026-10-26 05:50 UTC186
  return isRecord(reply) && reply['valid'] === true;
20fe182f3204atagen2026-10-26 05:50 UTC187
}
20fe182f3204atagen2026-10-26 05:50 UTC188
973f8fbdaf80atagen2026-09-28 09:10 UTC189
export function startVerification(): Promise<void> {
973f8fbdaf80atagen2026-09-28 09:10 UTC190
  return transport.startVerification();
973f8fbdaf80atagen2026-09-28 09:10 UTC191
}
973f8fbdaf80atagen2026-09-28 09:10 UTC192
973f8fbdaf80atagen2026-09-28 09:10 UTC193
export function verifyEncryption(action: VerifyAction): Promise<void> {
973f8fbdaf80atagen2026-09-28 09:10 UTC194
  return transport.verifyEncryption(action);
973f8fbdaf80atagen2026-09-28 09:10 UTC195
}
973f8fbdaf80atagen2026-09-28 09:10 UTC196
cf23dbf4e77catagen2026-10-23 06:36 UTC197
export function uploadAttachment(
cf23dbf4e77catagen2026-10-23 06:36 UTC198
  room_id: string,
cf23dbf4e77catagen2026-10-23 06:36 UTC199
  file: File,
cf23dbf4e77catagen2026-10-23 06:36 UTC200
  caption: string | null = null,
cf23dbf4e77catagen2026-10-23 06:36 UTC201
  reply_to: string | null = null,
6d6a4c34bdc1atagen2026-10-24 06:51 UTC202
  mentions: Mention[] = [],
dda384a360e0atagen2026-10-25 10:27 UTC203
  emotes: Emote[] = [],
dda384a360e0atagen2026-10-25 10:27 UTC204
  thread_root: string | null = null
cf23dbf4e77catagen2026-10-23 06:36 UTC205
): Promise<void> {
cf23dbf4e77catagen2026-10-23 06:36 UTC206
  return transport.uploadAttachment(room_id, file, {
cf23dbf4e77catagen2026-10-23 06:36 UTC207
    caption,
e822c659890fatagen2026-10-25 02:37 UTC208
    formatted_caption: caption ? formatBody(caption, mentions, true, emotes) : null,
cf23dbf4e77catagen2026-10-23 06:36 UTC209
    reply_to,
dda384a360e0atagen2026-10-25 10:27 UTC210
    mentions: mentionedUserIds(mentions),
dda384a360e0atagen2026-10-25 10:27 UTC211
    thread_root
cf23dbf4e77catagen2026-10-23 06:36 UTC212
  });
973f8fbdaf80atagen2026-09-28 09:10 UTC213
}
973f8fbdaf80atagen2026-09-28 09:10 UTC214
973f8fbdaf80atagen2026-09-28 09:10 UTC215
export function subscribeEvents(onEvent: (data: string) => void, onDisconnect: () => void): () => void {
973f8fbdaf80atagen2026-09-28 09:10 UTC216
  return transport.subscribeEvents(onEvent, onDisconnect);
973f8fbdaf80atagen2026-09-28 09:10 UTC217
}