atagen

malice

public
Star0
Watch0
945233a9ddf4866f292d438e83ded93cbf2512e0
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';
973f8fbdaf80atagen2026-09-28 09:10 UTC18
export { ApiError } from './transport';
973f8fbdaf80atagen2026-09-28 09:10 UTC19
export type { SessionInfo, SoftLogout, VerifyAction } from './transport';
973f8fbdaf80atagen2026-09-28 09:10 UTC20
973f8fbdaf80atagen2026-09-28 09:10 UTC21
function createTransport(): Transport {
973f8fbdaf80atagen2026-09-28 09:10 UTC22
  if (import.meta.env.VITE_MALICE_TARGET === 'device') return createDeviceTransport();
973f8fbdaf80atagen2026-09-28 09:10 UTC23
  return new HttpTransport();
973f8fbdaf80atagen2026-09-28 09:10 UTC24
}
973f8fbdaf80atagen2026-09-28 09:10 UTC25
973f8fbdaf80atagen2026-09-28 09:10 UTC26
const transport: Transport = createTransport();
973f8fbdaf80atagen2026-09-28 09:10 UTC27
973f8fbdaf80atagen2026-09-28 09:10 UTC28
export function getSession(): Promise<SessionInfo> {
973f8fbdaf80atagen2026-09-28 09:10 UTC29
  return transport.getSession();
973f8fbdaf80atagen2026-09-28 09:10 UTC30
}
973f8fbdaf80atagen2026-09-28 09:10 UTC31
973f8fbdaf80atagen2026-09-28 09:10 UTC32
export function login(homeserver: string, username: string, password: string): Promise<{ user_id: string }> {
973f8fbdaf80atagen2026-09-28 09:10 UTC33
  return transport.login(homeserver, username, password);
973f8fbdaf80atagen2026-09-28 09:10 UTC34
}
973f8fbdaf80atagen2026-09-28 09:10 UTC35
973f8fbdaf80atagen2026-09-28 09:10 UTC36
export function logout(): Promise<void> {
973f8fbdaf80atagen2026-09-28 09:10 UTC37
  return transport.logout();
973f8fbdaf80atagen2026-09-28 09:10 UTC38
}
973f8fbdaf80atagen2026-09-28 09:10 UTC39
973f8fbdaf80atagen2026-09-28 09:10 UTC40
export function getRooms(): Promise<{ rooms: RoomSummary[] }> {
973f8fbdaf80atagen2026-09-28 09:10 UTC41
  return transport.getRooms();
973f8fbdaf80atagen2026-09-28 09:10 UTC42
}
973f8fbdaf80atagen2026-09-28 09:10 UTC43
973f8fbdaf80atagen2026-09-28 09:10 UTC44
export const openRoom = (room_id: string) => transport.openRoom(room_id);
973f8fbdaf80atagen2026-09-28 09:10 UTC45
export const closeRoom = (room_id: string) => transport.closeRoom(room_id);
6d6a4c34bdc1atagen2026-10-24 06:51 UTC46
export const sendMessage = (
6d6a4c34bdc1atagen2026-10-24 06:51 UTC47
  room_id: string,
6d6a4c34bdc1atagen2026-10-24 06:51 UTC48
  body: string,
6d6a4c34bdc1atagen2026-10-24 06:51 UTC49
  reply_to: string | null = null,
6d6a4c34bdc1atagen2026-10-24 06:51 UTC50
  mentions: Mention[] = [],
05a48e9e7942atagen2026-10-24 09:32 UTC51
  kind: SendKind = 'text',
7ebc2472a9e3atagen2026-10-25 09:53 UTC52
  emotes: Emote[] = [],
7ebc2472a9e3atagen2026-10-25 09:53 UTC53
  thread_root: string | null = null
7ebc2472a9e3atagen2026-10-25 09:53 UTC54
) => transport.sendMessage(room_id, body, formatBody(body, mentions, true, emotes), reply_to, mentionedUserIds(mentions), kind, thread_root);
e4c64c36f105atagen2026-10-24 07:09 UTC55
export const runCommand = (request: CommandRequest) => transport.command(request);
e4c64c36f105atagen2026-10-24 07:09 UTC56
export const uploadAvatar = (file: File) => transport.uploadAvatar(file);
7cca1d5628d1atagen2026-10-24 07:36 UTC57
export const uploadRoomAvatar = (room_id: string, file: File) => transport.uploadRoomAvatar(room_id, file);
d095131eec0eatagen2026-10-24 08:39 UTC58
export const uploadMedia = (file: File) => transport.uploadMedia(file);
7ebc2472a9e3atagen2026-10-25 09:53 UTC59
export const paginateRoom = (room_id: string, thread_root: string | null = null) => transport.paginateRoom(room_id, thread_root);
249e439c27e5atagen2026-10-25 13:26 UTC60
export async function eventContext(room_id: string, event_id: string, limit = 20): Promise<EventContextSession> {
249e439c27e5atagen2026-10-25 13:26 UTC61
  const parsed = parseEventContextSession(
249e439c27e5atagen2026-10-25 13:26 UTC62
    await transport.command({ op: 'event_context', room_id, event_id, before_limit: limit, after_limit: limit })
249e439c27e5atagen2026-10-25 13:26 UTC63
  );
249e439c27e5atagen2026-10-25 13:26 UTC64
  if (!parsed) throw new ApiError(502, 'Invalid event context response');
249e439c27e5atagen2026-10-25 13:26 UTC65
  return parsed;
249e439c27e5atagen2026-10-25 13:26 UTC66
}
249e439c27e5atagen2026-10-25 13:26 UTC67
async function eventContextPage(op: 'event_context_before' | 'event_context_after', context_id: string, token: string, limit: number): Promise<EventContextPage> {
249e439c27e5atagen2026-10-25 13:26 UTC68
  const parsed = parseEventContextPage(await transport.command({ op, context_id, token, limit }));
249e439c27e5atagen2026-10-25 13:26 UTC69
  if (!parsed) throw new ApiError(502, 'Invalid event context page response');
249e439c27e5atagen2026-10-25 13:26 UTC70
  return parsed;
249e439c27e5atagen2026-10-25 13:26 UTC71
}
249e439c27e5atagen2026-10-25 13:26 UTC72
export const eventContextBefore = (context_id: string, token: string, limit = 20): Promise<EventContextPage> =>
249e439c27e5atagen2026-10-25 13:26 UTC73
  eventContextPage('event_context_before', context_id, token, limit);
