- Introduced support for nested comment replies in both frontend and backend. - Added comment liking and unliking features, including like count and "isLiked" state tracking. - Updated database schema with `parentId` and new `comment_likes` table. - Enhanced UI for threaded comments and implemented display of like counts and reply actions. - Refactored APIs and repositories to support replies, likes, and enriched comment data.
81 lines
2.3 KiB
TypeScript
81 lines
2.3 KiB
TypeScript
import {
|
|
Body,
|
|
Controller,
|
|
Delete,
|
|
Get,
|
|
Param,
|
|
Post,
|
|
Req,
|
|
UseGuards,
|
|
} from "@nestjs/common";
|
|
import { ConfigService } from "@nestjs/config";
|
|
import { getIronSession } from "iron-session";
|
|
import { AuthGuard } from "../auth/guards/auth.guard";
|
|
import { getSessionOptions } from "../auth/session.config";
|
|
import type { AuthenticatedRequest } from "../common/interfaces/request.interface";
|
|
import { JwtService } from "../crypto/services/jwt.service";
|
|
import { CommentsService } from "./comments.service";
|
|
import { CreateCommentDto } from "./dto/create-comment.dto";
|
|
|
|
@Controller()
|
|
export class CommentsController {
|
|
constructor(
|
|
private readonly commentsService: CommentsService,
|
|
private readonly jwtService: JwtService,
|
|
private readonly configService: ConfigService,
|
|
) {}
|
|
|
|
@Get("contents/:contentId/comments")
|
|
async findAllByContentId(
|
|
@Param("contentId") contentId: string,
|
|
@Req() req: any,
|
|
) {
|
|
// Tentative de récupération de l'utilisateur pour isLiked (optionnel)
|
|
let userId: string | undefined;
|
|
try {
|
|
const session = await getIronSession<any>(
|
|
req,
|
|
req.res,
|
|
getSessionOptions(this.configService.get("SESSION_PASSWORD") as string),
|
|
);
|
|
if (session.accessToken) {
|
|
const payload = await this.jwtService.verifyJwt(session.accessToken);
|
|
userId = payload.sub;
|
|
}
|
|
} catch (_e) {
|
|
// Ignorer les erreurs de session
|
|
}
|
|
|
|
return this.commentsService.findAllByContentId(contentId, userId);
|
|
}
|
|
|
|
@Post("contents/:contentId/comments")
|
|
@UseGuards(AuthGuard)
|
|
create(
|
|
@Req() req: AuthenticatedRequest,
|
|
@Param("contentId") contentId: string,
|
|
@Body() dto: CreateCommentDto,
|
|
) {
|
|
return this.commentsService.create(req.user.sub, contentId, dto);
|
|
}
|
|
|
|
@Delete("comments/:id")
|
|
@UseGuards(AuthGuard)
|
|
remove(@Req() req: AuthenticatedRequest, @Param("id") id: string) {
|
|
const isAdmin = req.user.role === "admin" || req.user.role === "moderator";
|
|
return this.commentsService.remove(req.user.sub, id, isAdmin);
|
|
}
|
|
|
|
@Post("comments/:id/like")
|
|
@UseGuards(AuthGuard)
|
|
like(@Req() req: AuthenticatedRequest, @Param("id") id: string) {
|
|
return this.commentsService.like(req.user.sub, id);
|
|
}
|
|
|
|
@Delete("comments/:id/like")
|
|
@UseGuards(AuthGuard)
|
|
unlike(@Req() req: AuthenticatedRequest, @Param("id") id: string) {
|
|
return this.commentsService.unlike(req.user.sub, id);
|
|
}
|
|
}
|