atagen

malice

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