diff --git a/src/main/java/run/halo/app/content/comment/CommentServiceImpl.java b/src/main/java/run/halo/app/content/comment/CommentServiceImpl.java index cabe88e1e..9c1030011 100644 --- a/src/main/java/run/halo/app/content/comment/CommentServiceImpl.java +++ b/src/main/java/run/halo/app/content/comment/CommentServiceImpl.java @@ -94,17 +94,22 @@ public class CommentServiceImpl implements CommentService { } comment.getSpec() .setApproved(Boolean.FALSE.equals(commentSetting.getRequireReviewForNew())); + if (BooleanUtils.isTrue(comment.getSpec().getApproved()) && comment.getSpec().getApprovedTime() == null) { comment.getSpec().setApprovedTime(Instant.now()); } - comment.getSpec().setHidden(false); - if (comment.getSpec().getOwner() != null) { - return Mono.just(comment); - } + if (comment.getSpec().getCreationTime() == null) { comment.getSpec().setCreationTime(Instant.now()); } + + comment.getSpec().setHidden(false); + + // return if the comment owner is not null + if (comment.getSpec().getOwner() != null) { + return Mono.just(comment); + } // populate owner from current user return fetchCurrentUser() .map(this::toCommentOwner) diff --git a/src/main/java/run/halo/app/core/extension/content/Comment.java b/src/main/java/run/halo/app/core/extension/content/Comment.java index e4bfd720b..320f1cca1 100644 --- a/src/main/java/run/halo/app/core/extension/content/Comment.java +++ b/src/main/java/run/halo/app/core/extension/content/Comment.java @@ -91,15 +91,6 @@ public class Comment extends AbstractExtension { @Schema(required = true, defaultValue = "false") private Boolean hidden; - - /** - * If the creation time is null, use approvedTime. - * - * @return if creationTime is null returned approved time, otherwise creationTime. - */ - public Instant getCreationTime() { - return defaultIfNull(creationTime, approvedTime); - } } @Data diff --git a/src/main/java/run/halo/app/core/extension/reconciler/CommentReconciler.java b/src/main/java/run/halo/app/core/extension/reconciler/CommentReconciler.java index 32222fc2f..8b8d56396 100644 --- a/src/main/java/run/halo/app/core/extension/reconciler/CommentReconciler.java +++ b/src/main/java/run/halo/app/core/extension/reconciler/CommentReconciler.java @@ -1,12 +1,14 @@ package run.halo.app.core.extension.reconciler; +import static org.apache.commons.lang3.ObjectUtils.defaultIfNull; + import java.time.Instant; import java.util.HashSet; import java.util.List; import java.util.Map; +import java.util.Objects; import java.util.Set; import org.apache.commons.lang3.BooleanUtils; -import org.apache.commons.lang3.ObjectUtils; import org.springframework.lang.Nullable; import org.springframework.stereotype.Component; import run.halo.app.content.comment.ReplyService; @@ -51,6 +53,7 @@ public class CommentReconciler implements Reconciler { return; } addFinalizerIfNecessary(comment); + compatibleCreationTime(request.name()); reconcileStatus(request.name()); updateSameSubjectRefCommentCounter(comment.getSpec().getSubjectRef()); }); @@ -64,6 +67,28 @@ public class CommentReconciler implements Reconciler { .build(); } + /** + * If the comment creation time is null, set it to the approved time or the current time. + * TODO remove this method in the future and fill in attributes in hook mode instead. + * + * @param name comment name + */ + void compatibleCreationTime(String name) { + client.fetch(Comment.class, name).ifPresent(comment -> { + Instant creationTime = comment.getSpec().getCreationTime(); + Instant oldCreationTime = + creationTime == null ? null : Instant.ofEpochMilli(creationTime.toEpochMilli()); + if (creationTime == null) { + creationTime = defaultIfNull(comment.getSpec().getApprovedTime(), Instant.now()); + comment.getSpec().setCreationTime(creationTime); + } + + if (!Objects.equals(oldCreationTime, comment.getSpec().getCreationTime())) { + client.update(comment); + } + }); + } + private boolean isDeleted(Comment comment) { return comment.getMetadata().getDeletionTimestamp() != null; } @@ -89,7 +114,7 @@ public class CommentReconciler implements Reconciler { client.fetch(Comment.class, name).ifPresent(comment -> { Comment oldComment = JsonUtils.deepCopy(comment); Comment.CommentStatus status = comment.getStatusOrDefault(); - status.setHasNewReply(ObjectUtils.defaultIfNull(status.getUnreadReplyCount(), 0) > 0); + status.setHasNewReply(defaultIfNull(status.getUnreadReplyCount(), 0) > 0); updateUnReplyCountIfNecessary(comment); if (!oldComment.equals(comment)) { client.update(comment); diff --git a/src/test/java/run/halo/app/core/extension/reconciler/CommentReconcilerTest.java b/src/test/java/run/halo/app/core/extension/reconciler/CommentReconcilerTest.java index ed77ae0bf..4aed78447 100644 --- a/src/test/java/run/halo/app/core/extension/reconciler/CommentReconcilerTest.java +++ b/src/test/java/run/halo/app/core/extension/reconciler/CommentReconcilerTest.java @@ -6,6 +6,7 @@ import static org.mockito.ArgumentMatchers.eq; import static org.mockito.Mockito.lenient; import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; import java.time.Instant; import java.util.HashSet; @@ -85,6 +86,29 @@ class CommentReconcilerTest { .contains(CommentReconciler.FINALIZER_NAME)).isFalse(); } + @Test + void compatibleCreationTime() { + Comment comment = new Comment(); + comment.setMetadata(new Metadata()); + comment.getMetadata().setName("fake-comment"); + comment.setSpec(new Comment.CommentSpec()); + comment.getSpec().setApprovedTime(Instant.now()); + comment.getSpec().setCreationTime(null); + when(client.fetch(eq(Comment.class), eq("fake-comment"))) + .thenReturn(Optional.of(comment)); + + commentReconciler.compatibleCreationTime("fake-comment"); + + verify(client, times(1)).fetch(eq(Comment.class), eq("fake-comment")); + + ArgumentCaptor captor = ArgumentCaptor.forClass(Comment.class); + verify(client, times(1)).update(captor.capture()); + Comment updated = captor.getValue(); + assertThat(updated.getSpec().getCreationTime()).isNotNull(); + assertThat(updated.getSpec().getCreationTime()) + .isEqualTo(updated.getSpec().getApprovedTime()); + } + private static Ref getRef() { Ref ref = new Ref(); ref.setGroup("content.halo.run");