NeahFront3/app/api/rocket-chat/messages/route.ts
2025-04-09 22:59:26 +02:00

65 lines
1.9 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 });
}
// First, get the user's info from Rocket.Chat using their email
const userInfoResponse = await fetch('https://parole.slm-lab.net/api/v1/users.info', {
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,
});
return NextResponse.json(
{ error: "Failed to fetch user info" },
{ status: userInfoResponse.status }
);
}
const userInfo = await userInfoResponse.json();
const userId = userInfo.user._id;
// Get the last messages for the user
const response = await fetch(`https://parole.slm-lab.net/api/v1/im.messages?userId=${userId}&count=50`, {
headers: {
'X-Auth-Token': 'C_3ekrsgtsaU0sVQzpJ8aRSyMQjBIvcsmXVvBI8Wmgb',
'X-User-Id': 'Tpuww59PJKsrGNQJB',
},
cache: 'no-store',
});
if (!response.ok) {
console.error('Rocket.Chat API error:', {
status: response.status,
statusText: response.statusText,
});
return NextResponse.json(
{ error: "Failed to fetch messages" },
{ status: response.status }
);
}
const data = await response.json();
return NextResponse.json(data);
} catch (error) {
console.error('Error fetching messages:', error);
return NextResponse.json(
{ error: "Internal server error" },
{ status: 500 }
);
}
}