Skip to content

Commit

Permalink
feat: 채팅방 메시지에 대한 컨트롤러 로직 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
kjungw1025 committed Jan 29, 2024
1 parent 9d93a21 commit 26d7b5f
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand All @@ -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;

/**
Expand All @@ -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);
Expand All @@ -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())
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -146,4 +165,12 @@ public void webSocketDisconnectListener(SessionDisconnectEvent event) {
public List<String> userList(String roomId) {
return chatService.getUserList(roomId);
}


@GetMapping("/chat/message/list")
@UserAuth
@ResponseBody
public List<ChatRoomMessage> list(@RequestParam("roomId") String roomId) {
return chatRoomMessageService.findAllChatRoomMessages(roomId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -24,6 +25,7 @@ public class ChatRoomController {
private final ChatService chatService;

private final UserService userService;
private final ChatRoomMessageService chatRoomMessageService;

/**
* 채팅방 리스트 화면
Expand Down Expand Up @@ -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());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ public class RequestChatDto {

private final String roomId;

private final Long userId;

private final String sender;

private final String message;
Expand Down

0 comments on commit 26d7b5f

Please sign in to comment.