atagen

malice

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