Spaces:
Configuration error
Configuration error
File size: 4,626 Bytes
2c6bb7b |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 |
import { Chat, Message, CreateChatRequest, SendMessageData, PaginatedResponse } from '../../../shared/types'
import { apiService } from './api'
class ChatService {
// Chat management
async getChats(): Promise<Chat[]> {
return await apiService.get<Chat[]>('/api/chats')
}
async getChat(chatId: string): Promise<Chat> {
return await apiService.get<Chat>(`/api/chats/${chatId}`)
}
async createChat(data: CreateChatRequest): Promise<Chat> {
return await apiService.post<Chat>('/api/chats', data)
}
async updateChat(chatId: string, data: Partial<Chat>): Promise<Chat> {
return await apiService.put<Chat>(`/api/chats/${chatId}`, data)
}
async deleteChat(chatId: string): Promise<void> {
await apiService.delete(`/api/chats/${chatId}`)
}
async leaveChat(chatId: string): Promise<void> {
await apiService.post(`/api/chats/${chatId}/leave`)
}
// Message management
async getMessages(chatId: string, page = 1, limit = 50): Promise<Message[]> {
const response = await apiService.get<PaginatedResponse<Message>>(
`/api/chats/${chatId}/messages?page=${page}&limit=${limit}`
)
return response.data
}
async sendMessage(chatId: string, data: Omit<SendMessageData, 'chatId'>): Promise<Message> {
if (data.attachments && data.attachments.length > 0) {
// Upload files first
const uploadedFiles = await this.uploadFiles(data.attachments)
return await apiService.post<Message>(`/api/chats/${chatId}/messages`, {
content: data.content,
type: data.type,
attachments: uploadedFiles,
replyTo: data.replyTo,
})
} else {
return await apiService.post<Message>(`/api/chats/${chatId}/messages`, {
content: data.content,
type: data.type,
replyTo: data.replyTo,
})
}
}
async editMessage(messageId: string, content: string): Promise<Message> {
return await apiService.put<Message>(`/api/messages/${messageId}`, { content })
}
async deleteMessage(messageId: string): Promise<void> {
await apiService.delete(`/api/messages/${messageId}`)
}
async addReaction(messageId: string, emoji: string): Promise<void> {
await apiService.post(`/api/messages/${messageId}/reactions`, { emoji })
}
async removeReaction(messageId: string, emoji: string): Promise<void> {
await apiService.delete(`/api/messages/${messageId}/reactions/${emoji}`)
}
// Group management
async addMember(chatId: string, userId: string): Promise<void> {
await apiService.post(`/api/chats/${chatId}/members`, { userId })
}
async removeMember(chatId: string, userId: string): Promise<void> {
await apiService.delete(`/api/chats/${chatId}/members/${userId}`)
}
async updateMemberRole(chatId: string, userId: string, role: string): Promise<void> {
await apiService.put(`/api/chats/${chatId}/members/${userId}`, { role })
}
async getChatMembers(chatId: string): Promise<any[]> {
return await apiService.get(`/api/chats/${chatId}/members`)
}
// File upload
async uploadFiles(files: File[]): Promise<any[]> {
const uploadPromises = files.map(file => this.uploadFile(file))
return await Promise.all(uploadPromises)
}
async uploadFile(file: File): Promise<any> {
return await apiService.upload('/api/upload', file)
}
// Search
async searchMessages(query: string, chatId?: string): Promise<Message[]> {
const params = new URLSearchParams({ q: query })
if (chatId) {
params.append('chatId', chatId)
}
return await apiService.get<Message[]>(`/api/search/messages?${params}`)
}
async searchChats(query: string): Promise<Chat[]> {
return await apiService.get<Chat[]>(`/api/search/chats?q=${encodeURIComponent(query)}`)
}
// Chat settings
async updateChatSettings(chatId: string, settings: any): Promise<void> {
await apiService.put(`/api/chats/${chatId}/settings`, settings)
}
async getChatSettings(chatId: string): Promise<any> {
return await apiService.get(`/api/chats/${chatId}/settings`)
}
// Notifications
async markAsRead(chatId: string): Promise<void> {
await apiService.post(`/api/chats/${chatId}/read`)
}
async muteChat(chatId: string, duration?: number): Promise<void> {
await apiService.post(`/api/chats/${chatId}/mute`, { duration })
}
async unmuteChat(chatId: string): Promise<void> {
await apiService.post(`/api/chats/${chatId}/unmute`)
}
}
export const chatService = new ChatService()
|