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) {
|
||||
|
||||
@@ -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
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user