- Added `GET /messages/unread-count` endpoint to retrieve the count of unread messages for a user. - Implemented `getUnreadCount` method in `MessagesService` and `MessagesRepository`. - Updated frontend service to support fetching unread message count via API.
59 lines
1.2 KiB
TypeScript
59 lines
1.2 KiB
TypeScript
import api from "@/lib/api";
|
|
|
|
export interface Conversation {
|
|
id: string;
|
|
updatedAt: string;
|
|
lastMessage?: {
|
|
text: string;
|
|
createdAt: string;
|
|
};
|
|
recipient: {
|
|
uuid: string;
|
|
username: string;
|
|
displayName?: string;
|
|
avatarUrl?: string;
|
|
};
|
|
}
|
|
|
|
export interface Message {
|
|
id: string;
|
|
text: string;
|
|
createdAt: string;
|
|
senderId: string;
|
|
readAt?: string;
|
|
}
|
|
|
|
export const MessageService = {
|
|
async getConversations(): Promise<Conversation[]> {
|
|
const { data } = await api.get<Conversation[]>("/messages/conversations");
|
|
return data;
|
|
},
|
|
|
|
async getUnreadCount(): Promise<number> {
|
|
const { data } = await api.get<number>("/messages/unread-count");
|
|
return data;
|
|
},
|
|
|
|
async getMessages(conversationId: string): Promise<Message[]> {
|
|
const { data } = await api.get<Message[]>(
|
|
`/messages/conversations/${conversationId}`,
|
|
);
|
|
return data;
|
|
},
|
|
|
|
async getConversationWith(userId: string): Promise<Conversation | null> {
|
|
const { data } = await api.get<Conversation | null>(
|
|
`/messages/conversations/with/${userId}`,
|
|
);
|
|
return data;
|
|
},
|
|
|
|
async sendMessage(recipientId: string, text: string): Promise<Message> {
|
|
const { data } = await api.post<Message>("/messages", {
|
|
recipientId,
|
|
text,
|
|
});
|
|
return data;
|
|
},
|
|
};
|