From e3d29ca527f9bbdc392e04acc89bbbfc78de74bb Mon Sep 17 00:00:00 2001 From: guqing Date: Wed, 13 Mar 2024 10:48:56 +0800 Subject: [PATCH] refactor: reply creation time --- .../java/run/halo/app/content/comment/ReplyQuery.java | 3 ++- .../app/core/extension/reconciler/ReplyReconciler.java | 10 ++++++++++ .../java/run/halo/app/infra/SchemeInitializer.java | 6 ++++-- 3 files changed, 16 insertions(+), 3 deletions(-) diff --git a/application/src/main/java/run/halo/app/content/comment/ReplyQuery.java b/application/src/main/java/run/halo/app/content/comment/ReplyQuery.java index dd2852b936..b32c33e1af 100644 --- a/application/src/main/java/run/halo/app/content/comment/ReplyQuery.java +++ b/application/src/main/java/run/halo/app/content/comment/ReplyQuery.java @@ -41,8 +41,9 @@ public String getCommentName() { public ListOptions toListOptions() { var listOptions = labelAndFieldSelectorToListOptions(getLabelSelector(), getFieldSelector()); - listOptions.getFieldSelector() + var newFieldSelector = listOptions.getFieldSelector() .andQuery(equal("spec.commentName", getCommentName())); + listOptions.setFieldSelector(newFieldSelector); return listOptions; } diff --git a/application/src/main/java/run/halo/app/core/extension/reconciler/ReplyReconciler.java b/application/src/main/java/run/halo/app/core/extension/reconciler/ReplyReconciler.java index 8d3a535216..97e9d59a30 100644 --- a/application/src/main/java/run/halo/app/core/extension/reconciler/ReplyReconciler.java +++ b/application/src/main/java/run/halo/app/core/extension/reconciler/ReplyReconciler.java @@ -1,5 +1,6 @@ package run.halo.app.core.extension.reconciler; +import static org.apache.commons.lang3.ObjectUtils.defaultIfNull; import static run.halo.app.extension.ExtensionUtil.addFinalizers; import java.util.Set; @@ -45,6 +46,15 @@ public Result reconcile(Request request) { eventPublisher.publishEvent(new ReplyCreatedEvent(this, reply)); } + if (reply.getSpec().getCreationTime() == null) { + reply.getSpec().setCreationTime( + defaultIfNull(reply.getSpec().getApprovedTime(), + reply.getMetadata().getCreationTimestamp() + ) + ); + } + client.update(reply); + replyNotificationSubscriptionHelper.subscribeNewReplyReasonForReply(reply); eventPublisher.publishEvent(new ReplyChangedEvent(this, reply)); diff --git a/application/src/main/java/run/halo/app/infra/SchemeInitializer.java b/application/src/main/java/run/halo/app/infra/SchemeInitializer.java index cd4856d54b..e2d1c692a9 100644 --- a/application/src/main/java/run/halo/app/infra/SchemeInitializer.java +++ b/application/src/main/java/run/halo/app/infra/SchemeInitializer.java @@ -223,7 +223,8 @@ public void onApplicationEvent(@NonNull ApplicationContextInitializedEvent event indexSpecs.add(new IndexSpec() .setName("spec.creationTime") .setIndexFunc(simpleAttribute(Comment.class, - comment -> comment.getSpec().getCreationTime().toString()) + comment -> defaultIfNull(comment.getSpec().getCreationTime(), + comment.getMetadata().getCreationTimestamp()).toString()) )); indexSpecs.add(new IndexSpec() .setName("spec.approved") @@ -286,7 +287,8 @@ public void onApplicationEvent(@NonNull ApplicationContextInitializedEvent event indexSpecs.add(new IndexSpec() .setName("spec.creationTime") .setIndexFunc(simpleAttribute(Reply.class, - reply -> reply.getSpec().getCreationTime().toString()) + reply -> defaultIfNull(reply.getSpec().getCreationTime(), + reply.getMetadata().getCreationTimestamp()).toString()) )); indexSpecs.add(new IndexSpec() .setName("spec.commentName")