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