Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,12 @@ public void scheduleForPost(Post post) {
}

@Transactional
public void scheduleReplyForAiComment(Comment aiComment, Post post) {
public void scheduleReplyForAiComment(Long aiCommentId, Long postId) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

AiReplyScheduleEvent와 이 메서드에서 postId를 별도로 전달받고 있으나, aiComment.getPost()를 통해 이미 해당 엔티티에 접근할 수 있습니다. 이벤트를 발행할 때 aiCommentId만 전달하고 서비스 내부에서 aiComment.getPost()를 활용하면, 이벤트 객체와 메서드 시그니처를 더 간결하게 유지할 수 있습니다.

Comment aiComment = commentRepository.findById(aiCommentId).orElse(null);
if (aiComment == null) {
return;
}

if (aiComment.getAiReplyCount() >= MAX_AI_REPLY_COUNT) {
return;
}
Expand All @@ -160,6 +165,11 @@ public void scheduleReplyForAiComment(Comment aiComment, Post post) {
return;
}

Post post = postRepository.findById(postId).orElse(null);
if (post == null) {
return;
}

aiComment.incrementAiReplyCount();

Instant scheduledAt = Instant.now().plusSeconds(replyDelayMinutes * 60L);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.daramg.server.aicomment.event;

public record AiReplyScheduleEvent(
Long aiCommentId,
Long postId
) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.daramg.server.aicomment.event;

import com.daramg.server.aicomment.application.AiCommentService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.transaction.event.TransactionalEventListener;

import static org.springframework.transaction.event.TransactionPhase.AFTER_COMMIT;

@Slf4j
@Component
@RequiredArgsConstructor
public class AiReplyScheduleEventListener {

private final AiCommentService aiCommentService;

@TransactionalEventListener(phase = AFTER_COMMIT)
public void handleAiReplyScheduleEvent(AiReplyScheduleEvent event) {
try {
aiCommentService.scheduleReplyForAiComment(event.aiCommentId(), event.postId());
} catch (Exception e) {
log.warn("AI 답글 잡 등록 실패 - aiCommentId={}, postId={}", event.aiCommentId(), event.postId(), e);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.daramg.server.comment.application;

import com.daramg.server.aicomment.application.AiCommentService;
import com.daramg.server.aicomment.event.AiReplyScheduleEvent;
import com.daramg.server.comment.domain.Comment;
import com.daramg.server.comment.domain.CommentLike;
import com.daramg.server.comment.dto.CommentLikeResponseDto;
Expand Down Expand Up @@ -30,7 +30,6 @@ public class CommentService {
private final CommentRepository commentRepository;
private final CommentLikeRepository commentLikeRepository;
private final ApplicationEventPublisher eventPublisher;
private final AiCommentService aiCommentService;

public void createComment(Long postId, CommentCreateDto request, User user){
Post post = entityUtils.getEntity(postId, Post.class);
Expand Down Expand Up @@ -80,7 +79,7 @@ public void createReply(Long commentId, CommentReplyCreateDto request, User user
}

if (parentComment.isAi()) {
aiCommentService.scheduleReplyForAiComment(parentComment, post);
eventPublisher.publishEvent(new AiReplyScheduleEvent(parentComment.getId(), post.getId()));
}
}

Expand Down
Loading