Fix post deletion, moderation races and cache leftovers

- PostService.delete did not remove favorites (FK without cascade -> 500 once a
  post was favorited) or chat links; owner, admin and nightly cleanup now share
  PostDeletionService, which deletes MinIO files only after commit. The nightly
  job also removes the images of expired posts.
- Moderation reads a snapshot in a short transaction, calls AI outside of it and
  applies the verdict only if the post is still in MODERATION and unchanged, so
  an edit made during a check can no longer go live unmoderated. Storage/AI
  outages keep the post in MODERATION instead of rejecting it.
- Posts without a category broke PostMapper with an NPE; title/phone/reward and
  address are checked against column sizes; a long address no longer overflows
  posts.city; Nominatim outages fall back to the address instead of 503.
- Favorites list hides posts that are back in moderation or rejected.
- Statistics counters use atomic UPDATEs instead of read-modify-write.
- Removed MapCacheService: its version key was bumped but never read.
- hibernate.default_batch_fetch_size=50 removes N+1 on images/users; show-sql
  is off by default.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
SlimusMinus
2026-10-20 01:02:12 +03:00
parent 98e6789b7e
commit 6e001fbc41
18 changed files with 397 additions and 328 deletions

View File

@@ -23,14 +23,12 @@ import com.krylov.refound.enums.PostCategory;
import com.krylov.refound.enums.PostStatus;
import com.krylov.refound.enums.PostType;
import com.krylov.refound.exception.ApiException;
import com.krylov.refound.repository.ChatRepository;
import com.krylov.refound.repository.FavoriteRepository;
import com.krylov.refound.repository.ImageRepository;
import com.krylov.refound.repository.PostRepository;
import com.krylov.refound.service.FileStorageService;
import com.krylov.refound.service.GeocodingService;
import com.krylov.refound.service.PostDeletionService;
import com.krylov.refound.service.StatisticsService;
import com.krylov.refound.service.redis.MapCacheService;
import com.krylov.refound.service.redis.PostCacheService;
import jakarta.persistence.criteria.CriteriaBuilder;
import jakarta.persistence.criteria.CriteriaQuery;
@@ -63,17 +61,13 @@ class AdminPostServiceTest {
@Mock
private ImageRepository imageRepository;
@Mock
private FavoriteRepository favoriteRepository;
@Mock
private ChatRepository chatRepository;
@Mock
private FileStorageService fileStorageService;
@Mock
private GeocodingService geocodingService;
@Mock
private PostCacheService postCacheService;
@Mock
private MapCacheService mapCacheService;
private PostDeletionService postDeletionService;
@Mock
private StatisticsService statisticsService;
@@ -244,7 +238,6 @@ class AdminPostServiceTest {
assertThat(post.getCity()).isEqualTo("Москва");
assertThat(post.getIsReward()).isTrue();
verify(postCacheService).invalidatePosts();
verify(mapCacheService).clearMapMarkersCache();
}
@Test
@@ -265,8 +258,9 @@ class AdminPostServiceTest {
false, null, List.of(), "[\"images/keep.png\"]");
assertThat(post.getImages()).extracting(Image::getUrl).containsExactly("images/keep.png");
verify(fileStorageService).deleteFile("images/drop.png");
verify(fileStorageService, never()).deleteFile("images/keep.png");
// Файл удаляется после коммита, а не посреди транзакции
verify(postDeletionService).deleteFilesAfterCommit(List.of("images/drop.png"));
verify(fileStorageService, never()).deleteFile(any());
}
@Test
@@ -357,38 +351,24 @@ class AdminPostServiceTest {
}
@Test
void deletePost_clearsFilesFavoritesAndChatLinks() {
void deletePost_delegatesToPostDeletionService() {
Post post = post(1L, PostStatus.CLOSED);
Image image = new Image();
image.setUrl("images/cat.png");
image.setPost(post);
post.getImages().add(image);
when(postRepository.findById(1L)).thenReturn(java.util.Optional.of(post));
service.deletePost(1L);
verify(fileStorageService).deleteFile("images/cat.png");
verify(favoriteRepository).deleteByPostIn(anyList());
verify(chatRepository).clearPostReference(1L);
verify(postRepository).delete(post);
verify(postCacheService).invalidatePosts();
verify(postDeletionService).delete(List.of(post));
}
@Test
void deletePost_doesNotFailWhenFileStorageIsDown() {
Post post = post(1L, PostStatus.CLOSED);
Image image = new Image();
image.setUrl("images/cat.png");
image.setPost(post);
post.getImages().add(image);
void updatePost_rejectsTooLongTitle() {
Post post = post(1L, PostStatus.ACTIVE);
when(postRepository.findById(1L)).thenReturn(java.util.Optional.of(post));
org.mockito.Mockito.doThrow(new RuntimeException("MinIO недоступен"))
.when(fileStorageService).deleteFile(any());
service.deletePost(1L);
verify(postRepository).delete(post);
assertThatThrownBy(() -> service.updatePost(1L, "LOSS", "к".repeat(256), null, null, null, null, null,
null, false, null, List.of(), "[]"))
.isInstanceOf(ApiException.class)
.satisfies(ex -> assertThat(((ApiException) ex).getStatus()).isEqualTo(HttpStatus.BAD_REQUEST));
verify(postRepository, never()).save(any(Post.class));
}
}