From a5a9307fdf38fa0b084e49fa0572d05a15327312 Mon Sep 17 00:00:00 2001 From: Alma Date: Thu, 10 Apr 2025 00:10:57 +0200 Subject: [PATCH] update widget token mail 15 --- app/api/rocket-chat/messages/route.ts | 72 ++++++++++++++++----------- 1 file changed, 43 insertions(+), 29 deletions(-) diff --git a/app/api/rocket-chat/messages/route.ts b/app/api/rocket-chat/messages/route.ts index bb795199..ce6c71ae 100644 --- a/app/api/rocket-chat/messages/route.ts +++ b/app/api/rocket-chat/messages/route.ts @@ -90,13 +90,13 @@ export async function GET() { username: userInfo.user.username }); - // Get the user's subscriptions (rooms they are in) using their user ID + // Get the user's subscriptions (rooms they are in) const subscriptionsResponse = await fetch( - `https://parole.slm-lab.net/api/v1/users.list.rooms?userId=${userInfo.user._id}`, + 'https://parole.slm-lab.net/api/v1/subscriptions.get', { headers: { 'X-Auth-Token': ROCKET_CHAT_TOKEN, - 'X-User-Id': ROCKET_CHAT_USER_ID, + 'X-User-Id': userInfo.user._id, 'Content-Type': 'application/json' } } @@ -117,43 +117,57 @@ export async function GET() { const subscriptions = await subscriptionsResponse.json(); console.log('Subscriptions response:', subscriptions); - if (!subscriptions.rooms || subscriptions.rooms.length === 0) { + if (!subscriptions.update || subscriptions.update.length === 0) { + console.log('No subscriptions found'); return NextResponse.json({ messages: [] }); } // Get the last message from each room const messages = await Promise.all( - subscriptions.rooms.map(async (room: any) => { - // Get the most recent messages from the room - const messagesResponse = await fetch( - `https://parole.slm-lab.net/api/v1/channels.messages?roomId=${room._id}&count=1`, - { - headers: { - 'X-Auth-Token': ROCKET_CHAT_TOKEN, - 'X-User-Id': ROCKET_CHAT_USER_ID, - 'Content-Type': 'application/json' - } + subscriptions.update.map(async (subscription: any) => { + try { + if (!subscription.rid) { + console.log('No room ID found for subscription:', subscription); + return null; } - ); - if (messagesResponse.ok) { - const messagesData = await messagesResponse.json(); - if (messagesData.messages && messagesData.messages.length > 0) { - const message = messagesData.messages[0]; + // Get the most recent messages from the room + const messagesResponse = await fetch( + `https://parole.slm-lab.net/api/v1/rooms.get?roomId=${subscription.rid}`, + { + headers: { + 'X-Auth-Token': ROCKET_CHAT_TOKEN, + 'X-User-Id': ROCKET_CHAT_USER_ID, + 'Content-Type': 'application/json' + } + } + ); + + if (!messagesResponse.ok) { + console.error('Failed to fetch room info:', { + roomId: subscription.rid, + status: messagesResponse.status, + response: await messagesResponse.text().catch(() => 'Could not get response text') + }); + return null; + } + + const roomData = await messagesResponse.json(); + + if (roomData.room?.lastMessage) { return { - ...message, - roomName: room.name || 'Direct Message', - roomType: room.t, + ...roomData.room.lastMessage, + roomName: subscription.name || roomData.room.fname || 'Direct Message', + roomType: subscription.t, }; } - } else { - console.error('Failed to fetch messages for room:', { - roomId: room._id, - status: messagesResponse.status, - response: await messagesResponse.text().catch(() => 'Could not get response text') - }); + + console.log('No last message found for room:', subscription.rid); + return null; + } catch (error) { + console.error('Error processing room:', subscription.rid, error); + return null; } - return null; }) );