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