update widget token 3

This commit is contained in:
Alma 2025-04-09 23:02:03 +02:00
parent 8e8894c819
commit c18ff84673

View File

@ -9,7 +9,7 @@ export async function GET() {
return NextResponse.json({ error: "Unauthorized" }, { status: 401 }); return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
} }
// First, get the user's info from Rocket.Chat using their email // First, get the user's info from Rocket.Chat
const userInfoResponse = await fetch('https://parole.slm-lab.net/api/v1/users.info', { const userInfoResponse = await fetch('https://parole.slm-lab.net/api/v1/users.info', {
method: 'GET', method: 'GET',
headers: { headers: {
@ -33,8 +33,8 @@ export async function GET() {
const userInfo = await userInfoResponse.json(); const userInfo = await userInfoResponse.json();
const userId = userInfo.user._id; const userId = userInfo.user._id;
// Get the last messages for the user // Get the user's subscriptions (rooms they are in)
const response = await fetch(`https://parole.slm-lab.net/api/v1/im.messages?userId=${userId}&count=50`, { const subscriptionsResponse = await fetch('https://parole.slm-lab.net/api/v1/subscriptions.get', {
headers: { headers: {
'X-Auth-Token': 'C_3ekrsgtsaU0sVQzpJ8aRSyMQjBIvcsmXVvBI8Wmgb', 'X-Auth-Token': 'C_3ekrsgtsaU0sVQzpJ8aRSyMQjBIvcsmXVvBI8Wmgb',
'X-User-Id': 'Tpuww59PJKsrGNQJB', 'X-User-Id': 'Tpuww59PJKsrGNQJB',
@ -42,19 +42,50 @@ export async function GET() {
cache: 'no-store', cache: 'no-store',
}); });
if (!response.ok) { if (!subscriptionsResponse.ok) {
console.error('Rocket.Chat API error:', { console.error('Rocket.Chat subscriptions error:', {
status: response.status, status: subscriptionsResponse.status,
statusText: response.statusText, statusText: subscriptionsResponse.statusText,
}); });
return NextResponse.json( return NextResponse.json(
{ error: "Failed to fetch messages" }, { error: "Failed to fetch subscriptions" },
{ status: response.status } { status: subscriptionsResponse.status }
); );
} }
const data = await response.json(); const subscriptions = await subscriptionsResponse.json();
return NextResponse.json(data); if (!subscriptions.update || subscriptions.update.length === 0) {
return NextResponse.json({ messages: [] });
}
// Get the last message from each room
const messages = await Promise.all(
subscriptions.update.map(async (room: any) => {
const messageResponse = await fetch(
`https://parole.slm-lab.net/api/v1/chat.getMessage?msgId=${room.lastMessage?._id}`,
{
headers: {
'X-Auth-Token': 'C_3ekrsgtsaU0sVQzpJ8aRSyMQjBIvcsmXVvBI8Wmgb',
'X-User-Id': 'Tpuww59PJKsrGNQJB',
},
cache: 'no-store',
}
);
if (messageResponse.ok) {
const messageData = await messageResponse.json();
return messageData.message;
}
return null;
})
);
// Filter out any null messages and sort by timestamp
const validMessages = messages
.filter((msg): msg is NonNullable<typeof msg> => msg !== null)
.sort((a, b) => new Date(b.ts).getTime() - new Date(a.ts).getTime());
return NextResponse.json({ messages: validMessages });
} catch (error) { } catch (error) {
console.error('Error fetching messages:', error); console.error('Error fetching messages:', error);
return NextResponse.json( return NextResponse.json(