From d19f03d412a3b683174e91795a8893e10d9b13ca Mon Sep 17 00:00:00 2001 From: SlimusMinus Date: Tue, 13 Oct 2026 01:07:26 +0300 Subject: [PATCH] fix feed pagination order, moderation reject and cache invalidation - getFeed: add id DESC as tie-breaker so posts with equal createdAt are not duplicated or lost between pages - moderation: AI verdict false now sets REJECTED instead of ACTIVE - nightly cleanup: invalidate feed and map caches after deleting posts Co-Authored-By: Claude Opus 5.5 (1M context) --- .../refound/service/PostModerationExecutorService.java | 6 ++++++ .../java/com/krylov/refound/service/PostService.java | 9 +++++++++ .../com/krylov/refound/service/SchedulerService.java | 10 ++++++++++ 3 files changed, 25 insertions(+) diff --git a/src/main/java/com/krylov/refound/service/PostModerationExecutorService.java b/src/main/java/com/krylov/refound/service/PostModerationExecutorService.java index 8fde5c9..acc47aa 100644 --- a/src/main/java/com/krylov/refound/service/PostModerationExecutorService.java +++ b/src/main/java/com/krylov/refound/service/PostModerationExecutorService.java @@ -55,6 +55,12 @@ public class PostModerationExecutorService { return; } + if (!approved) { + postRepository.updateStatus(postId, PostStatus.REJECTED); + log.info("Пост {} отклонён модерацией.", post.getId()); + return; + } + postRepository.updateStatus(postId, PostStatus.ACTIVE); log.info("Пост {} прошёл модерацию.", post.getId()); diff --git a/src/main/java/com/krylov/refound/service/PostService.java b/src/main/java/com/krylov/refound/service/PostService.java index 77d9393..be6ca1d 100644 --- a/src/main/java/com/krylov/refound/service/PostService.java +++ b/src/main/java/com/krylov/refound/service/PostService.java @@ -27,7 +27,9 @@ import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.data.domain.Page; import org.springframework.data.domain.PageImpl; +import org.springframework.data.domain.PageRequest; import org.springframework.data.domain.Pageable; +import org.springframework.data.domain.Sort; import org.springframework.data.jpa.domain.Specification; import org.springframework.http.HttpStatus; import org.springframework.stereotype.Service; @@ -92,6 +94,13 @@ public class PostService { public Page getFeed(String search, Pageable pageable) { + // Без уникального ключа в сортировке порядок постов с одинаковым createdAt + // недетерминирован, и при LIMIT/OFFSET посты дублируются или пропадают между страницами + if (pageable.getSort().getOrderFor("id") == null) { + pageable = PageRequest.of(pageable.getPageNumber(), pageable.getPageSize(), + pageable.getSort().and(Sort.by(Sort.Direction.DESC, "id"))); + } + // Ключ содержит версию ленты, она читается до запроса в БД String cacheKey = postCacheKeyGenerator.feedKey(search, pageable); diff --git a/src/main/java/com/krylov/refound/service/SchedulerService.java b/src/main/java/com/krylov/refound/service/SchedulerService.java index ad7bf8a..44b7b7c 100644 --- a/src/main/java/com/krylov/refound/service/SchedulerService.java +++ b/src/main/java/com/krylov/refound/service/SchedulerService.java @@ -4,6 +4,8 @@ import com.krylov.refound.entity.Post; import com.krylov.refound.enums.PostStatus; import com.krylov.refound.repository.FavoriteRepository; import com.krylov.refound.repository.PostRepository; +import com.krylov.refound.service.redis.MapCacheService; +import com.krylov.refound.service.redis.PostCacheService; import java.time.LocalDateTime; import java.util.List; import lombok.RequiredArgsConstructor; @@ -20,6 +22,8 @@ public class SchedulerService { private final PostRepository postRepository; private final FavoriteRepository favoriteRepository; private final PostModerationExecutorService postModerationExecutorService; + private final PostCacheService postCacheService; + private final MapCacheService mapCacheService; /** * Runs every day at 00:01 to delete: @@ -55,6 +59,12 @@ public class SchedulerService { log.info("Deleted {} posts older than 6 months", oldPosts.size()); } + if (!closedPosts.isEmpty() || !oldPosts.isEmpty()) { + // Иначе удалённые посты висят в ленте и на карте до TTL кэша + postCacheService.invalidatePosts(); + mapCacheService.clearMapMarkersCache(); + } + log.info("Scheduled cleanup completed successfully"); }