Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Spaces to muzer (Not in Websocket) and resolved conflicts #95

Merged
Show file tree
Hide file tree
Changes from 2 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
29 changes: 29 additions & 0 deletions next-app/actions/createSpace.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
"use server"

import { authOptions } from "@/lib/auth-options";
import prisma from "@/lib/db";
import { getServerSession } from "next-auth";

export default async function createSpace(spaceName:string){
try {
const session = await getServerSession(authOptions);
if (!session?.user || !session.user?.id) {
throw new Error("Unauthneticated Request")
}
console.log(session.user.id)
const space=await prisma.space.create({
data:{
name:spaceName,
hostId:session.user.id as string
}
})
return { success: true, message: "Space created successfully", space };
} catch (error:any) {
if (error.message === "Unauthenticated Request") {
return { success: false, message: "You must be logged in to create a space" };
}
else{
return { success: false, message: "An unexpected error occurred"+ error.message };
}
}
}
31 changes: 31 additions & 0 deletions next-app/actions/deleteSpace.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
"use server"

import { authOptions } from "@/lib/auth-options";
import prisma from "@/lib/db";
import { getServerSession } from "next-auth";


export const deleteSpaces=async(spaceId:string)=>{
try {
const session = await getServerSession(authOptions);
if (!session?.user || !session.user?.id) {
throw new Error("Unauthneticated Request")
}
const spaces=await prisma.space.delete({
where:{
id:spaceId
}
})
return { success: true, message: "Spaces Deleted successfully", spaces };
} catch (error:any) {
if (error.message === "Unauthenticated Request") {
console.error(error)
return { success: false, message: "You must be logged to delete a space" };
}
else{
console.error(error)
return { success: false, message: "Error Deleting spaces"};
}

}
}
31 changes: 31 additions & 0 deletions next-app/actions/getSpaces.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
"use server"

import { authOptions } from "@/lib/auth-options";
import prisma from "@/lib/db";
import { getServerSession } from "next-auth";


export const getSpaces=async()=>{
try {
const session = await getServerSession(authOptions);
if (!session?.user || !session.user?.id) {
throw new Error("Unauthneticated Request")
}
const spaces=await prisma.space.findMany({
where:{
hostId:session.user.id
}
})
return { success: true, message: "Spaces retrieved successfully", spaces };
} catch (error:any) {
if (error.message === "Unauthenticated Request") {
console.error(error)
return { success: false, message: "You must be logged get a space" };
}
else{
console.error(error)
return { success: false, message: "Error fetching spaces"};
}

}
}
6 changes: 4 additions & 2 deletions next-app/app/api/streams/empty-queue/route.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { authOptions } from "@/lib/auth-options";
import db from "@/lib/db";
import { getServerSession } from "next-auth";
import { NextResponse } from "next/server";
import { NextRequest, NextResponse } from "next/server";

