widget parole 3
This commit is contained in:
parent
a29f81286e
commit
f3953b673b
@ -45,26 +45,6 @@ export async function GET(request: Request) {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log('Session data:', {
|
|
||||||
hasToken: !!session.rocketChatToken,
|
|
||||||
hasUserId: !!session.rocketChatUserId,
|
|
||||||
tokenLength: session.rocketChatToken?.length,
|
|
||||||
userIdLength: session.rocketChatUserId?.length,
|
|
||||||
username: session.user?.username
|
|
||||||
});
|
|
||||||
|
|
||||||
if (!session.rocketChatToken || !session.rocketChatUserId) {
|
|
||||||
console.error('Missing Rocket.Chat credentials in session:', {
|
|
||||||
hasToken: !!session.rocketChatToken,
|
|
||||||
hasUserId: !!session.rocketChatUserId,
|
|
||||||
username: session.user?.username
|
|
||||||
});
|
|
||||||
return new Response(JSON.stringify({ error: 'Missing Rocket.Chat credentials' }), {
|
|
||||||
status: 401,
|
|
||||||
headers: { 'Content-Type': 'application/json' },
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const baseUrl = process.env.NEXT_PUBLIC_IFRAME_PAROLE_URL?.split('/channel')[0];
|
const baseUrl = process.env.NEXT_PUBLIC_IFRAME_PAROLE_URL?.split('/channel')[0];
|
||||||
if (!baseUrl) {
|
if (!baseUrl) {
|
||||||
@ -75,21 +55,16 @@ export async function GET(request: Request) {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
// Step 1 & 2: Get user token using admin credentials
|
// Use admin token to get messages
|
||||||
const userToken = await getUserToken(baseUrl);
|
const adminHeaders = {
|
||||||
if (!userToken) {
|
'X-Auth-Token': process.env.ROCKET_CHAT_TOKEN!,
|
||||||
return new Response(JSON.stringify({ error: 'Failed to create user token' }), {
|
'X-User-Id': process.env.ROCKET_CHAT_USER_ID!,
|
||||||
status: 401,
|
'Content-Type': 'application/json'
|
||||||
headers: { 'Content-Type': 'application/json' },
|
};
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
// Step 3: Use the user token to fetch subscriptions
|
// Get user's subscriptions using admin token
|
||||||
const subscriptionsResponse = await fetch(`${baseUrl}/api/v1/subscriptions.get`, {
|
const subscriptionsResponse = await fetch(`${baseUrl}/api/v1/subscriptions.get`, {
|
||||||
headers: {
|
headers: adminHeaders
|
||||||
'X-Auth-Token': userToken.authToken,
|
|
||||||
'X-User-Id': userToken.userId,
|
|
||||||
},
|
|
||||||
});
|
});
|
||||||
|
|
||||||
if (!subscriptionsResponse.ok) {
|
if (!subscriptionsResponse.ok) {
|
||||||
@ -105,7 +80,7 @@ export async function GET(request: Request) {
|
|||||||
const messages: any[] = [];
|
const messages: any[] = [];
|
||||||
const processedRooms = new Set();
|
const processedRooms = new Set();
|
||||||
|
|
||||||
// Step 3: Use the same user token to fetch messages
|
// 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.rid)) continue;
|
||||||
processedRooms.add(subscription.rid);
|
processedRooms.add(subscription.rid);
|
||||||
@ -114,11 +89,7 @@ export async function GET(request: Request) {
|
|||||||
const messagesResponse = await fetch(
|
const messagesResponse = await fetch(
|
||||||
`${baseUrl}/api/v1/chat.getMessage`, {
|
`${baseUrl}/api/v1/chat.getMessage`, {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
headers: {
|
headers: adminHeaders,
|
||||||
'X-Auth-Token': userToken.authToken,
|
|
||||||
'X-User-Id': userToken.userId,
|
|
||||||
'Content-Type': 'application/json',
|
|
||||||
},
|
|
||||||
body: JSON.stringify({
|
body: JSON.stringify({
|
||||||
msgId: subscription.lastMessage?._id,
|
msgId: subscription.lastMessage?._id,
|
||||||
roomId: subscription.rid,
|
roomId: subscription.rid,
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user