atagen

malice

public
Star0
Watch0
132eeceeb3625aabae038739d20d7954e510dc25
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).
824dfd38cdf3atagen2026-10-24 13:43 UTC3
import type { EventContext, EventContextPage, MemberSummary, RoomSummary } from './types';
05a48e9e7942atagen2026-10-24 09:32 UTC4
import type { Emote } from './emotes';
668ab5c30531atagen2026-10-25 03:13 UTC5
import type { MentionKey, MentionsBoundary, MentionsPage, SavedMentionContext } from './mentions-model';
9f996e377406atagen2026-10-25 03:32 UTC6
import type { HistoryRepairBudget, HistoryRepairPolicy, HistoryRepairReport } from './repair-model';
9f996e377406atagen2026-10-25 03:32 UTC7
export { parseHistoryRepairPolicy, parseHistoryRepairReport } from './repair-model';
9f996e377406atagen2026-10-25 03:32 UTC8
export type { HistoryRepairBudget, HistoryRepairFailure, HistoryRepairPolicy, HistoryRepairReport } from './repair-model';
668ab5c30531atagen2026-10-25 03:13 UTC9
export { mentionKey, parseMentionsPage, parseSavedMentionContext, parseMentionsUpdate } from './mentions-model';
668ab5c30531atagen2026-10-25 03:13 UTC10
export type { MentionKey, MentionReason, MentionCaptureState, MentionEntry, MentionCoverage, MentionGap, MentionsBoundary, MentionsPage, SavedMentionContext, MentionsUpdate } from './mentions-model';
6d6a4c34bdc1atagen2026-10-24 06:51 UTC11
import { formatBody } from './markdown';
cf23dbf4e77catagen2026-10-23 06:36 UTC12
import { mentionedUserIds, type Mention } from './mentions';
973f8fbdaf80atagen2026-09-28 09:10 UTC13
import { HttpTransport } from './transport-http';
7cca1d5628d1atagen2026-10-24 07:36 UTC14
import type { CommandRequest, SendKind, SessionInfo, Transport, VerifyAction } from './transport';
9f996e377406atagen2026-10-25 03:32 UTC15
export type { HistoryRepairRequest } from './transport';
973f8fbdaf80atagen2026-09-28 09:10 UTC16
import { createDeviceTransport } from 'malice:device-transport';
973f8fbdaf80atagen2026-09-28 09:10 UTC17
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',
05a48e9e7942atagen2026-10-24 09:32 UTC52
  emotes: Emote[] = []
e822c659890fatagen2026-10-25 02:37 UTC53
) => transport.sendMessage(room_id, body, formatBody(body, mentions, true, emotes), reply_to, mentionedUserIds(mentions), kind);
e4c64c36f105atagen2026-10-24 07:09 UTC54
export const runCommand = (request: CommandRequest) => transport.command(request);
e4c64c36f105atagen2026-10-24 07:09 UTC55
export const uploadAvatar = (file: File) => transport.uploadAvatar(file);
7cca1d5628d1atagen2026-10-24 07:36 UTC56
export const uploadRoomAvatar = (room_id: string, file: File) => transport.uploadRoomAvatar(room_id, file);
d095131eec0eatagen2026-10-24 08:39 UTC57
export const uploadMedia = (file: File) => transport.uploadMedia(file);
973f8fbdaf80atagen2026-09-28 09:10 UTC58
export const paginateRoom = (room_id: string) => transport.paginateRoom(room_id);
824dfd38cdf3atagen2026-10-24 13:43 UTC59
export const eventContext = (room_id: string, event_id: string, limit?: number): Promise<EventContext> =>
824dfd38cdf3atagen2026-10-24 13:43 UTC60
  transport.eventContext(room_id, event_id, limit);
824dfd38cdf3atagen2026-10-24 13:43 UTC61
export const eventContextBefore = (room_id: string, event_id: string, from: string, limit?: number): Promise<EventContextPage> =>
824dfd38cdf3atagen2026-10-24 13:43 UTC62
  transport.eventContextBefore(room_id, event_id, from, limit);
824dfd38cdf3atagen2026-10-24 13:43 UTC63
export const eventContextAfter = (room_id: string, event_id: string, from: string, limit?: number): Promise<EventContextPage> =>
824dfd38cdf3atagen2026-10-24 13:43 UTC64
  transport.eventContextAfter(room_id, event_id, from, limit);
668ab5c30531atagen2026-10-25 03:13 UTC65
export const mentionsPage = (cursor: string | null = null, limit = 50): Promise<MentionsPage> => transport.mentionsPage(cursor, limit);
668ab5c30531atagen2026-10-25 03:13 UTC66
export const mentionContext = (room_id: string, event_id: string): Promise<SavedMentionContext> => transport.mentionContext(room_id, event_id);
668ab5c30531atagen2026-10-25 03:13 UTC67
export const retryMentionContext = (room_id: string, event_id: string): Promise<SavedMentionContext> => transport.retryMentionContext(room_id, event_id);
668ab5c30531atagen2026-10-25 03:13 UTC68
export const setMentionsRead = (keys: MentionKey[], read: boolean) => transport.setMentionsRead(keys, read);
668ab5c30531atagen2026-10-25 03:13 UTC69
export const markMentionsReadThrough = (boundary: MentionsBoundary) => transport.markMentionsReadThrough(boundary);
668ab5c30531atagen2026-10-25 03:13 UTC70
export const recoverMentions = () => transport.recoverMentions();
9f996e377406atagen2026-10-25 03:32 UTC71
export const historyRepair = (room_id: string, policy: HistoryRepairPolicy | null = null, budget?: HistoryRepairBudget): Promise<HistoryRepairReport> =>
9f996e377406atagen2026-10-25 03:32 UTC72
  transport.historyRepair(room_id, policy, budget);
