35 lines
979 B
TypeScript
35 lines
979 B
TypeScript
import { getServerSession } from "next-auth";
|
|
import { authOptions } from "@/lib/auth-options";
|
|
import {
|
|
getAdminDiscordIds,
|
|
getSessionDiscordId,
|
|
} from "@/lib/auth";
|
|
import { isSseTopic, sse } from "@/lib/sse";
|
|
|
|
export async function GET(
|
|
request: Request,
|
|
{ params }: { params: Promise<{ topic: string }> },
|
|
) {
|
|
const { topic } = await params;
|
|
|
|
if (!isSseTopic(topic)) {
|
|
return new Response("Invalid SSE topic", { status: 404 });
|
|
}
|
|
|
|
const endpoint = sse[topic];
|
|
if (endpoint.adminOnly) {
|
|
const session = await getServerSession(authOptions);
|
|
const discordId = getSessionDiscordId(session);
|
|
if (!discordId || !getAdminDiscordIds().includes(discordId)) {
|
|
return new Response("Unauthorized", { status: 401 });
|
|
}
|
|
}
|
|
|
|
try {
|
|
return await endpoint.stream({ signal: request.signal });
|
|
} catch (error) {
|
|
console.error(`Failed to open SSE topic ${topic}`, error);
|
|
return new Response("SSE unavailable", { status: 503 });
|
|
}
|
|
}
|