Add lesson comments (Stage 6)
- LessonComment CRUD: addComment / deleteComment server actions - LessonComments client component with form, avatar, delete - Comments section at bottom of lesson page (enrolled users only) - Soft-delete support, moderation for curator/admin - ROADMAP: moved Квизы to end (Stage 11), marked Stage 7 done Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,62 @@
|
||||
"use server";
|
||||
|
||||
import { headers } from "next/headers";
|
||||
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) {
|
||||
const session = await auth.api.getSession({ headers: await headers() });
|
||||
if (!session) throw new Error("Unauthorized");
|
||||
|
||||
const trimmed = text.trim();
|
||||
if (!trimmed || trimmed.length > 2000) throw new Error("Invalid text");
|
||||
|
||||
// Verify user has access to this lesson's course
|
||||
const lesson = await prisma.lesson.findUnique({
|
||||
where: { id: lessonId },
|
||||
select: { module: { select: { course: { select: { id: true } } } } },
|
||||
});
|
||||
if (!lesson) throw new Error("Lesson not found");
|
||||
|
||||
const isAdmin = session.user.role === "admin";
|
||||
if (!isAdmin) {
|
||||
const enrollment = await prisma.courseEnrollment.findUnique({
|
||||
where: {
|
||||
userId_courseId: {
|
||||
userId: session.user.id,
|
||||
courseId: lesson.module.course.id,
|
||||
},
|
||||
},
|
||||
});
|
||||
if (!enrollment) throw new Error("Not enrolled");
|
||||
if (enrollment.expiresAt && enrollment.expiresAt < new Date()) throw new Error("Access expired");
|
||||
}
|
||||
|
||||
await prisma.lessonComment.create({
|
||||
data: { lessonId, userId: session.user.id, text: trimmed },
|
||||
});
|
||||
|
||||
revalidatePath(`/courses/${slug}/lessons/${lessonId}`);
|
||||
}
|
||||
|
||||
export async function deleteComment(commentId: string, lessonId: string, slug: string) {
|
||||
const session = await auth.api.getSession({ headers: await headers() });
|
||||
if (!session) throw new Error("Unauthorized");
|
||||
|
||||
const comment = await prisma.lessonComment.findUnique({ where: { id: commentId } });
|
||||
if (!comment) throw new Error("Not found");
|
||||
|
||||
const canDelete =
|
||||
comment.userId === session.user.id ||
|
||||
session.user.role === "curator" ||
|
||||
session.user.role === "admin";
|
||||
if (!canDelete) throw new Error("Forbidden");
|
||||
|
||||
await prisma.lessonComment.update({
|
||||
where: { id: commentId },
|
||||
data: { deleted: true },
|
||||
});
|
||||
|
||||
revalidatePath(`/courses/${slug}/lessons/${lessonId}`);
|
||||
}
|
||||
@@ -7,6 +7,7 @@ import { KinescopePlayer } from "@/components/player/kinescope-player";
|
||||
import { LessonContent } from "@/components/student/lesson-content";
|
||||
import { LessonCompleteButton } from "@/components/student/lesson-complete-button";
|
||||
import { HomeworkSection } from "@/components/student/homework-section";
|
||||
import { LessonComments } from "@/components/student/lesson-comments";
|
||||
|
||||
interface Props {
|
||||
params: Promise<{ slug: string; lessonId: string }>;
|
||||
@@ -18,7 +19,7 @@ export default async function LessonPage({ params }: Props) {
|
||||
const session = await auth.api.getSession({ headers: await headers() });
|
||||
const isAdmin = session?.user.role === "admin";
|
||||
|
||||
const [lesson, progress] = await Promise.all([
|
||||
const [lesson, progress, comments] = await Promise.all([
|
||||
prisma.lesson.findUnique({
|
||||
where: { id: lessonId, ...(isAdmin ? {} : { published: true }) },
|
||||
include: {
|
||||
@@ -49,6 +50,11 @@ export default async function LessonPage({ params }: Props) {
|
||||
where: { userId_lessonId: { userId: session.user.id, lessonId } },
|
||||
})
|
||||
: null,
|
||||
prisma.lessonComment.findMany({
|
||||
where: { lessonId },
|
||||
orderBy: { createdAt: "asc" },
|
||||
include: { user: { select: { id: true, name: true } } },
|
||||
}),
|
||||
]);
|
||||
|
||||
// Fetch homework submission for this student
|
||||
@@ -179,6 +185,25 @@ export default async function LessonPage({ params }: Props) {
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
|
||||
{/* Comments */}
|
||||
{session && (
|
||||
<div
|
||||
className="mt-10 pt-8"
|
||||
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})
|
||||
</p>
|
||||
<LessonComments
|
||||
lessonId={lessonId}
|
||||
slug={slug}
|
||||
comments={comments}
|
||||
currentUserId={session.user.id}
|
||||
currentUserRole={session.user.role ?? "student"}
|
||||
/>
|
||||
</div>
|
||||
)}
|
||||
</article>
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user