Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/main'
Browse files Browse the repository at this point in the history
  • Loading branch information
ruibaby committed Apr 8, 2024
2 parents 01778ef + 52204d6 commit 76a4b68
Show file tree
Hide file tree
Showing 7 changed files with 291 additions and 253 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ public static class CommentOwner {
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;
Expand Down
50 changes: 0 additions & 50 deletions api/src/main/java/run/halo/app/core/extension/content/Post.java
Original file line number Diff line number Diff line change
Expand Up @@ -222,54 +222,4 @@ public static VisibleEnum from(String value) {
return null;
}
}

@Data
public static class CompactPost {
private String name;

private VisibleEnum visible;

private Boolean published;

public static Builder builder() {
return new Builder();
}

/**
* <p>Compact post builder.</p>
* <p>Can not replace with lombok builder.</p>
* <p>The class used by subclasses of {@link AbstractExtension} must have a no-args
* constructor.</p>
*/
public static class Builder {
private String name;

private VisibleEnum visible;

private Boolean published;

public Builder name(String name) {
this.name = name;
return this;
}

public Builder visible(VisibleEnum visible) {
this.visible = visible;
return this;
}

public Builder published(Boolean published) {
this.published = published;
return this;
}

public CompactPost build() {
CompactPost compactPost = new CompactPost();
compactPost.setName(name);
compactPost.setVisible(visible);
compactPost.setPublished(published);
return compactPost;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import static run.halo.app.extension.index.query.QueryFactory.and;
import static run.halo.app.extension.index.query.QueryFactory.equal;
import static run.halo.app.extension.index.query.QueryFactory.in;
import static run.halo.app.extension.index.query.QueryFactory.isNull;

import java.time.Duration;
Expand All @@ -11,14 +12,13 @@
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 lombok.AllArgsConstructor;
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.stereotype.Component;
import run.halo.app.content.permalinks.CategoryPermalinkPolicy;
import run.halo.app.core.extension.content.Category;
Expand All @@ -27,10 +27,13 @@
import run.halo.app.extension.ExtensionClient;
import run.halo.app.extension.ListOptions;
import run.halo.app.extension.MetadataUtil;
import run.halo.app.extension.PageRequestImpl;
import run.halo.app.extension.controller.Controller;
import run.halo.app.extension.controller.ControllerBuilder;
import run.halo.app.extension.controller.Reconciler;
import run.halo.app.extension.index.query.Query;
import run.halo.app.extension.router.selector.FieldSelector;
import run.halo.app.extension.router.selector.LabelSelector;
import run.halo.app.infra.utils.JsonUtils;

/**
Expand All @@ -42,7 +45,7 @@
@Component
@AllArgsConstructor
public class CategoryReconciler implements Reconciler<Reconciler.Request> {
private static final String FINALIZER_NAME = "category-protection";
static final String FINALIZER_NAME = "category-protection";
private final ExtensionClient client;
private final CategoryPermalinkPolicy categoryPermalinkPolicy;

Expand Down Expand Up @@ -140,53 +143,46 @@ private void reconcileStatusPosts(String name) {

private void populatePosts(Category category) {
String name = category.getMetadata().getName();
List<String> categoryNames = listChildrenByName(name)
Set<String> categoryNames = listChildrenByName(name)
.stream()
.map(item -> item.getMetadata().getName())
.toList();
.collect(Collectors.toSet());

var totalPostCount = countTotalPosts(categoryNames);
category.getStatusOrDefault().setPostCount(totalPostCount);

var visiblePostCount = countVisiblePosts(categoryNames);
category.getStatusOrDefault().setVisiblePostCount(visiblePostCount);
}

int countTotalPosts(Set<String> categoryNames) {
var postListOptions = new ListOptions();
postListOptions.setFieldSelector(FieldSelector.of(
and(isNull("metadata.deletionTimestamp"),
equal("spec.deleted", "false")))
basePostQuery(categoryNames)
));
return (int) client.listBy(Post.class, postListOptions, PageRequestImpl.ofSize(1))
.getTotal();
}

int countVisiblePosts(Set<String> categoryNames) {
var postListOptions = new ListOptions();
var fieldQuery = and(basePostQuery(categoryNames),
equal("spec.visible", Post.VisibleEnum.PUBLIC.name())
);
var posts = client.listAll(Post.class, postListOptions, Sort.unsorted());

// populate post to status
List<Post.CompactPost> compactPosts = posts.stream()
.filter(post -> includes(post.getSpec().getCategories(), categoryNames))
.map(post -> Post.CompactPost.builder()
.name(post.getMetadata().getName())
.visible(post.getSpec().getVisible())
.published(post.isPublished())
.build())
.toList();
category.getStatusOrDefault().setPostCount(compactPosts.size());

long visiblePostCount = compactPosts.stream()
.filter(post -> Objects.equals(true, post.getPublished())
&& Post.VisibleEnum.PUBLIC.equals(post.getVisible()))
.count();
category.getStatusOrDefault().setVisiblePostCount((int) visiblePostCount);
var labelSelector = LabelSelector.builder()
.eq(Post.PUBLISHED_LABEL, BooleanUtils.TRUE)
.build();
postListOptions.setFieldSelector(FieldSelector.of(fieldQuery));
postListOptions.setLabelSelector(labelSelector);
return (int) client.listBy(Post.class, postListOptions, PageRequestImpl.ofSize(1))
.getTotal();
}

/**
* whether {@code categoryRefs} contains elements in {@code categoryNames}.
*
* @param categoryRefs category left to judge
* @param categoryNames category right to judge
* @return true if {@code categoryRefs} contains elements in {@code categoryNames}.
*/
private boolean includes(@Nullable List<String> categoryRefs, List<String> categoryNames) {
if (categoryRefs == null || categoryNames == null) {
return false;
}
for (String categoryRef : categoryRefs) {
if (categoryNames.contains(categoryRef)) {
return true;
}
}
return false;
private static Query basePostQuery(Set<String> categoryNames) {
return and(isNull("metadata.deletionTimestamp"),
equal("spec.deleted", BooleanUtils.FALSE),
in("spec.categories", categoryNames)
);
}

private List<Category> listChildrenByName(String name) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,19 @@
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;
Expand Down Expand Up @@ -167,6 +170,15 @@ private Mono<? extends CommentVo> filterCommentSensitiveData(CommentVo commentVo
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");
}
Expand Down Expand Up @@ -210,6 +222,15 @@ private Mono<? extends ReplyVo> filterReplySensitiveData(ReplyVo replyVo) {
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");
}
Expand Down
Loading

0 comments on commit 76a4b68

Please sign in to comment.