From f9edb66ffe38f090530c436aeabd2bbbc4fe5a95 Mon Sep 17 00:00:00 2001 From: k3vin7 Date: Wed, 1 Apr 2026 20:54:07 +0900 Subject: [PATCH] =?UTF-8?q?[fix]=20AI=20=EB=8B=B5=EA=B8=80=20=EC=8A=A4?= =?UTF-8?q?=EC=BC=80=EC=A4=84=EB=A7=81=EC=9D=84=20AFTER=5FCOMMIT=20?= =?UTF-8?q?=EC=9D=B4=EB=B2=A4=ED=8A=B8=EB=A1=9C=20=EB=B6=84=EB=A6=AC?= =?UTF-8?q?=ED=95=98=EC=97=AC=20=EB=8C=93=EA=B8=80=20=ED=8A=B8=EB=9E=9C?= =?UTF-8?q?=EC=9E=AD=EC=85=98=20=EB=B3=B4=ED=98=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../application/AiCommentService.java | 12 ++++++++- .../aicomment/event/AiReplyScheduleEvent.java | 7 +++++ .../event/AiReplyScheduleEventListener.java | 26 +++++++++++++++++++ .../comment/application/CommentService.java | 5 ++-- 4 files changed, 46 insertions(+), 4 deletions(-) create mode 100644 src/main/java/com/daramg/server/aicomment/event/AiReplyScheduleEvent.java create mode 100644 src/main/java/com/daramg/server/aicomment/event/AiReplyScheduleEventListener.java diff --git a/src/main/java/com/daramg/server/aicomment/application/AiCommentService.java b/src/main/java/com/daramg/server/aicomment/application/AiCommentService.java index 9033c94..7a5201c 100644 --- a/src/main/java/com/daramg/server/aicomment/application/AiCommentService.java +++ b/src/main/java/com/daramg/server/aicomment/application/AiCommentService.java @@ -145,7 +145,12 @@ public void scheduleForPost(Post post) { } @Transactional - public void scheduleReplyForAiComment(Comment aiComment, Post post) { + public void scheduleReplyForAiComment(Long aiCommentId, Long postId) { + Comment aiComment = commentRepository.findById(aiCommentId).orElse(null); + if (aiComment == null) { + return; + } + if (aiComment.getAiReplyCount() >= MAX_AI_REPLY_COUNT) { return; } @@ -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); diff --git a/src/main/java/com/daramg/server/aicomment/event/AiReplyScheduleEvent.java b/src/main/java/com/daramg/server/aicomment/event/AiReplyScheduleEvent.java new file mode 100644 index 0000000..09cf084 --- /dev/null +++ b/src/main/java/com/daramg/server/aicomment/event/AiReplyScheduleEvent.java @@ -0,0 +1,7 @@ +package com.daramg.server.aicomment.event; + +public record AiReplyScheduleEvent( + Long aiCommentId, + Long postId +) { +} diff --git a/src/main/java/com/daramg/server/aicomment/event/AiReplyScheduleEventListener.java b/src/main/java/com/daramg/server/aicomment/event/AiReplyScheduleEventListener.java new file mode 100644 index 0000000..b523afb --- /dev/null +++ b/src/main/java/com/daramg/server/aicomment/event/AiReplyScheduleEventListener.java @@ -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); + } + } +} diff --git a/src/main/java/com/daramg/server/comment/application/CommentService.java b/src/main/java/com/daramg/server/comment/application/CommentService.java index cbb57b8..a0f44c0 100644 --- a/src/main/java/com/daramg/server/comment/application/CommentService.java +++ b/src/main/java/com/daramg/server/comment/application/CommentService.java @@ -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; @@ -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); @@ -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())); } }