import { getConfig } from '../core/sdk' import { http } from '../core/http' import type { ChatType, ConversationView, HistoryQuery, ImMessage, PageResult, UserProfile, } from '../types' function appQuery(extra?: Record) { return { appId: getConfig().appKey, ...extra, } } function normalizeHistoryQuery(query: HistoryQuery = {}) { return { msgType: query.msgType, keyword: query.keyword, startTime: query.startTime, endTime: query.endTime, page: query.page ?? 0, size: query.size ?? 20, } } export function listConversations(size = 20): Promise { return http.get('/api/im/conversations', appQuery({ page: 0, size })) } export function fetchHistory(toId: string, query: HistoryQuery = {}): Promise> { return http.get>( `/api/im/messages/history/${encodeURIComponent(toId)}`, appQuery(normalizeHistoryQuery(query)), ) } export function fetchGroupHistory(groupId: string, query: HistoryQuery = {}): Promise> { return http.get>( `/api/im/messages/group-history/${encodeURIComponent(groupId)}`, appQuery(normalizeHistoryQuery(query)), ) } export function markRead(targetId: string, chatType: ChatType = 'SINGLE'): Promise { return http.put( `/api/im/conversations/${encodeURIComponent(targetId)}/read`, undefined, appQuery({ chatType }), ) } export function setConversationPinned(targetId: string, chatType: ChatType, pinned: boolean): Promise { return http.put( `/api/im/conversations/${encodeURIComponent(targetId)}/pinned`, undefined, appQuery({ chatType, pinned }), ) } export function setConversationMuted(targetId: string, chatType: ChatType, muted: boolean): Promise { return http.put( `/api/im/conversations/${encodeURIComponent(targetId)}/muted`, undefined, appQuery({ chatType, muted }), ) } export function setDraft(targetId: string, chatType: ChatType, draft: string): Promise { return http.put( `/api/im/conversations/${encodeURIComponent(targetId)}/draft`, undefined, appQuery({ chatType, draft }), ) } export function deleteConversation(targetId: string, chatType: ChatType): Promise { return http.delete( `/api/im/conversations/${encodeURIComponent(targetId)}`, appQuery({ chatType }), ) } export function getProfile(userId: string): Promise { return http.get(`/api/im/accounts/${encodeURIComponent(userId)}`, appQuery()) } export function updateProfile( userId: string, nickname?: string, avatar?: string, gender?: string, ): Promise { return http.put( `/api/im/accounts/${encodeURIComponent(userId)}`, undefined, appQuery({ nickname, avatar, gender }), ) }