atagen

malice

public
Star0
Watch0
00075327fc57931e14ac095900712973885c5a8e
2
ssh://brewery@pls.lobotomise.me:33/atagen/malice.git

Blame: web/src/lib/api.ts

973f8fbdaf80atagen2026-09-28 09:10 UTC1
// Backend-agnostic API surface. Every call forwards to whichever transport
973f8fbdaf80atagen2026-09-28 09:10 UTC2
// this build was compiled for, hosted (fetch + SSE) or on-device (worker).
973f8fbdaf80atagen2026-09-28 09:10 UTC3
import type { MemberSummary, RoomSummary } from './types';
05a48e9e7942atagen2026-10-24 09:32 UTC4
import type { Emote } from './emotes';
6d6a4c34bdc1atagen2026-10-24 06:51 UTC5
import { formatBody } from './markdown';
cf23dbf4e77catagen2026-10-23 06:36 UTC6
import { mentionedUserIds, type Mention } from './mentions';
973f8fbdaf80atagen2026-09-28 09:10 UTC7
import { HttpTransport } from './transport-http';
7cca1d5628d1atagen2026-10-24 07:36 UTC8
import type { CommandRequest, SendKind, SessionInfo, Transport, VerifyAction } from './transport';
973f8fbdaf80atagen2026-09-28 09:10 UTC9
import { createDeviceTransport } from 'malice:device-transport';
973f8fbdaf80atagen2026-09-28 09:10 UTC10
973f8fbdaf80atagen2026-09-28 09:10 UTC11
export { ApiError } from './transport';
973f8fbdaf80atagen2026-09-28 09:10 UTC12
export type { SessionInfo, SoftLogout, VerifyAction } from './transport';
973f8fbdaf80atagen2026-09-28 09:10 UTC13
973f8fbdaf80atagen2026-09-28 09:10 UTC14
function createTransport(): Transport {
973f8fbdaf80atagen2026-09-28 09:10 UTC15
  if (import.meta.env.VITE_MALICE_TARGET === 'device') return createDeviceTransport();
973f8fbdaf80atagen2026-09-28 09:10 UTC16
  return new HttpTransport();
973f8fbdaf80atagen2026-09-28 09:10 UTC17
}
973f8fbdaf80atagen2026-09-28 09:10 UTC18
973f8fbdaf80atagen2026-09-28 09:10 UTC19
const transport: Transport = createTransport();
973f8fbdaf80atagen2026-09-28 09:10 UTC20
973f8fbdaf80atagen2026-09-28 09:10 UTC21
export function getSession(): Promise<SessionInfo> {
973f8fbdaf80atagen2026-09-28 09:10 UTC22
  return transport.getSession();
973f8fbdaf80atagen2026-09-28 09:10 UTC23
}
973f8fbdaf80atagen2026-09-28 09:10 UTC24
973f8fbdaf80atagen2026-09-28 09:10 UTC25
export function login(homeserver: string, username: string, password: string): Promise<{ user_id: string }> {
973f8fbdaf80atagen2026-09-28 09:10 UTC26
  return transport.login(homeserver, username, password);
973f8fbdaf80atagen2026-09-28 09:10 UTC27
}
973f8fbdaf80atagen2026-09-28 09:10 UTC28
973f8fbdaf80atagen2026-09-28 09:10 UTC29
export function logout(): Promise<void> {
973f8fbdaf80atagen2026-09-28 09:10 UTC30
  return transport.logout();
973f8fbdaf80atagen2026-09-28 09:10 UTC31
}
973f8fbdaf80atagen2026-09-28 09:10 UTC32
973f8fbdaf80atagen2026-09-28 09:10 UTC33
export function getRooms(): Promise<{ rooms: RoomSummary[] }> {
973f8fbdaf80atagen2026-09-28 09:10 UTC34
  return transport.getRooms();
973f8fbdaf80atagen2026-09-28 09:10 UTC35
}
973f8fbdaf80atagen2026-09-28 09:10 UTC36
973f8fbdaf80atagen2026-09-28 09:10 UTC37
export const openRoom = (room_id: string) => transport.openRoom(room_id);
973f8fbdaf80atagen2026-09-28 09:10 UTC38
export const closeRoom = (room_id: string) => transport.closeRoom(room_id);
6d6a4c34bdc1atagen2026-10-24 06:51 UTC39
export const sendMessage = (
6d6a4c34bdc1atagen2026-10-24 06:51 UTC40
  room_id: string,
6d6a4c34bdc1atagen2026-10-24 06:51 UTC41
  body: string,
6d6a4c34bdc1atagen2026-10-24 06:51 UTC42
  reply_to: string | null = null,
6d6a4c34bdc1atagen2026-10-24 06:51 UTC43
  mentions: Mention[] = [],
e4c64c36f105atagen2026-10-24 07:09 UTC44
  markdown = false,
05a48e9e7942atagen2026-10-24 09:32 UTC45
  kind: SendKind = 'text',
05a48e9e7942atagen2026-10-24 09:32 UTC46
  emotes: Emote[] = []
05a48e9e7942atagen2026-10-24 09:32 UTC47
) => transport.sendMessage(room_id, body, formatBody(body, mentions, markdown, emotes), reply_to, mentionedUserIds(mentions), kind);
e4c64c36f105atagen2026-10-24 07:09 UTC48
export const runCommand = (request: CommandRequest) => transport.command(request);
e4c64c36f105atagen2026-10-24 07:09 UTC49
export const uploadAvatar = (file: File) => transport.uploadAvatar(file);
7cca1d5628d1atagen2026-10-24 07:36 UTC50
export const uploadRoomAvatar = (room_id: string, file: File) => transport.uploadRoomAvatar(room_id, file);
d095131eec0eatagen2026-10-24 08:39 UTC51
export const uploadMedia = (file: File) => transport.uploadMedia(file);
973f8fbdaf80atagen2026-09-28 09:10 UTC52
export const paginateRoom = (room_id: string) => transport.paginateRoom(room_id);
973f8fbdaf80atagen2026-09-28 09:10 UTC53
export const setTyping = (room_id: string, typing: boolean) => transport.setTyping(room_id, typing);
544a170ead2catagen2026-10-20 11:47 UTC54
export const getAccountData = (eventType: string) => transport.getAccountData(eventType);
ecb8598b38afatagen2026-10-22 09:19 UTC55
export const getUrlPreview = (room_id: string, url: string) => transport.getUrlPreview(room_id, url);
544a170ead2catagen2026-10-20 11:47 UTC56
export const setAccountData = (eventType: string, content: Record<string, unknown>) =>
544a170ead2catagen2026-10-20 11:47 UTC57
  transport.setAccountData(eventType, content);
