atagen

malice

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