atagen

malice

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