973f8fbdaf80atagen2026-09-28 09:10 UTC58
973f8fbdaf80atagen2026-09-28 09:10 UTC59
export function joinRoom(room: string): Promise<{ room_id: string }> {
973f8fbdaf80atagen2026-09-28 09:10 UTC60
  return transport.joinRoom(room);
973f8fbdaf80atagen2026-09-28 09:10 UTC61
}
973f8fbdaf80atagen2026-09-28 09:10 UTC62
973f8fbdaf80atagen2026-09-28 09:10 UTC63
export function createRoom(name: string, topic?: string): Promise<{ room_id: string }> {
973f8fbdaf80atagen2026-09-28 09:10 UTC64
  return transport.createRoom(name, topic);
973f8fbdaf80atagen2026-09-28 09:10 UTC65
}
973f8fbdaf80atagen2026-09-28 09:10 UTC66
973f8fbdaf80atagen2026-09-28 09:10 UTC67
export const leaveRoom = (room_id: string) => transport.leaveRoom(room_id);
ecb8598b38afatagen2026-10-22 09:19 UTC68
export const reactToMessage = (room_id: string, event_id: string, key: string, shortcode?: string) =>
ecb8598b38afatagen2026-10-22 09:19 UTC69
  transport.reactToMessage(room_id, event_id, key, shortcode);
05a48e9e7942atagen2026-10-24 09:32 UTC70
export const editMessage = (room_id: string, event_id: string, body: string, mentions: Mention[] = [], markdown = false, emotes: Emote[] = []) =>
05a48e9e7942atagen2026-10-24 09:32 UTC71
  transport.editMessage(room_id, event_id, body, formatBody(body, mentions, markdown, emotes), mentionedUserIds(mentions));
