import { getConfig } from '../core/sdk' import { http } from '../core/http' import type { ChatType, ConversationView, FriendRequest, HistoryQuery, GroupJoinRequest, ImGroup, 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 }), ) } export function listFriends(): Promise { return http.get('/api/im/friends', appQuery()) } export function listGroups(): Promise { return http.get('/api/im/groups', appQuery()) } export function getGroupInfo(groupId: string): Promise { return http.get(`/api/im/groups/${encodeURIComponent(groupId)}`, appQuery()) } export function sendFriendRequest(toUserId: string, remark?: string): Promise { return http.post( '/api/im/friend-requests', undefined, appQuery({ toUserId, ...(remark ? { remark } : {}), }), ) } export function listFriendRequests(direction: 'incoming' | 'outgoing' = 'incoming'): Promise { return http.get('/api/im/friend-requests', appQuery({ direction })) } export function acceptFriendRequest(requestId: string): Promise { return http.post(`/api/im/friend-requests/${encodeURIComponent(requestId)}/accept`, undefined, appQuery()) } export function rejectFriendRequest(requestId: string): Promise { return http.post(`/api/im/friend-requests/${encodeURIComponent(requestId)}/reject`, undefined, appQuery()) } export function sendGroupJoinRequest(groupId: string, remark?: string): Promise { return http.post( `/api/im/groups/${encodeURIComponent(groupId)}/join-requests`, undefined, appQuery({ ...(remark ? { remark } : {}), }), ) } export function listGroupJoinRequests(groupId: string): Promise { return http.get(`/api/im/groups/${encodeURIComponent(groupId)}/join-requests`, appQuery()) } export function acceptGroupJoinRequest(groupId: string, requestId: string): Promise { return http.post( `/api/im/groups/${encodeURIComponent(groupId)}/join-requests/${encodeURIComponent(requestId)}/accept`, undefined, appQuery(), ) } export function rejectGroupJoinRequest(groupId: string, requestId: string): Promise { return http.post( `/api/im/groups/${encodeURIComponent(groupId)}/join-requests/${encodeURIComponent(requestId)}/reject`, undefined, appQuery(), ) }