9f996e377406atagen2026-10-25 03:32 UTC73
export const continueHistoryRepair = (room_id: string, budget?: HistoryRepairBudget): Promise<HistoryRepairReport> =>
9f996e377406atagen2026-10-25 03:32 UTC74
  transport.continueHistoryRepair(room_id, budget);
9f996e377406atagen2026-10-25 03:32 UTC75
export const cancelHistoryRepair = (room_id: string): Promise<void> => transport.cancelHistoryRepair(room_id);
9f996e377406atagen2026-10-25 03:32 UTC76
export const startRecentHistory = (room_id: string): Promise<HistoryRepairReport> => transport.startRecentHistory(room_id);
9f996e377406atagen2026-10-25 03:32 UTC77
export const historyRepairPolicy = (): Promise<HistoryRepairPolicy> => transport.historyRepairPolicy();
9f996e377406atagen2026-10-25 03:32 UTC78
export const setHistoryRepairPolicy = (policy: HistoryRepairPolicy): Promise<void> => transport.setHistoryRepairPolicy(policy);
973f8fbdaf80atagen2026-09-28 09:10 UTC79
export const setTyping = (room_id: string, typing: boolean) => transport.setTyping(room_id, typing);
544a170ead2catagen2026-10-20 11:47 UTC80
export const getAccountData = (eventType: string) => transport.getAccountData(eventType);
ecb8598b38afatagen2026-10-22 09:19 UTC81
export const getUrlPreview = (room_id: string, url: string) => transport.getUrlPreview(room_id, url);
544a170ead2catagen2026-10-20 11:47 UTC82
export const setAccountData = (eventType: string, content: Record<string, unknown>) =>
544a170ead2catagen2026-10-20 11:47 UTC83
  transport.setAccountData(eventType, content);
973f8fbdaf80atagen2026-09-28 09:10 UTC84
973f8fbdaf80atagen2026-09-28 09:10 UTC85
export function joinRoom(room: string): Promise<{ room_id: string }> {
973f8fbdaf80atagen2026-09-28 09:10 UTC86
  return transport.joinRoom(room);
973f8fbdaf80atagen2026-09-28 09:10 UTC87
}
973f8fbdaf80atagen2026-09-28 09:10 UTC88
973f8fbdaf80atagen2026-09-28 09:10 UTC89
export function createRoom(name: string, topic?: string): Promise<{ room_id: string }> {
973f8fbdaf80atagen2026-09-28 09:10 UTC90
  return transport.createRoom(name, topic);
973f8fbdaf80atagen2026-09-28 09:10 UTC91
}
973f8fbdaf80atagen2026-09-28 09:10 UTC92
973f8fbdaf80atagen2026-09-28 09:10 UTC93
export const leaveRoom = (room_id: string) => transport.leaveRoom(room_id);
ecb8598b38afatagen2026-10-22 09:19 UTC94
export const reactToMessage = (room_id: string, event_id: string, key: string, shortcode?: string) =>
ecb8598b38afatagen2026-10-22 09:19 UTC95
  transport.reactToMessage(room_id, event_id, key, shortcode);
e822c659890fatagen2026-10-25 02:37 UTC96
export const editMessage = (room_id: string, event_id: string, body: string, mentions: Mention[] = [], emotes: Emote[] = []) =>
e822c659890fatagen2026-10-25 02:37 UTC97
  transport.editMessage(room_id, event_id, body, formatBody(body, mentions, true, emotes), mentionedUserIds(mentions));
