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