Add threaded comment replies for admin and curator
- Add parentId field to LessonComment (self-referential FK, SetNull on delete) - Show replies indented under parent comment with left border visual - Add reply button (visible to admin/curator only) with inline textarea - Only root comments shown in main list; replies nested below their parent - Update comment count to include replies - Server-side validation: only admin/curator can reply, parent must belong to same lesson Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -5,7 +5,7 @@ import { auth } from "@/lib/auth";
|
||||
import { prisma } from "@/lib/prisma";
|
||||
import { revalidatePath } from "next/cache";
|
||||
|
||||
export async function addComment(lessonId: string, slug: string, text: string) {
|
||||
export async function addComment(lessonId: string, slug: string, text: string, parentId?: string) {
|
||||
const session = await auth.api.getSession({ headers: await headers() });
|
||||
if (!session) throw new Error("Unauthorized");
|
||||
|
||||
@@ -20,7 +20,8 @@ export async function addComment(lessonId: string, slug: string, text: string) {
|
||||
if (!lesson) throw new Error("Lesson not found");
|
||||
|
||||
const isAdmin = session.user.role === "admin";
|
||||
if (!isAdmin) {
|
||||
const isCurator = session.user.role === "curator";
|
||||
if (!isAdmin && !isCurator) {
|
||||
const enrollment = await prisma.courseEnrollment.findUnique({
|
||||
where: {
|
||||
userId_courseId: {
|
||||
@@ -33,8 +34,14 @@ export async function addComment(lessonId: string, slug: string, text: string) {
|
||||
if (enrollment.expiresAt && enrollment.expiresAt < new Date()) throw new Error("Access expired");
|
||||
}
|
||||
|
||||
if (parentId) {
|
||||
if (!isAdmin && !isCurator) throw new Error("Forbidden");
|
||||
const parent = await prisma.lessonComment.findUnique({ where: { id: parentId } });
|
||||
if (!parent || parent.lessonId !== lessonId) throw new Error("Invalid parent");
|
||||
}
|
||||
|
||||
await prisma.lessonComment.create({
|
||||
data: { lessonId, userId: session.user.id, text: trimmed },
|
||||
data: { lessonId, userId: session.user.id, text: trimmed, ...(parentId ? { parentId } : {}) },
|
||||
});
|
||||
|
||||
revalidatePath(`/courses/${slug}/lessons/${lessonId}`);
|
||||
|
||||
@@ -56,9 +56,15 @@ export default async function LessonPage({ params }: Props) {
|
||||
})
|
||||
: null,
|
||||
prisma.lessonComment.findMany({
|
||||
where: { lessonId },
|
||||
where: { lessonId, parentId: null },
|
||||
orderBy: { createdAt: "asc" },
|
||||
include: { user: { select: { id: true, name: true } } },
|
||||
include: {
|
||||
user: { select: { id: true, name: true } },
|
||||
replies: {
|
||||
orderBy: { createdAt: "asc" },
|
||||
include: { user: { select: { id: true, name: true } } },
|
||||
},
|
||||
},
|
||||
}),
|
||||
]);
|
||||
|
||||
@@ -232,7 +238,10 @@ export default async function LessonPage({ params }: Props) {
|
||||
style={{ borderTop: "2px solid var(--border)" }}
|
||||
>
|
||||
<p className="text-xs font-bold uppercase tracking-widest mb-6" style={{ color: "var(--muted-foreground)" }}>
|
||||
Обсуждение ({comments.filter((c) => !c.deleted).length})
|
||||
Обсуждение ({
|
||||
comments.filter(c => !c.deleted).length +
|
||||
comments.flatMap(c => c.replies).filter(r => !r.deleted).length
|
||||
})
|
||||
</p>
|
||||
<LessonComments
|
||||
lessonId={lessonId}
|
||||
|
||||
Reference in New Issue
Block a user