973f8fbdaf80atagen2026-09-28 09:10 UTC98
export const markRead = (room_id: string, event_id: string) => transport.markRead(room_id, event_id);
973f8fbdaf80atagen2026-09-28 09:10 UTC99
973f8fbdaf80atagen2026-09-28 09:10 UTC100
export function getMembers(room_id: string): Promise<{ members: MemberSummary[] }> {
973f8fbdaf80atagen2026-09-28 09:10 UTC101
  return transport.getMembers(room_id);
973f8fbdaf80atagen2026-09-28 09:10 UTC102
}
973f8fbdaf80atagen2026-09-28 09:10 UTC103
973f8fbdaf80atagen2026-09-28 09:10 UTC104
/** Same URL shape in both builds: the hosted server and the device service worker both answer it. */
973f8fbdaf80atagen2026-09-28 09:10 UTC105
export function mediaUrl(mxc: string | null): string | null {
973f8fbdaf80atagen2026-09-28 09:10 UTC106
  if (!mxc) return null;
973f8fbdaf80atagen2026-09-28 09:10 UTC107
  return `/api/media?mxc=${encodeURIComponent(mxc)}`;
973f8fbdaf80atagen2026-09-28 09:10 UTC108
}
973f8fbdaf80atagen2026-09-28 09:10 UTC109
973f8fbdaf80atagen2026-09-28 09:10 UTC110
export function messageMediaUrl(mediaId: string, size: 'Thumbnail' | 'Full'): string {
973f8fbdaf80atagen2026-09-28 09:10 UTC111
  return `/api/message-media?media_id=${encodeURIComponent(mediaId)}&size=${size}`;
973f8fbdaf80atagen2026-09-28 09:10 UTC112
}
973f8fbdaf80atagen2026-09-28 09:10 UTC113
973f8fbdaf80atagen2026-09-28 09:10 UTC114
export function getProfile(): Promise<{ display_name: string | null; avatar_url: string | null }> {
973f8fbdaf80atagen2026-09-28 09:10 UTC115
  return transport.getProfile();
973f8fbdaf80atagen2026-09-28 09:10 UTC116
}
973f8fbdaf80atagen2026-09-28 09:10 UTC117
973f8fbdaf80atagen2026-09-28 09:10 UTC118
export function getSpaceChildren(room_id: string): Promise<{ room_ids: string[] }> {
973f8fbdaf80atagen2026-09-28 09:10 UTC119
  return transport.getSpaceChildren(room_id);
973f8fbdaf80atagen2026-09-28 09:10 UTC120
}
973f8fbdaf80atagen2026-09-28 09:10 UTC121
973f8fbdaf80atagen2026-09-28 09:10 UTC122
export function getEncryption(): Promise<{ state: unknown; verification: unknown }> {
973f8fbdaf80atagen2026-09-28 09:10 UTC123
  return transport.getEncryption();
973f8fbdaf80atagen2026-09-28 09:10 UTC124
}
973f8fbdaf80atagen2026-09-28 09:10 UTC125
973f8fbdaf80atagen2026-09-28 09:10 UTC126
export function recoverEncryption(recovery_key: string): Promise<void> {
973f8fbdaf80atagen2026-09-28 09:10 UTC127
  return transport.recoverEncryption(recovery_key);
973f8fbdaf80atagen2026-09-28 09:10 UTC128
}
973f8fbdaf80atagen2026-09-28 09:10 UTC129
973f8fbdaf80atagen2026-09-28 09:10 UTC130
export function startVerification(): Promise<void> {
973f8fbdaf80atagen2026-09-28 09:10 UTC131
  return transport.startVerification();
973f8fbdaf80atagen2026-09-28 09:10 UTC132
}
973f8fbdaf80atagen2026-09-28 09:10 UTC133
973f8fbdaf80atagen2026-09-28 09:10 UTC134
export function verifyEncryption(action: VerifyAction): Promise<void> {
973f8fbdaf80atagen2026-09-28 09:10 UTC135
  return transport.verifyEncryption(action);
973f8fbdaf80atagen2026-09-28 09:10 UTC136
}
973f8fbdaf80atagen2026-09-28 09:10 UTC137
cf23dbf4e77catagen2026-10-23 06:36 UTC138
export function uploadAttachment(
cf23dbf4e77catagen2026-10-23 06:36 UTC139
  room_id: string,
cf23dbf4e77catagen2026-10-23 06:36 UTC140
  file: File,
cf23dbf4e77catagen2026-10-23 06:36 UTC141
  caption: string | null = null,
cf23dbf4e77catagen2026-10-23 06:36 UTC142
  reply_to: string | null = null,
6d6a4c34bdc1atagen2026-10-24 06:51 UTC143
  mentions: Mention[] = [],
05a48e9e7942atagen2026-10-24 09:32 UTC144
  emotes: Emote[] = []
cf23dbf4e77catagen2026-10-23 06:36 UTC145
): Promise<void> {
cf23dbf4e77catagen2026-10-23 06:36 UTC146
  return transport.uploadAttachment(room_id, file, {
cf23dbf4e77catagen2026-10-23 06:36 UTC147
    caption,
e822c659890fatagen2026-10-25 02:37 UTC148
    formatted_caption: caption ? formatBody(caption, mentions, true, emotes) : null,
cf23dbf4e77catagen2026-10-23 06:36 UTC149
    reply_to,
cf23dbf4e77catagen2026-10-23 06:36 UTC150
    mentions: mentionedUserIds(mentions)
cf23dbf4e77catagen2026-10-23 06:36 UTC151
  });
973f8fbdaf80atagen2026-09-28 09:10 UTC152
}
973f8fbdaf80atagen2026-09-28 09:10 UTC153
973f8fbdaf80atagen2026-09-28 09:10 UTC154
export function subscribeEvents(onEvent: (data: string) => void, onDisconnect: () => void): () => void {
973f8fbdaf80atagen2026-09-28 09:10 UTC155
  return transport.subscribeEvents(onEvent, onDisconnect);
973f8fbdaf80atagen2026-09-28 09:10 UTC156
}