Skip to content

Commit

Permalink
#157 feat: 채팅 보내는 api 응답 수정 (#158)
Browse files Browse the repository at this point in the history
  • Loading branch information
CDBchan authored Oct 6, 2023
1 parent cc3493a commit 097d5ac
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ private Long getChatRoomId(StompHeaderAccessor accessor) {
private Long validateAccessToken(StompHeaderAccessor accessor) {
try {
String token = extractAccessTokenFromAccessor(accessor);
log.info("token : {}", token);
return jwtProvider.getClaims(token).get("memberId", Long.class);
} catch (RuntimeException e) {
throw new CustomRuntimeException(JwtException.from(e));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import com.codesquad.secondhand.domain.chat.dto.request.MessageRequest;
import com.codesquad.secondhand.domain.chat.dto.response.ChatRoomDetailsResponse;
import com.codesquad.secondhand.domain.chat.dto.response.ChatRoomListResponse;
import com.codesquad.secondhand.domain.chat.dto.response.ChatSendMessageResponse;
import com.codesquad.secondhand.domain.chat.service.ChatService;

import lombok.RequiredArgsConstructor;
Expand All @@ -36,10 +37,10 @@ public class ChatController {

@MessageMapping("/message")
public void sendMessage(MessageRequest messageRequest) {
chatService.sendMessage(messageRequest);
ChatSendMessageResponse chatSendMessageResponse = chatService.sendMessage(messageRequest);
// 채널아이디 만들기
simpMessageSendingOperations.convertAndSend("/sub/room/" + messageRequest.getChatRoomId(),
messageRequest.getMessage());
chatSendMessageResponse);
}

@PostMapping("/api/chats/room-id")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.codesquad.secondhand.domain.chat.dto.response;

import com.codesquad.secondhand.domain.chat.entity.ChatMessage;
import com.codesquad.secondhand.domain.chat.entity.ChatRoom;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;

@Builder
@Getter
@AllArgsConstructor
public class ChatSendMessageResponse {

private Long chatRoomId;
private ChatMessageResponse messages;

public static ChatSendMessageResponse of(ChatMessage chatMessage, ChatRoom chatRoom) {
return ChatSendMessageResponse.builder()
.messages(ChatMessageResponse.of(chatMessage))
.chatRoomId(chatRoom.getId())
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import com.codesquad.secondhand.domain.chat.dto.response.ChatRoomMessageResponse;
import com.codesquad.secondhand.domain.chat.dto.response.ChatRoomOpponentResponse;
import com.codesquad.secondhand.domain.chat.dto.response.ChatRoomProductResponse;
import com.codesquad.secondhand.domain.chat.dto.response.ChatSendMessageResponse;
import com.codesquad.secondhand.domain.chat.entity.ChatMessage;
import com.codesquad.secondhand.domain.chat.entity.ChatRoom;
import com.codesquad.secondhand.domain.chat.redis.RedisChatMember;
Expand All @@ -39,7 +40,7 @@ public class ChatService {
private final NotificationService notificationService;

@Transactional
public void sendMessage(MessageRequest messageRequest) {
public ChatSendMessageResponse sendMessage(MessageRequest messageRequest) {
//1. chatRoomId 가 존재하는지 검증
ChatRoom chatRoom = chatQueryService.findChatRoomByChatRoomId(messageRequest.getChatRoomId());

Expand All @@ -55,11 +56,12 @@ public void sendMessage(MessageRequest messageRequest) {
if (redisChatMembers.size() == MAX_CHAT_MEMBERS) {
// 메세지의 읽음 상태를 true 로 변경 (채팅방에 user 가 2명이기 때문에)
chatMessage.updateReadStatusToTrue();
return;
return ChatSendMessageResponse.of(chatMessage, chatRoom);
}
//SSE 재요청 알림 보내기
Long receiverId = chatRoom.findOpponentId(sender);
notificationService.refreshChatRoomList(receiverId);
return ChatSendMessageResponse.of(chatMessage, chatRoom);
}

@Transactional
Expand Down

0 comments on commit 097d5ac

Please sign in to comment.