252 lines
5.9 KiB
TypeScript
252 lines
5.9 KiB
TypeScript
import { request } from "$lib/api/client";
|
|
import type {
|
|
Account,
|
|
CaptureToggles,
|
|
Chat,
|
|
Folder,
|
|
JobStatus,
|
|
JobView,
|
|
MediaVersion,
|
|
MediaView,
|
|
MessageVersion,
|
|
MessageView,
|
|
PeerView,
|
|
PinnedView,
|
|
PolicyChatKind,
|
|
PolicyCreate,
|
|
PolicyRecord,
|
|
PresenceHourly,
|
|
PresenceSample,
|
|
ResponseStats,
|
|
SearchHit,
|
|
VolumeBucket,
|
|
} from "$lib/api/types";
|
|
import { accounts } from "$lib/stores/accounts.svelte";
|
|
|
|
interface Page {
|
|
limit?: number;
|
|
offset?: number;
|
|
}
|
|
|
|
export function listAccounts(): Promise<Account[]> {
|
|
return request<Account[]>("/accounts");
|
|
}
|
|
|
|
export function listChats(page: Page = {}): Promise<Chat[]> {
|
|
return request<Chat[]>("/chats", { account: true, query: { ...page } });
|
|
}
|
|
|
|
export function listFolders(): Promise<Folder[]> {
|
|
return request<Folder[]>("/folders", { account: true });
|
|
}
|
|
|
|
export function listPolicies(): Promise<PolicyRecord[]> {
|
|
return request<PolicyRecord[]>("/policy", { account: true });
|
|
}
|
|
|
|
export function createPolicy(body: PolicyCreate): Promise<PolicyRecord> {
|
|
return request<PolicyRecord>("/policy", {
|
|
method: "POST",
|
|
body: { ...body, account_id: accounts.selectedId },
|
|
});
|
|
}
|
|
|
|
export function updatePolicy(
|
|
id: number,
|
|
toggles: CaptureToggles
|
|
): Promise<PolicyRecord> {
|
|
return request<PolicyRecord>(`/policy/${id}`, {
|
|
method: "PUT",
|
|
body: toggles,
|
|
});
|
|
}
|
|
|
|
export function deletePolicy(id: number): Promise<void> {
|
|
return request<void>(`/policy/${id}`, { method: "DELETE" });
|
|
}
|
|
|
|
export function effectivePolicy(query: {
|
|
chat_id: number;
|
|
is_bot?: boolean;
|
|
is_contact?: boolean | null;
|
|
kind: PolicyChatKind;
|
|
}): Promise<CaptureToggles> {
|
|
return request<CaptureToggles>("/policy/effective", {
|
|
account: true,
|
|
query,
|
|
});
|
|
}
|
|
|
|
export function listMessages(
|
|
chatId: number,
|
|
options: Page & { include_deleted?: boolean } = {}
|
|
): Promise<MessageView[]> {
|
|
return request<MessageView[]>(`/chats/${chatId}/messages`, {
|
|
account: true,
|
|
query: { ...options },
|
|
});
|
|
}
|
|
|
|
export function getCurrentPresence(
|
|
peerId: number
|
|
): Promise<PresenceSample | null> {
|
|
return request<PresenceSample | null>("/presence/current", {
|
|
account: true,
|
|
query: { peer_id: peerId },
|
|
});
|
|
}
|
|
|
|
export function getPresenceHistory(
|
|
peerId: number,
|
|
page: Page = {}
|
|
): Promise<PresenceSample[]> {
|
|
return request<PresenceSample[]>("/presence", {
|
|
account: true,
|
|
query: { peer_id: peerId, ...page },
|
|
});
|
|
}
|
|
|
|
export function getPresenceHourly(peerId: number): Promise<PresenceHourly[]> {
|
|
return request<PresenceHourly[]>("/presence/hourly", {
|
|
account: true,
|
|
query: { peer_id: peerId },
|
|
});
|
|
}
|
|
|
|
export function getMessageVolume(
|
|
chatId: number,
|
|
days = 90
|
|
): Promise<VolumeBucket[]> {
|
|
return request<VolumeBucket[]>("/analytics/volume", {
|
|
account: true,
|
|
query: { chat_id: chatId, days },
|
|
});
|
|
}
|
|
|
|
export function getResponseStats(chatId: number): Promise<ResponseStats> {
|
|
return request<ResponseStats>("/analytics/response-time", {
|
|
account: true,
|
|
query: { chat_id: chatId },
|
|
});
|
|
}
|
|
|
|
export function getPinned(chatId: number): Promise<PinnedView | null> {
|
|
return request<PinnedView | null>(`/chats/${chatId}/pinned`, {
|
|
account: true,
|
|
});
|
|
}
|
|
|
|
export function listMessageVersions(
|
|
chatId: number,
|
|
messageId: number
|
|
): Promise<MessageVersion[]> {
|
|
return request<MessageVersion[]>(
|
|
`/chats/${chatId}/messages/${messageId}/versions`,
|
|
{ account: true }
|
|
);
|
|
}
|
|
|
|
export function listDeleted(
|
|
options: Page & { chat_id?: number } = {}
|
|
): Promise<MessageView[]> {
|
|
return request<MessageView[]>("/deleted", {
|
|
account: true,
|
|
query: { ...options },
|
|
});
|
|
}
|
|
|
|
export function getPeer(peerId: number): Promise<PeerView> {
|
|
return request<PeerView>(`/peers/${peerId}`, { account: true });
|
|
}
|
|
|
|
export function getPeers(ids: number[]): Promise<PeerView[]> {
|
|
if (ids.length === 0) {
|
|
return Promise.resolve([]);
|
|
}
|
|
return request<PeerView[]>("/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<JobView> {
|
|
return request<JobView>(`/jobs/${jobId}`, { account: true });
|
|
}
|
|
|
|
export function listJobs(status?: JobStatus): Promise<JobView[]> {
|
|
return request<JobView[]>("/jobs", {
|
|
account: true,
|
|
query: status ? { status } : {},
|
|
});
|
|
}
|
|
|
|
export function enqueueBackfill(
|
|
chatId: number,
|
|
media: boolean
|
|
): Promise<{ job_id: number }> {
|
|
return request<{ job_id: number }>("/backfill", {
|
|
method: "POST",
|
|
body: { account_id: accounts.selectedId, chat_id: chatId, media },
|
|
});
|
|
}
|
|
|
|
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<MediaVersion[]> {
|
|
return request<MediaVersion[]>(`/media/versions/${chatId}/${messageId}`, {
|
|
account: true,
|
|
});
|
|
}
|
|
|
|
export function getMediaMeta(mediaId: number): Promise<MediaView> {
|
|
return request<MediaView>(`/media/${mediaId}/meta`);
|
|
}
|
|
|
|
export function getMessageMedia(
|
|
chatId: number,
|
|
messageId: number
|
|
): Promise<MediaView> {
|
|
return request<MediaView>(`/media/message/${chatId}/${messageId}`, {
|
|
account: true,
|
|
});
|
|
}
|
|
|
|
export function searchMessages(
|
|
query: string,
|
|
options: Page & { chat_id?: number } = {}
|
|
): Promise<SearchHit[]> {
|
|
return request<SearchHit[]>("/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,
|
|
},
|
|
});
|
|
}
|