atagen

malice

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