XuqmGroup-Vue3SDK/src/im/api.ts

194 行
6.0 KiB
TypeScript

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<string, string | number | boolean | Date | null | undefined>) {
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<ConversationView[]> {
return http.get<ConversationView[]>('/api/im/conversations', appQuery({ page: 0, size }))
}
export function fetchHistory(toId: string, query: HistoryQuery = {}): Promise<PageResult<ImMessage>> {
return http.get<PageResult<ImMessage>>(
`/api/im/messages/history/${encodeURIComponent(toId)}`,
appQuery(normalizeHistoryQuery(query)),
)
}
export function fetchGroupHistory(groupId: string, query: HistoryQuery = {}): Promise<PageResult<ImMessage>> {
return http.get<PageResult<ImMessage>>(
`/api/im/messages/group-history/${encodeURIComponent(groupId)}`,
appQuery(normalizeHistoryQuery(query)),
)
}
export function markRead(targetId: string, chatType: ChatType = 'SINGLE'): Promise<void> {
return http.put<void>(
`/api/im/conversations/${encodeURIComponent(targetId)}/read`,
undefined,
appQuery({ chatType }),
)
}
export function setConversationPinned(targetId: string, chatType: ChatType, pinned: boolean): Promise<void> {
return http.put<void>(
`/api/im/conversations/${encodeURIComponent(targetId)}/pinned`,
undefined,
appQuery({ chatType, pinned }),
)
}
export function setConversationMuted(targetId: string, chatType: ChatType, muted: boolean): Promise<void> {
return http.put<void>(
`/api/im/conversations/${encodeURIComponent(targetId)}/muted`,
undefined,
appQuery({ chatType, muted }),
)
}
export function setDraft(targetId: string, chatType: ChatType, draft: string): Promise<void> {
return http.put<void>(
`/api/im/conversations/${encodeURIComponent(targetId)}/draft`,
undefined,
appQuery({ chatType, draft }),
)
}
export function deleteConversation(targetId: string, chatType: ChatType): Promise<void> {
return http.delete<void>(
`/api/im/conversations/${encodeURIComponent(targetId)}`,
appQuery({ chatType }),
)
}
export function getProfile(userId: string): Promise<UserProfile> {
return http.get<UserProfile>(`/api/im/accounts/${encodeURIComponent(userId)}`, appQuery())
}
export function updateProfile(
userId: string,
nickname?: string,
avatar?: string,
gender?: string,
): Promise<UserProfile> {
return http.put<UserProfile>(
`/api/im/accounts/${encodeURIComponent(userId)}`,
undefined,
appQuery({ nickname, avatar, gender }),
)
}
export function listFriends(): Promise<string[]> {
return http.get<string[]>('/api/im/friends', appQuery())
}
export function listGroups(): Promise<ImGroup[]> {
return http.get<ImGroup[]>('/api/im/groups', appQuery())
}
export function searchUsers(keyword: string, size = 20): Promise<UserProfile[]> {
return http.get<UserProfile[]>('/api/im/admin/users/search', appQuery({ keyword, size }))
}
export function searchGroups(keyword: string, size = 20): Promise<ImGroup[]> {
return http.get<ImGroup[]>('/api/im/admin/groups/search', appQuery({ keyword, size }))
}
export function searchMessages(
keyword: string | null = null,
chatType: ChatType | null = null,
msgType: string | null = null,
startTime: string | number | Date | null = null,
endTime: string | number | Date | null = null,
page = 0,
size = 20,
): Promise<PageResult<ImMessage>> {
return http.get<PageResult<ImMessage>>(
'/api/im/admin/messages/search',
appQuery({ keyword, chatType, msgType, startTime, endTime, page, size }),
)
}
export function getGroupInfo(groupId: string): Promise<ImGroup> {
return http.get<ImGroup>(`/api/im/groups/${encodeURIComponent(groupId)}`, appQuery())
}
export function sendFriendRequest(toUserId: string, remark?: string): Promise<FriendRequest> {
return http.post<FriendRequest>(
'/api/im/friend-requests',
undefined,
appQuery({
toUserId,
...(remark ? { remark } : {}),
}),
)
}
export function listFriendRequests(direction: 'incoming' | 'outgoing' = 'incoming'): Promise<FriendRequest[]> {
return http.get<FriendRequest[]>('/api/im/friend-requests', appQuery({ direction }))
}
export function acceptFriendRequest(requestId: string): Promise<FriendRequest> {
return http.post<FriendRequest>(`/api/im/friend-requests/${encodeURIComponent(requestId)}/accept`, undefined, appQuery())
}
export function rejectFriendRequest(requestId: string): Promise<FriendRequest> {
return http.post<FriendRequest>(`/api/im/friend-requests/${encodeURIComponent(requestId)}/reject`, undefined, appQuery())
}
export function sendGroupJoinRequest(groupId: string, remark?: string): Promise<GroupJoinRequest> {
return http.post<GroupJoinRequest>(
`/api/im/groups/${encodeURIComponent(groupId)}/join-requests`,
undefined,
appQuery({
...(remark ? { remark } : {}),
}),
)
}
export function listGroupJoinRequests(groupId: string): Promise<GroupJoinRequest[]> {
return http.get<GroupJoinRequest[]>(`/api/im/groups/${encodeURIComponent(groupId)}/join-requests`, appQuery())
}
export function acceptGroupJoinRequest(groupId: string, requestId: string): Promise<GroupJoinRequest> {
return http.post<GroupJoinRequest>(
`/api/im/groups/${encodeURIComponent(groupId)}/join-requests/${encodeURIComponent(requestId)}/accept`,
undefined,
appQuery(),
)
}
export function rejectGroupJoinRequest(groupId: string, requestId: string): Promise<GroupJoinRequest> {
return http.post<GroupJoinRequest>(
`/api/im/groups/${encodeURIComponent(groupId)}/join-requests/${encodeURIComponent(requestId)}/reject`,
undefined,
appQuery(),
)
}