From 2697471b03f247b1c7188286a2216a65aa669fd0 Mon Sep 17 00:00:00 2001 From: Alma Date: Sun, 13 Apr 2025 00:30:11 +0200 Subject: [PATCH] widget parole 10 --- app/api/rocket-chat/messages/route.ts | 38 ++++++++++++++++++--------- 1 file changed, 26 insertions(+), 12 deletions(-) diff --git a/app/api/rocket-chat/messages/route.ts b/app/api/rocket-chat/messages/route.ts index 3b645306..d32ca2ec 100644 --- a/app/api/rocket-chat/messages/route.ts +++ b/app/api/rocket-chat/messages/route.ts @@ -66,29 +66,43 @@ export async function GET(request: Request) { return NextResponse.json({ messages: [] }, { status: 200 }); } + let userInfo; + // Try to get user info by username first const userInfoResponse = await fetch(`${baseUrl}/api/v1/users.info?username=${encodeURIComponent(username)}`, { method: 'GET', headers: adminHeaders }); if (!userInfoResponse.ok) { - console.error('Failed to get user info:', userInfoResponse.status); + console.error('Failed to get user info by username:', userInfoResponse.status); const errorData = await userInfoResponse.json(); console.error('User info error details:', errorData); - // If user not found, return empty messages instead of error - if (errorData.error === 'User not found.') { + // Try to get user info by email as fallback + const emailUserInfoResponse = await fetch(`${baseUrl}/api/v1/users.info?email=${encodeURIComponent(session.user.email)}`, { + method: 'GET', + headers: adminHeaders + }); + + if (!emailUserInfoResponse.ok) { + console.error('Failed to get user info by email:', emailUserInfoResponse.status); + const emailErrorData = await emailUserInfoResponse.json(); + console.error('Email user info error details:', emailErrorData); return NextResponse.json({ messages: [] }, { status: 200 }); } - - return NextResponse.json({ error: 'Failed to get user info' }, { status: userInfoResponse.status }); - } - const userInfo = await userInfoResponse.json(); - console.log('Found Rocket.Chat user:', { - username: userInfo.user?.username, - id: userInfo.user?._id - }); + userInfo = await emailUserInfoResponse.json(); + console.log('Found Rocket.Chat user by email:', { + username: userInfo.user?.username, + id: userInfo.user?._id + }); + } else { + userInfo = await userInfoResponse.json(); + console.log('Found Rocket.Chat user by username:', { + username: userInfo.user?.username, + id: userInfo.user?._id + }); + } // Get user's subscriptions using admin token const subscriptionsResponse = await fetch(`${baseUrl}/api/v1/subscriptions.get`, { @@ -132,7 +146,7 @@ export async function GET(request: Request) { try { // Get the latest messages from the room const messagesResponse = await fetch( - `${baseUrl}/api/v1/chat.getMessage?roomId=${subscription.rid}`, { + `${baseUrl}/api/v1/channels.messages?roomId=${subscription.rid}&count=1`, { method: 'GET', headers: adminHeaders });