update widget token mail 5
This commit is contained in:
parent
bad7e590fa
commit
f68a0a873d
@ -15,141 +15,41 @@ export async function GET() {
|
|||||||
accessToken: session.accessToken ? 'present' : 'missing'
|
accessToken: session.accessToken ? 'present' : 'missing'
|
||||||
});
|
});
|
||||||
|
|
||||||
// Use email prefix as username if username is not available
|
// First, login to Rocket.Chat using OAuth
|
||||||
const username = session.user.username || session.user.email?.split('@')[0];
|
const loginResponse = await fetch('https://parole.slm-lab.net/api/v1/login', {
|
||||||
if (!username) {
|
method: 'POST',
|
||||||
return NextResponse.json({ error: "No username or email found in session" }, { status: 400 });
|
headers: {
|
||||||
|
'Content-Type': 'application/json'
|
||||||
|
},
|
||||||
|
body: JSON.stringify({
|
||||||
|
serviceName: 'keycloak',
|
||||||
|
accessToken: session.accessToken,
|
||||||
|
expiresIn: 200
|
||||||
|
})
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!loginResponse.ok) {
|
||||||
|
console.error('Rocket.Chat login error:', {
|
||||||
|
status: loginResponse.status,
|
||||||
|
statusText: loginResponse.statusText,
|
||||||
|
response: await loginResponse.text().catch(() => 'Could not get response text')
|
||||||
|
});
|
||||||
|
return NextResponse.json(
|
||||||
|
{ error: "Failed to authenticate with Rocket.Chat" },
|
||||||
|
{ status: loginResponse.status }
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get the user's info from Rocket.Chat using their username
|
const loginData = await loginResponse.json();
|
||||||
const userInfoResponse = await fetch(
|
const { authToken, userId } = loginData.data;
|
||||||
`https://parole.slm-lab.net/api/v1/users.info?username=${encodeURIComponent(username)}`,
|
|
||||||
{
|
|
||||||
method: 'GET',
|
|
||||||
headers: {
|
|
||||||
'X-Auth-Token': 'C_3ekrsgtsaU0sVQzpJ8aRSyMQjBIvcsmXVvBI8Wmgb',
|
|
||||||
'X-User-Id': 'Tpuww59PJKsrGNQJB',
|
|
||||||
},
|
|
||||||
cache: 'no-store',
|
|
||||||
}
|
|
||||||
);
|
|
||||||
|
|
||||||
if (!userInfoResponse.ok) {
|
|
||||||
// If user info request fails, try searching by email
|
|
||||||
const searchResponse = await fetch(
|
|
||||||
`https://parole.slm-lab.net/api/v1/users.list?query={"emails.address":"${session.user.email}"}`,
|
|
||||||
{
|
|
||||||
method: 'GET',
|
|
||||||
headers: {
|
|
||||||
'X-Auth-Token': 'C_3ekrsgtsaU0sVQzpJ8aRSyMQjBIvcsmXVvBI8Wmgb',
|
|
||||||
'X-User-Id': 'Tpuww59PJKsrGNQJB',
|
|
||||||
},
|
|
||||||
cache: 'no-store',
|
|
||||||
}
|
|
||||||
);
|
|
||||||
|
|
||||||
if (!searchResponse.ok) {
|
|
||||||
console.error('Rocket.Chat user search error:', {
|
|
||||||
status: searchResponse.status,
|
|
||||||
statusText: searchResponse.statusText,
|
|
||||||
email: session.user.email,
|
|
||||||
response: await searchResponse.text().catch(() => 'Could not get response text')
|
|
||||||
});
|
|
||||||
return NextResponse.json(
|
|
||||||
{ error: "Failed to find user" },
|
|
||||||
{ status: searchResponse.status }
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
const searchData = await searchResponse.json();
|
|
||||||
if (!searchData.users || searchData.users.length === 0) {
|
|
||||||
return NextResponse.json({ error: "User not found in Rocket.Chat" }, { status: 404 });
|
|
||||||
}
|
|
||||||
|
|
||||||
const userInfo = { user: searchData.users[0] };
|
|
||||||
const userId = userInfo.user._id;
|
|
||||||
|
|
||||||
// Get the user's subscriptions (rooms they are in)
|
|
||||||
const subscriptionsResponse = await fetch(
|
|
||||||
`https://parole.slm-lab.net/api/v1/subscriptions.get?userId=${userId}`,
|
|
||||||
{
|
|
||||||
headers: {
|
|
||||||
'X-Auth-Token': 'C_3ekrsgtsaU0sVQzpJ8aRSyMQjBIvcsmXVvBI8Wmgb',
|
|
||||||
'X-User-Id': 'Tpuww59PJKsrGNQJB',
|
|
||||||
},
|
|
||||||
cache: 'no-store',
|
|
||||||
}
|
|
||||||
);
|
|
||||||
|
|
||||||
if (!subscriptionsResponse.ok) {
|
|
||||||
console.error('Rocket.Chat subscriptions error:', {
|
|
||||||
status: subscriptionsResponse.status,
|
|
||||||
statusText: subscriptionsResponse.statusText,
|
|
||||||
response: await subscriptionsResponse.text().catch(() => 'Could not get response text')
|
|
||||||
});
|
|
||||||
return NextResponse.json(
|
|
||||||
{ error: "Failed to fetch subscriptions" },
|
|
||||||
{ status: subscriptionsResponse.status }
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
const subscriptions = await subscriptionsResponse.json();
|
|
||||||
if (!subscriptions.update || subscriptions.update.length === 0) {
|
|
||||||
return NextResponse.json({ messages: [] });
|
|
||||||
}
|
|
||||||
|
|
||||||
// Get the last message from each room
|
|
||||||
const messages = await Promise.all(
|
|
||||||
subscriptions.update.map(async (room: any) => {
|
|
||||||
if (!room.lastMessage?._id) return null;
|
|
||||||
|
|
||||||
const messageResponse = await fetch(
|
|
||||||
`https://parole.slm-lab.net/api/v1/chat.getMessage?msgId=${room.lastMessage._id}`,
|
|
||||||
{
|
|
||||||
headers: {
|
|
||||||
'X-Auth-Token': 'C_3ekrsgtsaU0sVQzpJ8aRSyMQjBIvcsmXVvBI8Wmgb',
|
|
||||||
'X-User-Id': 'Tpuww59PJKsrGNQJB',
|
|
||||||
},
|
|
||||||
cache: 'no-store',
|
|
||||||
}
|
|
||||||
);
|
|
||||||
|
|
||||||
if (messageResponse.ok) {
|
|
||||||
const messageData = await messageResponse.json();
|
|
||||||
return {
|
|
||||||
...messageData.message,
|
|
||||||
roomName: room.name || 'Direct Message',
|
|
||||||
roomType: room.t,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
console.error('Failed to fetch message:', {
|
|
||||||
roomId: room._id,
|
|
||||||
messageId: room.lastMessage._id,
|
|
||||||
status: messageResponse.status,
|
|
||||||
response: await messageResponse.text().catch(() => 'Could not get response text')
|
|
||||||
});
|
|
||||||
return null;
|
|
||||||
})
|
|
||||||
);
|
|
||||||
|
|
||||||
// Filter out any null messages and sort by timestamp
|
|
||||||
const validMessages = messages
|
|
||||||
.filter((msg): msg is NonNullable<typeof msg> => msg !== null)
|
|
||||||
.sort((a, b) => new Date(b.ts).getTime() - new Date(a.ts).getTime());
|
|
||||||
|
|
||||||
return NextResponse.json({ messages: validMessages });
|
|
||||||
}
|
|
||||||
|
|
||||||
const userInfo = await userInfoResponse.json();
|
|
||||||
const userId = userInfo.user._id;
|
|
||||||
|
|
||||||
// Get the user's subscriptions (rooms they are in)
|
// Get the user's subscriptions (rooms they are in)
|
||||||
const subscriptionsResponse = await fetch(
|
const subscriptionsResponse = await fetch(
|
||||||
`https://parole.slm-lab.net/api/v1/subscriptions.get?userId=${userId}`,
|
'https://parole.slm-lab.net/api/v1/subscriptions.get',
|
||||||
{
|
{
|
||||||
headers: {
|
headers: {
|
||||||
'X-Auth-Token': 'C_3ekrsgtsaU0sVQzpJ8aRSyMQjBIvcsmXVvBI8Wmgb',
|
'X-Auth-Token': authToken,
|
||||||
'X-User-Id': 'Tpuww59PJKsrGNQJB',
|
'X-User-Id': userId,
|
||||||
},
|
},
|
||||||
cache: 'no-store',
|
cache: 'no-store',
|
||||||
}
|
}
|
||||||
@ -181,8 +81,8 @@ export async function GET() {
|
|||||||
`https://parole.slm-lab.net/api/v1/chat.getMessage?msgId=${room.lastMessage._id}`,
|
`https://parole.slm-lab.net/api/v1/chat.getMessage?msgId=${room.lastMessage._id}`,
|
||||||
{
|
{
|
||||||
headers: {
|
headers: {
|
||||||
'X-Auth-Token': 'C_3ekrsgtsaU0sVQzpJ8aRSyMQjBIvcsmXVvBI8Wmgb',
|
'X-Auth-Token': authToken,
|
||||||
'X-User-Id': 'Tpuww59PJKsrGNQJB',
|
'X-User-Id': userId,
|
||||||
},
|
},
|
||||||
cache: 'no-store',
|
cache: 'no-store',
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user