From 5522a7d53eaa4323c7bc5c904626a62d6533f1f3 Mon Sep 17 00:00:00 2001 From: jonghun Date: Wed, 29 May 2024 06:45:40 +0900 Subject: [PATCH 1/4] =?UTF-8?q?#103=20fix=20:=20=EC=B1=84=ED=8C=85=20?= =?UTF-8?q?=EC=A0=84=EC=86=A1=EC=8B=9C=20=ED=98=95=EC=8B=9D=20String=20->?= =?UTF-8?q?=20ChatMessageResponse?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../domain/chat/handler/ChatHandler.java | 26 +++++++++++++------ 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/src/main/java/com/seoultech/synergybe/domain/chat/handler/ChatHandler.java b/src/main/java/com/seoultech/synergybe/domain/chat/handler/ChatHandler.java index a31fef0..ed5cec2 100644 --- a/src/main/java/com/seoultech/synergybe/domain/chat/handler/ChatHandler.java +++ b/src/main/java/com/seoultech/synergybe/domain/chat/handler/ChatHandler.java @@ -1,8 +1,10 @@ package com.seoultech.synergybe.domain.chat.handler; +import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; import com.seoultech.synergybe.domain.chat.domain.ChatType; import com.seoultech.synergybe.domain.chat.dto.request.ChatMessageRequest; +import com.seoultech.synergybe.domain.chat.dto.response.ChatMessageResponse; import com.seoultech.synergybe.domain.chat.exception.WebSocketBadRequestException; import com.seoultech.synergybe.domain.chat.service.ChatMessageService; import com.seoultech.synergybe.domain.user.User; @@ -110,9 +112,6 @@ protected void handleTextMessage(WebSocketSession session, TextMessage message) // 채팅 전송 sendMessageToChatRoom(chatMessageRequest, webSocketSessionList); - // 한사람에 대해서만 저장을 해야함 - saveMessage(chatMessageRequest); - } else if (chatMessageRequest.chatType().equals(ChatType.IMAGE)) { // todo // 이미지 혹은 영상 처리 @@ -141,19 +140,30 @@ private List getSessionListByChatRoomId(Long chatRoomId) { return webSocketSessionMap.getWebsocketListHashMap().get(chatRoomId).getWebSocketSessions(); } - private void sendMessageToChatRoom(ChatMessageRequest chatMessageRequest, WebSocketSessionList webSocketSessionList) { + private void sendMessageToChatRoom(ChatMessageRequest chatMessageRequest, WebSocketSessionList webSocketSessionList) throws JsonProcessingException { for (WebSocketSession session : webSocketSessionList.getWebSocketSessions()) { - sendMessage(session, chatMessageRequest.message()); + // 한사람에 대해서만 저장을 해야함 + String chatId = saveMessage(chatMessageRequest); + ChatMessageResponse chatMessageResponse = new ChatMessageResponse( + chatId, + chatMessageRequest.chatRoomId(), + chatMessageRequest.userId(), + chatMessageRequest.message(), + chatMessageRequest.chatType(), + null, + null + ); + sendMessage(session, objectMapper.writeValueAsString(chatMessageResponse)); } } - private void saveMessage(ChatMessageRequest chatMessageRequest) { - chatMessageService.saveChat(chatMessageRequest); + private String saveMessage(ChatMessageRequest chatMessageRequest) { + return chatMessageService.saveChat(chatMessageRequest); } private void sendMessage(WebSocketSession session, T message) { try { - session.sendMessage(new TextMessage(objectMapper.writeValueAsString(message))); + session.sendMessage(new TextMessage(message.toString())); } catch (IOException e) { log.error(e.getMessage(), e); } From 83b5963d527803242327328550648eeb0839a892 Mon Sep 17 00:00:00 2001 From: jonghun Date: Wed, 29 May 2024 06:46:03 +0900 Subject: [PATCH 2/4] =?UTF-8?q?#103=20feat=20:=20ChatMessageResponse=20?= =?UTF-8?q?=EC=83=9D=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../chat/dto/response/ChatMessageResponse.java | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 src/main/java/com/seoultech/synergybe/domain/chat/dto/response/ChatMessageResponse.java diff --git a/src/main/java/com/seoultech/synergybe/domain/chat/dto/response/ChatMessageResponse.java b/src/main/java/com/seoultech/synergybe/domain/chat/dto/response/ChatMessageResponse.java new file mode 100644 index 0000000..5f5fc2f --- /dev/null +++ b/src/main/java/com/seoultech/synergybe/domain/chat/dto/response/ChatMessageResponse.java @@ -0,0 +1,14 @@ +package com.seoultech.synergybe.domain.chat.dto.response; + +import com.seoultech.synergybe.domain.chat.domain.ChatType; + +public record ChatMessageResponse( + String id, + Long chatRoomId, + String userId, + String message, + ChatType chatType, + String imageName, + String imageUrl +) { +} From a6ad470e85f8bb9e958161820986dc55a5eaec3e Mon Sep 17 00:00:00 2001 From: jonghun Date: Wed, 29 May 2024 06:46:35 +0900 Subject: [PATCH 3/4] =?UTF-8?q?#103=20fix=20:=20=EC=B1=84=ED=8C=85=20?= =?UTF-8?q?=EC=A0=80=EC=9E=A5=EC=8B=9C=20ChatId=20=EB=B0=98=ED=99=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../domain/chat/service/ChatMessageService.java | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/seoultech/synergybe/domain/chat/service/ChatMessageService.java b/src/main/java/com/seoultech/synergybe/domain/chat/service/ChatMessageService.java index 0161abf..a565624 100644 --- a/src/main/java/com/seoultech/synergybe/domain/chat/service/ChatMessageService.java +++ b/src/main/java/com/seoultech/synergybe/domain/chat/service/ChatMessageService.java @@ -17,9 +17,8 @@ public class ChatMessageService { private final ChatMessageRepository chatMessageRepository; - public void saveChat(ChatMessageRequest chatMessageRequest) { + public String saveChat(ChatMessageRequest chatMessageRequest) { // mongoDB는 UTC로 저장되므로 +9를 해주어 한국시간과 맞춘다 -// LocalDateTime createAt = LocalDateTime.now().plusHours(9); LocalDateTime createAt = LocalDateTime.now(); if (chatMessageRequest.chatType().equals(ChatType.TEXT)) { @@ -31,10 +30,16 @@ public void saveChat(ChatMessageRequest chatMessageRequest) { .createAt(createAt) .build(); - log.info("chatMessage Id" + chatMessage.getId()); + log.info("chatMessage Id: " + chatMessage.getId()); + log.info("chatMessage message " + chatMessage.getMessage()); - chatMessageRepository.save(chatMessage); + ChatMessage chat = chatMessageRepository.save(chatMessage); + + log.info("chat Id: " + chat.getId()); + + + return chat.getId(); } else if (chatMessageRequest.chatType().equals(ChatType.IMAGE)) { ChatMessage chatMessage = ChatMessage.builder() @@ -46,7 +51,11 @@ public void saveChat(ChatMessageRequest chatMessageRequest) { .createAt(createAt) .build(); chatMessageRepository.save(chatMessage); + + return chatMessage.getId(); + } + return ""; } public List getChatListByChatRoomId(Long chatRoomId) { From e917a547395ce26fd43f19c187ceadcf6633f32c Mon Sep 17 00:00:00 2001 From: jonghun Date: Wed, 29 May 2024 06:47:22 +0900 Subject: [PATCH 4/4] =?UTF-8?q?#103=20fix=20:=20=EC=B1=84=ED=8C=85?= =?UTF-8?q?=EB=B0=A9=20=EC=9A=94=EC=B2=AD=EC=8B=9C=20userIds=20=EB=B0=98?= =?UTF-8?q?=ED=99=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../dto/response/GetChatRoomResponse.java | 5 ++++- .../domain/chat/service/ChatRoomService.java | 20 ++++++++++++------- 2 files changed, 17 insertions(+), 8 deletions(-) diff --git a/src/main/java/com/seoultech/synergybe/domain/chat/dto/response/GetChatRoomResponse.java b/src/main/java/com/seoultech/synergybe/domain/chat/dto/response/GetChatRoomResponse.java index 622f9ae..a41b65c 100644 --- a/src/main/java/com/seoultech/synergybe/domain/chat/dto/response/GetChatRoomResponse.java +++ b/src/main/java/com/seoultech/synergybe/domain/chat/dto/response/GetChatRoomResponse.java @@ -1,7 +1,10 @@ package com.seoultech.synergybe.domain.chat.dto.response; +import java.util.List; + public record GetChatRoomResponse( Long chatRoomId, - String chatRoomName + String chatRoomName, + List userIds ) { } diff --git a/src/main/java/com/seoultech/synergybe/domain/chat/service/ChatRoomService.java b/src/main/java/com/seoultech/synergybe/domain/chat/service/ChatRoomService.java index f03ea3f..ff500f9 100644 --- a/src/main/java/com/seoultech/synergybe/domain/chat/service/ChatRoomService.java +++ b/src/main/java/com/seoultech/synergybe/domain/chat/service/ChatRoomService.java @@ -9,6 +9,7 @@ import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Service; +import java.util.ArrayList; import java.util.List; @Service @@ -32,13 +33,18 @@ public void createRoom(CreateChatRoomRequest request) { public List getChatRoomsByUserId(String userId) { List chatRooms = chatRoomRepository.findAllByCreateUserIdOrAttendUserId(userId); - - List getChatRoomResponses = chatRooms.stream().map( - chatRoom -> new GetChatRoomResponse( - chatRoom.getId(), chatRoom.getName() - ) + return chatRooms.stream().map( + chatRoom -> { + // 기존 userIds 리스트를 복사하고, 새로운 userId를 추가 + List newUserIds = new ArrayList<>(); + newUserIds.add(chatRoom.getCreateUser().getId()); + newUserIds.add(chatRoom.getAttendUser().getId()); + return new GetChatRoomResponse( + chatRoom.getId(), + chatRoom.getName(), + newUserIds + ); + } ).toList(); - - return getChatRoomResponses; } }