From c630a37eea1e61001b39188e95b995d35d6210af Mon Sep 17 00:00:00 2001 From: mashirot Date: Mon, 8 Apr 2024 16:14:07 +0800 Subject: [PATCH] feat: add gravatar url into annotation of spec of comment for anonymous (#5642) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit /kind feature /area core #### What this PR does / why we need it: see https://github.com/halo-dev/plugin-comment-widget/issues/97 #### Which issue(s) this PR fixes: Fixes #5641 ```release-note 评论数据返回邮箱地址 hash 值以便生成默认头像 ``` --- .../app/core/extension/content/Comment.java | 1 + .../impl/CommentPublicQueryServiceImpl.java | 21 +++++++++++++++++++ ...mentPublicQueryServiceIntegrationTest.java | 2 +- 3 files changed, 23 insertions(+), 1 deletion(-) diff --git a/api/src/main/java/run/halo/app/core/extension/content/Comment.java b/api/src/main/java/run/halo/app/core/extension/content/Comment.java index 1d5818283..d7550eecb 100644 --- a/api/src/main/java/run/halo/app/core/extension/content/Comment.java +++ b/api/src/main/java/run/halo/app/core/extension/content/Comment.java @@ -100,6 +100,7 @@ public class Comment extends AbstractExtension { public static final String KIND_EMAIL = "Email"; public static final String AVATAR_ANNO = "avatar"; public static final String WEBSITE_ANNO = "website"; + public static final String EMAIL_HASH_ANNO = "email-hash"; @Schema(requiredMode = REQUIRED, minLength = 1) private String kind; diff --git a/application/src/main/java/run/halo/app/theme/finders/impl/CommentPublicQueryServiceImpl.java b/application/src/main/java/run/halo/app/theme/finders/impl/CommentPublicQueryServiceImpl.java index ccb04c2e7..3a39a80c2 100644 --- a/application/src/main/java/run/halo/app/theme/finders/impl/CommentPublicQueryServiceImpl.java +++ b/application/src/main/java/run/halo/app/theme/finders/impl/CommentPublicQueryServiceImpl.java @@ -8,16 +8,19 @@ import static run.halo.app.extension.index.query.QueryFactory.isNull; import static run.halo.app.extension.index.query.QueryFactory.or; import java.security.Principal; +import java.util.HashMap; import java.util.Optional; import java.util.function.Function; import lombok.RequiredArgsConstructor; import org.apache.commons.lang3.BooleanUtils; +import org.apache.commons.lang3.StringUtils; import org.springframework.data.domain.Sort; import org.springframework.lang.Nullable; import org.springframework.security.core.context.ReactiveSecurityContextHolder; import org.springframework.security.core.context.SecurityContext; import org.springframework.stereotype.Component; import org.springframework.util.Assert; +import org.springframework.util.DigestUtils; import reactor.core.publisher.Flux; import reactor.core.publisher.Mono; import run.halo.app.content.comment.OwnerInfo; @@ -167,6 +170,15 @@ public class CommentPublicQueryServiceImpl implements CommentPublicQueryService commentVo.getSpec().setIpAddress(""); var specOwner = commentVo.getSpec().getOwner(); specOwner.setName(""); + var email = owner.getEmail(); + if (StringUtils.isNotBlank(email)) { + var emailHash = DigestUtils.md5DigestAsHex(email.getBytes()); + if (specOwner.getAnnotations() == null) { + specOwner.setAnnotations(new HashMap<>(2)); + } + specOwner.getAnnotations() + .put(Comment.CommentOwner.EMAIL_HASH_ANNO, emailHash); + } if (specOwner.getAnnotations() != null) { specOwner.getAnnotations().remove("Email"); } @@ -210,6 +222,15 @@ public class CommentPublicQueryServiceImpl implements CommentPublicQueryService replyVo.getSpec().setIpAddress(""); var specOwner = replyVo.getSpec().getOwner(); specOwner.setName(""); + var email = owner.getEmail(); + if (StringUtils.isNotBlank(email)) { + var emailHash = DigestUtils.md5DigestAsHex(email.getBytes()); + if (specOwner.getAnnotations() == null) { + specOwner.setAnnotations(new HashMap<>(2)); + } + specOwner.getAnnotations() + .put(Comment.CommentOwner.EMAIL_HASH_ANNO, emailHash); + } if (specOwner.getAnnotations() != null) { specOwner.getAnnotations().remove("Email"); } diff --git a/application/src/test/java/run/halo/app/theme/finders/impl/CommentPublicQueryServiceIntegrationTest.java b/application/src/test/java/run/halo/app/theme/finders/impl/CommentPublicQueryServiceIntegrationTest.java index 3258ac08e..463e5b056 100644 --- a/application/src/test/java/run/halo/app/theme/finders/impl/CommentPublicQueryServiceIntegrationTest.java +++ b/application/src/test/java/run/halo/app/theme/finders/impl/CommentPublicQueryServiceIntegrationTest.java @@ -389,7 +389,7 @@ class CommentPublicQueryServiceIntegrationTest { "name":"", "displayName":"fake-display-name", "annotations":{ - + "email-hash": "4249f4df72b475e7894fabed1c5888cf" } }, "creationTime": "2024-03-11T06:23:42.923294424Z",