atagen

malice

public
Star0
Watch0
e15025643a2002d6ac85ce960505bdddc6bfe927
2
ssh://brewery@pls.lobotomise.me:33/atagen/malice.git
web/src/lib/api.tsTypescript117 lines4.7 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 { mentionedUserIds, type Mention } from './mentions';
6
import { HttpTransport } from './transport-http';
7
import type { 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 = (room_id: string, body: string, reply_to: string | null = null, mentions: Mention[] = []) =>
39
  transport.sendMessage(room_id, body, codeToHtml(body, mentions), reply_to, mentionedUserIds(mentions));
40
export const paginateRoom = (room_id: string) => transport.paginateRoom(room_id);
41
export const setTyping = (room_id: string, typing: boolean) => transport.setTyping(room_id, typing);
42
export const getAccountData = (eventType: string) => transport.getAccountData(eventType);
43
export const getUrlPreview = (room_id: string, url: string) => transport.getUrlPreview(room_id, url);
44
export const setAccountData = (eventType: string, content: Record<string, unknown>) =>
45
  transport.setAccountData(eventType, content);
46
47
export function joinRoom(room: string): Promise<{ room_id: string }> {
48
  return transport.joinRoom(room);
49
}
50
51
export function createRoom(name: string, topic?: string): Promise<{ room_id: string }> {
52
  return transport.createRoom(name, topic);
53
}
54
55
export const leaveRoom = (room_id: string) => transport.leaveRoom(room_id);
56
export const reactToMessage = (room_id: string, event_id: string, key: string, shortcode?: string) =>
57
  transport.reactToMessage(room_id, event_id, key, shortcode);
58
export const editMessage = (room_id: string, event_id: string, body: string, mentions: Mention[] = []) =>
59
  transport.editMessage(room_id, event_id, body, codeToHtml(body, mentions), mentionedUserIds(mentions));
60
export const markRead = (room_id: string, event_id: string) => transport.markRead(room_id, event_id);
61
62
export function getMembers(room_id: string): Promise<{ members: MemberSummary[] }> {
63
  return transport.getMembers(room_id);
64
}
65
66
/** Same URL shape in both builds: the hosted server and the device service worker both answer it. */
67
export function mediaUrl(mxc: string | null): string | null {
68
  if (!mxc) return null;
69
  return `/api/media?mxc=${encodeURIComponent(mxc)}`;
70
}
71
72
export function messageMediaUrl(mediaId: string, size: 'Thumbnail' | 'Full'): string {
73
  return `/api/message-media?media_id=${encodeURIComponent(mediaId)}&size=${size}`;
74
}
75
76
export function getProfile(): Promise<{ display_name: string | null; avatar_url: string | null }> {
77
  return transport.getProfile();
78
}
79
80
export function getSpaceChildren(room_id: string): Promise<{ room_ids: string[] }> {
81
  return transport.getSpaceChildren(room_id);
82
}
83
84
export function getEncryption(): Promise<{ state: unknown; verification: unknown }> {
85
  return transport.getEncryption();
86
}
87
88
export function recoverEncryption(recovery_key: string): Promise<void> {
89
  return transport.recoverEncryption(recovery_key);
90
}
91
92
export function startVerification(): Promise<void> {
93
  return transport.startVerification();
94
}
95
96
export function verifyEncryption(action: VerifyAction): Promise<void> {
97
  return transport.verifyEncryption(action);
98
}
99
100
export function uploadAttachment(
101
  room_id: string,
102
  file: File,
103
  caption: string | null = null,
104
  reply_to: string | null = null,
105
  mentions: Mention[] = []
106
): Promise<void> {
107
  return transport.uploadAttachment(room_id, file, {
108
    caption,
109
    formatted_caption: caption ? codeToHtml(caption, mentions) : null,
110
    reply_to,
111
    mentions: mentionedUserIds(mentions)
112
  });
113
}
114
115
export function subscribeEvents(onEvent: (data: string) => void, onDisconnect: () => void): () => void {
116
  return transport.subscribeEvents(onEvent, onDisconnect);
117
}