102 lines
3.2 KiB
TypeScript
102 lines
3.2 KiB
TypeScript
import { getServerSession } from "next-auth";
|
|
import { authOptions } from "@/app/api/auth/[...nextauth]/route";
|
|
import { NextResponse } from "next/server";
|
|
|
|
export async function GET() {
|
|
try {
|
|
const session = await getServerSession(authOptions);
|
|
if (!session) {
|
|
return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
|
|
}
|
|
|
|
// Get the user's info from Rocket.Chat using their username
|
|
const userInfoResponse = await fetch(
|
|
`https://parole.slm-lab.net/api/v1/users.info?username=${session.user.username}`,
|
|
{
|
|
method: 'GET',
|
|
headers: {
|
|
'X-Auth-Token': 'C_3ekrsgtsaU0sVQzpJ8aRSyMQjBIvcsmXVvBI8Wmgb',
|
|
'X-User-Id': 'Tpuww59PJKsrGNQJB',
|
|
},
|
|
cache: 'no-store',
|
|
}
|
|
);
|
|
|
|
if (!userInfoResponse.ok) {
|
|
console.error('Rocket.Chat user info error:', {
|
|
status: userInfoResponse.status,
|
|
statusText: userInfoResponse.statusText,
|
|
username: session.user.username,
|
|
});
|
|
return NextResponse.json(
|
|
{ error: "Failed to fetch user info" },
|
|
{ status: userInfoResponse.status }
|
|
);
|
|
}
|
|
|
|
const userInfo = await userInfoResponse.json();
|
|
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', {
|
|
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,
|
|
});
|
|
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;
|
|
}
|
|
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 });
|
|
} catch (error) {
|
|
console.error('Error fetching messages:', error);
|
|
return NextResponse.json(
|
|
{ error: "Internal server error" },
|
|
{ status: 500 }
|
|
);
|
|
}
|
|
}
|