diff --git a/app/api/rocket-chat/messages/route.ts b/app/api/rocket-chat/messages/route.ts index 43aa6b14..6a306229 100644 --- a/app/api/rocket-chat/messages/route.ts +++ b/app/api/rocket-chat/messages/route.ts @@ -146,21 +146,26 @@ export async function GET(request: Request) { // Filter subscriptions for the current user const userSubscriptions = subscriptionsData.update.filter((sub: any) => { - // For direct messages (t: 'd'), check if the room ID contains the user's ID + // For direct messages, only include if the user is a participant if (sub.t === 'd') { + // Check if the user is actually part of this direct message return sub.rid.includes(currentUser._id); } - // For channels (t: 'c'), include all channels - return sub.t === 'c'; + // For channels, only include if the user is a member + if (sub.t === 'c') { + return sub.alert !== undefined; // This indicates the user is actually a member + } + return false; }); - console.log('Filtered subscriptions:', { - total: userSubscriptions.length, - roomTypes: userSubscriptions.map((sub: any) => ({ + console.log('Filtered user subscriptions:', { + userId: currentUser._id, + username: currentUser.username, + totalSubscriptions: userSubscriptions.length, + subscriptionDetails: userSubscriptions.map((sub: any) => ({ type: sub.t, name: sub.fname || sub.name, - unread: sub.unread, - mentions: sub.userMentions, + rid: sub.rid, alert: sub.alert })) }); @@ -170,16 +175,19 @@ export async function GET(request: Request) { // Step 5: Fetch messages using user token for (const subscription of userSubscriptions) { - if (messages.length >= 10 || processedRooms.has(subscription._id)) continue; - processedRooms.add(subscription._id); + if (messages.length >= 10 || processedRooms.has(subscription.rid)) continue; + processedRooms.add(subscription.rid); try { - // Determine the correct endpoint based on room type + // Determine the correct endpoint and parameters based on room type const endpoint = subscription.t === 'c' ? 'channels.messages' : 'im.messages'; + const queryParams = new URLSearchParams({ + roomId: subscription.rid, + count: '3' + }); - // Get more messages from each room const messagesResponse = await fetch( - `${baseUrl}/api/v1/${endpoint}?roomId=${subscription.rid}&count=3`, { + `${baseUrl}/api/v1/${endpoint}?${queryParams}`, { method: 'GET', headers: userHeaders }