feat: Implement direct notification for new messages and read receipts to conversation participants.
This commit is contained in:
@@ -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) {
|
||||
|
||||
Reference in New Issue
Block a user