Skip to content

Commit

Permalink
Fix the problem of post list is incomplete due to no recursive query …
Browse files Browse the repository at this point in the history
…categories (halo-dev#1781)

* fix: Post list is incomplete due to no recursive query categories

* feat: Add unit test case for PostRefreshStatusListener
  • Loading branch information
guqing authored Mar 28, 2022
1 parent d47022a commit f5f7ddb
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
package run.halo.app.listener.post;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import org.apache.commons.lang3.StringUtils;
import org.springframework.context.event.EventListener;
import org.springframework.lang.NonNull;
import org.springframework.stereotype.Component;
import run.halo.app.event.category.CategoryUpdatedEvent;
import run.halo.app.event.post.PostUpdatedEvent;
import run.halo.app.model.entity.Category;
import run.halo.app.model.entity.Post;
import run.halo.app.model.entity.PostCategory;
import run.halo.app.model.enums.PostStatus;
import run.halo.app.service.CategoryService;
import run.halo.app.service.PostCategoryService;
import run.halo.app.service.PostService;
import run.halo.app.utils.ServiceUtils;

/**
* Post status management.
Expand Down Expand Up @@ -47,7 +53,7 @@ public void categoryUpdatedListener(CategoryUpdatedEvent event) {
}
boolean isPrivate = categoryService.isPrivate(category.getId());

List<Post> posts = postCategoryService.listPostBy(category.getId());
List<Post> posts = findPostsByCategoryIdRecursively(category.getId());
if (isPrivate) {
posts.forEach(post -> post.setStatus(PostStatus.INTIMATE));
} else {
Expand All @@ -56,6 +62,17 @@ public void categoryUpdatedListener(CategoryUpdatedEvent event) {
postService.updateInBatch(posts);
}

@NonNull
private List<Post> findPostsByCategoryIdRecursively(Integer categoryId) {
Set<Integer> categoryIds =
ServiceUtils.fetchProperty(categoryService.listAllByParentId(categoryId),
Category::getId);
List<PostCategory> postCategories =
postCategoryService.listByCategoryIdList(new ArrayList<>(categoryIds));
Set<Integer> postIds = ServiceUtils.fetchProperty(postCategories, PostCategory::getPostId);
return postService.listAllByIds(postIds);
}

/**
* If the post belongs to any encryption category, set the status to INTIMATE.
*
Expand All @@ -71,7 +88,7 @@ public void postUpdatedListener(PostUpdatedEvent event) {
.stream()
.anyMatch(postCategory -> categoryService.isPrivate(postCategory.getCategoryId()));

if (isPrivate) {
if (isPrivate || StringUtils.isNotBlank(post.getPassword())) {
post.setStatus(PostStatus.INTIMATE);
postService.update(post);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package run.halo.app.listener;

import static org.assertj.core.api.Assertions.assertThat;

import java.util.Set;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.jdbc.EmbeddedDatabaseConnection;
import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.event.ApplicationEvents;
import org.springframework.test.context.event.RecordApplicationEvents;
import run.halo.app.event.category.CategoryUpdatedEvent;
import run.halo.app.model.entity.Category;
import run.halo.app.model.entity.Post;
import run.halo.app.model.enums.PostStatus;
import run.halo.app.model.vo.PostDetailVO;
import run.halo.app.service.CategoryService;
import run.halo.app.service.PostService;

/**
* @author guqing
* @date 2022-03-28
*/
@SpringBootTest
@RecordApplicationEvents
@AutoConfigureTestDatabase(connection = EmbeddedDatabaseConnection.H2)
public class PostRefreshStatusListenerTest {

@Autowired
private ApplicationEvents applicationEvents;

@Autowired
private CategoryService categoryService;

@Autowired
private PostService postService;

private Post post;
private Category category1;

@BeforeEach
public void setUp() {
category1 = new Category();
category1.setId(1);
category1.setName("category-1");
category1.setSlug("category-1");
category1.setPassword("123");
category1.setParentId(0);

Category category2 = new Category();
category2.setId(2);
category2.setName("category-2");
category2.setSlug("category-2");
category2.setParentId(1);

categoryService.create(category1);
categoryService.create(category2);

post = new Post();
post.setId(1);
post.setSlug("post-1");
post.setTitle("post-title");
}

@Test
public void clearCategoryPasswordOfTopLevelTest() {
// After clearing the password of the top-level category,
// all articles belonging to this category and sub categories should be set
// to draft status.
PostDetailVO postDetailVO =
postService.createBy(post, Set.of(), Set.of(2), Set.of(), false);
assertThat(postDetailVO).isNotNull();
assertThat(postDetailVO.getStatus()).isEqualTo(PostStatus.INTIMATE);

category1.setPassword(null);
categoryService.update(category1);
assertThat(applicationEvents
.stream(CategoryUpdatedEvent.class)
.filter(event -> event.getCategory().getId() == 1)
.count())
.isEqualTo(1);

Post updatedPost = postService.getById(postDetailVO.getId());
assertThat(updatedPost).isNotNull();
assertThat(updatedPost.getStatus()).isEqualTo(PostStatus.DRAFT);
}
}

0 comments on commit f5f7ddb

Please sign in to comment.