feat: api route to check stream info

This commit is contained in:
2025-02-01 15:31:47 +01:00
parent 4d699340b3
commit ba4a90587d
4 changed files with 81 additions and 2 deletions

View File

@@ -0,0 +1,10 @@
import prisma from "@/lib/db";
import type { NextRequest } from "next/server";
export async function GET(request: NextRequest) {
const db = await prisma.streamInfo.findMany({
include: { ownedBy: true },
});
return Response.json(db);
}

View File

@@ -9,14 +9,13 @@ import {
} from '@/components/ui/dialog';
import { validateRequest } from '@/lib/auth';
import prisma from '@/lib/db';
import { roomService } from '@/lib/services/livekit';
import { UniversalForm } from '../UniversalForm/UniversalForm';
import { editStreamInfo } from '@/lib/form/actions';
import RegenerateKey from '../RegenerateKey/RegenerateKey';
export default async function EditLivestream() {
const { user } = await validateRequest();
if ((await prisma.streamInfo.count({ where: { username: user!.username } })) === 0) {
/* if ((await prisma.streamInfo.count({ where: { username: user!.username } })) === 0) {
const isLive =
(await roomService.listRooms()).filter((r) => r.name === user!.username)[0].numPublishers >= 1;
await prisma.streamInfo.create({
@@ -33,6 +32,7 @@ export default async function EditLivestream() {
});
console.log('created');
}
}); */
const streamInfo = await prisma.streamInfo.findUnique({
where: { username: user!.username! },
});

5
src/instrumentation.ts Normal file
View File

@@ -0,0 +1,5 @@
export async function register() {
if (process.env.NEXT_RUNTIME === 'nodejs') {
await (await import('@/lib/instrumentation/streamInfo')).default();
}
}

View File

@@ -0,0 +1,64 @@
import prisma from "@/lib/db";
import { roomService } from "@/lib/services/livekit";
export default async function runner() {
await syncStream();
setInterval(syncStream, 5000);
}
export async function syncStream() {
try {
// Get all active rooms
const rooms = await roomService.listRooms();
// Process each room
for (const room of rooms) {
const isLive = room.numPublishers >= 1;
const originalStreamInfo = await prisma.streamInfo.findUnique({
where: { username: room.name }
});
console.log(room)
// Upsert stream info
await prisma.streamInfo.upsert({
where: {
username: room.name
},
/* create: {
username: room.name,
title: originalStreamInfo?.title || 'Untitled',
category: originalStreamInfo?.category || 'Uncategorized',
startedAt: originalStreamInfo?.isLive ? originalStreamInfo.startedAt : new Date(),
thumbnail: originalStreamInfo?.thumbnail || 'https://placehold.co/150',
viewers: room.numParticipants,
isLive,
ownedBy: {
connect: { username: room.name }
}
}, */
create: {
username: room.name,
title: 'Untitled',
category: 'Uncategorized',
startedAt: new Date(),
thumbnail: 'https://placehold.co/150',
viewers: 0,
isLive,
ownedBy: {
connect: { username: room.name }
}
},
update: {
isLive,
viewers: room.numParticipants - 1,
startedAt: !isLive ? new Date(0) :
(originalStreamInfo?.isLive ? originalStreamInfo.startedAt : new Date())
}
});
}
} catch (error) {
console.error('Error syncing room streams:', error);
throw error;
}
}