feat: Implement direct notification for new messages and read receipts to conversation participants.

This commit is contained in:
pradeepkumar
2026-02-11 04:48:15 +05:30
parent 6bc0b41e1f
commit 755ddda123
2 changed files with 46 additions and 3 deletions

View File

@@ -178,6 +178,16 @@ export class MessagesGateway implements OnGatewayConnection, OnGatewayDisconnect
.to(`conversation:${data.conversationId}`)
.emit('new_message', message);
// Also send directly to the other participant's socket(s)
// so they receive the message even if they haven't joined this room
const participants = await this.messagesService.getConversationParticipants(data.conversationId);
if (participants) {
const otherUserId = client.userId === participants.userId
? participants.agentUserId
: participants.userId;
this.sendToUser(otherUserId, 'new_message', message);
}
return { success: true, message };
} catch (error) {
return { error: error.message };
@@ -232,12 +242,23 @@ export class MessagesGateway implements OnGatewayConnection, OnGatewayDisconnect
try {
await this.messagesService.markMessagesAsRead(data.conversationId, client.userId);
// Notify the other party that messages have been read
client.to(`conversation:${data.conversationId}`).emit('messages_read', {
const readEvent = {
conversationId: data.conversationId,
readBy: client.userId,
readAt: new Date(),
});
};
// Notify via room (for clients viewing this conversation)
client.to(`conversation:${data.conversationId}`).emit('messages_read', readEvent);
// Also notify the other participant directly
const participants = await this.messagesService.getConversationParticipants(data.conversationId);
if (participants) {
const otherUserId = client.userId === participants.userId
? participants.agentUserId
: participants.userId;
this.sendToUser(otherUserId, 'messages_read', readEvent);
}
return { success: true };
} catch (error) {

View File

@@ -555,6 +555,28 @@ export class MessagesService {
}
}
/**
* Get participant user IDs for a conversation (lightweight query)
*/
async getConversationParticipants(conversationId: string): Promise<{ userId: string; agentUserId: string } | null> {
const conversation = await this.prisma.conversation.findUnique({
where: { id: conversationId },
select: {
userId: true,
agentProfile: {
select: { userId: true },
},
},
});
if (!conversation) return null;
return {
userId: conversation.userId,
agentUserId: conversation.agentProfile.userId,
};
}
/**
* Get total unread count for a user across all conversations
*/