Files
nahodka-back/src/main/java/com/krylov/refound/service/FavoriteService.java
SlimusMinus 6e001fbc41 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>
2026-10-20 01:02:12 +03:00

85 lines
2.7 KiB
Java

package com.krylov.refound.service;
import com.krylov.refound.dto.PostResponse;
import com.krylov.refound.entity.Favorite;
import com.krylov.refound.entity.Post;
import com.krylov.refound.entity.User;
import com.krylov.refound.enums.ErrorCode;
import com.krylov.refound.exception.ApiException;
import com.krylov.refound.mapper.PostMapper;
import com.krylov.refound.repository.FavoriteRepository;
import com.krylov.refound.repository.PostRepository;
import com.krylov.refound.util.PostVisibility;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@Service
@RequiredArgsConstructor
public class FavoriteService {
private final FavoriteRepository repository;
private final PostRepository postRepository;
private final UserService userService;
private final PostMapper mapper;
private final PostService postService;
@Transactional
public void toggle(Long postId) {
User user = userService.getCurrentUser();
Post post = postRepository.findById(postId).orElseThrow(() -> new ApiException(
ErrorCode.NOT_FOUND,
"Post not found",
HttpStatus.NOT_FOUND
));
if (repository.existsByUserAndPost(user, post)) {
repository.deleteByUserAndPost(user, post);
} else {
Favorite f = new Favorite();
f.setUser(user);
f.setPost(post);
repository.save(f);
}
}
@Transactional(readOnly = true)
public List<PostResponse> getFavorites() {
User user = userService.getCurrentUser();
return repository.findByUser(user)
.stream()
.map(Favorite::getPost)
.filter(post -> isVisibleTo(post, user))
.map(mapper::toResponse)
.toList();
}
@Transactional(readOnly = true)
public List<PostResponse> getAllFavoritesPosts() {
User user = userService.getCurrentUser();
List<Long> favoritePostIds = repository.findPostIdsByUserId(user.getId());
if (favoritePostIds.isEmpty()) {
return List.of();
}
return postRepository.findAllById(favoritePostIds).stream()
.filter(post -> isVisibleTo(post, user))
.map(post -> postService.toResponse(post, true))
.toList();
}
/**
* Пост из избранного мог уйти на повторную модерацию (после правки) или быть
* отклонён — такой виден только владельцу, как и в ленте.
*/
private static boolean isVisibleTo(Post post, User user) {
return PostVisibility.FEED_VISIBLE_STATUSES.contains(post.getStatus())
|| post.getUser().getId().equals(user.getId());
}
}