atagen

malice

public
Star0
Watch0
dffd63b58af439639de9f8e155d0365d83605a8b
2
ssh://brewery@pls.lobotomise.me:33/atagen/malice.git
web/src/lib/api.tsTypescript97 lines3.6 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 { 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
40
export function joinRoom(room: string): Promise<{ room_id: string }> {
41
  return transport.joinRoom(room);
42
}
43
44
export function createRoom(name: string, topic?: string): Promise<{ room_id: string }> {
45
  return transport.createRoom(name, topic);
46
}
47
48
export const leaveRoom = (room_id: string) => transport.leaveRoom(room_id);
49
export const reactToMessage = (room_id: string, event_id: string, key: string) =>
50
  transport.reactToMessage(room_id, event_id, key);
51
export const markRead = (room_id: string, event_id: string) => transport.markRead(room_id, event_id);
52
53
export function getMembers(room_id: string): Promise<{ members: MemberSummary[] }> {
54
  return transport.getMembers(room_id);
55
}
56
57
/** Same URL shape in both builds: the hosted server and the device service worker both answer it. */
58
export function mediaUrl(mxc: string | null): string | null {
59
  if (!mxc) return null;
60
  return `/api/media?mxc=${encodeURIComponent(mxc)}`;
61
}
62
63
export function messageMediaUrl(mediaId: string, size: 'Thumbnail' | 'Full'): string {
64
  return `/api/message-media?media_id=${encodeURIComponent(mediaId)}&size=${size}`;
65
}
66
67
export function getProfile(): Promise<{ display_name: string | null; avatar_url: string | null }> {
68
  return transport.getProfile();
69
}
70
71
export function getSpaceChildren(room_id: string): Promise<{ room_ids: string[] }> {
72
  return transport.getSpaceChildren(room_id);
73
}
74
75
export function getEncryption(): Promise<{ state: unknown; verification: unknown }> {
76
  return transport.getEncryption();
77
}
78
79
export function recoverEncryption(recovery_key: string): Promise<void> {
80
  return transport.recoverEncryption(recovery_key);
81
}
82
83
export function startVerification(): Promise<void> {
84
  return transport.startVerification();
85
}
86
87
export function verifyEncryption(action: VerifyAction): Promise<void> {
88
  return transport.verifyEncryption(action);
89
}
90
91
export function uploadAttachment(room_id: string, file: File): Promise<void> {
92
  return transport.uploadAttachment(room_id, file);
93
}
94
95
export function subscribeEvents(onEvent: (data: string) => void, onDisconnect: () => void): () => void {
96
  return transport.subscribeEvents(onEvent, onDisconnect);
97
}