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