mirror of https://github.com/halo-dev/halo
feat: update default comment sorter (#3149)
#### What type of PR is this? /kind feature /kind improvement #### What this PR does / why we need it: Update comments default sorter for console #### Which issue(s) this PR fixes: Fixes #3138 #### Special notes for your reviewer: #### Does this PR introduce a user-facing change? ```release-note 修改评论列表默认排序规则:依据最后回复时间及评论创建时间进行排序 ```pull/3152/head^2
parent
575fc95876
commit
e03aa4ef72
|
@ -3,6 +3,7 @@ package run.halo.app.content.comment;
|
||||||
import java.time.Instant;
|
import java.time.Instant;
|
||||||
import java.util.Comparator;
|
import java.util.Comparator;
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
|
import java.util.Optional;
|
||||||
import java.util.function.Function;
|
import java.util.function.Function;
|
||||||
import org.springframework.util.comparator.Comparators;
|
import org.springframework.util.comparator.Comparators;
|
||||||
import run.halo.app.core.extension.content.Comment;
|
import run.halo.app.core.extension.content.Comment;
|
||||||
|
@ -65,9 +66,13 @@ public enum CommentSorter {
|
||||||
}
|
}
|
||||||
|
|
||||||
static Comparator<Comment> defaultCommentComparator() {
|
static Comparator<Comment> defaultCommentComparator() {
|
||||||
Function<Comment, Instant> lastReplyTime =
|
Function<Comment, Instant> comparatorFunc =
|
||||||
comment -> comment.getStatusOrDefault().getLastReplyTime();
|
comment -> {
|
||||||
return Comparator.comparing(lastReplyTime, Comparators.nullsLow())
|
Instant lastReplyTime = comment.getStatusOrDefault().getLastReplyTime();
|
||||||
|
return Optional.ofNullable(
|
||||||
|
lastReplyTime).orElse(comment.getMetadata().getCreationTimestamp());
|
||||||
|
};
|
||||||
|
return Comparator.comparing(comparatorFunc, Comparators.nullsLow())
|
||||||
.thenComparing(name);
|
.thenComparing(name);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,6 +3,7 @@ package run.halo.app.content.comment;
|
||||||
import static org.assertj.core.api.Assertions.assertThat;
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
|
||||||
import java.time.Instant;
|
import java.time.Instant;
|
||||||
|
import java.util.ArrayList;
|
||||||
import java.util.Comparator;
|
import java.util.Comparator;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
@ -99,6 +100,23 @@ class CommentSorterTest {
|
||||||
assertThat(commentNames).isEqualTo(List.of("B", "A", "C"));
|
assertThat(commentNames).isEqualTo(List.of("B", "A", "C"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void sortByDefaultDesc() {
|
||||||
|
Comparator<Comment> defaultComparator = CommentSorter.defaultCommentComparator().reversed();
|
||||||
|
List<String> commentNames = comments().stream()
|
||||||
|
.sorted(defaultComparator)
|
||||||
|
.map(comment -> comment.getMetadata().getName())
|
||||||
|
.toList();
|
||||||
|
assertThat(commentNames).isEqualTo(List.of("B", "A", "C"));
|
||||||
|
|
||||||
|
|
||||||
|
List<String> commentList = commentsIncludeNoReply().stream()
|
||||||
|
.sorted(defaultComparator)
|
||||||
|
.map(comment -> comment.getMetadata().getName())
|
||||||
|
.toList();
|
||||||
|
assertThat(commentList).isEqualTo(List.of("D", "E", "B", "A", "C"));
|
||||||
|
}
|
||||||
|
|
||||||
List<Comment> comments() {
|
List<Comment> comments() {
|
||||||
final Instant now = Instant.now();
|
final Instant now = Instant.now();
|
||||||
Comment commentA = new Comment();
|
Comment commentA = new Comment();
|
||||||
|
@ -136,4 +154,33 @@ class CommentSorterTest {
|
||||||
|
|
||||||
return List.of(commentA, commentB, commentC);
|
return List.of(commentA, commentB, commentC);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
List<Comment> 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<Comment> comments = new ArrayList<>(comments());
|
||||||
|
comments.add(commentD);
|
||||||
|
comments.add(commentE);
|
||||||
|
|
||||||
|
return comments;
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue