feat: add WebSocket notifications for new comments
- Introduced enriched comment retrieval with user information and like statistics. - Implemented WebSocket notifications to notify users of new comments on content. - Updated dependency injection to include `EventsGateway` and `RealtimeModule`.
This commit is contained in:
@@ -3,6 +3,7 @@ import {
|
||||
Injectable,
|
||||
NotFoundException,
|
||||
} from "@nestjs/common";
|
||||
import { EventsGateway } from "../realtime/events.gateway";
|
||||
import { S3Service } from "../s3/s3.service";
|
||||
import type { CreateCommentDto } from "./dto/create-comment.dto";
|
||||
import { CommentLikesRepository } from "./repositories/comment-likes.repository";
|
||||
@@ -14,6 +15,7 @@ export class CommentsService {
|
||||
private readonly commentsRepository: CommentsRepository,
|
||||
private readonly commentLikesRepository: CommentLikesRepository,
|
||||
private readonly s3Service: S3Service,
|
||||
private readonly eventsGateway: EventsGateway,
|
||||
) {}
|
||||
|
||||
async create(userId: string, contentId: string, dto: CreateCommentDto) {
|
||||
@@ -24,11 +26,36 @@ export class CommentsService {
|
||||
parentId: dto.parentId,
|
||||
});
|
||||
|
||||
// Enrichir le commentaire créé (pour le retour API)
|
||||
// Récupérer le commentaire avec les infos utilisateur pour le WebSocket
|
||||
const enrichedComment = await this.findOneEnriched(comment.id, userId);
|
||||
|
||||
// Notifier les autres utilisateurs sur ce contenu
|
||||
this.eventsGateway.sendToContent(contentId, "new_comment", enrichedComment);
|
||||
|
||||
return enrichedComment;
|
||||
}
|
||||
|
||||
async findOneEnriched(commentId: string, currentUserId?: string) {
|
||||
const comment = await this.commentsRepository.findOneEnriched(commentId);
|
||||
if (!comment) return null;
|
||||
|
||||
const [likesCount, isLiked] = await Promise.all([
|
||||
this.commentLikesRepository.countByCommentId(comment.id),
|
||||
currentUserId
|
||||
? this.commentLikesRepository.isLikedByUser(comment.id, currentUserId)
|
||||
: Promise.resolve(false),
|
||||
]);
|
||||
|
||||
return {
|
||||
...comment,
|
||||
likesCount: 0,
|
||||
isLiked: false,
|
||||
likesCount,
|
||||
isLiked,
|
||||
user: {
|
||||
...comment.user,
|
||||
avatarUrl: comment.user.avatarUrl
|
||||
? this.s3Service.getPublicUrl(comment.user.avatarUrl)
|
||||
: null,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user