249e439c27e5atagen2026-10-25 13:26 UTC74
export const eventContextAfter = (context_id: string, token: string, limit = 20): Promise<EventContextPage> =>
249e439c27e5atagen2026-10-25 13:26 UTC75
  eventContextPage('event_context_after', context_id, token, limit);
249e439c27e5atagen2026-10-25 13:26 UTC76
export async function closeEventContext(context_id: string): Promise<void> {
249e439c27e5atagen2026-10-25 13:26 UTC77
  await transport.command({ op: 'close_event_context', context_id });
249e439c27e5atagen2026-10-25 13:26 UTC78
}
668ab5c30531atagen2026-10-25 03:13 UTC79
export const mentionsPage = (cursor: string | null = null, limit = 50): Promise<MentionsPage> => transport.mentionsPage(cursor, limit);
668ab5c30531atagen2026-10-25 03:13 UTC80
export const mentionContext = (room_id: string, event_id: string): Promise<SavedMentionContext> => transport.mentionContext(room_id, event_id);
668ab5c30531atagen2026-10-25 03:13 UTC81
export const retryMentionContext = (room_id: string, event_id: string): Promise<SavedMentionContext> => transport.retryMentionContext(room_id, event_id);
668ab5c30531atagen2026-10-25 03:13 UTC82
export const setMentionsRead = (keys: MentionKey[], read: boolean) => transport.setMentionsRead(keys, read);
668ab5c30531atagen2026-10-25 03:13 UTC83
export const markMentionsReadThrough = (boundary: MentionsBoundary) => transport.markMentionsReadThrough(boundary);
668ab5c30531atagen2026-10-25 03:13 UTC84
export const recoverMentions = () => transport.recoverMentions();
249e439c27e5atagen2026-10-25 13:26 UTC85
export async function searchMessages(query: string, scope: SearchScope, limit = 30): Promise<SearchSession> {
249e439c27e5atagen2026-10-25 13:26 UTC86
  const session = parseSearchSession(await transport.command({ op: 'search', query, scope: scopeWire(scope), limit }));
249e439c27e5atagen2026-10-25 13:26 UTC87
  if (!session) throw new ApiError(502, 'Invalid search response');
249e439c27e5atagen2026-10-25 13:26 UTC88
  return session;
249e439c27e5atagen2026-10-25 13:26 UTC89
}
249e439c27e5atagen2026-10-25 13:26 UTC90
export async function searchMore(search_id: string, cursor: string, limit = 30): Promise<SearchPage> {
249e439c27e5atagen2026-10-25 13:26 UTC91
  const page = parseSearchPage(await transport.command({ op: 'search_more', search_id, cursor, limit }));
ba90983cec2eatagen2026-10-25 10:51 UTC92
  if (!page) throw new ApiError(502, 'Invalid search response');
ba90983cec2eatagen2026-10-25 10:51 UTC93
  return page;
ba90983cec2eatagen2026-10-25 10:51 UTC94
}
249e439c27e5atagen2026-10-25 13:26 UTC95
export async function closeSearch(search_id: string): Promise<void> {
249e439c27e5atagen2026-10-25 13:26 UTC96
  await transport.command({ op: 'close_search', search_id });
249e439c27e5atagen2026-10-25 13:26 UTC97
}
249e439c27e5atagen2026-10-25 13:26 UTC98
export async function retryRecentHistory(room_id: string): Promise<void> {
249e439c27e5atagen2026-10-25 13:26 UTC99
  await transport.command({ op: 'retry_recent_history', room_id });
249e439c27e5atagen2026-10-25 13:26 UTC100
}
249e439c27e5atagen2026-10-25 13:26 UTC101
export async function fillHistoryGap(room_id: string, gap_id: string): Promise<void> {
249e439c27e5atagen2026-10-25 13:26 UTC102
  await transport.command({ op: 'fill_history_gap', room_id, gap_id });
249e439c27e5atagen2026-10-25 13:26 UTC103
}
249e439c27e5atagen2026-10-25 13:26 UTC104
export async function openThreadList(room_id: string): Promise<void> {
249e439c27e5atagen2026-10-25 13:26 UTC105
  await transport.command({ op: 'open_thread_list', room_id });
249e439c27e5atagen2026-10-25 13:26 UTC106
}
249e439c27e5atagen2026-10-25 13:26 UTC107
export async function paginateThreadList(room_id: string, limit = 20): Promise<void> {
249e439c27e5atagen2026-10-25 13:26 UTC108
  await transport.command({ op: 'paginate_thread_list', room_id, limit });
249e439c27e5atagen2026-10-25 13:26 UTC109
}
249e439c27e5atagen2026-10-25 13:26 UTC110
export async function closeThreadList(room_id: string): Promise<void> {
249e439c27e5atagen2026-10-25 13:26 UTC111
  await transport.command({ op: 'close_thread_list', room_id });
249e439c27e5atagen2026-10-25 13:26 UTC112
}
973f8fbdaf80atagen2026-09-28 09:10 UTC113
export const setTyping = (room_id: string, typing: boolean) => transport.setTyping(room_id, typing);
544a170ead2catagen2026-10-20 11:47 UTC114
export const getAccountData = (eventType: string) => transport.getAccountData(eventType);
ecb8598b38afatagen2026-10-22 09:19 UTC115
export const getUrlPreview = (room_id: string, url: string) => transport.getUrlPreview(room_id, url);
544a170ead2catagen2026-10-20 11:47 UTC116
export const setAccountData = (eventType: string, content: Record<string, unknown>) =>
544a170ead2catagen2026-10-20 11:47 UTC117
  transport.setAccountData(eventType, content);
