mirror of
https://github.com/SrIzan10/hctv.git
synced 2026-06-06 00:56:56 +00:00
feat: api route to check stream info
This commit is contained in:
10
src/app/(protected)/api/stream/info/route.ts
Normal file
10
src/app/(protected)/api/stream/info/route.ts
Normal 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);
|
||||
}
|
||||
@@ -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
5
src/instrumentation.ts
Normal file
@@ -0,0 +1,5 @@
|
||||
export async function register() {
|
||||
if (process.env.NEXT_RUNTIME === 'nodejs') {
|
||||
await (await import('@/lib/instrumentation/streamInfo')).default();
|
||||
}
|
||||
}
|
||||
64
src/lib/instrumentation/streamInfo.ts
Normal file
64
src/lib/instrumentation/streamInfo.ts
Normal 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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user