From 8d33c7eb622a9e569a4544075a50e010a7f25380 Mon Sep 17 00:00:00 2001 From: Izan Gil <66965250+SrIzan10@users.noreply.github.com> Date: Mon, 10 Mar 2025 20:18:28 +0100 Subject: [PATCH] feat: following stuff --- .../(protected)/api/stream/follow/route.ts | 26 +++++++++++++++ .../api/stream/followers/[channel]/route.ts | 32 +++++++++++++++++++ .../app/UserInfoCard/UserInfoCard.tsx | 7 ++-- src/components/app/UserInfoCard/follow.tsx | 23 ++++++++++--- .../app/UserInfoCard/followCount.tsx | 26 +++++++++++++++ src/lib/db/resolve.ts | 16 ++++++++++ 6 files changed, 122 insertions(+), 8 deletions(-) create mode 100644 src/app/(protected)/api/stream/followers/[channel]/route.ts create mode 100644 src/components/app/UserInfoCard/followCount.tsx create mode 100644 src/lib/db/resolve.ts diff --git a/src/app/(protected)/api/stream/follow/route.ts b/src/app/(protected)/api/stream/follow/route.ts index e3b322b..b787932 100644 --- a/src/app/(protected)/api/stream/follow/route.ts +++ b/src/app/(protected)/api/stream/follow/route.ts @@ -2,6 +2,32 @@ import { validateRequest } from '@/lib/auth'; import prisma from '@/lib/db'; import { NextRequest } from 'next/server'; +export async function GET(request: NextRequest) { + const { user } = await validateRequest(); + const searchParams = new URL(request.url).searchParams; + const username = searchParams.get('username'); + if (!user) { + return new Response('Unauthorized', { status: 401 }); + } + if (!username) { + return new Response('Bad Request', { status: 400 }); + } + + const isFollowing = + (await prisma.follow.count({ + where: { + channel: { + name: username, + }, + user: { + id: user.id, + }, + }, + })) > 0; + + return new Response(JSON.stringify({ following: isFollowing }), { status: 200 }); +} + export async function POST(request: NextRequest) { const { user } = await validateRequest(); const searchParams = new URL(request.url).searchParams; diff --git a/src/app/(protected)/api/stream/followers/[channel]/route.ts b/src/app/(protected)/api/stream/followers/[channel]/route.ts new file mode 100644 index 0000000..d360690 --- /dev/null +++ b/src/app/(protected)/api/stream/followers/[channel]/route.ts @@ -0,0 +1,32 @@ +import { NextRequest, NextResponse } from 'next/server'; +import db from '@/lib/db'; +import { resolveChannelNameId } from '@/lib/db/resolve'; + +export async function GET( + request: NextRequest, + { params }: { params: Promise<{ channel: string }> } +) { + try { + const { channel } = await params; + + if (!channel) { + return NextResponse.json({ error: 'channel ID is required' }, { status: 400 }); + } + + const channelId = await resolveChannelNameId(channel); + + const count = await db.follow.count({ + where: { + channelId, + }, + }); + + return NextResponse.json({ + count, + success: true, + }); + } catch (error) { + console.error('Error fetching followers count:', error); + return NextResponse.json({ error: 'Failed to fetch followers count' }, { status: 500 }); + } +} diff --git a/src/components/app/UserInfoCard/UserInfoCard.tsx b/src/components/app/UserInfoCard/UserInfoCard.tsx index 08a1342..09c21d8 100644 --- a/src/components/app/UserInfoCard/UserInfoCard.tsx +++ b/src/components/app/UserInfoCard/UserInfoCard.tsx @@ -1,7 +1,7 @@ -import { Button } from '@/components/ui/button'; -import { Avatar, AvatarFallback, AvatarImage } from '@/components/ui/avatar'; +import { Avatar, AvatarImage } from '@/components/ui/avatar'; import type { StreamInfo, User } from '@prisma/client'; import FollowButton from './follow'; +import FollowCountText from './followCount'; export default function UserInfoCard(props: Props) { return ( @@ -14,9 +14,10 @@ export default function UserInfoCard(props: Props) {
{props.streamInfo.username}
+markdown description here
{/*