atagen

malice

public
Star0
Watch0
9f996e377406aff15037311e5ef2bb8bdc3efcfb
2
ssh://brewery@pls.lobotomise.me:33/atagen/malice.git
web/src/lib/api.tsTypescript156 lines8.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 { 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 historyRepairPolicy = (): Promise<HistoryRepairPolicy> => transport.historyRepairPolicy();
78
export const setHistoryRepairPolicy = (policy: HistoryRepairPolicy): Promise<void> => transport.setHistoryRepairPolicy(policy);
79
export const setTyping = (room_id: string, typing: boolean) => transport.setTyping(room_id, typing);
80
export const getAccountData = (eventType: string) => transport.getAccountData(eventType);
81
export const getUrlPreview = (room_id: string, url: string) => transport.getUrlPreview(room_id, url);
82
export const setAccountData = (eventType: string, content: Record<string, unknown>) =>
83
  transport.setAccountData(eventType, content);
84
85
export function joinRoom(room: string): Promise<{ room_id: string }> {
86
  return transport.joinRoom(room);
87
}
88
89
export function createRoom(name: string, topic?: string): Promise<{ room_id: string }> {
90
  return transport.createRoom(name, topic);
91
}
92
93
export const leaveRoom = (room_id: string) => transport.leaveRoom(room_id);
94
export const reactToMessage = (room_id: string, event_id: string, key: string, shortcode?: string) =>
95
  transport.reactToMessage(room_id, event_id, key, shortcode);
96
export const editMessage = (room_id: string, event_id: string, body: string, mentions: Mention[] = [], emotes: Emote[] = []) =>
97
  transport.editMessage(room_id, event_id, body, formatBody(body, mentions, true, emotes), mentionedUserIds(mentions));
98
export const markRead = (room_id: string, event_id: string) => transport.markRead(room_id, event_id);
99
100
export function getMembers(room_id: string): Promise<{ members: MemberSummary[] }> {
101
  return transport.getMembers(room_id);
102
}
103
104
/** Same URL shape in both builds: the hosted server and the device service worker both answer it. */
105
export function mediaUrl(mxc: string | null): string | null {
106
  if (!mxc) return null;
107
  return `/api/media?mxc=${encodeURIComponent(mxc)}`;
108
}
109
110
export function messageMediaUrl(mediaId: string, size: 'Thumbnail' | 'Full'): string {
111
  return `/api/message-media?media_id=${encodeURIComponent(mediaId)}&size=${size}`;
112
}
113
114
export function getProfile(): Promise<{ display_name: string | null; avatar_url: string | null }> {
115
  return transport.getProfile();
116
}
117
118
export function getSpaceChildren(room_id: string): Promise<{ room_ids: string[] }> {
119
  return transport.getSpaceChildren(room_id);
120
}
121
122
export function getEncryption(): Promise<{ state: unknown; verification: unknown }> {
123
  return transport.getEncryption();
124
}
125
126
export function recoverEncryption(recovery_key: string): Promise<void> {
127
  return transport.recoverEncryption(recovery_key);
128
}
129
130
export function startVerification(): Promise<void> {
131
  return transport.startVerification();
132
}
133
134
export function verifyEncryption(action: VerifyAction): Promise<void> {
135
  return transport.verifyEncryption(action);
136
}
137
138
export function uploadAttachment(
139
  room_id: string,
140
  file: File,
141
  caption: string | null = null,
142
  reply_to: string | null = null,
143
  mentions: Mention[] = [],
144
  emotes: Emote[] = []
145
): Promise<void> {
146
  return transport.uploadAttachment(room_id, file, {
147
    caption,
148
    formatted_caption: caption ? formatBody(caption, mentions, true, emotes) : null,
149
    reply_to,
150
    mentions: mentionedUserIds(mentions)
151
  });
152
}
153
154
export function subscribeEvents(onEvent: (data: string) => void, onDisconnect: () => void): () => void {
155
  return transport.subscribeEvents(onEvent, onDisconnect);
156
}