508903b67d
- #7: SSE events 接口校验 userId 房间成员身份,start() 加 try/catch - #9: Favorite 新增 restaurantId 字段做精确去重,不再用 JSON contains - #10: 补齐 Decision/Favorite/Room/BlindBoxIdea 缺失索引 - #11: Decision/Favorite/BlindBoxMember/BlindBoxIdea 加 onDelete Cascade
96 lines
2.3 KiB
TypeScript
96 lines
2.3 KiB
TypeScript
import { buildRoomStatus } from "@/lib/buildRoomStatus";
|
|
import { getRoomData } from "@/lib/store";
|
|
import { subscribe } from "@/lib/roomEvents";
|
|
|
|
export const dynamic = "force-dynamic";
|
|
|
|
export async function GET(
|
|
req: Request,
|
|
{ params }: { params: Promise<{ id: string }> },
|
|
) {
|
|
const { id } = await params;
|
|
|
|
const url = new URL(req.url);
|
|
const userId = url.searchParams.get("userId");
|
|
|
|
if (userId) {
|
|
const data = await getRoomData(id);
|
|
if (data && !data.users.includes(userId)) {
|
|
return new Response(JSON.stringify({ error: "not_a_member" }), {
|
|
status: 403,
|
|
headers: { "Content-Type": "application/json" },
|
|
});
|
|
}
|
|
}
|
|
|
|
const encoder = new TextEncoder();
|
|
|
|
const stream = new ReadableStream({
|
|
start(controller) {
|
|
const send = (obj: object) => {
|
|
try {
|
|
controller.enqueue(
|
|
encoder.encode(`data: ${JSON.stringify(obj)}\n\n`),
|
|
);
|
|
} catch {
|
|
/* controller already closed */
|
|
}
|
|
};
|
|
|
|
let alive = true;
|
|
|
|
(async () => {
|
|
try {
|
|
const status = await buildRoomStatus(id);
|
|
if (!status) {
|
|
send({ error: "room_not_found" });
|
|
controller.close();
|
|
return;
|
|
}
|
|
if (alive) send(status);
|
|
} catch {
|
|
send({ error: "load_failed" });
|
|
controller.close();
|
|
}
|
|
})();
|
|
|
|
const unsubscribe = subscribe(id, async () => {
|
|
if (!alive) return;
|
|
try {
|
|
const status = await buildRoomStatus(id);
|
|
if (status && alive) send(status);
|
|
} catch {
|
|
/* ignore transient read errors */
|
|
}
|
|
});
|
|
|
|
const heartbeat = setInterval(() => {
|
|
try {
|
|
controller.enqueue(encoder.encode(": heartbeat\n\n"));
|
|
} catch {
|
|
clearInterval(heartbeat);
|
|
}
|
|
}, 30_000);
|
|
|
|
req.signal.addEventListener("abort", () => {
|
|
alive = false;
|
|
unsubscribe();
|
|
clearInterval(heartbeat);
|
|
try {
|
|
controller.close();
|
|
} catch {
|
|
/* already closed */
|
|
}
|
|
});
|
|
},
|
|
});
|
|
|
|
return new Response(stream, {
|
|
headers: {
|
|
"Content-Type": "text/event-stream",
|
|
"Cache-Control": "no-cache, no-transform",
|
|
Connection: "keep-alive",
|
|
},
|
|
});
|
|
}
|