fix: incorrect number of comments (#3295)

<!--  Thanks for sending a pull request!  Here are some tips for you:
1. 如果这是你的第一次,请阅读我们的贡献指南:<https://github.com/halo-dev/halo/blob/master/CONTRIBUTING.md>。
1. If this is your first time, please read our contributor guidelines: <https://github.com/halo-dev/halo/blob/master/CONTRIBUTING.md>.
2. 请根据你解决问题的类型为 Pull Request 添加合适的标签。
2. Please label this pull request according to what type of issue you are addressing, especially if this is a release targeted pull request.
3. 请确保你已经添加并运行了适当的测试。
3. Ensure you have added or ran the appropriate tests for your PR.
-->

#### What type of PR is this?
/kind bug
/area core
<!--
添加其中一个类别:
Add one of the following kinds:

/kind bug
/kind cleanup
/kind documentation
/kind feature
/kind improvement

适当添加其中一个或多个类别(可选):
Optionally add one or more of the following kinds if applicable:

/kind api-change
/kind deprecation
/kind failing-test
/kind flake
/kind regression
-->

#### What this PR does / why we need it:
Fixed incorrect number of comments after comment deletion.
#### Which issue(s) this PR fixes:

<!--
PR 合并时自动关闭 issue。
Automatically closes linked issue when PR is merged.

用法:`Fixes #<issue 号>`,或者 `Fixes (粘贴 issue 完整链接)`
Usage: `Fixes #<issue number>`, or `Fixes (paste link of issue)`.
-->
Fixes https://github.com/halo-dev/halo/issues/3287

#### Special notes for your reviewer:
为一篇文章添加多个评论,期望评论数量正确。
删除评论,期望评论数量正常减少。
删除全部评论,期望评论数量归零。
#### Does this PR introduce a user-facing change?

<!--
如果当前 Pull Request 的修改不会造成用户侧的任何变更,在 `release-note` 代码块儿中填写 `NONE`。
否则请填写用户侧能够理解的 Release Note。如果当前 Pull Request 包含破坏性更新(Break Change),
Release Note 需要以 `action required` 开头。
If no, just write "NONE" in the release-note block below.
If yes, a release note is required:
Enter your extended release note in the block below. If the PR requires additional action from users switching to the new release, include the string "action required".
-->

```release-note
修复删除评论后Console端文章和单页评论数量不正确
```
pull/3314/head^2
longjuan 2023-02-15 14:52:12 +08:00 committed by GitHub
parent 5f7ea18f7c
commit 9dee9b0409
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 31 additions and 42 deletions

View File

@ -5,10 +5,9 @@ import java.util.Comparator;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Collectors;
import org.apache.commons.lang3.BooleanUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.lang.Nullable;
import org.springframework.stereotype.Component;
@ -54,7 +53,7 @@ public class CommentReconciler implements Reconciler<Reconciler.Request> {
}
addFinalizerIfNecessary(comment);
reconcileStatus(request.name());
updateCommentCounter();
updateSameSubjectRefCommentCounter(comment.getSpec().getSubjectRef());
});
return new Result(false, null);
}
@ -122,49 +121,39 @@ public class CommentReconciler implements Reconciler<Reconciler.Request> {
}
}
private void updateCommentCounter() {
Map<Ref, List<RefCommentTuple>> map = client.list(Comment.class, null, null)
.stream()
.map(comment -> {
boolean approved =
Objects.equals(true, comment.getSpec().getApproved())
&& !isDeleted(comment);
return new RefCommentTuple(comment.getSpec().getSubjectRef(),
comment.getMetadata().getName(), approved);
})
.collect(Collectors.groupingBy(RefCommentTuple::ref));
map.forEach((ref, pairs) -> {
GroupVersionKind groupVersionKind = groupVersionKind(ref);
if (groupVersionKind == null) {
return;
}
// approved total count
long approvedTotalCount = pairs.stream()
.filter(refCommentPair -> refCommentPair.approved)
.count();
// total count
int totalCount = pairs.size();
private void updateSameSubjectRefCommentCounter(Ref commentSubjectRef) {
List<Comment> comments = client.list(Comment.class,
comment -> !isDeleted(comment)
&& commentSubjectRef.equals(comment.getSpec().getSubjectRef()),
null);
schemeManager.fetch(groupVersionKind).ifPresent(scheme -> {
String counterName = MeterUtils.nameOf(ref.getGroup(), scheme.plural(),
ref.getName());
client.fetch(Counter.class, counterName).ifPresentOrElse(counter -> {
counter.setTotalComment(totalCount);
counter.setApprovedComment((int) approvedTotalCount);
client.update(counter);
}, () -> {
Counter counter = Counter.emptyCounter(counterName);
counter.setTotalComment(totalCount);
counter.setApprovedComment((int) approvedTotalCount);
client.create(counter);
});
GroupVersionKind groupVersionKind = groupVersionKind(commentSubjectRef);
if (groupVersionKind == null) {
return;
}
// approved total count
long approvedTotalCount = comments.stream()
.filter(comment -> BooleanUtils.isTrue(comment.getSpec().getApproved()))
.count();
// total count
int totalCount = comments.size();
schemeManager.fetch(groupVersionKind).ifPresent(scheme -> {
String counterName = MeterUtils.nameOf(commentSubjectRef.getGroup(), scheme.plural(),
commentSubjectRef.getName());
client.fetch(Counter.class, counterName).ifPresentOrElse(counter -> {
counter.setTotalComment(totalCount);
counter.setApprovedComment((int) approvedTotalCount);
client.update(counter);
}, () -> {
Counter counter = Counter.emptyCounter(counterName);
counter.setTotalComment(totalCount);
counter.setApprovedComment((int) approvedTotalCount);
client.create(counter);
});
});
}
record RefCommentTuple(Ref ref, String name, boolean approved) {
}
private void cleanUpResourcesAndRemoveFinalizer(String commentName) {
client.fetch(Comment.class, commentName).ifPresent(comment -> {
cleanUpResources(comment);
@ -182,7 +171,7 @@ public class CommentReconciler implements Reconciler<Reconciler.Request> {
null)
.forEach(client::delete);
// decrement total comment count
updateCommentCounter();
updateSameSubjectRefCommentCounter(comment.getSpec().getSubjectRef());
}
@Nullable