973f8fbdaf80atagen2026-09-28 09:10 UTC118
973f8fbdaf80atagen2026-09-28 09:10 UTC119
export function joinRoom(room: string): Promise<{ room_id: string }> {
973f8fbdaf80atagen2026-09-28 09:10 UTC120
  return transport.joinRoom(room);
973f8fbdaf80atagen2026-09-28 09:10 UTC121
}
973f8fbdaf80atagen2026-09-28 09:10 UTC122
973f8fbdaf80atagen2026-09-28 09:10 UTC123
export function createRoom(name: string, topic?: string): Promise<{ room_id: string }> {
973f8fbdaf80atagen2026-09-28 09:10 UTC124
  return transport.createRoom(name, topic);
973f8fbdaf80atagen2026-09-28 09:10 UTC125
}
973f8fbdaf80atagen2026-09-28 09:10 UTC126
973f8fbdaf80atagen2026-09-28 09:10 UTC127
export const leaveRoom = (room_id: string) => transport.leaveRoom(room_id);
7ebc2472a9e3atagen2026-10-25 09:53 UTC128
export const reactToMessage = (room_id: string, event_id: string, key: string, shortcode?: string, thread_root: string | null = null) =>
7ebc2472a9e3atagen2026-10-25 09:53 UTC129
  transport.reactToMessage(room_id, event_id, key, shortcode, thread_root);
