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) <noreply@anthropic.com>
This commit is contained in:
SlimusMinus
2026-10-13 01:07:26 +03:00
parent 5ea7b438f5
commit d19f03d412
3 changed files with 25 additions and 0 deletions

View File

@@ -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());

View File

@@ -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<PostResponse> 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);

View File

@@ -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");
}