diff --git a/app/api/rocket-chat/messages/route.ts b/app/api/rocket-chat/messages/route.ts index 8c5c7713..03a4395b 100644 --- a/app/api/rocket-chat/messages/route.ts +++ b/app/api/rocket-chat/messages/route.ts @@ -110,14 +110,30 @@ export async function GET() { if (messagesData.messages && messagesData.messages.length > 0) { const message = messagesData.messages[0]; - // Only include messages where the user is either the sender or the recipient - if (message.u._id === rocketChatUserId || - (subscription.t === 'd' && subscription.rid.includes(rocketChatUserId))) { - return { - ...message, - roomName: subscription.fname || subscription.name || 'Direct Message', - roomType: subscription.t, - }; + + // For direct messages, check if the room ID contains the user's ID + if (subscription.t === 'd') { + // Extract the other user's ID from the room ID + const roomIdParts = subscription.rid.split(rocketChatUserId); + const otherUserId = roomIdParts[0] || roomIdParts[1]; + + // Only include messages where the user is either the sender or the recipient + if (message.u._id === rocketChatUserId || message.u._id === otherUserId) { + return { + ...message, + roomName: subscription.fname || subscription.name || 'Direct Message', + roomType: subscription.t, + }; + } + } else { + // For channels and groups, only include messages where the user is the sender + if (message.u._id === rocketChatUserId) { + return { + ...message, + roomName: subscription.fname || subscription.name || 'Direct Message', + roomType: subscription.t, + }; + } } }