Files
lingxi-web/src/services/api/channels.ts
T

178 lines
5.4 KiB
TypeScript

import { http } from './client'
import type { ChannelConnection, ChannelSyncLog, ChannelType } from '@/types'
/** GET /api/channels */
export function getChannels() {
return http.get<ChannelConnection[]>('/api/channels')
}
/** POST /api/channels/connect */
export function connectChannel(payload: {
type: ChannelType
account: string
syncRange?: string
defaultOwnerId?: string
autoReply?: boolean
}) {
return http.post<ChannelConnection>('/api/channels/connect', payload)
}
export function startTelegramLogin(phone: string) {
return http.post<ChannelConnection>('/api/channels/telegram/start', { phone })
}
export function connectEmailChannel(payload: {
name?: string
displayName?: string
account: string
password: string
imapHost: string
imapPort: number
imapSecure: boolean
imapFolder?: string
smtpHost: string
smtpPort: number
smtpSecure: boolean
syncDays?: number
}) {
return http.post<ChannelConnection>('/api/channels/email/connect', payload)
}
export function submitTelegramCode(channelId: string, code: string) {
return http.post<{ status: string; channel: ChannelConnection }>(`/api/channels/telegram/${channelId}/code`, { code })
}
export function submitTelegramPassword(channelId: string, password: string) {
return http.post<{ status: string; channel: ChannelConnection }>(`/api/channels/telegram/${channelId}/password`, { password })
}
export function getTelegramLoginState(channelId: string) {
return http.post<{ status: string; connectionState?: string; channel: ChannelConnection }>(
`/api/channels/telegram/${channelId}/state`,
)
}
export function startWhatsAppLogin(phone: string, name?: string) {
return http.post<ChannelConnection>('/api/channels/whatsapp/start', {
phone,
name,
acceptedUnofficialRisk: true,
})
}
export function getWhatsAppLoginState(channelId: string) {
return http.post<{
status: string
connectionState?: string
pairingCode?: string
qrDataUrl?: string
channel: ChannelConnection
}>(`/api/channels/whatsapp/${channelId}/state`)
}
export function setDefaultChannel(channelId: string) {
return http.post<ChannelConnection>(`/api/channels/${channelId}/default`)
}
export function syncChannel(channelId: string, options: { full?: boolean } = {}) {
return http.post<{ chats: number; messages: number }>(`/api/channels/${channelId}/sync`, options)
}
/** POST /api/channels/test */
export function testChannel(id: string) {
return http.post<{ success: boolean; latencyMs: number; message: string }>('/api/channels/test', { id })
}
/** PATCH /api/channels/:id */
export function updateChannel(id: string, payload: Partial<ChannelConnection>) {
return http.patch<ChannelConnection>(`/api/channels/${id}`, payload)
}
/** DELETE /api/channels/:id */
export function deleteChannel(id: string) {
return http.del<{ success: boolean }>(`/api/channels/${id}`)
}
/** GET /api/channels/:id/logs */
export function getChannelLogs(id: string) {
return http.get<ChannelSyncLog[]>(`/api/channels/${id}/logs`)
}
export function getWhatsAppContacts(
channelId: string,
query: { query?: string; cursor?: string; limit?: number } = {},
) {
return http.get<{
items: Array<{ id: string; name: string; phone: string }>
nextCursor: string | null
total: number
}>(`/api/channels/${channelId}/whatsapp/contacts`, query)
}
export function updateWhatsAppProxy(channelId: string, url: string) {
return http.put<ChannelConnection>(`/api/channels/${channelId}/whatsapp/proxy`, {
url,
test: true,
})
}
export function updateWhatsAppProfile(
channelId: string,
payload: { name?: string; status?: string },
) {
return http.patch<ChannelConnection>(`/api/channels/${channelId}/whatsapp/profile`, payload)
}
export function updateWhatsAppProfilePicture(
channelId: string,
payload: { name: string; mimeType: string; base64: string },
) {
return http.post<ChannelConnection>(
`/api/channels/${channelId}/whatsapp/profile/picture`,
payload,
)
}
export function reauthorizeWhatsApp(channelId: string) {
return http.post<ChannelConnection>(`/api/channels/${channelId}/whatsapp/reauthorize`)
}
export function getWhatsAppDevices(channelId: string) {
return http.get<Array<{
id: string
name: string
platform: string
current: boolean
status: string
connectedAt?: string
}>>(`/api/channels/${channelId}/whatsapp/devices`)
}
export function terminateWhatsAppDevice(channelId: string, deviceId: string) {
return http.del<{ success: boolean; deviceId: string }>(
`/api/channels/${channelId}/whatsapp/devices/${encodeURIComponent(deviceId)}`,
)
}
export function getWhatsAppGatewayMetrics() {
return http.get<{
accounts: { total: number; connected: number; disconnected: number }
outbound: { queued: number; dead: number; sent: number; riskBackoff: number; averageLatencyMs: number }
webhooks: { queued: number; dead: number; delivered: number }
alerts: Array<{ severity: string; code: string; accountId: string; message: string }>
}>('/api/channels/whatsapp/metrics')
}
export function createWhatsAppBackup(channelId: string) {
return http.get<{ version: number; accountId: string; createdAt: string; blob: string }>(
`/api/channels/${channelId}/whatsapp/backup`,
)
}
export function restoreWhatsAppBackup(channelId: string, blob: string) {
return http.post<{ restored: boolean }>(
`/api/channels/${channelId}/whatsapp/backup/restore`,
{ blob },
)
}