diff --git a/app/api/rocket-chat/messages/route.ts b/app/api/rocket-chat/messages/route.ts index d32ca2ec..f80a8238 100644 --- a/app/api/rocket-chat/messages/route.ts +++ b/app/api/rocket-chat/messages/route.ts @@ -66,44 +66,6 @@ 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 by username:', userInfoResponse.status); - const errorData = await userInfoResponse.json(); - console.error('User info error details:', errorData); - - // 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 }); - } - - 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`, { method: 'GET', @@ -124,9 +86,44 @@ export async function GET(request: Request) { subscriptionsCount: subscriptionsData.subscriptions?.length }); + // Get all users to find the current user + const usersResponse = await fetch(`${baseUrl}/api/v1/users.list`, { + method: 'GET', + headers: adminHeaders + }); + + if (!usersResponse.ok) { + console.error('Failed to get users list:', usersResponse.status); + const errorText = await usersResponse.text(); + console.error('Users list error details:', errorText); + return NextResponse.json({ messages: [] }, { status: 200 }); + } + + const usersData = await usersResponse.json(); + console.log('Users list response:', { + success: usersData.success, + count: usersData.count, + usersCount: usersData.users?.length + }); + + // Find the current user in the list + const currentUser = usersData.users.find((user: any) => + user.username === username || user.emails?.some((email: any) => email.address === session.user.email) + ); + + if (!currentUser) { + console.error('User not found in users list'); + return NextResponse.json({ messages: [] }, { status: 200 }); + } + + console.log('Found Rocket.Chat user:', { + username: currentUser.username, + id: currentUser._id + }); + // Filter subscriptions for the current user const userSubscriptions = subscriptionsData.subscriptions.filter((sub: any) => - sub.u?._id === userInfo.user._id || + sub.u?._id === currentUser._id || (sub.userMentions > 0 && sub.alert) );