atagen

malice

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