From 0ce380072e4f34f40dd23328d70cf2c8ce005883 Mon Sep 17 00:00:00 2001 From: Evan Wang <37762337+wangming2674@users.noreply.github.com> Date: Thu, 2 Jan 2020 23:52:52 +0800 Subject: [PATCH] =?UTF-8?q?fix:repeated=20updates=20of=20visits=20and=20pr?= =?UTF-8?q?ivate=20article=20visits=20abnorm=E2=80=A6=20(#463)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../app/controller/content/ContentArchiveController.java | 3 ++- .../app/controller/content/ContentSheetController.java | 2 ++ src/main/java/run/halo/app/service/PostService.java | 7 +++++++ src/main/java/run/halo/app/service/SheetService.java | 7 +++++++ .../java/run/halo/app/service/impl/PostServiceImpl.java | 8 +++----- .../java/run/halo/app/service/impl/SheetServiceImpl.java | 8 +++----- 6 files changed, 24 insertions(+), 11 deletions(-) diff --git a/src/main/java/run/halo/app/controller/content/ContentArchiveController.java b/src/main/java/run/halo/app/controller/content/ContentArchiveController.java index 754d3d975..7d0a2ae71 100644 --- a/src/main/java/run/halo/app/controller/content/ContentArchiveController.java +++ b/src/main/java/run/halo/app/controller/content/ContentArchiveController.java @@ -35,6 +35,7 @@ import static org.springframework.data.domain.Sort.Direction.DESC; * * @author ryanwang * @author guqing + * @author evanwang * @date 2019-03-17 */ @Slf4j @@ -135,7 +136,7 @@ public class ContentArchiveController { } post.setFormatContent(MarkdownUtils.renderHtml(post.getOriginalContent())); } - + postService.publishVisitEvent(post.getId()); postService.getNextPost(post.getCreateTime()).ifPresent(nextPost -> model.addAttribute("nextPost", nextPost)); postService.getPrePost(post.getCreateTime()).ifPresent(prePost -> model.addAttribute("prePost", prePost)); diff --git a/src/main/java/run/halo/app/controller/content/ContentSheetController.java b/src/main/java/run/halo/app/controller/content/ContentSheetController.java index 8194323b9..89c7e2e56 100644 --- a/src/main/java/run/halo/app/controller/content/ContentSheetController.java +++ b/src/main/java/run/halo/app/controller/content/ContentSheetController.java @@ -28,6 +28,7 @@ import static org.springframework.data.domain.Sort.Direction.DESC; * Content sheet controller. * * @author ryanwang + * @author evanwang * @date 2019-03-21 */ @Controller @@ -119,6 +120,7 @@ public class ContentSheetController { throw new ForbiddenException("您没有该页面的访问权限"); } } + sheetService.publishVisitEvent(sheet.getId()); SheetDetailVO sheetDetailVO = sheetService.convertToDetailVo(sheet); diff --git a/src/main/java/run/halo/app/service/PostService.java b/src/main/java/run/halo/app/service/PostService.java index e9bb6c29a..dbb978cf2 100755 --- a/src/main/java/run/halo/app/service/PostService.java +++ b/src/main/java/run/halo/app/service/PostService.java @@ -182,4 +182,11 @@ public interface PostService extends BasePostService { * @return a page of post detail vo */ Page convertToDetailVo(@NonNull Page postPage); + + /** + * Publish a post visit event. + * + * @param postId postId must not be null + */ + void publishVisitEvent(@NonNull Integer postId); } diff --git a/src/main/java/run/halo/app/service/SheetService.java b/src/main/java/run/halo/app/service/SheetService.java index e426e42f8..cf1d6c161 100644 --- a/src/main/java/run/halo/app/service/SheetService.java +++ b/src/main/java/run/halo/app/service/SheetService.java @@ -124,4 +124,11 @@ public interface SheetService extends BasePostService { */ @NonNull SheetDetailVO convertToDetailVo(@NonNull Sheet sheet); + + /** + * Publish a sheet visit event. + * + * @param sheetId sheetId must not be null + */ + void publishVisitEvent(@NonNull Integer sheetId); } diff --git a/src/main/java/run/halo/app/service/impl/PostServiceImpl.java b/src/main/java/run/halo/app/service/impl/PostServiceImpl.java index 1392f47c2..40148396f 100644 --- a/src/main/java/run/halo/app/service/impl/PostServiceImpl.java +++ b/src/main/java/run/halo/app/service/impl/PostServiceImpl.java @@ -51,6 +51,7 @@ import static org.springframework.data.domain.Sort.Direction.DESC; * @author johnniang * @author ryanwang * @author guqing + * @author evanwang * @date 2019-03-14 */ @Slf4j @@ -159,8 +160,6 @@ public class PostServiceImpl extends BasePostServiceImpl implements PostSe public Post getBy(PostStatus status, String url) { Post post = super.getBy(status, url); - fireVisitEvent(post.getId()); - return post; } @@ -176,8 +175,6 @@ public class PostServiceImpl extends BasePostServiceImpl implements PostSe public Post getByUrl(String url) { Post post = super.getByUrl(url); - fireVisitEvent(post.getId()); - return post; } @@ -630,7 +627,8 @@ public class PostServiceImpl extends BasePostServiceImpl implements PostSe return convertTo(post, tags, categories, postMetaList); } - private void fireVisitEvent(@NonNull Integer postId) { + @Override + public void publishVisitEvent(Integer postId) { eventPublisher.publishEvent(new PostVisitEvent(this, postId)); } } diff --git a/src/main/java/run/halo/app/service/impl/SheetServiceImpl.java b/src/main/java/run/halo/app/service/impl/SheetServiceImpl.java index 6dd7fd9c2..a3b4b92c7 100644 --- a/src/main/java/run/halo/app/service/impl/SheetServiceImpl.java +++ b/src/main/java/run/halo/app/service/impl/SheetServiceImpl.java @@ -33,6 +33,7 @@ import java.util.*; * * @author johnniang * @author ryanwang + * @author evanwang * @date 2019-04-24 */ @Slf4j @@ -130,8 +131,6 @@ public class SheetServiceImpl extends BasePostServiceImpl implements Shee Sheet sheet = sheetRepository.getByUrl(url).orElseThrow(() -> new NotFoundException("查询不到该页面的信息").setErrorData(url)); - fireVisitEvent(sheet.getId()); - return sheet; } @@ -144,8 +143,6 @@ public class SheetServiceImpl extends BasePostServiceImpl implements Shee Sheet sheet = postOptional.orElseThrow(() -> new NotFoundException("查询不到该页面的信息").setErrorData(url)); - fireVisitEvent(sheet.getId()); - return sheet; } @@ -261,7 +258,8 @@ public class SheetServiceImpl extends BasePostServiceImpl implements Shee }); } - private void fireVisitEvent(@NonNull Integer sheetId) { + @Override + public void publishVisitEvent(Integer sheetId) { eventPublisher.publishEvent(new SheetVisitEvent(this, sheetId)); }