Исправление валидации и обеспечение консинстентности кэша после падения редис
Skeleton contracts / contracts (pull_request) Canceled after 0s

Исправление security context утечки
This commit is contained in:
2026-10-10 08:33:01 +03:00
parent 30cf79907b
commit d496190646
15 changed files with 283 additions and 137 deletions
@@ -14,6 +14,7 @@ import ru.mtusi.messenger.mapper.ChatMapper;
import ru.mtusi.messenger.model.Chat;
import ru.mtusi.messenger.model.User;
import ru.mtusi.messenger.repository.ChatRepository;
import ru.mtusi.messenger.validation.ChatValidator;
import ru.mtusi.messenger.util.CursorCodec;
import java.util.*;
@@ -23,55 +24,74 @@ import java.util.*;
@RequiredArgsConstructor
public class ChatService {
private final ChatMapper chatMapper;
private final UserService userService;
private final ChatValidator chatValidator;
private final ChatRepository chatRepository;
@Transactional
public ChatDTO createChat(UUID caller , ChatDTO chatDTO) {
Set<User> members = validateCreate(caller, chatDTO);
public ChatDTO createChat(UUID caller, ChatDTO chatDTO) {
Set<User> members = chatValidator.validateCreate(caller, chatDTO);
Chat chat = buildChat(chatDTO, members);
try {
chat = chatRepository.saveAndFlush(chat);
} catch (DataIntegrityViolationException e) {
throw new NotFoundException("Cannot create chat: referenced entity is missing");
}
chat = persistChat(chat);
return chatMapper.toChatDto(chat);
}
private Set<User> validateCreate(UUID caller, ChatDTO chatDTO) {
Set<UUID> memberIds = chatDTO.getMemberIds();
if (!memberIds.contains(caller)) {
throw new InvalidRequestException("Вызывающий должен быть участником");
}
return userService.requireUsers(memberIds);
}
private Chat buildChat(ChatDTO chatDTO, Set<User> members) {
Chat chat = chatMapper.toChat(chatDTO);
chat.setMembers(members);
return chat;
}
private Chat persistChat(Chat chat) {
try {
return chatRepository.saveAndFlush(chat);
} catch (DataIntegrityViolationException e) {
throw new InvalidRequestException("Cannot create chat: invalid data");
}
}
@Transactional(readOnly = true)
public ChatDTO getChatDTO(UUID caller, UUID chatId) {
Chat chat = chatRepository.findById(chatId)
.orElseThrow(() -> new NotFoundException("Chat not found"));
if (chat.getMembers().stream().noneMatch(m -> m.getId().equals(caller))) {
throw new NotFoundException("Chat not found");
}
Chat chat = requireChatForMember(caller, chatId);
return chatMapper.toChatDto(chat);
}
@Transactional(readOnly = true)
public Chat getChat(UUID caller, UUID chatId) {
return requireChatForMember(caller, chatId);
}
private Chat requireChatForMember(UUID caller, UUID chatId) {
Chat chat = chatRepository.findById(chatId)
.orElseThrow(() -> new NotFoundException("Chat not found"));
if (chat.getMembers().stream().noneMatch(m -> m.getId().equals(caller))) {
throw new NotFoundException("Chat not found");
}
return chat;
}
@Transactional(readOnly = true)
public ChatPageDTO listChats(UUID userId, Integer limit, String cursor) {
int size = (limit == null) ? 50 : limit;
int size = resolveSize(limit);
CursorCodec.Cursor decoded = parseChatsCursor(cursor);
List<Chat> rows = (decoded == null) ? chatRepository.findPageByMember(userId, Pageable.ofSize(size + 1))
: chatRepository.findPageByMemberAfterCursor(userId, decoded.boundary().createdAt(), decoded.boundary().id(), Pageable.ofSize(size + 1));
List<Chat> rows = loadChatPage(userId, decoded, size);
return buildPage(rows, size);
}
private int resolveSize(Integer limit) {
return (limit == null) ? 50 : limit;
}
private List<Chat> loadChatPage(UUID userId, CursorCodec.Cursor cursor, int size) {
if (cursor == null) {
return chatRepository.findPageByMember(userId, Pageable.ofSize(size + 1));
}
return chatRepository.findPageByMemberAfterCursor(
userId,
cursor.boundary().createdAt(),
cursor.boundary().id(),
Pageable.ofSize(size + 1));
}
private CursorCodec.Cursor parseChatsCursor(String cursor) {
if (cursor == null || cursor.isBlank()) {
return null;
@@ -106,15 +126,4 @@ public class ChatService {
private CursorCodec.PageBoundary boundaryOf(Chat chat) {
return new CursorCodec.PageBoundary(chat.getCreatedAt(), chat.getId());
}
@Transactional(readOnly = true)
public Chat getChat(UUID caller, UUID chatId) {
Chat chat = chatRepository.findById(chatId)
.orElseThrow(() -> new NotFoundException("Chat not found"));
if (chat.getMembers().stream().noneMatch(m -> m.getId().equals(caller))) {
throw new NotFoundException("Chat not found");
}
return chat;
}
}
}