atagen

malice

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