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

@@ -0,0 +1,52 @@
package com.krylov.refound.util;
import com.krylov.refound.enums.ErrorCode;
import com.krylov.refound.exception.ApiException;
import org.springframework.http.HttpStatus;
import org.springframework.util.StringUtils;
/**
* Длины текстовых полей объявления по схеме БД (changelog 002, 003, 015, 018).
* Без проверки слишком длинное значение доходило до INSERT и давало 500.
*/
public final class PostFieldLimits {
public static final int TITLE = 255;
public static final int PHONE = 255;
public static final int REWARD_TEXT = 255;
public static final int ADDRESS = 500;
public static final int CITY = 100;
private PostFieldLimits() {
}
/** Проверяет поля формы объявления; 400 VALIDATION_ERROR при нарушении. */
public static void validate(String title, String address, String phone, String rewardText) {
if (!StringUtils.hasText(title)) {
throw new ApiException(ErrorCode.VALIDATION_ERROR, "Заголовок обязателен", HttpStatus.BAD_REQUEST);
}
checkLength("Заголовок", title, TITLE);
checkLength("Адрес", address, ADDRESS);
checkLength("Телефон", phone, PHONE);
checkLength("Текст вознаграждения", rewardText, REWARD_TEXT);
}
/**
* Город, когда геокодер его не дал: берётся адрес из формы, обрезанный под
* колонку city (адрес допускает 500 символов, город — 100).
*/
public static String cityFromAddress(String address) {
if (!StringUtils.hasText(address)) {
return null;
}
String city = address.trim();
return city.length() > CITY ? city.substring(0, CITY) : city;
}
private static void checkLength(String field, String value, int max) {
if (value != null && value.length() > max) {
throw new ApiException(ErrorCode.VALIDATION_ERROR,
field + " длиннее " + max + " символов", HttpStatus.BAD_REQUEST);
}
}
}