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'; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 4 | import { HttpTransport } from './transport-http'; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 5 | import type { SessionInfo, Transport, VerifyAction } from './transport'; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 6 | import { createDeviceTransport } from 'malice:device-transport'; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 7 | |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 8 | export { ApiError } from './transport'; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 9 | export type { SessionInfo, SoftLogout, VerifyAction } from './transport'; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 10 | |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 11 | function createTransport(): Transport { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 12 | if (import.meta.env.VITE_MALICE_TARGET === 'device') return createDeviceTransport(); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 13 | return new HttpTransport(); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 14 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 15 | |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 16 | const transport: Transport = createTransport(); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 17 | |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 18 | export function getSession(): Promise<SessionInfo> { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 19 | return transport.getSession(); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 20 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 21 | |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 22 | export function login(homeserver: string, username: string, password: string): Promise<{ user_id: string }> { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 23 | return transport.login(homeserver, username, password); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 24 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 25 | |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 26 | export function logout(): Promise<void> { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 27 | return transport.logout(); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 28 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 29 | |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 30 | export function getRooms(): Promise<{ rooms: RoomSummary[] }> { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 31 | return transport.getRooms(); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 32 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 33 | |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 34 | export const openRoom = (room_id: string) => transport.openRoom(room_id); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 35 | export const closeRoom = (room_id: string) => transport.closeRoom(room_id); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 36 | export const sendMessage = (room_id: string, body: string) => transport.sendMessage(room_id, body); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 37 | export const paginateRoom = (room_id: string) => transport.paginateRoom(room_id); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 38 | export const setTyping = (room_id: string, typing: boolean) => transport.setTyping(room_id, typing); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 39 | |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 40 | export function joinRoom(room: string): Promise<{ room_id: string }> { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 41 | return transport.joinRoom(room); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 42 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 43 | |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 44 | export function createRoom(name: string, topic?: string): Promise<{ room_id: string }> { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 45 | return transport.createRoom(name, topic); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 46 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 47 | |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 48 | export const leaveRoom = (room_id: string) => transport.leaveRoom(room_id); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 49 | export const reactToMessage = (room_id: string, event_id: string, key: string) => |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 50 | transport.reactToMessage(room_id, event_id, key); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 51 | export const markRead = (room_id: string, event_id: string) => transport.markRead(room_id, event_id); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 52 | |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 53 | export function getMembers(room_id: string): Promise<{ members: MemberSummary[] }> { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 54 | return transport.getMembers(room_id); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 55 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 56 | |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 57 | /** 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 | 58 | export function mediaUrl(mxc: string | null): string | null { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 59 | if (!mxc) return null; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 60 | return `/api/media?mxc=${encodeURIComponent(mxc)}`; |
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 messageMediaUrl(mediaId: string, size: 'Thumbnail' | 'Full'): string { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 64 | return `/api/message-media?media_id=${encodeURIComponent(mediaId)}&size=${size}`; |
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 function getProfile(): Promise<{ display_name: string | null; avatar_url: string | null }> { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 68 | return transport.getProfile(); |
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 | export function getSpaceChildren(room_id: string): Promise<{ room_ids: string[] }> { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 72 | return transport.getSpaceChildren(room_id); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 73 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 74 | |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 75 | export function getEncryption(): Promise<{ state: unknown; verification: unknown }> { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 76 | return transport.getEncryption(); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 77 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 78 | |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 79 | export function recoverEncryption(recovery_key: string): Promise<void> { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 80 | return transport.recoverEncryption(recovery_key); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 81 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 82 | |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 83 | export function startVerification(): Promise<void> { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 84 | return transport.startVerification(); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 85 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 86 | |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 87 | export function verifyEncryption(action: VerifyAction): Promise<void> { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 88 | return transport.verifyEncryption(action); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 89 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 90 | |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 91 | export function uploadAttachment(room_id: string, file: File): Promise<void> { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 92 | return transport.uploadAttachment(room_id, file); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 93 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 94 | |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 95 | export function subscribeEvents(onEvent: (data: string) => void, onDisconnect: () => void): () => void { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 96 | return transport.subscribeEvents(onEvent, onDisconnect); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 97 | } |