From c18ff84673b7901a7ee100a2f2107fe69f30b31c Mon Sep 17 00:00:00 2001 From: Alma Date: Wed, 9 Apr 2025 23:02:03 +0200 Subject: [PATCH] update widget token 3 --- app/api/rocket-chat/messages/route.ts | 53 +++++++++++++++++++++------ 1 file changed, 42 insertions(+), 11 deletions(-) diff --git a/app/api/rocket-chat/messages/route.ts b/app/api/rocket-chat/messages/route.ts index b1848057..aeadffa2 100644 --- a/app/api/rocket-chat/messages/route.ts +++ b/app/api/rocket-chat/messages/route.ts @@ -9,7 +9,7 @@ export async function GET() { return NextResponse.json({ error: "Unauthorized" }, { status: 401 }); } - // First, get the user's info from Rocket.Chat using their email + // First, get the user's info from Rocket.Chat const userInfoResponse = await fetch('https://parole.slm-lab.net/api/v1/users.info', { method: 'GET', headers: { @@ -33,8 +33,8 @@ export async function GET() { const userInfo = await userInfoResponse.json(); const userId = userInfo.user._id; - // Get the last messages for the user - const response = await fetch(`https://parole.slm-lab.net/api/v1/im.messages?userId=${userId}&count=50`, { + // Get the user's subscriptions (rooms they are in) + const subscriptionsResponse = await fetch('https://parole.slm-lab.net/api/v1/subscriptions.get', { headers: { 'X-Auth-Token': 'C_3ekrsgtsaU0sVQzpJ8aRSyMQjBIvcsmXVvBI8Wmgb', 'X-User-Id': 'Tpuww59PJKsrGNQJB', @@ -42,19 +42,50 @@ export async function GET() { cache: 'no-store', }); - if (!response.ok) { - console.error('Rocket.Chat API error:', { - status: response.status, - statusText: response.statusText, + if (!subscriptionsResponse.ok) { + console.error('Rocket.Chat subscriptions error:', { + status: subscriptionsResponse.status, + statusText: subscriptionsResponse.statusText, }); return NextResponse.json( - { error: "Failed to fetch messages" }, - { status: response.status } + { error: "Failed to fetch subscriptions" }, + { status: subscriptionsResponse.status } ); } - const data = await response.json(); - return NextResponse.json(data); + const subscriptions = await subscriptionsResponse.json(); + if (!subscriptions.update || subscriptions.update.length === 0) { + return NextResponse.json({ messages: [] }); + } + + // Get the last message from each room + const messages = await Promise.all( + subscriptions.update.map(async (room: any) => { + const messageResponse = await fetch( + `https://parole.slm-lab.net/api/v1/chat.getMessage?msgId=${room.lastMessage?._id}`, + { + headers: { + 'X-Auth-Token': 'C_3ekrsgtsaU0sVQzpJ8aRSyMQjBIvcsmXVvBI8Wmgb', + 'X-User-Id': 'Tpuww59PJKsrGNQJB', + }, + cache: 'no-store', + } + ); + + if (messageResponse.ok) { + const messageData = await messageResponse.json(); + return messageData.message; + } + return null; + }) + ); + + // Filter out any null messages and sort by timestamp + const validMessages = messages + .filter((msg): msg is NonNullable => msg !== null) + .sort((a, b) => new Date(b.ts).getTime() - new Date(a.ts).getTime()); + + return NextResponse.json({ messages: validMessages }); } catch (error) { console.error('Error fetching messages:', error); return NextResponse.json(