From 23e74e05420e2a01019fdec9c44c90291429b2af Mon Sep 17 00:00:00 2001 From: HyemIin Date: Fri, 2 Feb 2024 20:20:00 +0900 Subject: [PATCH 1/3] =?UTF-8?q?Refactor=20:=20ChatRoom=20=EB=A6=AC?= =?UTF-8?q?=ED=8C=A9=ED=86=A0=EB=A7=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - chatRoomService 몽고DB 메세지 저장 메서드 수정 - CorsConfig 이름 수정 --- .../chat/chatroom/service/ChatRoomService.java | 18 ++++++++---------- .../config/{WebConfig.java => CorsConfig.java} | 2 +- .../chat/global/config/RedisConfig.java | 1 + 3 files changed, 10 insertions(+), 11 deletions(-) rename src/main/java/com/catchroom/chat/global/config/{WebConfig.java => CorsConfig.java} (94%) diff --git a/src/main/java/com/catchroom/chat/chatroom/service/ChatRoomService.java b/src/main/java/com/catchroom/chat/chatroom/service/ChatRoomService.java index 233194c..749f2c2 100644 --- a/src/main/java/com/catchroom/chat/chatroom/service/ChatRoomService.java +++ b/src/main/java/com/catchroom/chat/chatroom/service/ChatRoomService.java @@ -80,21 +80,19 @@ private void setListChatLastMessage(ChatRoomListGetResponse chatRoomListGetRespo // 몽고 디비에서 마지막 메시지 가져와서 저장. String chatRoomNumber = chatRoomListGetResponse.getChatRoomNumber(); - + //TODO 레디스에 마지막 메세지가 없으면?? if (chatRoomRedisRepository.getLastMessage(chatRoomNumber) != null) { chatRoomListGetResponse.updateChatMessageDto( - chatRoomRedisRepository.getLastMessage(chatRoomNumber) + chatRoomRedisRepository.getLastMessage(chatRoomNumber) ); - return; - } - - ChatMessage chatMessage = chatMongoService.findLatestMessageByRoomId(chatRoomNumber); - if (chatMessage != null) { - chatRoomListGetResponse.updateChatMessageDto( + } else { + ChatMessage chatMessage = chatMongoService.findLatestMessageByRoomId(chatRoomNumber); + if (chatMessage != null) { + chatRoomListGetResponse.updateChatMessageDto( ChatMessageDto.fromEntity(chatMessage) - ); + ); + } } - } /** diff --git a/src/main/java/com/catchroom/chat/global/config/WebConfig.java b/src/main/java/com/catchroom/chat/global/config/CorsConfig.java similarity index 94% rename from src/main/java/com/catchroom/chat/global/config/WebConfig.java rename to src/main/java/com/catchroom/chat/global/config/CorsConfig.java index 4bddbbb..6b66627 100644 --- a/src/main/java/com/catchroom/chat/global/config/WebConfig.java +++ b/src/main/java/com/catchroom/chat/global/config/CorsConfig.java @@ -5,7 +5,7 @@ import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; @Configuration -public class WebConfig implements WebMvcConfigurer { +public class CorsConfig implements WebMvcConfigurer { @Override public void addCorsMappings(CorsRegistry registry) { diff --git a/src/main/java/com/catchroom/chat/global/config/RedisConfig.java b/src/main/java/com/catchroom/chat/global/config/RedisConfig.java index 23e254b..74a5f23 100644 --- a/src/main/java/com/catchroom/chat/global/config/RedisConfig.java +++ b/src/main/java/com/catchroom/chat/global/config/RedisConfig.java @@ -20,6 +20,7 @@ @Configuration public class RedisConfig { + // yml 파일 redis 설정 불러오기 private final RedisProperties redisProperties; /** * 단일 Topic 사용을 위한 Bean 설정 From 79a0ba88061cb3e3ce84bfb788e5d8a37ee503ab Mon Sep 17 00:00:00 2001 From: HyemIin Date: Sat, 3 Feb 2024 10:59:56 +0900 Subject: [PATCH 2/3] =?UTF-8?q?Refactor=20:=20ChatRoom=20=EB=A6=AC?= =?UTF-8?q?=ED=8C=A9=ED=86=A0=EB=A7=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - chatRoomService의 getChatRoomListByUserId 위치 변경 --- .../controller/ChatRoomController.java | 4 ++-- .../chat/chatroom/service/ChatRoomService.java | 14 ++------------ .../chat/message/service/ChatService.java | 18 +++++++++++++++++- 3 files changed, 21 insertions(+), 15 deletions(-) diff --git a/src/main/java/com/catchroom/chat/chatroom/controller/ChatRoomController.java b/src/main/java/com/catchroom/chat/chatroom/controller/ChatRoomController.java index 89ee1ce..5f5dcbf 100644 --- a/src/main/java/com/catchroom/chat/chatroom/controller/ChatRoomController.java +++ b/src/main/java/com/catchroom/chat/chatroom/controller/ChatRoomController.java @@ -14,6 +14,8 @@ public class ChatRoomController { private final ChatRoomService chatRoomService; + //TODO 같은게 2개..? getChatRoomList에 모든 정보가 다 담기는데 왜?? + @GetMapping("/list") public List getChatRoomList( @RequestHeader("Authorization") String accessToken, @@ -31,6 +33,4 @@ public ChatRoomListGetResponse getChatRoom( return chatRoomService.getChatRoomInfo(accessToken, roomId); } - - } diff --git a/src/main/java/com/catchroom/chat/chatroom/service/ChatRoomService.java b/src/main/java/com/catchroom/chat/chatroom/service/ChatRoomService.java index 24d5dcb..adedf3d 100644 --- a/src/main/java/com/catchroom/chat/chatroom/service/ChatRoomService.java +++ b/src/main/java/com/catchroom/chat/chatroom/service/ChatRoomService.java @@ -32,20 +32,10 @@ public List getChatRoomListByHttp(Long userId, String a List chatRoomListGetResponseList = mainFeignClient.getChatRoomList(accessToken); chatRoomListGetResponseList.forEach(this::setListChatLastMessage); chatRoomRedisRepository.initChatRoomList(userId, chatRoomListGetResponseList); - chatRoomListGetResponseList = sortChatRoomListLatest(chatRoomListGetResponseList); - return chatRoomListGetResponseList; + return sortChatRoomListLatest(chatRoomListGetResponseList); } - public List getChatRoomListByUserId(Long userId) { - List chatRoomListGetResponseList = new ArrayList<>(); - if (chatRoomRedisRepository.existChatRoomList(userId)) { - chatRoomListGetResponseList = chatRoomRedisRepository.getChatRoomList(userId); - chatRoomListGetResponseList.forEach(this::setListChatLastMessage); - } - - return chatRoomListGetResponseList; - } public List getChatRoomList(Long userId, String accessToken) { @@ -74,7 +64,7 @@ public List getChatRoomList(Long userId, String accessT * 몽고 디비에서 마지막 메시지 가져와서 저장하는 로직 * @param chatRoomListGetResponse */ - private void setListChatLastMessage(ChatRoomListGetResponse chatRoomListGetResponse) { + public void setListChatLastMessage(ChatRoomListGetResponse chatRoomListGetResponse) { // 몽고 디비에서 마지막 메시지 가져와서 저장. String chatRoomNumber = chatRoomListGetResponse.getChatRoomNumber(); diff --git a/src/main/java/com/catchroom/chat/message/service/ChatService.java b/src/main/java/com/catchroom/chat/message/service/ChatService.java index 59b13a8..0071669 100644 --- a/src/main/java/com/catchroom/chat/message/service/ChatService.java +++ b/src/main/java/com/catchroom/chat/message/service/ChatService.java @@ -9,6 +9,7 @@ import com.catchroom.chat.message.type.MessageType; import com.catchroom.chat.message.type.UserIdentity; +import java.util.ArrayList; import java.util.List; import lombok.RequiredArgsConstructor; @@ -65,7 +66,7 @@ public void sendChatMessage(ChatMessageDto chatMessage, String accessToken) { // 3. 마지막 메시지들이 담긴 채팅방 리스트들을 가져온다. // 4. 파트너 채팅방 리스트도 가져온다. (파트너는 userId 로만) List chatRoomListGetResponseList = chatRoomService.getChatRoomList(userId, accessToken); - List partnerChatRoomGetResponseList = chatRoomService.getChatRoomListByUserId(partnerId); + List partnerChatRoomGetResponseList = getChatRoomListByUserId(partnerId); // 5. 마지막 메세지 기준으로 정렬 채팅방 리스트 정렬 @@ -133,6 +134,21 @@ private void setNewChatRoomInfo(ChatMessageDto chatMessage, ChatRoomListGetRespo } + // redis에서 채팅방 리스트 불러오는 로직 + private List getChatRoomListByUserId(Long userId) { + List chatRoomListGetResponseList = new ArrayList<>(); + + if (chatRoomRedisRepository.existChatRoomList(userId)) { + chatRoomListGetResponseList = chatRoomRedisRepository.getChatRoomList(userId); + for (ChatRoomListGetResponse chatRoomListGetResponse : chatRoomListGetResponseList) { + chatRoomService.setListChatLastMessage(chatRoomListGetResponse); + } + } + + + return chatRoomListGetResponseList; + } + From 6f6a11859c814522d5184febd70e008775e8e5b5 Mon Sep 17 00:00:00 2001 From: HyemIin Date: Sun, 4 Feb 2024 21:27:32 +0900 Subject: [PATCH 3/3] =?UTF-8?q?Refactor=20:=20ChatRoom=20=EB=A6=AC?= =?UTF-8?q?=ED=8C=A9=ED=86=A0=EB=A7=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 로그 삭제 - 변수명 변경 --- .../chatroom/service/ChatRoomService.java | 3 --- .../chat/message/service/ChatService.java | 23 ++++++++----------- 2 files changed, 9 insertions(+), 17 deletions(-) diff --git a/src/main/java/com/catchroom/chat/chatroom/service/ChatRoomService.java b/src/main/java/com/catchroom/chat/chatroom/service/ChatRoomService.java index adedf3d..9cb7399 100644 --- a/src/main/java/com/catchroom/chat/chatroom/service/ChatRoomService.java +++ b/src/main/java/com/catchroom/chat/chatroom/service/ChatRoomService.java @@ -39,19 +39,16 @@ public List getChatRoomListByHttp(Long userId, String a public List getChatRoomList(Long userId, String accessToken) { - Long beforeTime = System.currentTimeMillis(); List chatRoomListGetResponseList = null; if (chatRoomRedisRepository.existChatRoomList(userId)) { chatRoomListGetResponseList = chatRoomRedisRepository.getChatRoomList(userId); -// log.info("YES ChatRoom!!!!! time : {}", System.currentTimeMillis() - beforeTime); } else { // 채팅방이 레디스에 없으면 페인 사용해서 불러온다! chatRoomListGetResponseList = mainFeignClient.getChatRoomList(accessToken); chatRoomRedisRepository.initChatRoomList(userId, chatRoomListGetResponseList); -// log.info("NOT ChatRoom!!!!! time : {}", System.currentTimeMillis() - beforeTime); } chatRoomListGetResponseList.forEach(this::setListChatLastMessage); diff --git a/src/main/java/com/catchroom/chat/message/service/ChatService.java b/src/main/java/com/catchroom/chat/message/service/ChatService.java index 0071669..7db4391 100644 --- a/src/main/java/com/catchroom/chat/message/service/ChatService.java +++ b/src/main/java/com/catchroom/chat/message/service/ChatService.java @@ -27,6 +27,7 @@ public class ChatService { /** * destination 정보에서 roomId 추출 + * TODO 이거 필요?? */ public String getRoomId(String destination) { int lastIndex = destination.lastIndexOf('/'); @@ -45,35 +46,33 @@ public void sendChatMessage(ChatMessageDto chatMessage, String accessToken) { Long userId = chatMessage.getUserId(); Long partnerId; - Long before = System.currentTimeMillis(); - // 1. 채팅방이 삭제되는 것이라면 delete 를 해준다. if (chatMessage.getType().equals(MessageType.DELETE)) { chatRoomService.deleteChatRoom(accessToken, chatMessage.getRoomId(), userId); + //TODO 레디스에서도 삭제 필요?? + chatRoomRedisRepository.deleteChatRoom(userId,chatMessage.getRoomId()); } - ChatRoomListGetResponse newChatRoomListResponse = null; - if (!chatRoomRedisRepository.existChatRoom(userId, chatMessage.getRoomId())) { - newChatRoomListResponse = chatRoomService.getChatRoomInfo(accessToken, chatMessage.getRoomId()); + ChatRoomListGetResponse newChatRoomList = null; + if (chatRoomRedisRepository.existChatRoom(userId, chatMessage.getRoomId())) { + newChatRoomList = chatRoomRedisRepository.getChatRoom(userId, chatMessage.getRoomId()); } else { - newChatRoomListResponse = chatRoomRedisRepository.getChatRoom(userId, chatMessage.getRoomId()); + newChatRoomList = chatRoomService.getChatRoomInfo(accessToken, chatMessage.getRoomId()); } - partnerId = getPartnerId(chatMessage, newChatRoomListResponse); + partnerId = getPartnerId(chatMessage, newChatRoomList); // 2. 채팅방 리스트에 새로운 채팅방 정보가 없다면, 넣어준다. 마지막 메시지도 같이 담는다. 상대방 레디스에도 업데이트 해준다. - setNewChatRoomInfo(chatMessage, newChatRoomListResponse); + setNewChatRoomInfo(chatMessage, newChatRoomList); // 3. 마지막 메시지들이 담긴 채팅방 리스트들을 가져온다. // 4. 파트너 채팅방 리스트도 가져온다. (파트너는 userId 로만) List chatRoomListGetResponseList = chatRoomService.getChatRoomList(userId, accessToken); List partnerChatRoomGetResponseList = getChatRoomListByUserId(partnerId); - // 5. 마지막 메세지 기준으로 정렬 채팅방 리스트 정렬 chatRoomListGetResponseList = chatRoomService.sortChatRoomListLatest(chatRoomListGetResponseList); partnerChatRoomGetResponseList = chatRoomService.sortChatRoomListLatest(partnerChatRoomGetResponseList); - MessageSubDto messageSubDto = MessageSubDto.builder() .userId(userId) .partnerId(partnerId) @@ -82,9 +81,6 @@ public void sendChatMessage(ChatMessageDto chatMessage, String accessToken) { .partnerList(partnerChatRoomGetResponseList) .build(); - Long after = System.currentTimeMillis() - before; - log.info("message Time : {}", after); - redisPublisher.publish(messageSubDto); } @@ -108,7 +104,6 @@ private void setNewChatRoomInfo(ChatMessageDto chatMessage, ChatRoomListGetRespo newChatRoomListResponse.updateChatMessageDto(chatMessage); - /** 상대방 채팅 리스트와 내 리스트 둘다 채팅방을 저장한다. */ if (newChatRoomListResponse.getLoginUserIdentity().equals(UserIdentity.SELLER)) {