feat: add API for fetching direct conversation with a user

- Added `GET /messages/conversations/with/:userId` endpoint in the backend to retrieve direct conversation data.
- Implemented corresponding method in `MessagesService` and `MessagesRepository`.
- Updated the frontend service to support fetching direct conversations via API.
This commit is contained in:
Mathis HERRIOT
2026-01-29 15:46:38 +01:00
parent 6d80795e44
commit 2c18fd1c1a
3 changed files with 39 additions and 0 deletions

View File

@@ -22,6 +22,17 @@ export class MessagesController {
return this.messagesService.getConversations(req.user.sub);
}
@Get("conversations/with/:userId")
getConversationWithUser(
@Req() req: AuthenticatedRequest,
@Param("userId") targetUserId: string,
) {
return this.messagesService.getConversationWithUser(
req.user.sub,
targetUserId,
);
}
@Get("conversations/:id")
getMessages(
@Req() req: AuthenticatedRequest,

View File

@@ -42,6 +42,13 @@ export class MessagesService {
return this.messagesRepository.findAllConversations(userId);
}
async getConversationWithUser(userId: string, targetUserId: string) {
return this.messagesRepository.findConversationBetweenUsers(
userId,
targetUserId,
);
}
async getMessages(userId: string, conversationId: string) {
const isParticipant = await this.messagesRepository.isParticipant(
conversationId,
@@ -51,6 +58,20 @@ export class MessagesService {
throw new ForbiddenException("You are not part of this conversation");
}
// Marquer comme lus
await this.messagesRepository.markAsRead(conversationId, userId);
return this.messagesRepository.findMessagesByConversationId(conversationId);
}
async markAsRead(userId: string, conversationId: string) {
const isParticipant = await this.messagesRepository.isParticipant(
conversationId,
userId,
);
if (!isParticipant) {
throw new ForbiddenException("You are not part of this conversation");
}
return this.messagesRepository.markAsRead(conversationId, userId);
}
}

View File

@@ -36,6 +36,13 @@ export const MessageService = {
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,