update widget token mail 15

This commit is contained in:
Alma 2025-04-10 00:10:57 +02:00
parent 797e478fbb
commit a5a9307fdf

View File

@ -90,13 +90,13 @@ export async function GET() {
username: userInfo.user.username
});
// Get the user's subscriptions (rooms they are in) using their user ID
// Get the user's subscriptions (rooms they are in)
const subscriptionsResponse = await fetch(
`https://parole.slm-lab.net/api/v1/users.list.rooms?userId=${userInfo.user._id}`,
'https://parole.slm-lab.net/api/v1/subscriptions.get',
{
headers: {
'X-Auth-Token': ROCKET_CHAT_TOKEN,
'X-User-Id': ROCKET_CHAT_USER_ID,
'X-User-Id': userInfo.user._id,
'Content-Type': 'application/json'
}
}
@ -117,16 +117,23 @@ export async function GET() {
const subscriptions = await subscriptionsResponse.json();
console.log('Subscriptions response:', subscriptions);
if (!subscriptions.rooms || subscriptions.rooms.length === 0) {
if (!subscriptions.update || subscriptions.update.length === 0) {
console.log('No subscriptions found');
return NextResponse.json({ messages: [] });
}
// Get the last message from each room
const messages = await Promise.all(
subscriptions.rooms.map(async (room: any) => {
subscriptions.update.map(async (subscription: any) => {
try {
if (!subscription.rid) {
console.log('No room ID found for subscription:', subscription);
return null;
}
// Get the most recent messages from the room
const messagesResponse = await fetch(
`https://parole.slm-lab.net/api/v1/channels.messages?roomId=${room._id}&count=1`,
`https://parole.slm-lab.net/api/v1/rooms.get?roomId=${subscription.rid}`,
{
headers: {
'X-Auth-Token': ROCKET_CHAT_TOKEN,
@ -136,24 +143,31 @@ export async function GET() {
}
);
if (messagesResponse.ok) {
const messagesData = await messagesResponse.json();
if (messagesData.messages && messagesData.messages.length > 0) {
const message = messagesData.messages[0];
return {
...message,
roomName: room.name || 'Direct Message',
roomType: room.t,
};
}
} else {
console.error('Failed to fetch messages for room:', {
roomId: room._id,
if (!messagesResponse.ok) {
console.error('Failed to fetch room info:', {
roomId: subscription.rid,
status: messagesResponse.status,
response: await messagesResponse.text().catch(() => 'Could not get response text')
});
}
return null;
}
const roomData = await messagesResponse.json();
if (roomData.room?.lastMessage) {
return {
...roomData.room.lastMessage,
roomName: subscription.name || roomData.room.fname || 'Direct Message',
roomType: subscription.t,
};
}
console.log('No last message found for room:', subscription.rid);
return null;
} catch (error) {
console.error('Error processing room:', subscription.rid, error);
return null;
}
})
);