diff --git a/src/main/java/run/halo/app/service/base/BaseCommentService.java b/src/main/java/run/halo/app/service/base/BaseCommentService.java index 6bcad1570..37172c378 100644 --- a/src/main/java/run/halo/app/service/base/BaseCommentService.java +++ b/src/main/java/run/halo/app/service/base/BaseCommentService.java @@ -220,19 +220,20 @@ public interface BaseCommentService extends CrudSer * * @param comment comment dto must not be null */ - void filterIpAddress(@NonNull BaseCommentDTO comment); + T filterIpAddress(@NonNull T comment); /** * Filters comment ip address. * * @param comments comment dto list */ - void filterIpAddress(@Nullable List comments); + List filterIpAddress(@Nullable List comments); /** * Filters comment ip address. * * @param commentPage comment page */ - void filterIpAddress(@NonNull Page commentPage); + Page filterIpAddress(@NonNull Page commentPage); + } diff --git a/src/main/java/run/halo/app/service/impl/BaseCommentServiceImpl.java b/src/main/java/run/halo/app/service/impl/BaseCommentServiceImpl.java index 929d57e39..33dcb8e81 100644 --- a/src/main/java/run/halo/app/service/impl/BaseCommentServiceImpl.java +++ b/src/main/java/run/halo/app/service/impl/BaseCommentServiceImpl.java @@ -471,26 +471,50 @@ public abstract class BaseCommentServiceImpl extend } @Override - public void filterIpAddress(BaseCommentDTO comment) { + public T filterIpAddress(@NonNull T comment) { Assert.notNull(comment, "Base comment dto must not be null"); // Clear ip address comment.setIpAddress(""); + + // Handle base comment vo + if (comment instanceof BaseCommentVO) { + BaseCommentVO baseCommentVO = (BaseCommentVO) comment; + Queue commentQueue = new LinkedList<>(); + commentQueue.offer(baseCommentVO); + while (!commentQueue.isEmpty()) { + BaseCommentVO current = commentQueue.poll(); + + // Clear ip address + current.setIpAddress(""); + + if (!CollectionUtils.isEmpty(current.getChildren())) { + // Add children + commentQueue.addAll(current.getChildren()); + } + } + } + + return comment; } @Override - public void filterIpAddress(List comments) { + public List filterIpAddress(List comments) { if (CollectionUtils.isEmpty(comments)) { - return; + return Collections.emptyList(); } comments.forEach(this::filterIpAddress); + + return comments; } @Override - public void filterIpAddress(Page commentPage) { + public Page filterIpAddress(Page commentPage) { Assert.notNull(commentPage, "Comment page must not be null"); commentPage.forEach(this::filterIpAddress); + + return commentPage; } /**