From 26d7b5ff0e079e174aa6a0512693caf6f3e1b94e Mon Sep 17 00:00:00 2001 From: Jungwoo Kim Date: Mon, 29 Jan 2024 20:56:47 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20=EC=B1=84=ED=8C=85=EB=B0=A9=20=EB=A9=94?= =?UTF-8?q?=EC=8B=9C=EC=A7=80=EC=97=90=20=EB=8C=80=ED=95=9C=20=EC=BB=A8?= =?UTF-8?q?=ED=8A=B8=EB=A1=A4=EB=9F=AC=20=EB=A1=9C=EC=A7=81=20=EC=B6=94?= =?UTF-8?q?=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../chat/controller/ChatController.java | 31 +++++++++++++++++-- .../chat/controller/ChatRoomController.java | 4 +++ .../model/dto/request/RequestChatDto.java | 2 ++ 3 files changed, 35 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/dku/council/domain/chat/controller/ChatController.java b/src/main/java/com/dku/council/domain/chat/controller/ChatController.java index 37d0c522..b1b0308f 100644 --- a/src/main/java/com/dku/council/domain/chat/controller/ChatController.java +++ b/src/main/java/com/dku/council/domain/chat/controller/ChatController.java @@ -6,6 +6,9 @@ import com.dku.council.domain.chat.model.dto.response.ResponseChatDto; import com.dku.council.domain.chat.service.ChatService; import com.dku.council.domain.chat.service.MessageSender; +import com.dku.council.domain.chatmessage.model.entity.ChatRoomMessage; +import com.dku.council.domain.chatmessage.service.ChatRoomMessageService; +import com.dku.council.domain.user.service.UserService; import com.dku.council.global.auth.role.UserAuth; import io.swagger.v3.oas.annotations.tags.Tag; import lombok.RequiredArgsConstructor; @@ -18,7 +21,9 @@ import org.springframework.messaging.simp.SimpMessageSendingOperations; import org.springframework.messaging.simp.stomp.StompHeaderAccessor; import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.DeleteMapping; import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.socket.messaging.SessionDisconnectEvent; @@ -39,7 +44,9 @@ public class ChatController { */ private final SimpMessageSendingOperations template; + private final UserService userService; private final ChatService chatService; + private final ChatRoomMessageService chatRoomMessageService; private final MessageSender sender; /** @@ -61,16 +68,22 @@ public void enterUser(@Payload RequestChatDto chat, String username = chatService.addUser(chat.getRoomId(), chat.getSender()); log.info("enterUser에서 uuid " + username); log.info("enterUser에서 roomId " + chat.getRoomId()); + log.info("enterUser에서 userId " + chat.getUserId()); // 반환 결과를 socket session 에 userUUID 로 저장 headerAccessor.getSessionAttributes().put("username", username); + headerAccessor.getSessionAttributes().put("userId", chat.getUserId()); headerAccessor.getSessionAttributes().put("roomId", chat.getRoomId()); + String enterMessage = chat.getSender() + " 님 입장!!"; + // 입장 메시지 저장 + chatRoomMessageService.create(chat.getRoomId(), chat.getType().toString(), chat.getUserId(), chat.getSender(), enterMessage); + Message message = Message.builder() .type(chat.getType()) .roomId(chat.getRoomId()) .sender(chat.getSender()) - .message(chat.getSender() + " 님 입장!!") + .message(enterMessage) .build(); sender.send(topic, message); @@ -85,6 +98,8 @@ public void enterUser(@Payload RequestChatDto chat, public void sendMessage(@Payload RequestChatDto chat) { log.info("CHAT {}", chat); + chatRoomMessageService.create(chat.getRoomId(), chat.getType().toString(), chat.getUserId(), chat.getSender(), chat.getMessage()); + Message message = Message.builder() .type(chat.getType()) .roomId(chat.getRoomId()) @@ -108,6 +123,7 @@ public void webSocketDisconnectListener(SessionDisconnectEvent event) { // stomp 세션에 있던 username과 roomId 를 확인해서 채팅방 유저 리스트와 room 에서 해당 유저를 삭제 String username = (String) headerAccessor.getSessionAttributes().get("username"); + Long userId = (Long) headerAccessor.getSessionAttributes().get("userId"); String roomId = (String) headerAccessor.getSessionAttributes().get("roomId"); log.info("퇴장 controller에서 uuid " + username); log.info("퇴장 controller에서 roomId " + roomId); @@ -123,12 +139,15 @@ public void webSocketDisconnectListener(SessionDisconnectEvent event) { if (username != null) { log.info("User Disconnected : ", username); + String exitMessage = username + " 님 퇴장!!"; + // 퇴장 메시지 저장 + chatRoomMessageService.create(roomId, MessageType.LEAVE.toString(), userId, username, exitMessage); // builder 어노테이션 활용 Message message = Message.builder() .type(MessageType.LEAVE) .sender(username) .roomId(roomId) - .message(username + " 님 퇴장!!") + .message(exitMessage) .build(); sender.send(topic, message); @@ -146,4 +165,12 @@ public void webSocketDisconnectListener(SessionDisconnectEvent event) { public List userList(String roomId) { return chatService.getUserList(roomId); } + + + @GetMapping("/chat/message/list") + @UserAuth + @ResponseBody + public List list(@RequestParam("roomId") String roomId) { + return chatRoomMessageService.findAllChatRoomMessages(roomId); + } } \ No newline at end of file diff --git a/src/main/java/com/dku/council/domain/chat/controller/ChatRoomController.java b/src/main/java/com/dku/council/domain/chat/controller/ChatRoomController.java index f982f607..c46901a8 100644 --- a/src/main/java/com/dku/council/domain/chat/controller/ChatRoomController.java +++ b/src/main/java/com/dku/council/domain/chat/controller/ChatRoomController.java @@ -2,6 +2,7 @@ import com.dku.council.domain.chat.model.dto.response.ResponseChatRoomDto; import com.dku.council.domain.chat.service.ChatService; +import com.dku.council.domain.chatmessage.service.ChatRoomMessageService; import com.dku.council.domain.user.model.dto.response.ResponseUserInfoForChattingDto; import com.dku.council.domain.user.service.UserService; import com.dku.council.global.auth.jwt.AppAuthentication; @@ -24,6 +25,7 @@ public class ChatRoomController { private final ChatService chatService; private final UserService userService; + private final ChatRoomMessageService chatRoomMessageService; /** * 채팅방 리스트 화면 @@ -111,6 +113,8 @@ public boolean confirmPwd(@PathVariable String roomId, @UserAuth public String delChatRoom(@PathVariable String roomId, AppAuthentication auth){ + chatRoomMessageService.deleteChatRoomMessages(roomId); + // roomId(UUID 값) 기준으로 채팅방 삭제 chatService.delChatRoom(auth.getUserId(), roomId, auth.isAdmin()); diff --git a/src/main/java/com/dku/council/domain/chat/model/dto/request/RequestChatDto.java b/src/main/java/com/dku/council/domain/chat/model/dto/request/RequestChatDto.java index 9a753724..641e3998 100644 --- a/src/main/java/com/dku/council/domain/chat/model/dto/request/RequestChatDto.java +++ b/src/main/java/com/dku/council/domain/chat/model/dto/request/RequestChatDto.java @@ -12,6 +12,8 @@ public class RequestChatDto { private final String roomId; + private final Long userId; + private final String sender; private final String message;