From 7fe27f8b49f7ced4593b1688fb3d18b234941438 Mon Sep 17 00:00:00 2001 From: Alma Date: Sun, 13 Apr 2025 01:30:54 +0200 Subject: [PATCH] widget parole 22 --- app/api/rocket-chat/messages/route.ts | 30 ++++++++++++++++----------- 1 file changed, 18 insertions(+), 12 deletions(-) diff --git a/app/api/rocket-chat/messages/route.ts b/app/api/rocket-chat/messages/route.ts index 6a306229..bbdd0b2d 100644 --- a/app/api/rocket-chat/messages/route.ts +++ b/app/api/rocket-chat/messages/route.ts @@ -137,7 +137,6 @@ export async function GET(request: Request) { } const subscriptionsData = await subscriptionsResponse.json(); - console.log('Subscriptions response:', subscriptionsData); if (!subscriptionsData.success || !Array.isArray(subscriptionsData.update)) { console.error('Invalid subscriptions response structure'); @@ -146,16 +145,14 @@ export async function GET(request: Request) { // Filter subscriptions for the current user const userSubscriptions = subscriptionsData.update.filter((sub: any) => { - // For direct messages, only include if the user is a participant + // For direct messages (t: 'd') if (sub.t === 'd') { - // Check if the user is actually part of this direct message - return sub.rid.includes(currentUser._id); + // Make sure the room ID contains the current user's ID + const participants = sub.rid.split(''); + return participants.includes(currentUser._id); } - // 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; + // For channels (t: 'c') and private groups (t: 'p') + return ['c', 'p'].includes(sub.t) && sub.u._id === currentUser._id; }); console.log('Filtered user subscriptions:', { @@ -166,7 +163,8 @@ export async function GET(request: Request) { type: sub.t, name: sub.fname || sub.name, rid: sub.rid, - alert: sub.alert + alert: sub.alert, + isOwn: sub.u._id === currentUser._id })) }); @@ -179,8 +177,16 @@ export async function GET(request: Request) { processedRooms.add(subscription.rid); try { - // Determine the correct endpoint and parameters based on room type - const endpoint = subscription.t === 'c' ? 'channels.messages' : 'im.messages'; + // Determine the correct endpoint based on room type + let endpoint; + if (subscription.t === 'c') { + endpoint = 'channels.messages'; + } else if (subscription.t === 'p') { + endpoint = 'groups.messages'; + } else { + endpoint = 'im.messages'; + } + const queryParams = new URLSearchParams({ roomId: subscription.rid, count: '3'