| 1 | // Backend-agnostic API surface. Every call forwards to whichever transport |
| 2 | // this build was compiled for, hosted (fetch + SSE) or on-device (worker). |
| 3 | import type { MemberSummary, RoomSummary } from './types'; |
| 4 | import { HttpTransport } from './transport-http'; |
| 5 | import type { SessionInfo, Transport, VerifyAction } from './transport'; |
| 6 | import { createDeviceTransport } from 'malice:device-transport'; |
| 7 | |
| 8 | export { ApiError } from './transport'; |
| 9 | export type { SessionInfo, SoftLogout, VerifyAction } from './transport'; |
| 10 | |
| 11 | function createTransport(): Transport { |
| 12 | if (import.meta.env.VITE_MALICE_TARGET === 'device') return createDeviceTransport(); |
| 13 | return new HttpTransport(); |
| 14 | } |
| 15 | |
| 16 | const transport: Transport = createTransport(); |
| 17 | |
| 18 | export function getSession(): Promise<SessionInfo> { |
| 19 | return transport.getSession(); |
| 20 | } |
| 21 | |
| 22 | export function login(homeserver: string, username: string, password: string): Promise<{ user_id: string }> { |
| 23 | return transport.login(homeserver, username, password); |
| 24 | } |
| 25 | |
| 26 | export function logout(): Promise<void> { |
| 27 | return transport.logout(); |
| 28 | } |
| 29 | |
| 30 | export function getRooms(): Promise<{ rooms: RoomSummary[] }> { |
| 31 | return transport.getRooms(); |
| 32 | } |
| 33 | |
| 34 | export const openRoom = (room_id: string) => transport.openRoom(room_id); |
| 35 | export const closeRoom = (room_id: string) => transport.closeRoom(room_id); |
| 36 | export const sendMessage = (room_id: string, body: string) => transport.sendMessage(room_id, body); |
| 37 | export const paginateRoom = (room_id: string) => transport.paginateRoom(room_id); |
| 38 | export const setTyping = (room_id: string, typing: boolean) => transport.setTyping(room_id, typing); |
| 39 | export const getAccountData = (eventType: string) => transport.getAccountData(eventType); |
| 40 | export const getUrlPreview = (room_id: string, url: string) => transport.getUrlPreview(room_id, url); |
| 41 | export const setAccountData = (eventType: string, content: Record<string, unknown>) => |
| 42 | transport.setAccountData(eventType, content); |
| 43 | |
| 44 | export function joinRoom(room: string): Promise<{ room_id: string }> { |
| 45 | return transport.joinRoom(room); |
| 46 | } |
| 47 | |
| 48 | export function createRoom(name: string, topic?: string): Promise<{ room_id: string }> { |
| 49 | return transport.createRoom(name, topic); |
| 50 | } |
| 51 | |
| 52 | export const leaveRoom = (room_id: string) => transport.leaveRoom(room_id); |
| 53 | export const reactToMessage = (room_id: string, event_id: string, key: string, shortcode?: string) => |
| 54 | transport.reactToMessage(room_id, event_id, key, shortcode); |
| 55 | export const markRead = (room_id: string, event_id: string) => transport.markRead(room_id, event_id); |
| 56 | |
| 57 | export function getMembers(room_id: string): Promise<{ members: MemberSummary[] }> { |
| 58 | return transport.getMembers(room_id); |
| 59 | } |
| 60 | |
| 61 | /** Same URL shape in both builds: the hosted server and the device service worker both answer it. */ |
| 62 | export function mediaUrl(mxc: string | null): string | null { |
| 63 | if (!mxc) return null; |
| 64 | return `/api/media?mxc=${encodeURIComponent(mxc)}`; |
| 65 | } |
| 66 | |
| 67 | export function messageMediaUrl(mediaId: string, size: 'Thumbnail' | 'Full'): string { |
| 68 | return `/api/message-media?media_id=${encodeURIComponent(mediaId)}&size=${size}`; |
| 69 | } |
| 70 | |
| 71 | export function getProfile(): Promise<{ display_name: string | null; avatar_url: string | null }> { |
| 72 | return transport.getProfile(); |
| 73 | } |
| 74 | |
| 75 | export function getSpaceChildren(room_id: string): Promise<{ room_ids: string[] }> { |
| 76 | return transport.getSpaceChildren(room_id); |
| 77 | } |
| 78 | |
| 79 | export function getEncryption(): Promise<{ state: unknown; verification: unknown }> { |
| 80 | return transport.getEncryption(); |
| 81 | } |
| 82 | |
| 83 | export function recoverEncryption(recovery_key: string): Promise<void> { |
| 84 | return transport.recoverEncryption(recovery_key); |
| 85 | } |
| 86 | |
| 87 | export function startVerification(): Promise<void> { |
| 88 | return transport.startVerification(); |
| 89 | } |
| 90 | |
| 91 | export function verifyEncryption(action: VerifyAction): Promise<void> { |
| 92 | return transport.verifyEncryption(action); |
| 93 | } |
| 94 | |
| 95 | export function uploadAttachment(room_id: string, file: File): Promise<void> { |
| 96 | return transport.uploadAttachment(room_id, file); |
| 97 | } |
| 98 | |
| 99 | export function subscribeEvents(onEvent: (data: string) => void, onDisconnect: () => void): () => void { |
| 100 | return transport.subscribeEvents(onEvent, onDisconnect); |
| 101 | } |