Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
tidavid1 committed Mar 25, 2024
2 parents 6905f90 + f542531 commit 44dd0b0
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import io.oeid.mogakgo.domain.chat.exception.ChatException;
import io.oeid.mogakgo.domain.chat.infrastructure.ChatRepository;
import io.oeid.mogakgo.domain.chat.infrastructure.ChatUserJpaRepository;
import io.oeid.mogakgo.domain.user.infrastructure.UserJpaRepository;
import io.oeid.mogakgo.exception.code.ErrorCode400;
import io.oeid.mogakgo.exception.code.ErrorCode404;
import java.util.UUID;
Expand All @@ -24,24 +25,29 @@ public class ChatWebSocketService {
private final ChatUserJpaRepository chatUserRepository;
private final ChatRepository chatRepository;
private final ChatIdSequenceGeneratorService sequenceGeneratorService;
private final UserJpaRepository userRepository;

public ChatDataRes handleChatMessage(Long userId, String roomId, ChatReq request) {
log.info("handleChatMessage userId: {}, roomId: {}", userId, roomId);
verifyChatRoomByRoomIdAndUser(roomId, userId);
var receiver = chatUserRepository.findReceiverByRoomIdAndUserId(UUID.fromString(roomId), userId)
var receiver = chatUserRepository.findReceiverByRoomIdAndUserId(UUID.fromString(roomId),
userId)
.orElseThrow(() -> new ChatException(ErrorCode404.CHAT_USER_NOT_FOUND));
var sender = userRepository.findById(userId)
.orElseThrow(() -> new ChatException(ErrorCode404.CHAT_USER_NOT_FOUND));
ChatMessage chatMessage = chatRepository.save(
ChatMessage.builder().id(sequenceGeneratorService.generateSequence(roomId))
.senderId(userId)
.messageType(request.getMessageType())
.message(request.getMessage())
.build(), roomId);
return ChatDataRes.of(receiver.getUser(), chatMessage);
return ChatDataRes.of(receiver.getUser().getId(), sender.getUsername(), chatMessage);
}

private void verifyChatRoomByRoomIdAndUser(String roomId, Long userId) {
log.info("verifyChatRoomByRoomIdAndUser - roomId: {}, UserId: {}", roomId, userId);
ChatRoom chatRoom = chatUserRepository.findByChatRoomIdAndUserId(UUID.fromString(roomId), userId)
ChatRoom chatRoom = chatUserRepository.findByChatRoomIdAndUserId(UUID.fromString(roomId),
userId)
.orElseThrow(() -> new ChatException(ErrorCode404.CHAT_ROOM_NOT_FOUND)).getChatRoom();
if (chatRoom.getStatus().equals(ChatStatus.CLOSED)) {
throw new ChatException(ErrorCode400.CHAT_ROOM_CLOSED);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import io.oeid.mogakgo.domain.chat.entity.document.ChatMessage;
import io.oeid.mogakgo.domain.chat.entity.enums.ChatMessageType;
import io.oeid.mogakgo.domain.chat.presentation.dto.res.ChatDataApiRes;
import io.oeid.mogakgo.domain.user.domain.User;
import java.time.LocalDateTime;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
Expand All @@ -14,16 +13,21 @@
public class ChatDataRes {

private Long receiverId;
private String receiverUsername;
private Long id;
private ChatMessageType messageType;
private Long senderId;
private String senderUserName;
private String message;
private LocalDateTime createdAt;

public static ChatDataRes of(User receiver, ChatMessage chatMessage) {
return new ChatDataRes(receiver.getId(), receiver.getUsername(), chatMessage.getId(),
chatMessage.getMessageType(), chatMessage.getSenderId(), chatMessage.getMessage(),
public static ChatDataRes of(Long receiverId, String senderUserName, ChatMessage chatMessage) {
return new ChatDataRes(
receiverId,
chatMessage.getId(),
chatMessage.getMessageType(),
chatMessage.getSenderId(),
senderUserName,
chatMessage.getMessage(),
chatMessage.getCreatedAt());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public ChatDataApiRes sendChatData(@DestinationVariable("chatRoomId") String cha
var response = chatWebSocketService.handleChatMessage(request.getUserId(), chatRoomId,
ChatReq.from(request));
fcmNotificationService.sendNotification(response.getReceiverId(),
response.getReceiverUsername(), response.getMessage());
response.getSenderUserName(), response.getMessage());
return response.toApiResponse();
}
}

0 comments on commit 44dd0b0

Please sign in to comment.