widget parole 9

This commit is contained in:
Alma 2025-04-13 00:28:19 +02:00
parent 7e2151b5ff
commit f9b7ef1891

View File

@ -35,24 +35,18 @@ async function getUserToken(baseUrl: string) {
}
export async function GET(request: Request) {
const session = await getServerSession(authOptions);
if (!session) {
console.error('No session found');
return new Response(JSON.stringify({ error: 'Unauthorized' }), {
status: 401,
headers: { 'Content-Type': 'application/json' },
});
}
try {
const session = await getServerSession(authOptions);
if (!session?.user?.email) {
console.error('No valid session or email found');
return NextResponse.json({ messages: [] }, { status: 200 });
}
const baseUrl = process.env.NEXT_PUBLIC_IFRAME_PAROLE_URL?.split('/channel')[0];
if (!baseUrl) {
console.error('Failed to get Rocket.Chat base URL');
return new Response(JSON.stringify({ error: 'Server configuration error' }), {
status: 500,
headers: { 'Content-Type': 'application/json' },
});
return NextResponse.json({ error: 'Server configuration error' }, { status: 500 });
}
console.log('Using Rocket.Chat base URL:', baseUrl);
@ -61,25 +55,20 @@ export async function GET(request: Request) {
const adminHeaders = {
'X-Auth-Token': process.env.ROCKET_CHAT_TOKEN!,
'X-User-Id': process.env.ROCKET_CHAT_USER_ID!,
'Content-Type': 'application/json'
'Content-Type': 'application/json',
'Accept': 'application/json'
};
// First, get the user's Rocket.Chat ID using their email
const username = session.user.email?.split('@')[0];
const username = session.user.email.split('@')[0];
if (!username) {
console.error('No username found in session email');
return new Response(JSON.stringify({ error: 'No username found' }), {
status: 400,
headers: { 'Content-Type': 'application/json' },
});
return NextResponse.json({ messages: [] }, { status: 200 });
}
const userInfoResponse = await fetch(`${baseUrl}/api/v1/users.info?username=${encodeURIComponent(username)}`, {
method: 'GET',
headers: {
...adminHeaders,
'Content-Type': 'application/json'
}
headers: adminHeaders
});
if (!userInfoResponse.ok) {
@ -89,16 +78,10 @@ export async function GET(request: Request) {
// If user not found, return empty messages instead of error
if (errorData.error === 'User not found.') {
return new Response(JSON.stringify({ messages: [] }), {
status: 200,
headers: { 'Content-Type': 'application/json' },
});
return NextResponse.json({ messages: [] }, { status: 200 });
}
return new Response(JSON.stringify({ error: 'Failed to get user info' }), {
status: userInfoResponse.status,
headers: { 'Content-Type': 'application/json' },
});
return NextResponse.json({ error: 'Failed to get user info' }, { status: userInfoResponse.status });
}
const userInfo = await userInfoResponse.json();
@ -110,20 +93,14 @@ export async function GET(request: Request) {
// Get user's subscriptions using admin token
const subscriptionsResponse = await fetch(`${baseUrl}/api/v1/subscriptions.get`, {
method: 'GET',
headers: {
...adminHeaders,
'Accept': 'application/json'
}
headers: adminHeaders
});
if (!subscriptionsResponse.ok) {
console.error('Failed to get subscriptions:', subscriptionsResponse.status);
const errorText = await subscriptionsResponse.text();
console.error('Subscriptions error details:', errorText);
return new Response(JSON.stringify({ error: 'Failed to get subscriptions' }), {
status: subscriptionsResponse.status,
headers: { 'Content-Type': 'application/json' },
});
return NextResponse.json({ messages: [] }, { status: 200 });
}
const subscriptionsData = await subscriptionsResponse.json();
@ -157,10 +134,7 @@ export async function GET(request: Request) {
const messagesResponse = await fetch(
`${baseUrl}/api/v1/chat.getMessage?roomId=${subscription.rid}`, {
method: 'GET',
headers: {
...adminHeaders,
'Accept': 'application/json'
}
headers: adminHeaders
});
if (!messagesResponse.ok) {
@ -200,21 +174,13 @@ export async function GET(request: Request) {
}
}
console.log('Final messages count:', messages.length);
// Sort messages by timestamp (newest first) and limit to 6
messages.sort((a, b) => new Date(b.ts).getTime() - new Date(a.ts).getTime());
const limitedMessages = messages.slice(0, 6);
return new Response(JSON.stringify({ messages: limitedMessages }), {
status: 200,
headers: { 'Content-Type': 'application/json' },
});
return NextResponse.json({ messages: limitedMessages }, { status: 200 });
} catch (error) {
console.error('Error fetching messages:', error);
return new Response(JSON.stringify({ error: 'Internal server error' }), {
status: 500,
headers: { 'Content-Type': 'application/json' },
});
console.error('Error in messages endpoint:', error);
return NextResponse.json({ messages: [] }, { status: 200 });
}
}