widget parole 18
This commit is contained in:
parent
5f41d9ed8d
commit
1a7bdddac0
@ -98,40 +98,23 @@ export async function GET(request: Request) {
|
||||
id: currentUser._id
|
||||
});
|
||||
|
||||
// Step 3: Login as the user to get their token
|
||||
const loginResponse = await fetch(`${baseUrl}/api/v1/login`, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json'
|
||||
},
|
||||
body: JSON.stringify({
|
||||
user: currentUser.username,
|
||||
password: process.env.ROCKET_CHAT_USER_PASSWORD // This should be the user's password or a shared password
|
||||
})
|
||||
});
|
||||
|
||||
if (!loginResponse.ok) {
|
||||
console.error('Failed to login as user:', loginResponse.status);
|
||||
const errorText = await loginResponse.text();
|
||||
console.error('Login error details:', errorText);
|
||||
return NextResponse.json({ messages: [] }, { status: 200 });
|
||||
}
|
||||
|
||||
const loginData = await loginResponse.json();
|
||||
// Step 3: Use admin token to get messages
|
||||
const userHeaders = {
|
||||
'X-Auth-Token': loginData.data.authToken,
|
||||
'X-User-Id': loginData.data.userId,
|
||||
'X-Auth-Token': process.env.ROCKET_CHAT_TOKEN!,
|
||||
'X-User-Id': process.env.ROCKET_CHAT_USER_ID!,
|
||||
'Content-Type': 'application/json'
|
||||
};
|
||||
|
||||
// Step 4: Get user's subscriptions using user token
|
||||
const subscriptionsResponse = await fetch(`${baseUrl}/api/v1/subscriptions.get`, {
|
||||
// Step 4: Get user's subscriptions using admin token
|
||||
const subscriptionsResponse = await fetch(`${baseUrl}/api/v1/subscriptions.get?userId=${currentUser._id}`, {
|
||||
method: 'GET',
|
||||
headers: userHeaders
|
||||
});
|
||||
|
||||
if (!subscriptionsResponse.ok) {
|
||||
console.error('Failed to get subscriptions:', subscriptionsResponse.status);
|
||||
const errorText = await subscriptionsResponse.text();
|
||||
console.error('Subscriptions error details:', errorText);
|
||||
return NextResponse.json({ messages: [] }, { status: 200 });
|
||||
}
|
||||
|
||||
@ -155,8 +138,8 @@ export async function GET(request: Request) {
|
||||
if (sub.t === 'd') {
|
||||
return sub.rid.includes(currentUser._id);
|
||||
}
|
||||
// For channels (t: 'c'), include if user has unread messages or mentions
|
||||
return sub.t === 'c' && (sub.unread > 0 || sub.userMentions > 0 || sub.alert);
|
||||
// For channels (t: 'c'), include all channels
|
||||
return sub.t === 'c';
|
||||
});
|
||||
|
||||
console.log('Filtered subscriptions:', {
|
||||
@ -173,7 +156,7 @@ export async function GET(request: Request) {
|
||||
const messages: any[] = [];
|
||||
const processedRooms = new Set();
|
||||
|
||||
// Step 5: Fetch messages using user token
|
||||
// Step 5: Fetch messages using admin token
|
||||
for (const subscription of userSubscriptions) {
|
||||
if (messages.length >= 7 || processedRooms.has(subscription._id)) continue;
|
||||
processedRooms.add(subscription._id);
|
||||
|
||||
Loading…
Reference in New Issue
Block a user