diff --git a/src/main/java/run/halo/app/content/comment/CommentSorter.java b/src/main/java/run/halo/app/content/comment/CommentSorter.java index 1305285e8..62d3a08e2 100644 --- a/src/main/java/run/halo/app/content/comment/CommentSorter.java +++ b/src/main/java/run/halo/app/content/comment/CommentSorter.java @@ -3,6 +3,7 @@ package run.halo.app.content.comment; import java.time.Instant; import java.util.Comparator; import java.util.Objects; +import java.util.Optional; import java.util.function.Function; import org.springframework.util.comparator.Comparators; import run.halo.app.core.extension.content.Comment; @@ -65,9 +66,13 @@ public enum CommentSorter { } static Comparator defaultCommentComparator() { - Function lastReplyTime = - comment -> comment.getStatusOrDefault().getLastReplyTime(); - return Comparator.comparing(lastReplyTime, Comparators.nullsLow()) + Function comparatorFunc = + comment -> { + Instant lastReplyTime = comment.getStatusOrDefault().getLastReplyTime(); + return Optional.ofNullable( + lastReplyTime).orElse(comment.getMetadata().getCreationTimestamp()); + }; + return Comparator.comparing(comparatorFunc, Comparators.nullsLow()) .thenComparing(name); } } diff --git a/src/test/java/run/halo/app/content/comment/CommentSorterTest.java b/src/test/java/run/halo/app/content/comment/CommentSorterTest.java index 692c57430..20f5a50e7 100644 --- a/src/test/java/run/halo/app/content/comment/CommentSorterTest.java +++ b/src/test/java/run/halo/app/content/comment/CommentSorterTest.java @@ -3,6 +3,7 @@ package run.halo.app.content.comment; import static org.assertj.core.api.Assertions.assertThat; import java.time.Instant; +import java.util.ArrayList; import java.util.Comparator; import java.util.List; import org.junit.jupiter.api.Test; @@ -99,6 +100,23 @@ class CommentSorterTest { assertThat(commentNames).isEqualTo(List.of("B", "A", "C")); } + @Test + void sortByDefaultDesc() { + Comparator defaultComparator = CommentSorter.defaultCommentComparator().reversed(); + List commentNames = comments().stream() + .sorted(defaultComparator) + .map(comment -> comment.getMetadata().getName()) + .toList(); + assertThat(commentNames).isEqualTo(List.of("B", "A", "C")); + + + List commentList = commentsIncludeNoReply().stream() + .sorted(defaultComparator) + .map(comment -> comment.getMetadata().getName()) + .toList(); + assertThat(commentList).isEqualTo(List.of("D", "E", "B", "A", "C")); + } + List comments() { final Instant now = Instant.now(); Comment commentA = new Comment(); @@ -136,4 +154,33 @@ class CommentSorterTest { return List.of(commentA, commentB, commentC); } + + List commentsIncludeNoReply() { + + final Instant now = Instant.now(); + Comment commentD = new Comment(); + commentD.setMetadata(new Metadata()); + commentD.getMetadata().setName("D"); + + commentD.getMetadata().setCreationTimestamp(now.plusSeconds(50)); + + commentD.setSpec(new Comment.CommentSpec()); + commentD.setStatus(new Comment.CommentStatus()); + + Comment commentE = new Comment(); + commentE.setMetadata(new Metadata()); + commentE.getMetadata().setName("E"); + + commentE.getMetadata().setCreationTimestamp(now.plusSeconds(20)); + + commentE.setSpec(new Comment.CommentSpec()); + commentE.setStatus(new Comment.CommentStatus()); + + List comments = new ArrayList<>(comments()); + comments.add(commentD); + comments.add(commentE); + + return comments; + + } } \ No newline at end of file