973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 1 | // Backend-agnostic API surface. Every call forwards to whichever transport |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 2 | // this build was compiled for, hosted (fetch + SSE) or on-device (worker). |
824dfd38cdf3 | atagen | 2026-10-24 13:43 UTC | 3 | import type { EventContext, EventContextPage, MemberSummary, RoomSummary } from './types'; |
05a48e9e7942 | atagen | 2026-10-24 09:32 UTC | 4 | import type { Emote } from './emotes'; |
668ab5c30531 | atagen | 2026-10-25 03:13 UTC | 5 | import type { MentionKey, MentionsBoundary, MentionsPage, SavedMentionContext } from './mentions-model'; |
668ab5c30531 | atagen | 2026-10-25 03:13 UTC | 6 | export { mentionKey, parseMentionsPage, parseSavedMentionContext, parseMentionsUpdate } from './mentions-model'; |
668ab5c30531 | atagen | 2026-10-25 03:13 UTC | 7 | export type { MentionKey, MentionReason, MentionCaptureState, MentionEntry, MentionCoverage, MentionGap, MentionsBoundary, MentionsPage, SavedMentionContext, MentionsUpdate } from './mentions-model'; |
6d6a4c34bdc1 | atagen | 2026-10-24 06:51 UTC | 8 | import { formatBody } from './markdown'; |
cf23dbf4e77c | atagen | 2026-10-23 06:36 UTC | 9 | import { mentionedUserIds, type Mention } from './mentions'; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 10 | import { HttpTransport } from './transport-http'; |
7cca1d5628d1 | atagen | 2026-10-24 07:36 UTC | 11 | import type { CommandRequest, SendKind, SessionInfo, Transport, VerifyAction } from './transport'; |
c51e1fe62291 | atagen | 2026-10-25 07:58 UTC | 12 | export type { HistoryRequest } from './transport'; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 13 | import { createDeviceTransport } from 'malice:device-transport'; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 14 | |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 15 | export { ApiError } from './transport'; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 16 | export type { SessionInfo, SoftLogout, VerifyAction } from './transport'; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 17 | |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 18 | function createTransport(): Transport { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 19 | if (import.meta.env.VITE_MALICE_TARGET === 'device') return createDeviceTransport(); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 20 | return new HttpTransport(); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 21 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 22 | |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 23 | const transport: Transport = createTransport(); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 24 | |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 25 | export function getSession(): Promise<SessionInfo> { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 26 | return transport.getSession(); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 27 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 28 | |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 29 | export function login(homeserver: string, username: string, password: string): Promise<{ user_id: string }> { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 30 | return transport.login(homeserver, username, password); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 31 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 32 | |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 33 | export function logout(): Promise<void> { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 34 | return transport.logout(); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 35 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 36 | |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 37 | export function getRooms(): Promise<{ rooms: RoomSummary[] }> { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 38 | return transport.getRooms(); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 39 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 40 | |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 41 | export const openRoom = (room_id: string) => transport.openRoom(room_id); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 42 | export const closeRoom = (room_id: string) => transport.closeRoom(room_id); |
6d6a4c34bdc1 | atagen | 2026-10-24 06:51 UTC | 43 | export const sendMessage = ( |
6d6a4c34bdc1 | atagen | 2026-10-24 06:51 UTC | 44 | room_id: string, |
6d6a4c34bdc1 | atagen | 2026-10-24 06:51 UTC | 45 | body: string, |
6d6a4c34bdc1 | atagen | 2026-10-24 06:51 UTC | 46 | reply_to: string | null = null, |
6d6a4c34bdc1 | atagen | 2026-10-24 06:51 UTC | 47 | mentions: Mention[] = [], |
05a48e9e7942 | atagen | 2026-10-24 09:32 UTC | 48 | kind: SendKind = 'text', |
7ebc2472a9e3 | atagen | 2026-10-25 09:53 UTC | 49 | emotes: Emote[] = [], |
7ebc2472a9e3 | atagen | 2026-10-25 09:53 UTC | 50 | thread_root: string | null = null |
7ebc2472a9e3 | atagen | 2026-10-25 09:53 UTC | 51 | ) => transport.sendMessage(room_id, body, formatBody(body, mentions, true, emotes), reply_to, mentionedUserIds(mentions), kind, thread_root); |
e4c64c36f105 | atagen | 2026-10-24 07:09 UTC | 52 | export const runCommand = (request: CommandRequest) => transport.command(request); |
e4c64c36f105 | atagen | 2026-10-24 07:09 UTC | 53 | export const uploadAvatar = (file: File) => transport.uploadAvatar(file); |
7cca1d5628d1 | atagen | 2026-10-24 07:36 UTC | 54 | export const uploadRoomAvatar = (room_id: string, file: File) => transport.uploadRoomAvatar(room_id, file); |
d095131eec0e | atagen | 2026-10-24 08:39 UTC | 55 | export const uploadMedia = (file: File) => transport.uploadMedia(file); |
7ebc2472a9e3 | atagen | 2026-10-25 09:53 UTC | 56 | export const paginateRoom = (room_id: string, thread_root: string | null = null) => transport.paginateRoom(room_id, thread_root); |
824dfd38cdf3 | atagen | 2026-10-24 13:43 UTC | 57 | export const eventContext = (room_id: string, event_id: string, limit?: number): Promise<EventContext> => |
824dfd38cdf3 | atagen | 2026-10-24 13:43 UTC | 58 | transport.eventContext(room_id, event_id, limit); |
824dfd38cdf3 | atagen | 2026-10-24 13:43 UTC | 59 | export const eventContextBefore = (room_id: string, event_id: string, from: string, limit?: number): Promise<EventContextPage> => |
824dfd38cdf3 | atagen | 2026-10-24 13:43 UTC | 60 | transport.eventContextBefore(room_id, event_id, from, limit); |
824dfd38cdf3 | atagen | 2026-10-24 13:43 UTC | 61 | export const eventContextAfter = (room_id: string, event_id: string, from: string, limit?: number): Promise<EventContextPage> => |
824dfd38cdf3 | atagen | 2026-10-24 13:43 UTC | 62 | transport.eventContextAfter(room_id, event_id, from, limit); |
668ab5c30531 | atagen | 2026-10-25 03:13 UTC | 63 | export const mentionsPage = (cursor: string | null = null, limit = 50): Promise<MentionsPage> => transport.mentionsPage(cursor, limit); |
668ab5c30531 | atagen | 2026-10-25 03:13 UTC | 64 | export const mentionContext = (room_id: string, event_id: string): Promise<SavedMentionContext> => transport.mentionContext(room_id, event_id); |
668ab5c30531 | atagen | 2026-10-25 03:13 UTC | 65 | export const retryMentionContext = (room_id: string, event_id: string): Promise<SavedMentionContext> => transport.retryMentionContext(room_id, event_id); |
668ab5c30531 | atagen | 2026-10-25 03:13 UTC | 66 | export const setMentionsRead = (keys: MentionKey[], read: boolean) => transport.setMentionsRead(keys, read); |
668ab5c30531 | atagen | 2026-10-25 03:13 UTC | 67 | export const markMentionsReadThrough = (boundary: MentionsBoundary) => transport.markMentionsReadThrough(boundary); |
668ab5c30531 | atagen | 2026-10-25 03:13 UTC | 68 | export const recoverMentions = () => transport.recoverMentions(); |
c51e1fe62291 | atagen | 2026-10-25 07:58 UTC | 69 | export const startRecentHistory = (room_id: string): Promise<void> => transport.startRecentHistory(room_id); |
43c2cc753052 | atagen | 2026-10-25 07:50 UTC | 70 | export const fillHistoryGap = (room_id: string, gap_id: string): Promise<void> => transport.fillHistoryGap(room_id, gap_id); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 71 | export const setTyping = (room_id: string, typing: boolean) => transport.setTyping(room_id, typing); |
544a170ead2c | atagen | 2026-10-20 11:47 UTC | 72 | export const getAccountData = (eventType: string) => transport.getAccountData(eventType); |
ecb8598b38af | atagen | 2026-10-22 09:19 UTC | 73 | export const getUrlPreview = (room_id: string, url: string) => transport.getUrlPreview(room_id, url); |
544a170ead2c | atagen | 2026-10-20 11:47 UTC | 74 | export const setAccountData = (eventType: string, content: Record<string, unknown>) => |
544a170ead2c | atagen | 2026-10-20 11:47 UTC | 75 | transport.setAccountData(eventType, content); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 76 | |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 77 | export function joinRoom(room: string): Promise<{ room_id: string }> { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 78 | return transport.joinRoom(room); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 79 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 80 | |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 81 | export function createRoom(name: string, topic?: string): Promise<{ room_id: string }> { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 82 | return transport.createRoom(name, topic); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 83 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 84 | |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 85 | export const leaveRoom = (room_id: string) => transport.leaveRoom(room_id); |
7ebc2472a9e3 | atagen | 2026-10-25 09:53 UTC | 86 | export const reactToMessage = (room_id: string, event_id: string, key: string, shortcode?: string, thread_root: string | null = null) => |
7ebc2472a9e3 | atagen | 2026-10-25 09:53 UTC | 87 | transport.reactToMessage(room_id, event_id, key, shortcode, thread_root); |
7ebc2472a9e3 | atagen | 2026-10-25 09:53 UTC | 88 | export const editMessage = (room_id: string, event_id: string, body: string, mentions: Mention[] = [], emotes: Emote[] = [], thread_root: string | null = null) => |
7ebc2472a9e3 | atagen | 2026-10-25 09:53 UTC | 89 | transport.editMessage(room_id, event_id, body, formatBody(body, mentions, true, emotes), mentionedUserIds(mentions), thread_root); |
7ebc2472a9e3 | atagen | 2026-10-25 09:53 UTC | 90 | export const markRead = (room_id: string, event_id: string, thread_root: string | null = null) => transport.markRead(room_id, event_id, thread_root); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 91 | |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 92 | export function getMembers(room_id: string): Promise<{ members: MemberSummary[] }> { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 93 | return transport.getMembers(room_id); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 94 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 95 | |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 96 | /** Same URL shape in both builds: the hosted server and the device service worker both answer it. */ |
7643577c76bb | atagen | 2026-10-25 07:37 UTC | 97 | export function mediaUrl(mxc: string | null, size = 256): string | null { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 98 | if (!mxc) return null; |
7643577c76bb | atagen | 2026-10-25 07:37 UTC | 99 | return `/api/media?mxc=${encodeURIComponent(mxc)}&size=${Math.max(16, Math.min(512, Math.round(size)))}`; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 100 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 101 | |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 102 | export function messageMediaUrl(mediaId: string, size: 'Thumbnail' | 'Full'): string { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 103 | return `/api/message-media?media_id=${encodeURIComponent(mediaId)}&size=${size}`; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 104 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 105 | |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 106 | export function getProfile(): Promise<{ display_name: string | null; avatar_url: string | null }> { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 107 | return transport.getProfile(); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 108 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 109 | |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 110 | export function getSpaceChildren(room_id: string): Promise<{ room_ids: string[] }> { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 111 | return transport.getSpaceChildren(room_id); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 112 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 113 | |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 114 | export function getEncryption(): Promise<{ state: unknown; verification: unknown }> { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 115 | return transport.getEncryption(); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 116 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 117 | |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 118 | export function recoverEncryption(recovery_key: string): Promise<void> { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 119 | return transport.recoverEncryption(recovery_key); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 120 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 121 | |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 122 | export function startVerification(): Promise<void> { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 123 | return transport.startVerification(); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 124 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 125 | |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 126 | export function verifyEncryption(action: VerifyAction): Promise<void> { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 127 | return transport.verifyEncryption(action); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 128 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 129 | |
cf23dbf4e77c | atagen | 2026-10-23 06:36 UTC | 130 | export function uploadAttachment( |
cf23dbf4e77c | atagen | 2026-10-23 06:36 UTC | 131 | room_id: string, |
cf23dbf4e77c | atagen | 2026-10-23 06:36 UTC | 132 | file: File, |
cf23dbf4e77c | atagen | 2026-10-23 06:36 UTC | 133 | caption: string | null = null, |
cf23dbf4e77c | atagen | 2026-10-23 06:36 UTC | 134 | reply_to: string | null = null, |
6d6a4c34bdc1 | atagen | 2026-10-24 06:51 UTC | 135 | mentions: Mention[] = [], |
dda384a360e0 | atagen | 2026-10-25 10:27 UTC | 136 | emotes: Emote[] = [], |
dda384a360e0 | atagen | 2026-10-25 10:27 UTC | 137 | thread_root: string | null = null |
cf23dbf4e77c | atagen | 2026-10-23 06:36 UTC | 138 | ): Promise<void> { |
cf23dbf4e77c | atagen | 2026-10-23 06:36 UTC | 139 | return transport.uploadAttachment(room_id, file, { |
cf23dbf4e77c | atagen | 2026-10-23 06:36 UTC | 140 | caption, |
e822c659890f | atagen | 2026-10-25 02:37 UTC | 141 | formatted_caption: caption ? formatBody(caption, mentions, true, emotes) : null, |
cf23dbf4e77c | atagen | 2026-10-23 06:36 UTC | 142 | reply_to, |
dda384a360e0 | atagen | 2026-10-25 10:27 UTC | 143 | mentions: mentionedUserIds(mentions), |
dda384a360e0 | atagen | 2026-10-25 10:27 UTC | 144 | thread_root |
cf23dbf4e77c | atagen | 2026-10-23 06:36 UTC | 145 | }); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 146 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 147 | |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 148 | export function subscribeEvents(onEvent: (data: string) => void, onDisconnect: () => void): () => void { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 149 | return transport.subscribeEvents(onEvent, onDisconnect); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 150 | } |