atagen

malice

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