Add stream_replays table, related schema, and API endpoints for managing stream replays

This commit is contained in:
Nikita
2025-12-23 23:20:22 -08:00
parent 1bb0450d02
commit 7f6f7d2f37
9 changed files with 643 additions and 2 deletions

View File

@@ -0,0 +1,66 @@
import { createFileRoute } from "@tanstack/react-router"
import { and, desc, eq } from "drizzle-orm"
import { db } from "@/db/connection"
import { getAuth } from "@/lib/auth"
import { stream_replays, users } from "@/db/schema"
const json = (data: unknown, status = 200) =>
new Response(JSON.stringify(data), {
status,
headers: { "content-type": "application/json" },
})
const handleGet = async ({
request,
params,
}: {
request: Request
params: { username: string }
}) => {
const { username } = params
if (!username) {
return json({ error: "Username required" }, 400)
}
const database = db()
const user = await database.query.users.findFirst({
where: eq(users.username, username),
})
if (!user) {
return json({ error: "User not found" }, 404)
}
const auth = getAuth()
const session = await auth.api.getSession({ headers: request.headers })
const isOwner = session?.user?.id === user.id
const conditions = [eq(stream_replays.user_id, user.id)]
if (!isOwner) {
conditions.push(eq(stream_replays.is_public, true))
conditions.push(eq(stream_replays.status, "ready"))
}
try {
const replays = await database
.select()
.from(stream_replays)
.where(and(...conditions))
.orderBy(desc(stream_replays.started_at), desc(stream_replays.created_at))
return json({ replays })
} catch (error) {
console.error("[stream-replays] Error fetching replays:", error)
return json({ error: "Failed to fetch replays" }, 500)
}
}
export const Route = createFileRoute("/api/streams/$username/replays")({
server: {
handlers: {
GET: handleGet,
},
},
})