From 1a7bdddac05a765d83c19da13395f21def46a5e9 Mon Sep 17 00:00:00 2001 From: Alma Date: Sun, 13 Apr 2025 01:05:07 +0200 Subject: [PATCH] widget parole 18 --- app/api/rocket-chat/messages/route.ts | 37 ++++++++------------------- 1 file changed, 10 insertions(+), 27 deletions(-) diff --git a/app/api/rocket-chat/messages/route.ts b/app/api/rocket-chat/messages/route.ts index e1df29f8..84437f9f 100644 --- a/app/api/rocket-chat/messages/route.ts +++ b/app/api/rocket-chat/messages/route.ts @@ -98,40 +98,23 @@ export async function GET(request: Request) { id: currentUser._id }); - // Step 3: Login as the user to get their token - const loginResponse = await fetch(`${baseUrl}/api/v1/login`, { - method: 'POST', - headers: { - 'Content-Type': 'application/json' - }, - body: JSON.stringify({ - user: currentUser.username, - password: process.env.ROCKET_CHAT_USER_PASSWORD // This should be the user's password or a shared password - }) - }); - - if (!loginResponse.ok) { - console.error('Failed to login as user:', loginResponse.status); - const errorText = await loginResponse.text(); - console.error('Login error details:', errorText); - return NextResponse.json({ messages: [] }, { status: 200 }); - } - - const loginData = await loginResponse.json(); + // Step 3: Use admin token to get messages const userHeaders = { - 'X-Auth-Token': loginData.data.authToken, - 'X-User-Id': loginData.data.userId, + 'X-Auth-Token': process.env.ROCKET_CHAT_TOKEN!, + 'X-User-Id': process.env.ROCKET_CHAT_USER_ID!, 'Content-Type': 'application/json' }; - // Step 4: Get user's subscriptions using user token - const subscriptionsResponse = await fetch(`${baseUrl}/api/v1/subscriptions.get`, { + // Step 4: Get user's subscriptions using admin token + const subscriptionsResponse = await fetch(`${baseUrl}/api/v1/subscriptions.get?userId=${currentUser._id}`, { method: 'GET', headers: userHeaders }); if (!subscriptionsResponse.ok) { console.error('Failed to get subscriptions:', subscriptionsResponse.status); + const errorText = await subscriptionsResponse.text(); + console.error('Subscriptions error details:', errorText); return NextResponse.json({ messages: [] }, { status: 200 }); } @@ -155,8 +138,8 @@ export async function GET(request: Request) { if (sub.t === 'd') { return sub.rid.includes(currentUser._id); } - // For channels (t: 'c'), include if user has unread messages or mentions - return sub.t === 'c' && (sub.unread > 0 || sub.userMentions > 0 || sub.alert); + // For channels (t: 'c'), include all channels + return sub.t === 'c'; }); console.log('Filtered subscriptions:', { @@ -173,7 +156,7 @@ export async function GET(request: Request) { const messages: any[] = []; const processedRooms = new Set(); - // Step 5: Fetch messages using user token + // Step 5: Fetch messages using admin token for (const subscription of userSubscriptions) { if (messages.length >= 7 || processedRooms.has(subscription._id)) continue; processedRooms.add(subscription._id);