Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
119 changes: 54 additions & 65 deletions next-app/app/api/streams/route.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
import { z } from "zod";
import { NextRequest, NextResponse } from "next/server";
import db from "@/lib/db";
//@ts-ignore
import youtubesearchapi from "youtube-search-api";
import { YT_REGEX } from "@/lib/utils";
import { getServerSession } from "next-auth";
import { authOptions } from "@/lib/auth-options";

import axios from "axios";

const CreateStreamSchema = z.object({
creatorId: z.string(),
url: z.string(),
spaceId:z.string()
spaceId: z.string(),
});

const MAX_QUEUE_LEN = 20;
Expand Down Expand Up @@ -58,7 +56,9 @@ export async function POST(req: NextRequest) {
);
}

const res = await youtubesearchapi.GetVideoDetails(videoId);
const res = await axios.get(
`https://www.youtube.com/oembed?url=https://www.youtube.com/watch?v=${videoId}&format=json`,
);

// Check if the user is not the creator
if (user.id !== data.creatorId) {
Expand Down Expand Up @@ -132,11 +132,6 @@ export async function POST(req: NextRequest) {
}
}

const thumbnails = res.thumbnail.thumbnails;
thumbnails.sort((a: { width: number }, b: { width: number }) =>
a.width < b.width ? -1 : 1,
);

const existingActiveStreams = await db.stream.count({
where: {
spaceId: data.spaceId,
Expand All @@ -162,16 +157,10 @@ export async function POST(req: NextRequest) {
url: data.url,
extractedId: videoId,
type: "Youtube",
title: res.title ?? "Can't find video",
smallImg:
(thumbnails.length > 1
? thumbnails[thumbnails.length - 2].url
: thumbnails[thumbnails.length - 1].url) ??
"https://cdn.pixabay.com/photo/2024/02/28/07/42/european-shorthair-8601492_640.jpg",
bigImg:
thumbnails[thumbnails.length - 1].url ??
"https://cdn.pixabay.com/photo/2024/02/28/07/42/european-shorthair-8601492_640.jpg",
spaceId:data.spaceId
title: res.data.title ?? "Can't find video",
smallImg: `https://img.youtube.com/vi/${videoId}/mqdefault.jpg`,
bigImg: `https://img.youtube.com/vi/${videoId}/maxresdefault.jpg`,
spaceId: data.spaceId,
},
});

Expand Down Expand Up @@ -209,68 +198,68 @@ export async function GET(req: NextRequest) {
const user = session.user;

if (!spaceId) {
return NextResponse.json({
message: "Error"
}, {
status: 411
})
}
return NextResponse.json(
{
message: "Error",
},
{
status: 411,
},
);
}

const [space, activeStream] = await Promise.all([
db.space.findUnique({
where: {
id: spaceId,
id: spaceId,
},
include: {
streams: {
include: {
_count: {
select: {
upvotes: true
}
},
upvotes: {
where: {
userId: session?.user.id
}
}

streams: {
include: {
_count: {
select: {
upvotes: true,
},
where:{
played:false
}
},
upvotes: {
where: {
userId: session?.user.id,
},
},
},
_count: {
select: {
streams: true
}
},

}

}),
db.currentStream.findFirst({
where: {
played: false,
},
},
_count: {
select: {
streams: true,
},
},
},
}),
db.currentStream.findFirst({
where: {
spaceId: spaceId
spaceId: spaceId,
},
include: {
stream: true
}
})
stream: true,
},
}),
]);

const hostId =space?.hostId;
const isCreator = session.user.id=== hostId
const hostId = space?.hostId;
const isCreator = session.user.id === hostId;

return NextResponse.json({
streams: space?.streams.map(({_count, ...rest}) => ({
...rest,
upvotes: _count.upvotes,
haveUpvoted: rest.upvotes.length ? true : false
streams: space?.streams.map(({ _count, ...rest }) => ({
...rest,
upvotes: _count.upvotes,
haveUpvoted: rest.upvotes.length ? true : false,
})),
activeStream,
hostId,
isCreator,
spaceName:space?.name
});
spaceName: space?.name,
});
}
Loading