feat: user blocking and content reporting/flagging
- user_blocks table (composite PK), Block/Unblock button on profiles. Blocking severs any existing follow relationship both ways and prevents the blocked party from following, commenting on the blocker's recipes, or the blocker from seeing their comments. - reports table (recipe/comment/user targets, pending/reviewed/ dismissed status). ReportButton on comments, admin review queue at /admin/reports with dismiss/mark-reviewed actions, audit-logged. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
@@ -7,6 +7,7 @@ import {
|
||||
users,
|
||||
recipes,
|
||||
userFollows,
|
||||
userBlocks,
|
||||
eq,
|
||||
and,
|
||||
desc,
|
||||
@@ -15,6 +16,7 @@ import {
|
||||
import { Avatar, AvatarImage, AvatarFallback } from "@/components/ui/avatar";
|
||||
import { Badge } from "@/components/ui/badge";
|
||||
import { FollowButton } from "@/components/social/follow-button";
|
||||
import { BlockButton } from "@/components/social/block-button";
|
||||
import { getPublicUrl } from "@/lib/storage";
|
||||
|
||||
type Params = { params: Promise<{ username: string }> };
|
||||
@@ -61,15 +63,25 @@ export default async function UserProfilePage({ params }: Params) {
|
||||
const recipeCount = recipeCountRow[0]?.count ?? 0;
|
||||
|
||||
let isFollowing = false;
|
||||
let isBlocked = false;
|
||||
const isOwnProfile = session?.user.id === user.id;
|
||||
if (session && !isOwnProfile) {
|
||||
const followRow = await db.query.userFollows.findFirst({
|
||||
where: and(
|
||||
eq(userFollows.followerId, session.user.id),
|
||||
eq(userFollows.followingId, user.id),
|
||||
),
|
||||
});
|
||||
const [followRow, blockRow] = await Promise.all([
|
||||
db.query.userFollows.findFirst({
|
||||
where: and(
|
||||
eq(userFollows.followerId, session.user.id),
|
||||
eq(userFollows.followingId, user.id),
|
||||
),
|
||||
}),
|
||||
db.query.userBlocks.findFirst({
|
||||
where: and(
|
||||
eq(userBlocks.blockerId, session.user.id),
|
||||
eq(userBlocks.blockedId, user.id),
|
||||
),
|
||||
}),
|
||||
]);
|
||||
isFollowing = !!followRow;
|
||||
isBlocked = !!blockRow;
|
||||
}
|
||||
|
||||
const initials = user.name
|
||||
@@ -95,11 +107,14 @@ export default async function UserProfilePage({ params }: Params) {
|
||||
<p className="text-muted-foreground text-sm">@{user.username}</p>
|
||||
</div>
|
||||
{!isOwnProfile && session && (
|
||||
<FollowButton
|
||||
targetUserId={user.id}
|
||||
targetUsername={user.username!}
|
||||
initialFollowing={isFollowing}
|
||||
/>
|
||||
<div className="flex items-center gap-2">
|
||||
<FollowButton
|
||||
targetUserId={user.id}
|
||||
targetUsername={user.username!}
|
||||
initialFollowing={isFollowing}
|
||||
/>
|
||||
<BlockButton targetUsername={user.username!} initialBlocked={isBlocked} />
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
|
||||
|
||||
@@ -3,7 +3,7 @@ import { headers } from "next/headers";
|
||||
import { auth } from "@/lib/auth/server";
|
||||
import { db, users, eq } from "@epicure/db";
|
||||
import Link from "next/link";
|
||||
import { Shield, Users, BookOpen, Settings, BarChart3, ClipboardList, HardDrive, Bot, ArrowLeft, Gauge, Mail } from "lucide-react";
|
||||
import { Shield, Users, BookOpen, Settings, BarChart3, ClipboardList, HardDrive, Bot, ArrowLeft, Gauge, Mail, Flag } from "lucide-react";
|
||||
import { cn } from "@/lib/utils";
|
||||
|
||||
const adminNav = [
|
||||
@@ -11,6 +11,7 @@ const adminNav = [
|
||||
{ href: "/admin/users", label: "Users", icon: Users },
|
||||
{ href: "/admin/invites", label: "Invites", icon: Mail },
|
||||
{ href: "/admin/recipes", label: "Recipes", icon: BookOpen },
|
||||
{ href: "/admin/reports", label: "Reports", icon: Flag },
|
||||
{ href: "/admin/tiers", label: "Tier Limits", icon: Gauge },
|
||||
{ href: "/admin/audit-logs", label: "Audit Logs", icon: ClipboardList },
|
||||
{ href: "/admin/storage", label: "Storage", icon: HardDrive },
|
||||
|
||||
@@ -0,0 +1,37 @@
|
||||
import type { Metadata } from "next";
|
||||
import { db, reports, users, eq, desc } from "@epicure/db";
|
||||
import { ReportsQueue } from "@/components/admin/reports-queue";
|
||||
|
||||
export const metadata: Metadata = { title: "Reports – Admin" };
|
||||
|
||||
export default async function AdminReportsPage() {
|
||||
const rows = await db
|
||||
.select({
|
||||
id: reports.id,
|
||||
targetType: reports.targetType,
|
||||
targetId: reports.targetId,
|
||||
reason: reports.reason,
|
||||
createdAt: reports.createdAt,
|
||||
reporterName: users.name,
|
||||
reporterEmail: users.email,
|
||||
})
|
||||
.from(reports)
|
||||
.innerJoin(users, eq(reports.reporterId, users.id))
|
||||
.where(eq(reports.status, "pending"))
|
||||
.orderBy(desc(reports.createdAt))
|
||||
.limit(100);
|
||||
|
||||
return (
|
||||
<div className="space-y-6">
|
||||
<div>
|
||||
<h1 className="text-2xl font-bold tracking-tight">Reports</h1>
|
||||
<p className="text-muted-foreground text-sm mt-1">
|
||||
Content flagged by users. Dismiss if unfounded, or review and take action manually.
|
||||
</p>
|
||||
</div>
|
||||
<ReportsQueue
|
||||
reports={rows.map((r) => ({ ...r, createdAt: r.createdAt.toISOString() }))}
|
||||
/>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
import { NextRequest, NextResponse } from "next/server";
|
||||
import { requireAdmin } from "@/lib/api-auth";
|
||||
import { db, reports, auditLogs, eq } from "@epicure/db";
|
||||
import { randomUUID } from "crypto";
|
||||
|
||||
interface RouteContext {
|
||||
params: Promise<{ id: string }>;
|
||||
}
|
||||
|
||||
export async function PATCH(req: NextRequest, { params }: RouteContext) {
|
||||
const { session, response } = await requireAdmin();
|
||||
if (response) return response;
|
||||
|
||||
const { id } = await params;
|
||||
const body = (await req.json()) as { status?: string };
|
||||
if (body.status !== "reviewed" && body.status !== "dismissed") {
|
||||
return NextResponse.json({ error: "Invalid status" }, { status: 400 });
|
||||
}
|
||||
|
||||
const [updated] = await db
|
||||
.update(reports)
|
||||
.set({ status: body.status, reviewedById: session!.user.id, reviewedAt: new Date() })
|
||||
.where(eq(reports.id, id))
|
||||
.returning();
|
||||
|
||||
if (!updated) return NextResponse.json({ error: "Not found" }, { status: 404 });
|
||||
|
||||
await db.insert(auditLogs).values({
|
||||
id: randomUUID(),
|
||||
userId: session!.user.id,
|
||||
action: "admin.report.resolve",
|
||||
targetType: "report",
|
||||
targetId: id,
|
||||
metadata: JSON.stringify({ status: body.status }),
|
||||
createdAt: new Date(),
|
||||
});
|
||||
|
||||
return NextResponse.json({ report: updated });
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
import { NextResponse } from "next/server";
|
||||
import { requireAdmin } from "@/lib/api-auth";
|
||||
import { db, reports, users, eq, desc } from "@epicure/db";
|
||||
|
||||
export async function GET() {
|
||||
const { response } = await requireAdmin();
|
||||
if (response) return response;
|
||||
|
||||
const rows = await db
|
||||
.select({
|
||||
id: reports.id,
|
||||
targetType: reports.targetType,
|
||||
targetId: reports.targetId,
|
||||
reason: reports.reason,
|
||||
status: reports.status,
|
||||
createdAt: reports.createdAt,
|
||||
reporterId: reports.reporterId,
|
||||
reporterName: users.name,
|
||||
reporterEmail: users.email,
|
||||
})
|
||||
.from(reports)
|
||||
.innerJoin(users, eq(reports.reporterId, users.id))
|
||||
.where(eq(reports.status, "pending"))
|
||||
.orderBy(desc(reports.createdAt))
|
||||
.limit(100);
|
||||
|
||||
return NextResponse.json({ reports: rows });
|
||||
}
|
||||
@@ -1,11 +1,12 @@
|
||||
import { NextRequest, NextResponse } from "next/server";
|
||||
import { z } from "zod";
|
||||
import { db, recipes, comments, users, eq, and } from "@epicure/db";
|
||||
import { db, recipes, comments, users, userBlocks, eq, and } from "@epicure/db";
|
||||
import { requireSession } from "@/lib/api-auth";
|
||||
import { applyRateLimit } from "@/lib/rate-limit";
|
||||
import { dispatchWebhook } from "@/lib/webhooks";
|
||||
import { sendPushNotification } from "@/lib/push";
|
||||
import { createNotification } from "@/lib/notifications";
|
||||
import { isBlockedEitherWay } from "@/lib/blocks";
|
||||
|
||||
const Schema = z.object({
|
||||
content: z.string().min(1).max(5000),
|
||||
@@ -21,6 +22,8 @@ export async function GET(_req: NextRequest, { params }: Params) {
|
||||
return NextResponse.json({ error: "Not found" }, { status: 404 });
|
||||
}
|
||||
|
||||
const { session } = await requireSession();
|
||||
|
||||
const rows = await db
|
||||
.select({
|
||||
id: comments.id,
|
||||
@@ -38,7 +41,15 @@ export async function GET(_req: NextRequest, { params }: Params) {
|
||||
.where(eq(comments.recipeId, id))
|
||||
.orderBy(comments.createdAt);
|
||||
|
||||
return NextResponse.json(rows);
|
||||
if (!session) return NextResponse.json(rows);
|
||||
|
||||
const blocked = await db
|
||||
.select({ blockedId: userBlocks.blockedId })
|
||||
.from(userBlocks)
|
||||
.where(eq(userBlocks.blockerId, session.user.id));
|
||||
const blockedIds = new Set(blocked.map((b) => b.blockedId));
|
||||
|
||||
return NextResponse.json(rows.filter((r) => !blockedIds.has(r.userId)));
|
||||
}
|
||||
|
||||
export async function POST(req: NextRequest, { params }: Params) {
|
||||
@@ -54,6 +65,10 @@ export async function POST(req: NextRequest, { params }: Params) {
|
||||
return NextResponse.json({ error: "Not found" }, { status: 404 });
|
||||
}
|
||||
|
||||
if (recipe.authorId !== session!.user.id && await isBlockedEitherWay(session!.user.id, recipe.authorId)) {
|
||||
return NextResponse.json({ error: "Not found" }, { status: 404 });
|
||||
}
|
||||
|
||||
const body = await req.json() as unknown;
|
||||
const parsed = Schema.safeParse(body);
|
||||
if (!parsed.success) return NextResponse.json({ error: "Validation error" }, { status: 400 });
|
||||
|
||||
@@ -0,0 +1,45 @@
|
||||
import { NextRequest, NextResponse } from "next/server";
|
||||
import { z } from "zod";
|
||||
import { db, reports, comments, recipes, users, eq } from "@epicure/db";
|
||||
import { requireSession } from "@/lib/api-auth";
|
||||
import { applyRateLimit } from "@/lib/rate-limit";
|
||||
import { randomUUID } from "crypto";
|
||||
|
||||
const Schema = z.object({
|
||||
targetType: z.enum(["recipe", "comment", "user"]),
|
||||
targetId: z.string().min(1),
|
||||
reason: z.string().min(1).max(1000),
|
||||
});
|
||||
|
||||
export async function POST(req: NextRequest) {
|
||||
const { session, response } = await requireSession();
|
||||
if (response) return response;
|
||||
|
||||
const limited = await applyRateLimit(`rl:report:${session!.user.id}`, 10, 60);
|
||||
if (limited) return limited;
|
||||
|
||||
const body = await req.json() as unknown;
|
||||
const parsed = Schema.safeParse(body);
|
||||
if (!parsed.success) return NextResponse.json({ error: "Validation error" }, { status: 400 });
|
||||
|
||||
const { targetType, targetId, reason } = parsed.data;
|
||||
|
||||
const exists =
|
||||
targetType === "recipe"
|
||||
? await db.query.recipes.findFirst({ where: eq(recipes.id, targetId) })
|
||||
: targetType === "comment"
|
||||
? await db.query.comments.findFirst({ where: eq(comments.id, targetId) })
|
||||
: await db.query.users.findFirst({ where: eq(users.id, targetId) });
|
||||
|
||||
if (!exists) return NextResponse.json({ error: "Target not found" }, { status: 404 });
|
||||
|
||||
await db.insert(reports).values({
|
||||
id: randomUUID(),
|
||||
reporterId: session!.user.id,
|
||||
targetType,
|
||||
targetId,
|
||||
reason,
|
||||
});
|
||||
|
||||
return NextResponse.json({ ok: true }, { status: 201 });
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
import { NextRequest, NextResponse } from "next/server";
|
||||
import { db, users, userBlocks, userFollows, eq, and, or } from "@epicure/db";
|
||||
import { requireSession } from "@/lib/api-auth";
|
||||
|
||||
type Params = { params: Promise<{ username: string }> };
|
||||
|
||||
export async function POST(_req: NextRequest, { params }: Params) {
|
||||
const { session, response } = await requireSession();
|
||||
if (response) return response;
|
||||
const { username } = await params;
|
||||
|
||||
const target = await db.query.users.findFirst({ where: eq(users.username, username) });
|
||||
if (!target) return NextResponse.json({ error: "Not found" }, { status: 404 });
|
||||
if (target.id === session!.user.id) return NextResponse.json({ error: "Cannot block yourself" }, { status: 400 });
|
||||
|
||||
await db.insert(userBlocks)
|
||||
.values({ blockerId: session!.user.id, blockedId: target.id })
|
||||
.onConflictDoNothing();
|
||||
|
||||
// Blocking severs any existing follow relationship in either direction.
|
||||
await db.delete(userFollows).where(
|
||||
or(
|
||||
and(eq(userFollows.followerId, session!.user.id), eq(userFollows.followingId, target.id)),
|
||||
and(eq(userFollows.followerId, target.id), eq(userFollows.followingId, session!.user.id))
|
||||
)
|
||||
);
|
||||
|
||||
return NextResponse.json({ blocked: true });
|
||||
}
|
||||
|
||||
export async function DELETE(_req: NextRequest, { params }: Params) {
|
||||
const { session, response } = await requireSession();
|
||||
if (response) return response;
|
||||
const { username } = await params;
|
||||
|
||||
const target = await db.query.users.findFirst({ where: eq(users.username, username) });
|
||||
if (!target) return NextResponse.json({ error: "Not found" }, { status: 404 });
|
||||
|
||||
await db.delete(userBlocks).where(
|
||||
and(eq(userBlocks.blockerId, session!.user.id), eq(userBlocks.blockedId, target.id))
|
||||
);
|
||||
|
||||
return NextResponse.json({ blocked: false });
|
||||
}
|
||||
@@ -3,6 +3,7 @@ import { db, users, userFollows, eq, and } from "@epicure/db";
|
||||
import { requireSession } from "@/lib/api-auth";
|
||||
import { applyRateLimit } from "@/lib/rate-limit";
|
||||
import { createNotification } from "@/lib/notifications";
|
||||
import { isBlockedEitherWay } from "@/lib/blocks";
|
||||
|
||||
type Params = { params: Promise<{ username: string }> };
|
||||
|
||||
@@ -19,6 +20,10 @@ export async function POST(_req: NextRequest, { params }: Params) {
|
||||
if (!target) return NextResponse.json({ error: "Not found" }, { status: 404 });
|
||||
if (target.id === session!.user.id) return NextResponse.json({ error: "Cannot follow yourself" }, { status: 400 });
|
||||
|
||||
if (await isBlockedEitherWay(session!.user.id, target.id)) {
|
||||
return NextResponse.json({ error: "Not found" }, { status: 404 });
|
||||
}
|
||||
|
||||
await db.insert(userFollows)
|
||||
.values({ followerId: session!.user.id, followingId: target.id })
|
||||
.onConflictDoNothing();
|
||||
|
||||
Reference in New Issue
Block a user