mirror of
https://github.com/linsa-io/linsa.git
synced 2026-04-27 02:38:45 +02:00
Add stream_replays table, related schema, and API endpoints for managing stream replays
This commit is contained in:
66
packages/web/src/routes/api/streams.$username.replays.ts
Normal file
66
packages/web/src/routes/api/streams.$username.replays.ts
Normal 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,
|
||||
},
|
||||
},
|
||||
})
|
||||
Reference in New Issue
Block a user