mirror of https://github.com/halo-dev/halo
Fix the problem of post list is incomplete due to no recursive query categories (#1781)
* fix: Post list is incomplete due to no recursive query categories * feat: Add unit test case for PostRefreshStatusListenerpull/1785/head
parent
d47022ac31
commit
f5f7ddb410
|
@ -1,16 +1,22 @@
|
||||||
package run.halo.app.listener.post;
|
package run.halo.app.listener.post;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Set;
|
||||||
|
import org.apache.commons.lang3.StringUtils;
|
||||||
import org.springframework.context.event.EventListener;
|
import org.springframework.context.event.EventListener;
|
||||||
|
import org.springframework.lang.NonNull;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
import run.halo.app.event.category.CategoryUpdatedEvent;
|
import run.halo.app.event.category.CategoryUpdatedEvent;
|
||||||
import run.halo.app.event.post.PostUpdatedEvent;
|
import run.halo.app.event.post.PostUpdatedEvent;
|
||||||
import run.halo.app.model.entity.Category;
|
import run.halo.app.model.entity.Category;
|
||||||
import run.halo.app.model.entity.Post;
|
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.model.enums.PostStatus;
|
||||||
import run.halo.app.service.CategoryService;
|
import run.halo.app.service.CategoryService;
|
||||||
import run.halo.app.service.PostCategoryService;
|
import run.halo.app.service.PostCategoryService;
|
||||||
import run.halo.app.service.PostService;
|
import run.halo.app.service.PostService;
|
||||||
|
import run.halo.app.utils.ServiceUtils;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Post status management.
|
* Post status management.
|
||||||
|
@ -47,7 +53,7 @@ public class PostRefreshStatusListener {
|
||||||
}
|
}
|
||||||
boolean isPrivate = categoryService.isPrivate(category.getId());
|
boolean isPrivate = categoryService.isPrivate(category.getId());
|
||||||
|
|
||||||
List<Post> posts = postCategoryService.listPostBy(category.getId());
|
List<Post> posts = findPostsByCategoryIdRecursively(category.getId());
|
||||||
if (isPrivate) {
|
if (isPrivate) {
|
||||||
posts.forEach(post -> post.setStatus(PostStatus.INTIMATE));
|
posts.forEach(post -> post.setStatus(PostStatus.INTIMATE));
|
||||||
} else {
|
} else {
|
||||||
|
@ -56,6 +62,17 @@ public class PostRefreshStatusListener {
|
||||||
postService.updateInBatch(posts);
|
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.
|
* If the post belongs to any encryption category, set the status to INTIMATE.
|
||||||
*
|
*
|
||||||
|
@ -71,7 +88,7 @@ public class PostRefreshStatusListener {
|
||||||
.stream()
|
.stream()
|
||||||
.anyMatch(postCategory -> categoryService.isPrivate(postCategory.getCategoryId()));
|
.anyMatch(postCategory -> categoryService.isPrivate(postCategory.getCategoryId()));
|
||||||
|
|
||||||
if (isPrivate) {
|
if (isPrivate || StringUtils.isNotBlank(post.getPassword())) {
|
||||||
post.setStatus(PostStatus.INTIMATE);
|
post.setStatus(PostStatus.INTIMATE);
|
||||||
postService.update(post);
|
postService.update(post);
|
||||||
}
|
}
|
||||||
|
|
|
@ -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);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue