widget parole 5
This commit is contained in:
parent
f3953b673b
commit
ed34ef30eb
@ -55,6 +55,8 @@ export async function GET(request: Request) {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
console.log('Using Rocket.Chat base URL:', baseUrl);
|
||||||
|
|
||||||
// Use admin token to get messages
|
// Use admin token to get messages
|
||||||
const adminHeaders = {
|
const adminHeaders = {
|
||||||
'X-Auth-Token': process.env.ROCKET_CHAT_TOKEN!,
|
'X-Auth-Token': process.env.ROCKET_CHAT_TOKEN!,
|
||||||
@ -62,8 +64,34 @@ export async function GET(request: Request) {
|
|||||||
'Content-Type': 'application/json'
|
'Content-Type': 'application/json'
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// First, get the user's Rocket.Chat ID using their email
|
||||||
|
const userInfoResponse = await fetch(`${baseUrl}/api/v1/users.info`, {
|
||||||
|
method: 'GET',
|
||||||
|
headers: {
|
||||||
|
...adminHeaders,
|
||||||
|
'Content-Type': 'application/json'
|
||||||
|
},
|
||||||
|
// Add email as query parameter
|
||||||
|
query: `?username=${session.user.email?.split('@')[0]}`
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!userInfoResponse.ok) {
|
||||||
|
console.error('Failed to get user info:', userInfoResponse.status);
|
||||||
|
return new Response(JSON.stringify({ error: 'Failed to get user info' }), {
|
||||||
|
status: userInfoResponse.status,
|
||||||
|
headers: { 'Content-Type': 'application/json' },
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
const userInfo = await userInfoResponse.json();
|
||||||
|
console.log('Found Rocket.Chat user:', {
|
||||||
|
username: userInfo.user?.username,
|
||||||
|
id: userInfo.user?._id
|
||||||
|
});
|
||||||
|
|
||||||
// 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.list`, {
|
||||||
|
method: 'GET',
|
||||||
headers: adminHeaders
|
headers: adminHeaders
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -76,32 +104,55 @@ export async function GET(request: Request) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const subscriptionsData = await subscriptionsResponse.json();
|
const subscriptionsData = await subscriptionsResponse.json();
|
||||||
const userSubscriptions = subscriptionsData.update;
|
console.log('Subscriptions response:', {
|
||||||
|
success: subscriptionsData.success,
|
||||||
|
count: subscriptionsData.count,
|
||||||
|
subscriptionsCount: subscriptionsData.subscriptions?.length
|
||||||
|
});
|
||||||
|
|
||||||
|
// Filter subscriptions for the current user
|
||||||
|
const userSubscriptions = subscriptionsData.subscriptions.filter((sub: any) =>
|
||||||
|
sub.u?._id === userInfo.user._id ||
|
||||||
|
(sub.userMentions > 0 && sub.alert)
|
||||||
|
);
|
||||||
|
|
||||||
|
console.log('Filtered subscriptions:', {
|
||||||
|
total: userSubscriptions.length,
|
||||||
|
roomTypes: userSubscriptions.map((sub: any) => sub.t)
|
||||||
|
});
|
||||||
|
|
||||||
const messages: any[] = [];
|
const messages: any[] = [];
|
||||||
const processedRooms = new Set();
|
const processedRooms = new Set();
|
||||||
|
|
||||||
// Fetch messages using admin token
|
// Fetch messages using admin token
|
||||||
for (const subscription of userSubscriptions) {
|
for (const subscription of userSubscriptions) {
|
||||||
if (messages.length >= 6 || processedRooms.has(subscription.rid)) continue;
|
if (messages.length >= 6 || processedRooms.has(subscription._id)) continue;
|
||||||
processedRooms.add(subscription.rid);
|
processedRooms.add(subscription._id);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
// Get the latest messages from the room
|
||||||
const messagesResponse = await fetch(
|
const messagesResponse = await fetch(
|
||||||
`${baseUrl}/api/v1/chat.getMessage`, {
|
`${baseUrl}/api/v1/channels.messages`, {
|
||||||
method: 'POST',
|
method: 'GET',
|
||||||
headers: adminHeaders,
|
headers: adminHeaders,
|
||||||
body: JSON.stringify({
|
query: `?roomId=${subscription.rid}&count=1`
|
||||||
msgId: subscription.lastMessage?._id,
|
|
||||||
roomId: subscription.rid,
|
|
||||||
}),
|
|
||||||
});
|
});
|
||||||
|
|
||||||
if (!messagesResponse.ok) continue;
|
if (!messagesResponse.ok) {
|
||||||
|
console.error(`Failed to get messages for room ${subscription.name}:`, messagesResponse.status);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
const messageData = await messagesResponse.json();
|
const messageData = await messagesResponse.json();
|
||||||
if (messageData.message) {
|
console.log(`Messages for room ${subscription.name}:`, {
|
||||||
|
success: messageData.success,
|
||||||
|
count: messageData.count,
|
||||||
|
hasMessages: messageData.messages?.length > 0
|
||||||
|
});
|
||||||
|
|
||||||
|
if (messageData.success && messageData.messages?.length > 0) {
|
||||||
messages.push({
|
messages.push({
|
||||||
...messageData.message,
|
...messageData.messages[0],
|
||||||
roomName: subscription.fname || subscription.name || 'Direct Message',
|
roomName: subscription.fname || subscription.name || 'Direct Message',
|
||||||
roomType: subscription.t,
|
roomType: subscription.t,
|
||||||
unread: subscription.unread || 0,
|
unread: subscription.unread || 0,
|
||||||
@ -110,11 +161,13 @@ export async function GET(request: Request) {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error(`Error fetching message for room ${subscription.rid}`);
|
console.error(`Error fetching message for room ${subscription.name}:`, error);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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);
|
||||||
@ -124,7 +177,7 @@ export async function GET(request: Request) {
|
|||||||
headers: { 'Content-Type': 'application/json' },
|
headers: { 'Content-Type': 'application/json' },
|
||||||
});
|
});
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Error fetching messages');
|
console.error('Error fetching messages:', error);
|
||||||
return new Response(JSON.stringify({ error: 'Internal server error' }), {
|
return new Response(JSON.stringify({ error: 'Internal server error' }), {
|
||||||
status: 500,
|
status: 500,
|
||||||
headers: { 'Content-Type': 'application/json' },
|
headers: { 'Content-Type': 'application/json' },
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user