973f8fbdaf80atagen2026-09-28 09:10 UTC72
export const markRead = (room_id: string, event_id: string) => transport.markRead(room_id, event_id);
973f8fbdaf80atagen2026-09-28 09:10 UTC73
973f8fbdaf80atagen2026-09-28 09:10 UTC74
export function getMembers(room_id: string): Promise<{ members: MemberSummary[] }> {
973f8fbdaf80atagen2026-09-28 09:10 UTC75
  return transport.getMembers(room_id);
973f8fbdaf80atagen2026-09-28 09:10 UTC76
}
973f8fbdaf80atagen2026-09-28 09:10 UTC77
973f8fbdaf80atagen2026-09-28 09:10 UTC78
/** Same URL shape in both builds: the hosted server and the device service worker both answer it. */
973f8fbdaf80atagen2026-09-28 09:10 UTC79
export function mediaUrl(mxc: string | null): string | null {
973f8fbdaf80atagen2026-09-28 09:10 UTC80
  if (!mxc) return null;
973f8fbdaf80atagen2026-09-28 09:10 UTC81
  return `/api/media?mxc=${encodeURIComponent(mxc)}`;
973f8fbdaf80atagen2026-09-28 09:10 UTC82
}
973f8fbdaf80atagen2026-09-28 09:10 UTC83
973f8fbdaf80atagen2026-09-28 09:10 UTC84
export function messageMediaUrl(mediaId: string, size: 'Thumbnail' | 'Full'): string {
973f8fbdaf80atagen2026-09-28 09:10 UTC85
  return `/api/message-media?media_id=${encodeURIComponent(mediaId)}&size=${size}`;
973f8fbdaf80atagen2026-09-28 09:10 UTC86
}
973f8fbdaf80atagen2026-09-28 09:10 UTC87
973f8fbdaf80atagen2026-09-28 09:10 UTC88
export function getProfile(): Promise<{ display_name: string | null; avatar_url: string | null }> {
973f8fbdaf80atagen2026-09-28 09:10 UTC89
  return transport.getProfile();
973f8fbdaf80atagen2026-09-28 09:10 UTC90
}
973f8fbdaf80atagen2026-09-28 09:10 UTC91
973f8fbdaf80atagen2026-09-28 09:10 UTC92
export function getSpaceChildren(room_id: string): Promise<{ room_ids: string[] }> {
973f8fbdaf80atagen2026-09-28 09:10 UTC93
  return transport.getSpaceChildren(room_id);
973f8fbdaf80atagen2026-09-28 09:10 UTC94
}
973f8fbdaf80atagen2026-09-28 09:10 UTC95
973f8fbdaf80atagen2026-09-28 09:10 UTC96
export function getEncryption(): Promise<{ state: unknown; verification: unknown }> {
973f8fbdaf80atagen2026-09-28 09:10 UTC97
  return transport.getEncryption();
973f8fbdaf80atagen2026-09-28 09:10 UTC98
}
973f8fbdaf80atagen2026-09-28 09:10 UTC99
973f8fbdaf80atagen2026-09-28 09:10 UTC100
export function recoverEncryption(recovery_key: string): Promise<void> {
973f8fbdaf80atagen2026-09-28 09:10 UTC101
  return transport.recoverEncryption(recovery_key);
973f8fbdaf80atagen2026-09-28 09:10 UTC102
}
973f8fbdaf80atagen2026-09-28 09:10 UTC103
973f8fbdaf80atagen2026-09-28 09:10 UTC104
export function startVerification(): Promise<void> {
973f8fbdaf80atagen2026-09-28 09:10 UTC105
  return transport.startVerification();
973f8fbdaf80atagen2026-09-28 09:10 UTC106
}
973f8fbdaf80atagen2026-09-28 09:10 UTC107
973f8fbdaf80atagen2026-09-28 09:10 UTC108
export function verifyEncryption(action: VerifyAction): Promise<void> {
973f8fbdaf80atagen2026-09-28 09:10 UTC109
  return transport.verifyEncryption(action);
973f8fbdaf80atagen2026-09-28 09:10 UTC110
}
973f8fbdaf80atagen2026-09-28 09:10 UTC111
cf23dbf4e77catagen2026-10-23 06:36 UTC112
export function uploadAttachment(
cf23dbf4e77catagen2026-10-23 06:36 UTC113
  room_id: string,
cf23dbf4e77catagen2026-10-23 06:36 UTC114
  file: File,
cf23dbf4e77catagen2026-10-23 06:36 UTC115
  caption: string | null = null,
cf23dbf4e77catagen2026-10-23 06:36 UTC116
  reply_to: string | null = null,
6d6a4c34bdc1atagen2026-10-24 06:51 UTC117
  mentions: Mention[] = [],
05a48e9e7942atagen2026-10-24 09:32 UTC118
  markdown = false,
05a48e9e7942atagen2026-10-24 09:32 UTC119
  emotes: Emote[] = []
cf23dbf4e77catagen2026-10-23 06:36 UTC120
): Promise<void> {
cf23dbf4e77catagen2026-10-23 06:36 UTC121
  return transport.uploadAttachment(room_id, file, {
cf23dbf4e77catagen2026-10-23 06:36 UTC122
    caption,
05a48e9e7942atagen2026-10-24 09:32 UTC123
    formatted_caption: caption ? formatBody(caption, mentions, markdown, emotes) : null,
cf23dbf4e77catagen2026-10-23 06:36 UTC124
    reply_to,
cf23dbf4e77catagen2026-10-23 06:36 UTC125
    mentions: mentionedUserIds(mentions)
cf23dbf4e77catagen2026-10-23 06:36 UTC126
  });
973f8fbdaf80atagen2026-09-28 09:10 UTC127
}
973f8fbdaf80atagen2026-09-28 09:10 UTC128
973f8fbdaf80atagen2026-09-28 09:10 UTC129
export function subscribeEvents(onEvent: (data: string) => void, onDisconnect: () => void): () => void {
973f8fbdaf80atagen2026-09-28 09:10 UTC130
  return transport.subscribeEvents(onEvent, onDisconnect);
973f8fbdaf80atagen2026-09-28 09:10 UTC131
}