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). |
f7069be9f61e | atagen | 2026-10-24 13:05 UTC | 3 | import type { MemberSummary, RoomSummary } from './types'; |
05a48e9e7942 | atagen | 2026-10-24 09:32 UTC | 4 | import type { Emote } from './emotes'; |
6d6a4c34bdc1 | atagen | 2026-10-24 06:51 UTC | 5 | import { formatBody } from './markdown'; |
cf23dbf4e77c | atagen | 2026-10-23 06:36 UTC | 6 | import { mentionedUserIds, type Mention } from './mentions'; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 7 | import { HttpTransport } from './transport-http'; |
7cca1d5628d1 | atagen | 2026-10-24 07:36 UTC | 8 | import type { CommandRequest, SendKind, SessionInfo, Transport, VerifyAction } from './transport'; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 9 | import { createDeviceTransport } from 'malice:device-transport'; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 10 | |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 11 | export { ApiError } from './transport'; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 12 | export type { SessionInfo, SoftLogout, VerifyAction } from './transport'; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 13 | |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 14 | function createTransport(): Transport { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 15 | if (import.meta.env.VITE_MALICE_TARGET === 'device') return createDeviceTransport(); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 16 | return new HttpTransport(); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 17 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 18 | |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 19 | const transport: Transport = createTransport(); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 20 | |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 21 | export function getSession(): Promise<SessionInfo> { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 22 | return transport.getSession(); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 23 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 24 | |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 25 | export function login(homeserver: string, username: string, password: string): Promise<{ user_id: string }> { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 26 | return transport.login(homeserver, username, password); |
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 logout(): Promise<void> { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 30 | return transport.logout(); |
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 getRooms(): Promise<{ rooms: RoomSummary[] }> { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 34 | return transport.getRooms(); |
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 const openRoom = (room_id: string) => transport.openRoom(room_id); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 38 | export const closeRoom = (room_id: string) => transport.closeRoom(room_id); |
6d6a4c34bdc1 | atagen | 2026-10-24 06:51 UTC | 39 | export const sendMessage = ( |
6d6a4c34bdc1 | atagen | 2026-10-24 06:51 UTC | 40 | room_id: string, |
6d6a4c34bdc1 | atagen | 2026-10-24 06:51 UTC | 41 | body: string, |
6d6a4c34bdc1 | atagen | 2026-10-24 06:51 UTC | 42 | reply_to: string | null = null, |
6d6a4c34bdc1 | atagen | 2026-10-24 06:51 UTC | 43 | mentions: Mention[] = [], |
e4c64c36f105 | atagen | 2026-10-24 07:09 UTC | 44 | markdown = false, |
05a48e9e7942 | atagen | 2026-10-24 09:32 UTC | 45 | kind: SendKind = 'text', |
05a48e9e7942 | atagen | 2026-10-24 09:32 UTC | 46 | emotes: Emote[] = [] |
05a48e9e7942 | atagen | 2026-10-24 09:32 UTC | 47 | ) => transport.sendMessage(room_id, body, formatBody(body, mentions, markdown, emotes), reply_to, mentionedUserIds(mentions), kind); |
e4c64c36f105 | atagen | 2026-10-24 07:09 UTC | 48 | export const runCommand = (request: CommandRequest) => transport.command(request); |
e4c64c36f105 | atagen | 2026-10-24 07:09 UTC | 49 | export const uploadAvatar = (file: File) => transport.uploadAvatar(file); |
7cca1d5628d1 | atagen | 2026-10-24 07:36 UTC | 50 | export const uploadRoomAvatar = (room_id: string, file: File) => transport.uploadRoomAvatar(room_id, file); |
d095131eec0e | atagen | 2026-10-24 08:39 UTC | 51 | export const uploadMedia = (file: File) => transport.uploadMedia(file); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 52 | export const paginateRoom = (room_id: string) => transport.paginateRoom(room_id); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 53 | export const setTyping = (room_id: string, typing: boolean) => transport.setTyping(room_id, typing); |
544a170ead2c | atagen | 2026-10-20 11:47 UTC | 54 | export const getAccountData = (eventType: string) => transport.getAccountData(eventType); |
ecb8598b38af | atagen | 2026-10-22 09:19 UTC | 55 | export const getUrlPreview = (room_id: string, url: string) => transport.getUrlPreview(room_id, url); |
544a170ead2c | atagen | 2026-10-20 11:47 UTC | 56 | export const setAccountData = (eventType: string, content: Record<string, unknown>) => |
544a170ead2c | atagen | 2026-10-20 11:47 UTC | 57 | transport.setAccountData(eventType, content); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 58 | |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 59 | export function joinRoom(room: string): Promise<{ room_id: string }> { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 60 | return transport.joinRoom(room); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 61 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 62 | |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 63 | export function createRoom(name: string, topic?: string): Promise<{ room_id: string }> { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 64 | return transport.createRoom(name, topic); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 65 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 66 | |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 67 | export const leaveRoom = (room_id: string) => transport.leaveRoom(room_id); |
ecb8598b38af | atagen | 2026-10-22 09:19 UTC | 68 | export const reactToMessage = (room_id: string, event_id: string, key: string, shortcode?: string) => |
ecb8598b38af | atagen | 2026-10-22 09:19 UTC | 69 | transport.reactToMessage(room_id, event_id, key, shortcode); |
05a48e9e7942 | atagen | 2026-10-24 09:32 UTC | 70 | export const editMessage = (room_id: string, event_id: string, body: string, mentions: Mention[] = [], markdown = false, emotes: Emote[] = []) => |
05a48e9e7942 | atagen | 2026-10-24 09:32 UTC | 71 | transport.editMessage(room_id, event_id, body, formatBody(body, mentions, markdown, emotes), mentionedUserIds(mentions)); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 72 | export const markRead = (room_id: string, event_id: string) => transport.markRead(room_id, event_id); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 73 | |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 74 | export function getMembers(room_id: string): Promise<{ members: MemberSummary[] }> { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 75 | return transport.getMembers(room_id); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 76 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 77 | |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 78 | /** Same URL shape in both builds: the hosted server and the device service worker both answer it. */ |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 79 | export function mediaUrl(mxc: string | null): string | null { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 80 | if (!mxc) return null; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 81 | return `/api/media?mxc=${encodeURIComponent(mxc)}`; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 82 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 83 | |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 84 | export function messageMediaUrl(mediaId: string, size: 'Thumbnail' | 'Full'): string { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 85 | return `/api/message-media?media_id=${encodeURIComponent(mediaId)}&size=${size}`; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 86 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 87 | |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 88 | export function getProfile(): Promise<{ display_name: string | null; avatar_url: string | null }> { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 89 | return transport.getProfile(); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 90 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 91 | |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 92 | export function getSpaceChildren(room_id: string): Promise<{ room_ids: string[] }> { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 93 | return transport.getSpaceChildren(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 | export function getEncryption(): Promise<{ state: unknown; verification: unknown }> { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 97 | return transport.getEncryption(); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 98 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 99 | |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 100 | export function recoverEncryption(recovery_key: string): Promise<void> { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 101 | return transport.recoverEncryption(recovery_key); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 102 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 103 | |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 104 | export function startVerification(): Promise<void> { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 105 | return transport.startVerification(); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 106 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 107 | |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 108 | export function verifyEncryption(action: VerifyAction): Promise<void> { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 109 | return transport.verifyEncryption(action); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 110 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 111 | |
cf23dbf4e77c | atagen | 2026-10-23 06:36 UTC | 112 | export function uploadAttachment( |
cf23dbf4e77c | atagen | 2026-10-23 06:36 UTC | 113 | room_id: string, |
cf23dbf4e77c | atagen | 2026-10-23 06:36 UTC | 114 | file: File, |
cf23dbf4e77c | atagen | 2026-10-23 06:36 UTC | 115 | caption: string | null = null, |
cf23dbf4e77c | atagen | 2026-10-23 06:36 UTC | 116 | reply_to: string | null = null, |
6d6a4c34bdc1 | atagen | 2026-10-24 06:51 UTC | 117 | mentions: Mention[] = [], |
05a48e9e7942 | atagen | 2026-10-24 09:32 UTC | 118 | markdown = false, |
05a48e9e7942 | atagen | 2026-10-24 09:32 UTC | 119 | emotes: Emote[] = [] |
cf23dbf4e77c | atagen | 2026-10-23 06:36 UTC | 120 | ): Promise<void> { |
cf23dbf4e77c | atagen | 2026-10-23 06:36 UTC | 121 | return transport.uploadAttachment(room_id, file, { |
cf23dbf4e77c | atagen | 2026-10-23 06:36 UTC | 122 | caption, |
05a48e9e7942 | atagen | 2026-10-24 09:32 UTC | 123 | formatted_caption: caption ? formatBody(caption, mentions, markdown, emotes) : null, |
cf23dbf4e77c | atagen | 2026-10-23 06:36 UTC | 124 | reply_to, |
cf23dbf4e77c | atagen | 2026-10-23 06:36 UTC | 125 | mentions: mentionedUserIds(mentions) |
cf23dbf4e77c | atagen | 2026-10-23 06:36 UTC | 126 | }); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 127 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 128 | |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 129 | export function subscribeEvents(onEvent: (data: string) => void, onDisconnect: () => void): () => void { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 130 | return transport.subscribeEvents(onEvent, onDisconnect); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 131 | } |