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
vayci 2023-01-16 10:58:13 +08:00 committed by GitHub
parent 575fc95876
commit e03aa4ef72
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 55 additions and 3 deletions

View File

@ -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<Comment> defaultCommentComparator() {
Function<Comment, Instant> lastReplyTime =
comment -> comment.getStatusOrDefault().getLastReplyTime();
return Comparator.comparing(lastReplyTime, Comparators.nullsLow())
Function<Comment, Instant> comparatorFunc =
comment -> {
Instant lastReplyTime = comment.getStatusOrDefault().getLastReplyTime();
return Optional.ofNullable(
lastReplyTime).orElse(comment.getMetadata().getCreationTimestamp());
};
return Comparator.comparing(comparatorFunc, Comparators.nullsLow())
.thenComparing(name);
}
}

View File

@ -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<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() {
final Instant now = Instant.now();
Comment commentA = new Comment();
@ -136,4 +154,33 @@ class CommentSorterTest {
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;
}
}