export async function POST() {
export async function POST(req:NextRequest) {
const session = await getServerSession(authOptions);

if (!session?.user) {
Expand All @@ -17,12 +17,14 @@ export async function POST() {
);
}
const user = session.user;
const data = await req.json()

try {
await db.stream.updateMany({
where: {
userId: user.id,
played: false,
spaceId:data.spaceId
},
data: {
played: true,
Expand Down
11 changes: 8 additions & 3 deletions next-app/app/api/streams/next/route.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { authOptions } from "@/lib/auth-options";
import db from "@/lib/db";
import { randomUUID } from "crypto";
import { getServerSession } from "next-auth";
import { NextResponse } from "next/server";
import { NextRequest, NextResponse } from "next/server";

export async function GET() {
export async function GET(req: NextRequest) {
const session = await getServerSession(authOptions);

if (!session?.user.id) {
Expand All @@ -17,11 +18,13 @@ export async function GET() {
);
}
const user = session.user;
const spaceId = req.nextUrl.searchParams.get("spaceId");

const mostUpvotedStream = await db.stream.findFirst({
where: {
userId: user.id,
played: false,
spaceId:spaceId
},
orderBy: {
upvotes: {
Expand All @@ -33,15 +36,17 @@ export async function GET() {
await Promise.all([
db.currentStream.upsert({
where: {
userId: user.id,
spaceId:spaceId as string
},
update: {
userId: user.id,
streamId: mostUpvotedStream?.id,
spaceId:spaceId
},
create: {
userId: user.id,
streamId: mostUpvotedStream?.id,
spaceId:spaceId
},
}),
db.stream.update({
Expand Down
3 changes: 3 additions & 0 deletions next-app/app/api/streams/remove/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { z } from "zod";

const RemoveStreamSchema = z.object({
streamId: z.string(),
spaceId:z.string()
});

export async function DELETE(req: NextRequest) {
Expand All @@ -26,6 +27,7 @@ export async function DELETE(req: NextRequest) {
try {
const { searchParams } = new URL(req.url);
const streamId = searchParams.get("streamId");
const spaceId = searchParams.get('spaceId')

if (!streamId) {
return NextResponse.json(
Expand All @@ -42,6 +44,7 @@ export async function DELETE(req: NextRequest) {
where: {
id: streamId,
userId: user.id,
spaceId:spaceId
},
});

Expand Down
95 changes: 55 additions & 40 deletions next-app/app/api/streams/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@ import { YT_REGEX } from "@/lib/utils";
import { getServerSession } from "next-auth";
import { authOptions } from "@/lib/auth-options";


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

const MAX_QUEUE_LEN = 20;
Expand Down Expand Up @@ -137,7 +139,7 @@ export async function POST(req: NextRequest) {

const existingActiveStreams = await db.stream.count({
where: {
userId: data.creatorId,
spaceId: data.spaceId,
played: false,
},
});
Expand Down Expand Up @@ -169,6 +171,7 @@ export async function POST(req: NextRequest) {
bigImg:
thumbnails[thumbnails.length - 1].url ??
"https://cdn.pixabay.com/photo/2024/02/28/07/42/european-shorthair-8601492_640.jpg",
spaceId:data.spaceId
},
});

Expand All @@ -191,9 +194,8 @@ export async function POST(req: NextRequest) {
}

export async function GET(req: NextRequest) {
const creatorId = req.nextUrl.searchParams.get("creatorId");
const spaceId = req.nextUrl.searchParams.get("spaceId");
const session = await getServerSession(authOptions);

if (!session?.user.id) {
return NextResponse.json(
{
Expand All @@ -206,56 +208,69 @@ export async function GET(req: NextRequest) {
}
const user = session.user;

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

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

},
where:{
played:false
}
},
},
},
}),
db.currentStream.findFirst({
_count: {
select: {
streams: true
}
},

}

}),
db.currentStream.findFirst({
where: {
userId: creatorId,
spaceId: spaceId
},
include: {
stream: true,
},
}),
stream: true
}
})
]);

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

return NextResponse.json({
streams: 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,
creatorId,
hostId,
isCreator,
});
spaceName:space?.name
});
}
1 change: 1 addition & 0 deletions next-app/app/api/streams/upvote/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { z } from "zod";

const UpvoteSchema = z.object({
streamId: z.string(),
spaceId:z.string()
});

export async function POST(req: NextRequest) {
Expand Down
22 changes: 22 additions & 0 deletions next-app/app/creator/[creatorId]/[spaceId]/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import OldStreamView from "@/components/OldStreamView";
SujithThirumalaisamy marked this conversation as resolved.
Show resolved Hide resolved
import { authOptions } from "@/lib/auth-options";
import { getServerSession } from "next-auth";
import { redirect } from "next/navigation";

// Use this when using old stream view for spaces
export default async function Creator({
params: {
creatorId,
spaceId
}
}: {
params: {
creatorId: string;
spaceId:string
}
}) {

return <div>
<OldStreamView creatorId={creatorId} spaceId={spaceId} playVideo={false} />
</div>
}
Loading
Loading