atagen

malice

public
Star0
Watch0
8e8f1cef327b55432644ae1b843f39ca20e91609
2
ssh://brewery@pls.lobotomise.me:33/atagen/malice.git
web/src/lib/api.tsTypescript147 lines7.2 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
) => transport.sendMessage(room_id, body, formatBody(body, mentions, true, emotes), reply_to, mentionedUserIds(mentions), kind);
51
export const runCommand = (request: CommandRequest) => transport.command(request);
52
export const uploadAvatar = (file: File) => transport.uploadAvatar(file);
53
export const uploadRoomAvatar = (room_id: string, file: File) => transport.uploadRoomAvatar(room_id, file);
54
export const uploadMedia = (file: File) => transport.uploadMedia(file);
55
export const paginateRoom = (room_id: string) => transport.paginateRoom(room_id);
56
export const eventContext = (room_id: string, event_id: string, limit?: number): Promise<EventContext> =>
57
  transport.eventContext(room_id, event_id, limit);
58
export const eventContextBefore = (room_id: string, event_id: string, from: string, limit?: number): Promise<EventContextPage> =>
59
  transport.eventContextBefore(room_id, event_id, from, limit);
60
export const eventContextAfter = (room_id: string, event_id: string, from: string, limit?: number): Promise<EventContextPage> =>
61
  transport.eventContextAfter(room_id, event_id, from, limit);
62
export const mentionsPage = (cursor: string | null = null, limit = 50): Promise<MentionsPage> => transport.mentionsPage(cursor, limit);
63
export const mentionContext = (room_id: string, event_id: string): Promise<SavedMentionContext> => transport.mentionContext(room_id, event_id);
64
export const retryMentionContext = (room_id: string, event_id: string): Promise<SavedMentionContext> => transport.retryMentionContext(room_id, event_id);
65
export const setMentionsRead = (keys: MentionKey[], read: boolean) => transport.setMentionsRead(keys, read);
66
export const markMentionsReadThrough = (boundary: MentionsBoundary) => transport.markMentionsReadThrough(boundary);
67
export const recoverMentions = () => transport.recoverMentions();
68
export const startRecentHistory = (room_id: string): Promise<void> => transport.startRecentHistory(room_id);
69
export const fillHistoryGap = (room_id: string, gap_id: string): Promise<void> => transport.fillHistoryGap(room_id, gap_id);
70
export const setTyping = (room_id: string, typing: boolean) => transport.setTyping(room_id, typing);
71
export const getAccountData = (eventType: string) => transport.getAccountData(eventType);
72
export const getUrlPreview = (room_id: string, url: string) => transport.getUrlPreview(room_id, url);
73
export const setAccountData = (eventType: string, content: Record<string, unknown>) =>
74
  transport.setAccountData(eventType, content);
75
76
export function joinRoom(room: string): Promise<{ room_id: string }> {
77
  return transport.joinRoom(room);
78
}
79
80
export function createRoom(name: string, topic?: string): Promise<{ room_id: string }> {
81
  return transport.createRoom(name, topic);
82
}
83
84
export const leaveRoom = (room_id: string) => transport.leaveRoom(room_id);
85
export const reactToMessage = (room_id: string, event_id: string, key: string, shortcode?: string) =>
86
  transport.reactToMessage(room_id, event_id, key, shortcode);
87
export const editMessage = (room_id: string, event_id: string, body: string, mentions: Mention[] = [], emotes: Emote[] = []) =>
88
  transport.editMessage(room_id, event_id, body, formatBody(body, mentions, true, emotes), mentionedUserIds(mentions));
89
export const markRead = (room_id: string, event_id: string) => transport.markRead(room_id, event_id);
90
91
export function getMembers(room_id: string): Promise<{ members: MemberSummary[] }> {
92
  return transport.getMembers(room_id);
93
}
94
95
/** Same URL shape in both builds: the hosted server and the device service worker both answer it. */
96
export function mediaUrl(mxc: string | null, size = 256): string | null {
97
  if (!mxc) return null;
98
  return `/api/media?mxc=${encodeURIComponent(mxc)}&size=${Math.max(16, Math.min(512, Math.round(size)))}`;
99
}
100
101
export function messageMediaUrl(mediaId: string, size: 'Thumbnail' | 'Full'): string {
102
  return `/api/message-media?media_id=${encodeURIComponent(mediaId)}&size=${size}`;
103
}
104
105
export function getProfile(): Promise<{ display_name: string | null; avatar_url: string | null }> {
106
  return transport.getProfile();
107
}
108
109
export function getSpaceChildren(room_id: string): Promise<{ room_ids: string[] }> {
110
  return transport.getSpaceChildren(room_id);
111
}
112
113
export function getEncryption(): Promise<{ state: unknown; verification: unknown }> {
114
  return transport.getEncryption();
115
}
116
117
export function recoverEncryption(recovery_key: string): Promise<void> {
118
  return transport.recoverEncryption(recovery_key);
119
}
120
121
export function startVerification(): Promise<void> {
122
  return transport.startVerification();
123
}
124
125
export function verifyEncryption(action: VerifyAction): Promise<void> {
126
  return transport.verifyEncryption(action);
127
}
128
129
export function uploadAttachment(
130
  room_id: string,
131
  file: File,
132
  caption: string | null = null,
133
  reply_to: string | null = null,
134
  mentions: Mention[] = [],
135
  emotes: Emote[] = []
136
): Promise<void> {
137
  return transport.uploadAttachment(room_id, file, {
138
    caption,
139
    formatted_caption: caption ? formatBody(caption, mentions, true, emotes) : null,
140
    reply_to,
141
    mentions: mentionedUserIds(mentions)
142
  });
143
}
144
145
export function subscribeEvents(onEvent: (data: string) => void, onDisconnect: () => void): () => void {
146
  return transport.subscribeEvents(onEvent, onDisconnect);
147
}