update widget token 3
This commit is contained in:
parent
8e8894c819
commit
c18ff84673
@ -9,7 +9,7 @@ export async function GET() {
|
||||
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', {
|
||||
method: 'GET',
|
||||
headers: {
|
||||
@ -33,8 +33,8 @@ export async function GET() {
|
||||
const userInfo = await userInfoResponse.json();
|
||||
const userId = userInfo.user._id;
|
||||
|
||||
// Get the last messages for the user
|
||||
const response = await fetch(`https://parole.slm-lab.net/api/v1/im.messages?userId=${userId}&count=50`, {
|
||||
// Get the user's subscriptions (rooms they are in)
|
||||
const subscriptionsResponse = await fetch('https://parole.slm-lab.net/api/v1/subscriptions.get', {
|
||||
headers: {
|
||||
'X-Auth-Token': 'C_3ekrsgtsaU0sVQzpJ8aRSyMQjBIvcsmXVvBI8Wmgb',
|
||||
'X-User-Id': 'Tpuww59PJKsrGNQJB',
|
||||
@ -42,19 +42,50 @@ export async function GET() {
|
||||
cache: 'no-store',
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
console.error('Rocket.Chat API error:', {
|
||||
status: response.status,
|
||||
statusText: response.statusText,
|
||||
if (!subscriptionsResponse.ok) {
|
||||
console.error('Rocket.Chat subscriptions error:', {
|
||||
status: subscriptionsResponse.status,
|
||||
statusText: subscriptionsResponse.statusText,
|
||||
});
|
||||
return NextResponse.json(
|
||||
{ error: "Failed to fetch messages" },
|
||||
{ status: response.status }
|
||||
{ error: "Failed to fetch subscriptions" },
|
||||
{ status: subscriptionsResponse.status }
|
||||
);
|
||||
}
|
||||
|
||||
const data = await response.json();
|
||||
return NextResponse.json(data);
|
||||
const subscriptions = await subscriptionsResponse.json();
|
||||
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) {
|
||||
console.error('Error fetching messages:', error);
|
||||
return NextResponse.json(
|
||||
|
||||
Loading…
Reference in New Issue
Block a user