Skip to content

Commit

Permalink
Merge pull request #140 from sharemindteam/fix/137-connect-dto
Browse files Browse the repository at this point in the history
fix:새로운 채팅 생성시 dto 누락 문제 해결
  • Loading branch information
aeyongdodam authored Mar 10, 2024
2 parents 1399286 + cb9a996 commit 6ffb697
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -244,22 +244,20 @@ private List<Chat> getFilterChats(List<Consult> consults, Boolean filter) {
@Override
public void getAndSendChatStatus(Long chatId, Map<String, Object> sessionAttributes, ChatStatusUpdateRequest chatStatusUpdateRequest,
Boolean isCustomer) {
Long userId = (Long) sessionAttributes.get("userId");

ChatGetStatusResponse chatGetStatusResponse = getChatStatus(userId, chatId, chatStatusUpdateRequest, isCustomer);
ChatGetStatusResponse chatGetStatusResponse = getChatStatus(chatId, chatStatusUpdateRequest, isCustomer);

sendChatStatus(chatId, chatGetStatusResponse);
}

private ChatGetStatusResponse getChatStatus(Long userId, Long chatId, ChatStatusUpdateRequest chatStatusUpdateRequest,
private ChatGetStatusResponse getChatStatus(Long chatId, ChatStatusUpdateRequest chatStatusUpdateRequest,
Boolean isCustomer) {
Chat chat = chatRepository.findByChatIdAndIsActivatedIsTrue(chatId)
.orElseThrow(() -> new ChatException(ChatErrorCode.CHAT_NOT_FOUND, chatId.toString()));
Consult consult = consultService.getConsultByChat(chat);

validateChatStatusRequest(chat, chatStatusUpdateRequest, isCustomer);

handleStatusRequest(userId, chat, isCustomer, chatStatusUpdateRequest);
handleStatusRequest(chat, chatStatusUpdateRequest);

return ChatGetStatusResponse.of(consult, chatStatusUpdateRequest.getChatWebsocketStatus());
}
Expand All @@ -273,7 +271,7 @@ private void sendChatStatus(Long chatId, ChatGetStatusResponse chatGetStatusResp
}


private void handleStatusRequest(Long userId, Chat chat, Boolean isCustomer, ChatStatusUpdateRequest chatStatusUpdateRequest) {
private void handleStatusRequest(Chat chat, ChatStatusUpdateRequest chatStatusUpdateRequest) {

ChatWebsocketStatus chatWebsocketStatus = chatStatusUpdateRequest.getChatWebsocketStatus();
switch (chatWebsocketStatus) {
Expand Down Expand Up @@ -305,7 +303,7 @@ private void handleStatusRequest(Long userId, Chat chat, Boolean isCustomer, Cha

chatNoticeService.createChatNoticeMessage(chat, ChatMessageStatus.FINISH);

removeChatIdInRedis(userId, chat.getChatId(), isCustomer);
removeChatIdInRedis(chat.getChatId());
notifyFinishChat(chat, chat.getConsult());
break;
}
Expand Down Expand Up @@ -413,6 +411,7 @@ public void setChatInSessionRedis(Long chatId, Long customerId, Boolean isCustom
}

private void sendReadAllEvent(Long chatId, Long customerId, Boolean isCustomer) {

if (isCustomer)
simpMessagingTemplate.convertAndSend(
"/queue/chattings/notifications/customers/" + customerId,
Expand Down Expand Up @@ -458,9 +457,16 @@ public ChatGetRoomInfoResponse getChatInfoByCounselor(Long chatId, Long customer
return ChatGetRoomInfoResponse.of(chat);
}

private void removeChatIdInRedis(Long userId, Long chatId, Boolean isCustomer) {
String redisKey = isCustomer ? CUSTOMER_PREFIX + userId.toString() : COUNSELOR_PREFIX + userId.toString();
private void removeChatIdInRedis(Long chatId) {
Chat chat = getChatByChatId(chatId);
String customerKey = CUSTOMER_PREFIX + chat.getConsult().getCustomer().getCustomerId();
String counselorKey = COUNSELOR_PREFIX + chat.getConsult().getCounselor().getCounselorId();

removeChatIdWithKey(chatId, customerKey);
removeChatIdWithKey(chatId, counselorKey);
}

private void removeChatIdWithKey(Long chatId, String redisKey) {
List<Long> chatRoomIds = redisTemplate.opsForValue().get(redisKey);
if (chatRoomIds != null && chatRoomIds.contains(chatId)) {
chatRoomIds.remove(chatId);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.example.sharemind.chat.dto.response;

import com.example.sharemind.chat.content.ChatRoomWebsocketStatus;
import com.example.sharemind.chat.domain.Chat;
import com.fasterxml.jackson.annotation.JsonFormat;
import io.swagger.v3.oas.annotations.media.Schema;

Expand All @@ -24,7 +25,15 @@ public class ChatNotifyEventResponse {
@Schema(description = "채팅 이벤트")
private final ChatRoomWebsocketStatus chatRoomWebsocketStatus;

@Schema(description = "채팅 스타일")
private final String consultStyle;

public static ChatNotifyEventResponse of(Long chatId, ChatRoomWebsocketStatus chatRoomWebsocketStatus) {
return new ChatNotifyEventResponse(chatId, LocalDateTime.now(), chatRoomWebsocketStatus);
return new ChatNotifyEventResponse(chatId, LocalDateTime.now(), chatRoomWebsocketStatus, null);
}

public static ChatNotifyEventResponse of(Chat chat, ChatRoomWebsocketStatus chatRoomWebsocketStatus) {
return new ChatNotifyEventResponse(chat.getChatId(), LocalDateTime.now(), chatRoomWebsocketStatus,
chat.getConsult().getCounselor().getConsultStyle().getDisplayName());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ public interface ConsultRepository extends JpaRepository<Consult, Long> {
List<Consult> findAllByIsPaidIsFalseAndIsActivatedIsTrue();

@Query("SELECT chat.chatId FROM Consult c JOIN c.chat chat " +
"WHERE c.customer.customerId = :customerId AND c.isActivated = true")
"WHERE c.customer.customerId = :customerId AND c.isActivated = true AND c.consultStatus != 'FINISH'")
List<Long> findChatIdsByCustomerId(Long customerId); //todo: 쿼리 최적화 필요

@Query("SELECT chat.chatId FROM Consult c JOIN c.chat chat " +
"WHERE c.counselor.counselorId = :counselorId AND c.isActivated = true")
"WHERE c.counselor.counselorId = :counselorId AND c.isActivated = true AND c.consultStatus != 'FINISH'")
List<Long> findChatIdsByCounselorId(Long counselorId);

@Query("SELECT c FROM Consult c JOIN FETCH c.payment p " +
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.example.sharemind.global.websocket;

import com.example.sharemind.chat.application.ChatService;
import com.example.sharemind.chat.content.ChatRoomWebsocketStatus;
import com.example.sharemind.chat.domain.Chat;
import com.example.sharemind.chat.domain.ChatNotifyEvent;
import com.example.sharemind.chat.domain.ChatUpdateStatusEvent;
import com.example.sharemind.chat.dto.response.ChatNotifyEventResponse;
Expand All @@ -23,6 +25,7 @@
public class WebSocketEventListener {

private final SimpMessageSendingOperations messageSendingOperations;
private final ChatService chatService;

@EventListener
public void handleWebSocketConnectListener(SessionConnectedEvent event) {
Expand All @@ -40,12 +43,14 @@ public void handleWebSocketDisconnectListener(SessionDisconnectEvent event) {
public void handleChatNotifyEvent(ChatNotifyEvent chatNotifyEvent) {
System.out.println("customerId : " + chatNotifyEvent.getCustomerId() + "\ncounselorId: "
+ chatNotifyEvent.getCounselorId()); //todo: 로깅용으로 찍은 것.. 채팅 연결 후 삭제

Chat chat = chatService.getChatByChatId(chatNotifyEvent.getChatId());
messageSendingOperations.convertAndSend(
"/queue/chattings/notifications/customers/" + chatNotifyEvent.getCustomerId(),
ChatNotifyEventResponse.of(chatNotifyEvent.getChatId(), ChatRoomWebsocketStatus.CHAT_ROOM_CREATE));
ChatNotifyEventResponse.of(chat, ChatRoomWebsocketStatus.CHAT_ROOM_CREATE));
messageSendingOperations.convertAndSend(
"/queue/chattings/notifications/counselors/" + chatNotifyEvent.getCounselorId(),
ChatNotifyEventResponse.of(chatNotifyEvent.getChatId(), ChatRoomWebsocketStatus.CHAT_ROOM_CREATE));
ChatNotifyEventResponse.of(chat, ChatRoomWebsocketStatus.CHAT_ROOM_CREATE));
}

@EventListener
Expand Down

0 comments on commit 6ffb697

Please sign in to comment.