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