atagen

malice

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