7ebc2472a9e3atagen2026-10-25 09:53 UTC130
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 UTC131
  transport.editMessage(room_id, event_id, body, formatBody(body, mentions, true, emotes), mentionedUserIds(mentions), thread_root);
7ebc2472a9e3atagen2026-10-25 09:53 UTC132
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 UTC133
973f8fbdaf80atagen2026-09-28 09:10 UTC134
export function getMembers(room_id: string): Promise<{ members: MemberSummary[] }> {
973f8fbdaf80atagen2026-09-28 09:10 UTC135
  return transport.getMembers(room_id);
973f8fbdaf80atagen2026-09-28 09:10 UTC136
}
973f8fbdaf80atagen2026-09-28 09:10 UTC137
973f8fbdaf80atagen2026-09-28 09:10 UTC138
/** Same URL shape in both builds: the hosted server and the device service worker both answer it. */
7643577c76bbatagen2026-10-25 07:37 UTC139
export function mediaUrl(mxc: string | null, size = 256): string | null {
973f8fbdaf80atagen2026-09-28 09:10 UTC140
  if (!mxc) return null;
7643577c76bbatagen2026-10-25 07:37 UTC141
  return `/api/media?mxc=${encodeURIComponent(mxc)}&size=${Math.max(16, Math.min(512, Math.round(size)))}`;
973f8fbdaf80atagen2026-09-28 09:10 UTC142
}
973f8fbdaf80atagen2026-09-28 09:10 UTC143
973f8fbdaf80atagen2026-09-28 09:10 UTC144
export function messageMediaUrl(mediaId: string, size: 'Thumbnail' | 'Full'): string {
973f8fbdaf80atagen2026-09-28 09:10 UTC145
  return `/api/message-media?media_id=${encodeURIComponent(mediaId)}&size=${size}`;
973f8fbdaf80atagen2026-09-28 09:10 UTC146
}
973f8fbdaf80atagen2026-09-28 09:10 UTC147
973f8fbdaf80atagen2026-09-28 09:10 UTC148
export function getProfile(): Promise<{ display_name: string | null; avatar_url: string | null }> {
973f8fbdaf80atagen2026-09-28 09:10 UTC149
  return transport.getProfile();
973f8fbdaf80atagen2026-09-28 09:10 UTC150
}
973f8fbdaf80atagen2026-09-28 09:10 UTC151
973f8fbdaf80atagen2026-09-28 09:10 UTC152
export function getSpaceChildren(room_id: string): Promise<{ room_ids: string[] }> {
973f8fbdaf80atagen2026-09-28 09:10 UTC153
  return transport.getSpaceChildren(room_id);
973f8fbdaf80atagen2026-09-28 09:10 UTC154
}
973f8fbdaf80atagen2026-09-28 09:10 UTC155
973f8fbdaf80atagen2026-09-28 09:10 UTC156
export function getEncryption(): Promise<{ state: unknown; verification: unknown }> {
973f8fbdaf80atagen2026-09-28 09:10 UTC157
  return transport.getEncryption();
973f8fbdaf80atagen2026-09-28 09:10 UTC158
}
973f8fbdaf80atagen2026-09-28 09:10 UTC159
973f8fbdaf80atagen2026-09-28 09:10 UTC160
export function recoverEncryption(recovery_key: string): Promise<void> {
973f8fbdaf80atagen2026-09-28 09:10 UTC161
  return transport.recoverEncryption(recovery_key);
973f8fbdaf80atagen2026-09-28 09:10 UTC162
}
973f8fbdaf80atagen2026-09-28 09:10 UTC163
973f8fbdaf80atagen2026-09-28 09:10 UTC164
export function startVerification(): Promise<void> {
973f8fbdaf80atagen2026-09-28 09:10 UTC165
  return transport.startVerification();
973f8fbdaf80atagen2026-09-28 09:10 UTC166
}
973f8fbdaf80atagen2026-09-28 09:10 UTC167
973f8fbdaf80atagen2026-09-28 09:10 UTC168
export function verifyEncryption(action: VerifyAction): Promise<void> {
973f8fbdaf80atagen2026-09-28 09:10 UTC169
  return transport.verifyEncryption(action);
973f8fbdaf80atagen2026-09-28 09:10 UTC170
}
973f8fbdaf80atagen2026-09-28 09:10 UTC171
cf23dbf4e77catagen2026-10-23 06:36 UTC172
export function uploadAttachment(
cf23dbf4e77catagen2026-10-23 06:36 UTC173
  room_id: string,
cf23dbf4e77catagen2026-10-23 06:36 UTC174
  file: File,
cf23dbf4e77catagen2026-10-23 06:36 UTC175
  caption: string | null = null,
cf23dbf4e77catagen2026-10-23 06:36 UTC176
  reply_to: string | null = null,
6d6a4c34bdc1atagen2026-10-24 06:51 UTC177
  mentions: Mention[] = [],
dda384a360e0atagen2026-10-25 10:27 UTC178
  emotes: Emote[] = [],
dda384a360e0atagen2026-10-25 10:27 UTC179
  thread_root: string | null = null
cf23dbf4e77catagen2026-10-23 06:36 UTC180
): Promise<void> {
cf23dbf4e77catagen2026-10-23 06:36 UTC181
  return transport.uploadAttachment(room_id, file, {
cf23dbf4e77catagen2026-10-23 06:36 UTC182
    caption,
e822c659890fatagen2026-10-25 02:37 UTC183
    formatted_caption: caption ? formatBody(caption, mentions, true, emotes) : null,
cf23dbf4e77catagen2026-10-23 06:36 UTC184
    reply_to,
dda384a360e0atagen2026-10-25 10:27 UTC185
    mentions: mentionedUserIds(mentions),
dda384a360e0atagen2026-10-25 10:27 UTC186
    thread_root
cf23dbf4e77catagen2026-10-23 06:36 UTC187
  });
973f8fbdaf80atagen2026-09-28 09:10 UTC188
}
973f8fbdaf80atagen2026-09-28 09:10 UTC189
973f8fbdaf80atagen2026-09-28 09:10 UTC190
export function subscribeEvents(onEvent: (data: string) => void, onDisconnect: () => void): () => void {
973f8fbdaf80atagen2026-09-28 09:10 UTC191
  return transport.subscribeEvents(onEvent, onDisconnect);
973f8fbdaf80atagen2026-09-28 09:10 UTC192
}