import { request } from "$lib/api/client"; import type { Account, Alert, Annotation, AvatarHistoryView, CallbackView, CaptureToggles, Chat, ChatLinkView, DayCount, DiscoverItem, Folder, JobStatus, JobView, LinkView, LoginState, MediaVersion, MediaView, MessageAt, MessageVersion, MessageView, PeerHistoryView, PeerView, PinnedView, PolicyChatKind, PolicyCreate, PolicyRecord, PresenceHourly, PresenceSample, ReactionView, ResponseStats, SearchHit, StoryView, VolumeBucket, Watch, } from "$lib/api/types"; import { accounts } from "$lib/stores/accounts.svelte"; interface Page { limit?: number; offset?: number; } export function listAccounts(): Promise { return request("/accounts"); } export function startLogin(phone: string): Promise { return request("/accounts/login", { method: "POST", body: { phone }, }); } export function startQrLogin(): Promise { return request("/accounts/login/qr", { method: "POST" }); } export function pollQrLogin(loginId: string): Promise { return request(`/accounts/login/${loginId}/qr`); } export function submitLoginCode( loginId: string, code: string ): Promise { return request(`/accounts/login/${loginId}/code`, { method: "POST", body: { code }, }); } export function submitLoginPassword( loginId: string, password: string ): Promise { return request(`/accounts/login/${loginId}/password`, { method: "POST", body: { password }, }); } export function cancelLogin(loginId: string): Promise { return request(`/accounts/login/${loginId}`, { method: "DELETE" }); } export function renameAccountDevice( accountId: number, deviceModel: string ): Promise { return request(`/accounts/${accountId}/device`, { method: "PATCH", body: { device_model: deviceModel }, }); } export function logoutAccount(accountId: number): Promise { return request(`/accounts/${accountId}`, { method: "DELETE" }); } interface ChatPage extends Page { folder_id?: number; search?: string; } export function listChats(page: ChatPage = {}): Promise { return request("/chats", { account: true, query: { ...page } }); } export function getChat(chatId: number): Promise { return request(`/chats/${chatId}`, { account: true }); } export function listFolders(): Promise { return request("/folders", { account: true }); } export function listPolicies(): Promise { return request("/policy", { account: true }); } export function createPolicy(body: PolicyCreate): Promise { return request("/policy", { method: "POST", body: { ...body, account_id: accounts.selectedId }, }); } export function updatePolicy( id: number, toggles: CaptureToggles ): Promise { return request(`/policy/${id}`, { method: "PUT", account: true, body: toggles, }); } export function deletePolicy(id: number): Promise { return request(`/policy/${id}`, { method: "DELETE" }); } export function effectivePolicy(query: { chat_id: number; is_bot?: boolean; is_contact?: boolean | null; kind: PolicyChatKind; }): Promise { return request("/policy/effective", { account: true, query, }); } export function listMessages( chatId: number, options: Page & { include_deleted?: boolean; before_id?: number; after_id?: number; } = {} ): Promise { return request(`/chats/${chatId}/messages`, { account: true, query: { ...options }, }); } export function getCurrentPresence( peerId: number ): Promise { return request("/presence/current", { account: true, query: { peer_id: peerId }, }); } export function getPresenceHistory( peerId: number, page: Page = {} ): Promise { return request("/presence", { account: true, query: { peer_id: peerId, ...page }, }); } export function getPresenceHourly(peerId: number): Promise { return request("/presence/hourly", { account: true, query: { peer_id: peerId }, }); } export function getMessageVolume( chatId: number, days = 90 ): Promise { return request("/analytics/volume", { account: true, query: { chat_id: chatId, days }, }); } export function getResponseStats(chatId: number): Promise { return request("/analytics/response-time", { account: true, query: { chat_id: chatId }, }); } export function getPinned(chatId: number): Promise { return request(`/chats/${chatId}/pinned`, { account: true, }); } export function listMessageVersions( chatId: number, messageId: number ): Promise { return request( `/chats/${chatId}/messages/${messageId}/versions`, { account: true } ); } export function listDeleted( options: Page & { chat_id?: number } = {} ): Promise { return request("/deleted", { account: true, query: { ...options }, }); } export function getPeer(peerId: number): Promise { return request(`/peers/${peerId}`, { account: true }); } export function getPeerHistory(peerId: number): Promise { return request(`/peers/${peerId}/history`, { account: true, }); } export function getAvatarHistory( kind: "peer" | "chat", id: number ): Promise { return request(`/avatars/${kind}/${id}/history`, { account: true, }); } export function getChatMedia( chatId: number, kinds: string[], page: { limit?: number; offset?: number } = {} ): Promise { return request(`/chats/${chatId}/media`, { account: true, query: { kinds: kinds.join(","), ...page }, }); } export function getChatLinks( chatId: number, page: { limit?: number; offset?: number } = {} ): Promise { return request(`/chats/${chatId}/links`, { account: true, query: { ...page }, }); } export function getMessageReactions( chatId: number, messageId: number ): Promise { return request(`/messages/${chatId}/${messageId}/reactions`, { account: true, }); } export function getMessageCallbacks( chatId: number, messageId: number ): Promise { return request(`/messages/${chatId}/${messageId}/callbacks`, { account: true, }); } export function getMessageLinks( chatId: number, messageId: number ): Promise { return request(`/messages/${chatId}/${messageId}/links`, { account: true, }); } export function getChatCalendar(chatId: number): Promise { return request(`/chats/${chatId}/calendar`, { account: true }); } export function getMessageAt(chatId: number, date: string): Promise { return request(`/chats/${chatId}/message-at`, { account: true, query: { date }, }); } export function getStories( peerId: number | null, page: Page = {} ): Promise { return request("/stories", { account: true, query: { peer_id: peerId, ...page }, }); } export function getPeers(ids: number[]): Promise { if (ids.length === 0) { return Promise.resolve([]); } return request("/peers/batch", { account: true, query: { ids: ids.join(",") }, }); } export function enrichChat(chatId: number): Promise<{ job_id: number }> { return request<{ job_id: number }>(`/chats/${chatId}/enrich`, { method: "POST", body: { account_id: accounts.selectedId }, }); } export function getJob(jobId: number): Promise { return request(`/jobs/${jobId}`, { account: true }); } export function cancelJob(jobId: number): Promise { return request(`/jobs/${jobId}/cancel`, { method: "POST" }); } export function listJobs(status?: JobStatus): Promise { return request("/jobs", { account: true, query: status ? { status } : {}, }); } export function enqueueBackfill( chatId: number, media: boolean, full = false ): Promise<{ job_id: number }> { return request<{ job_id: number }>("/backfill", { method: "POST", body: { account_id: accounts.selectedId, chat_id: chatId, media, full }, }); } export function enqueueStoriesBackfill( peerId: number ): Promise<{ job_id: number }> { return request<{ job_id: number }>("/stories/backfill", { method: "POST", body: { account_id: accounts.selectedId, peer_id: peerId }, }); } export function discoverPeers( query: string, remote = false ): Promise { return request("/discover", { account: true, query: { query, remote }, }); } export function getDiscoverItem(chatId: number): Promise { return request(`/discover/${chatId}`, { account: true }); } export function trackChat( chatId: number, backfill = true ): Promise { return request(`/chats/${chatId}/track`, { method: "POST", body: { account_id: accounts.selectedId, backfill }, }); } export function syncContacts(): Promise<{ job_id: number }> { return request<{ job_id: number }>("/contacts/sync", { method: "POST", body: { account_id: accounts.selectedId }, }); } export function syncDialogs(): Promise<{ job_id: number }> { return request<{ job_id: number }>("/dialogs/sync", { method: "POST", body: { account_id: accounts.selectedId }, }); } export function getMediaVersions( chatId: number, messageId: number ): Promise { return request(`/media/versions/${chatId}/${messageId}`, { account: true, }); } export function getMediaMeta(mediaId: number): Promise { return request(`/media/${mediaId}/meta`); } export function getMessageMedia( chatId: number, messageId: number ): Promise { return request(`/media/message/${chatId}/${messageId}`, { account: true, }); } export function searchMessages( query: string, options: Page & { chat_id?: number } = {} ): Promise { return request("/search", { account: true, query: { query, ...options }, }); } export function fetchMedia( chatId: number, messageId: number ): Promise<{ job_id: number }> { return request<{ job_id: number }>("/media/fetch", { method: "POST", body: { account_id: accounts.selectedId, chat_id: chatId, message_id: messageId, }, }); } export function transcribeMedia( chatId: number, messageId: number ): Promise<{ job_id: number }> { return request<{ job_id: number }>("/media/transcribe", { method: "POST", body: { account_id: accounts.selectedId, chat_id: chatId, message_id: messageId, }, }); } export function listWatches(): Promise { return request("/watches", { account: true }); } export function createWatch( kind: string, params: Record, enabled: boolean ): Promise { return request("/watches", { method: "POST", body: { account_id: accounts.selectedId, kind, params, enabled }, }); } export function updateWatch( id: number, params: Record, enabled: boolean ): Promise { return request(`/watches/${id}`, { method: "PUT", body: { params, enabled }, }); } export function deleteWatch(id: number): Promise { return request(`/watches/${id}`, { method: "DELETE" }); } export function listAlerts( options: Page & { seen?: boolean } = {} ): Promise { return request("/alerts", { account: true, query: { ...options } }); } export function markAlertSeen(id: number): Promise { return request(`/alerts/${id}/seen`, { method: "POST" }); } export function listAnnotations( options: { chatId?: number; messageId?: number } = {} ): Promise { return request("/annotations", { account: true, query: { chat_id: options.chatId, message_id: options.messageId }, }); } export function createAnnotation( chatId: number, messageId: number, text: string ): Promise { return request("/annotations", { method: "POST", body: { account_id: accounts.selectedId, chat_id: chatId, message_id: messageId, text, }, }); } export function updateAnnotation( id: number, text: string ): Promise { return request(`/annotations/${id}`, { method: "PUT", body: { text }, }); } export function deleteAnnotation(id: number): Promise { return request(`/annotations/${id}`, { method: "DELETE" }); }