From 6da76dccf0d57e9dbd7e4667d3bf8f831ad0ade7 Mon Sep 17 00:00:00 2001 From: balex Date: Sat, 28 Feb 2026 02:41:37 +0100 Subject: [PATCH] fix list chat --- .gitlab-ci.yml | 1 + .../balex/rag/controller/ChatController.java | 16 +++++------ .../rag/controller/ChatEntryController.java | 5 +++- .../com/balex/rag/repo/ChatRepository.java | 4 ++- .../com/balex/rag/service/ChatService.java | 7 ++--- .../rag/service/impl/ChatServiceImpl.java | 28 ++++++++++--------- 6 files changed, 34 insertions(+), 27 deletions(-) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index d0bd123..0298d57 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -269,6 +269,7 @@ deploy-all: rules: - if: $CI_COMMIT_BRANCH == "main" when: manual + allow_failure: true needs: [] script: - | diff --git a/rag-service/src/main/java/com/balex/rag/controller/ChatController.java b/rag-service/src/main/java/com/balex/rag/controller/ChatController.java index 18144bb..abf792c 100644 --- a/rag-service/src/main/java/com/balex/rag/controller/ChatController.java +++ b/rag-service/src/main/java/com/balex/rag/controller/ChatController.java @@ -22,24 +22,24 @@ public class ChatController { private final ChatService chatService; private final EventPublisher eventPublisher; + private final ApiUtils apiUtils; @GetMapping("") public ResponseEntity> mainPage() { - log.trace(ApiLogMessage.NAME_OF_CURRENT_METHOD.getValue(), ApiUtils.getMethodName()); - List response = chatService.getAllChats(); + Long ownerId = apiUtils.getUserIdFromAuthentication().longValue(); + List response = chatService.getAllChatsByOwner(ownerId); return ResponseEntity.ok(response); } @GetMapping("/{chatId}") public ResponseEntity showChat(@PathVariable Long chatId) { - log.trace(ApiLogMessage.NAME_OF_CURRENT_METHOD.getValue(), ApiUtils.getMethodName()); - Chat response = chatService.getChat(chatId); + Long ownerId = apiUtils.getUserIdFromAuthentication().longValue(); + Chat response = chatService.getChat(chatId, ownerId); return ResponseEntity.ok(response); } @PostMapping("/new") public ResponseEntity newChat(@RequestParam String title) { - log.trace(ApiLogMessage.NAME_OF_CURRENT_METHOD.getValue(), ApiUtils.getMethodName()); Chat chat = chatService.createNewChat(title); eventPublisher.publishChatCreated( @@ -51,9 +51,9 @@ public class ChatController { @DeleteMapping("/{chatId}") public ResponseEntity deleteChat(@PathVariable Long chatId) { - log.trace(ApiLogMessage.NAME_OF_CURRENT_METHOD.getValue(), ApiUtils.getMethodName()); - Chat chat = chatService.getChat(chatId); - chatService.deleteChat(chatId); + Long ownerId = apiUtils.getUserIdFromAuthentication().longValue(); + Chat chat = chatService.getChat(chatId, ownerId); + chatService.deleteChat(chatId, ownerId); eventPublisher.publishChatDeleted( chat.getIdOwner().toString(), diff --git a/rag-service/src/main/java/com/balex/rag/controller/ChatEntryController.java b/rag-service/src/main/java/com/balex/rag/controller/ChatEntryController.java index 1b9be88..82b9486 100644 --- a/rag-service/src/main/java/com/balex/rag/controller/ChatEntryController.java +++ b/rag-service/src/main/java/com/balex/rag/controller/ChatEntryController.java @@ -26,6 +26,7 @@ public class ChatEntryController { private final ChatService chatService; private final RagDefaultsProperties ragDefaults; private final EventPublisher eventPublisher; + private final ApiUtils apiUtils; @PostMapping("/{chatId}") public ResponseEntity addUserEntry( @@ -33,10 +34,12 @@ public class ChatEntryController { @RequestBody UserEntryRequest request) { log.trace(ApiLogMessage.NAME_OF_CURRENT_METHOD.getValue(), ApiUtils.getMethodName()); + Long ownerId = apiUtils.getUserIdFromAuthentication().longValue(); + boolean onlyContext = request.onlyContext() != null ? request.onlyContext() : ragDefaults.onlyContext(); double topP = request.topP() != null ? request.topP() : ragDefaults.topP(); - Chat chat = chatService.getChat(chatId); + Chat chat = chatService.getChat(chatId, ownerId); ChatEntry entry = chatEntryService.addUserEntry(chatId, request.content(), onlyContext, topP, chat.getIdOwner()); eventPublisher.publishQuerySent( diff --git a/rag-service/src/main/java/com/balex/rag/repo/ChatRepository.java b/rag-service/src/main/java/com/balex/rag/repo/ChatRepository.java index 78adbcf..901a49e 100644 --- a/rag-service/src/main/java/com/balex/rag/repo/ChatRepository.java +++ b/rag-service/src/main/java/com/balex/rag/repo/ChatRepository.java @@ -3,7 +3,9 @@ package com.balex.rag.repo; import com.balex.rag.model.entity.Chat; import org.springframework.data.jpa.repository.JpaRepository; -public interface ChatRepository extends JpaRepository { +import java.util.List; +public interface ChatRepository extends JpaRepository { + List findByIdOwnerOrderByCreatedAtDesc(Long idOwner); } diff --git a/rag-service/src/main/java/com/balex/rag/service/ChatService.java b/rag-service/src/main/java/com/balex/rag/service/ChatService.java index bc7e48d..b4473c8 100644 --- a/rag-service/src/main/java/com/balex/rag/service/ChatService.java +++ b/rag-service/src/main/java/com/balex/rag/service/ChatService.java @@ -9,12 +9,11 @@ public interface ChatService { Chat createNewChat(String title); - List getAllChats(); + List getAllChatsByOwner(Long ownerId); - Chat getChat(Long chatId); + Chat getChat(Long chatId, Long ownerId); - void deleteChat(Long chatId); + void deleteChat(Long chatId, Long ownerId); SseEmitter proceedInteractionWithStreaming(Long chatId, String userPrompt); - } diff --git a/rag-service/src/main/java/com/balex/rag/service/impl/ChatServiceImpl.java b/rag-service/src/main/java/com/balex/rag/service/impl/ChatServiceImpl.java index 6e323e9..92941cf 100644 --- a/rag-service/src/main/java/com/balex/rag/service/impl/ChatServiceImpl.java +++ b/rag-service/src/main/java/com/balex/rag/service/impl/ChatServiceImpl.java @@ -10,7 +10,9 @@ import org.springframework.ai.chat.client.ChatClient; import org.springframework.ai.chat.memory.ChatMemory; import org.springframework.ai.chat.model.ChatResponse; import org.springframework.data.domain.Sort; +import org.springframework.http.HttpStatus; import org.springframework.stereotype.Service; +import org.springframework.web.server.ResponseStatusException; import org.springframework.web.servlet.mvc.method.annotation.SseEmitter; import java.util.List; @@ -20,13 +22,11 @@ import java.util.List; public class ChatServiceImpl implements ChatService { private final ChatRepository chatRepo; - private final ChatClient chatClient; - private final ApiUtils apiUtils; - public List getAllChats() { - return chatRepo.findAll(Sort.by(Sort.Direction.DESC, "createdAt")); + public List getAllChatsByOwner(Long ownerId) { + return chatRepo.findByIdOwnerOrderByCreatedAtDesc(ownerId); } public Chat createNewChat(String title) { @@ -39,17 +39,21 @@ public class ChatServiceImpl implements ChatService { return chat; } - public Chat getChat(Long chatId) { - return chatRepo.findById(chatId).orElseThrow(); + public Chat getChat(Long chatId, Long ownerId) { + Chat chat = chatRepo.findById(chatId).orElseThrow( + () -> new ResponseStatusException(HttpStatus.NOT_FOUND, "Chat not found")); + if (!chat.getIdOwner().equals(ownerId)) { + throw new ResponseStatusException(HttpStatus.FORBIDDEN, "Access denied"); + } + return chat; } - public void deleteChat(Long chatId) { - chatRepo.deleteById(chatId); + public void deleteChat(Long chatId, Long ownerId) { + Chat chat = getChat(chatId, ownerId); + chatRepo.deleteById(chat.getId()); } - public SseEmitter proceedInteractionWithStreaming(Long chatId, String userPrompt) { - SseEmitter sseEmitter = new SseEmitter(0L); final StringBuilder answer = new StringBuilder(); @@ -65,12 +69,10 @@ public class ChatServiceImpl implements ChatService { return sseEmitter; } - @SneakyThrows private static void processToken(ChatResponse response, SseEmitter emitter, StringBuilder answer) { var token = response.getResult().getOutput(); emitter.send(token); answer.append(token.getText()); } - -} +} \ No newline at end of file