diff --git a/app/api/rocket-chat/messages/route.ts b/app/api/rocket-chat/messages/route.ts index aeadffa2..95c1cb19 100644 --- a/app/api/rocket-chat/messages/route.ts +++ b/app/api/rocket-chat/messages/route.ts @@ -9,20 +9,24 @@ export async function GET() { return NextResponse.json({ error: "Unauthorized" }, { status: 401 }); } - // 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: { - 'X-Auth-Token': 'C_3ekrsgtsaU0sVQzpJ8aRSyMQjBIvcsmXVvBI8Wmgb', - 'X-User-Id': 'Tpuww59PJKsrGNQJB', - }, - cache: 'no-store', - }); + // Get the user's info from Rocket.Chat using their username + const userInfoResponse = await fetch( + `https://parole.slm-lab.net/api/v1/users.info?username=${session.user.username}`, + { + method: 'GET', + headers: { + 'X-Auth-Token': 'C_3ekrsgtsaU0sVQzpJ8aRSyMQjBIvcsmXVvBI8Wmgb', + 'X-User-Id': 'Tpuww59PJKsrGNQJB', + }, + cache: 'no-store', + } + ); if (!userInfoResponse.ok) { console.error('Rocket.Chat user info error:', { status: userInfoResponse.status, statusText: userInfoResponse.statusText, + username: session.user.username, }); return NextResponse.json( { error: "Failed to fetch user info" }, @@ -61,8 +65,10 @@ export async function GET() { // Get the last message from each room const messages = await Promise.all( subscriptions.update.map(async (room: any) => { + if (!room.lastMessage?._id) return null; + const messageResponse = await fetch( - `https://parole.slm-lab.net/api/v1/chat.getMessage?msgId=${room.lastMessage?._id}`, + `https://parole.slm-lab.net/api/v1/chat.getMessage?msgId=${room.lastMessage._id}`, { headers: { 'X-Auth-Token': 'C_3ekrsgtsaU0sVQzpJ8aRSyMQjBIvcsmXVvBI8Wmgb',