atagen

malice

public
Star0
Watch0
c04b1e46303fc98620efa4a4e18798510df98208
2
ssh://brewery@pls.lobotomise.me:33/atagen/malice.git
web/src/lib/api.tsTypescript131 lines5.3 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 type { Emote } from './emotes';
5
import { formatBody } from './markdown';
6
import { mentionedUserIds, type Mention } from './mentions';
7
import { HttpTransport } from './transport-http';
8
import type { CommandRequest, SendKind, SessionInfo, Transport, VerifyAction } from './transport';
9
import { createDeviceTransport } from 'malice:device-transport';
10
11
export { ApiError } from './transport';
12
export type { SessionInfo, SoftLogout, VerifyAction } from './transport';
13
14
function createTransport(): Transport {
15
  if (import.meta.env.VITE_MALICE_TARGET === 'device') return createDeviceTransport();
16
  return new HttpTransport();
17
}
18
19
const transport: Transport = createTransport();
20
21
export function getSession(): Promise<SessionInfo> {
22
  return transport.getSession();
23
}
24
25
export function login(homeserver: string, username: string, password: string): Promise<{ user_id: string }> {
26
  return transport.login(homeserver, username, password);
27
}
28
29
export function logout(): Promise<void> {
30
  return transport.logout();
31
}
32
33
export function getRooms(): Promise<{ rooms: RoomSummary[] }> {
34
  return transport.getRooms();
35
}
36
37
export const openRoom = (room_id: string) => transport.openRoom(room_id);
38
export const closeRoom = (room_id: string) => transport.closeRoom(room_id);
39
export const sendMessage = (
40
  room_id: string,
41
  body: string,
42
  reply_to: string | null = null,
43
  mentions: Mention[] = [],
44
  markdown = false,
45
  kind: SendKind = 'text',
46
  emotes: Emote[] = []
47
) => transport.sendMessage(room_id, body, formatBody(body, mentions, markdown, emotes), reply_to, mentionedUserIds(mentions), kind);
48
export const runCommand = (request: CommandRequest) => transport.command(request);
49
export const uploadAvatar = (file: File) => transport.uploadAvatar(file);
50
export const uploadRoomAvatar = (room_id: string, file: File) => transport.uploadRoomAvatar(room_id, file);
51
export const uploadMedia = (file: File) => transport.uploadMedia(file);
52
export const paginateRoom = (room_id: string) => transport.paginateRoom(room_id);
53
export const setTyping = (room_id: string, typing: boolean) => transport.setTyping(room_id, typing);
54
export const getAccountData = (eventType: string) => transport.getAccountData(eventType);
55
export const getUrlPreview = (room_id: string, url: string) => transport.getUrlPreview(room_id, url);
56
export const setAccountData = (eventType: string, content: Record<string, unknown>) =>
57
  transport.setAccountData(eventType, content);
58
59
export function joinRoom(room: string): Promise<{ room_id: string }> {
60
  return transport.joinRoom(room);
61
}
62
63
export function createRoom(name: string, topic?: string): Promise<{ room_id: string }> {
64
  return transport.createRoom(name, topic);
65
}
66
67
export const leaveRoom = (room_id: string) => transport.leaveRoom(room_id);
68
export const reactToMessage = (room_id: string, event_id: string, key: string, shortcode?: string) =>
69
  transport.reactToMessage(room_id, event_id, key, shortcode);
70
export const editMessage = (room_id: string, event_id: string, body: string, mentions: Mention[] = [], markdown = false, emotes: Emote[] = []) =>
71
  transport.editMessage(room_id, event_id, body, formatBody(body, mentions, markdown, emotes), mentionedUserIds(mentions));
72
export const markRead = (room_id: string, event_id: string) => transport.markRead(room_id, event_id);
73
74
export function getMembers(room_id: string): Promise<{ members: MemberSummary[] }> {
75
  return transport.getMembers(room_id);
76
}
77
78
/** Same URL shape in both builds: the hosted server and the device service worker both answer it. */
79
export function mediaUrl(mxc: string | null): string | null {
80
  if (!mxc) return null;
81
  return `/api/media?mxc=${encodeURIComponent(mxc)}`;
82
}
83
84
export function messageMediaUrl(mediaId: string, size: 'Thumbnail' | 'Full'): string {
85
  return `/api/message-media?media_id=${encodeURIComponent(mediaId)}&size=${size}`;
86
}
87
88
export function getProfile(): Promise<{ display_name: string | null; avatar_url: string | null }> {
89
  return transport.getProfile();
90
}
91
92
export function getSpaceChildren(room_id: string): Promise<{ room_ids: string[] }> {
93
  return transport.getSpaceChildren(room_id);
94
}
95
96
export function getEncryption(): Promise<{ state: unknown; verification: unknown }> {
97
  return transport.getEncryption();
98
}
99
100
export function recoverEncryption(recovery_key: string): Promise<void> {
101
  return transport.recoverEncryption(recovery_key);
102
}
103
104
export function startVerification(): Promise<void> {
105
  return transport.startVerification();
106
}
107
108
export function verifyEncryption(action: VerifyAction): Promise<void> {
109
  return transport.verifyEncryption(action);
110
}
111
112
export function uploadAttachment(
113
  room_id: string,
114
  file: File,
115
  caption: string | null = null,
116
  reply_to: string | null = null,
117
  mentions: Mention[] = [],
118
  markdown = false,
119
  emotes: Emote[] = []
120
): Promise<void> {
121
  return transport.uploadAttachment(room_id, file, {
122
    caption,
123
    formatted_caption: caption ? formatBody(caption, mentions, markdown, emotes) : null,
124
    reply_to,
125
    mentions: mentionedUserIds(mentions)
126
  });
127
}
128
129
export function subscribeEvents(onEvent: (data: string) => void, onDisconnect: () => void): () => void {
130
  return transport.subscribeEvents(onEvent, onDisconnect);
131
}