Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.example.solidconnection.chat.controller;

import com.example.solidconnection.chat.dto.ChatMessageResponse;
import com.example.solidconnection.chat.dto.ChatParticipantResponse;
import com.example.solidconnection.chat.dto.ChatRoomListResponse;
import com.example.solidconnection.chat.service.ChatService;
import com.example.solidconnection.common.dto.SliceResponse;
Expand Down Expand Up @@ -41,6 +42,15 @@ public ResponseEntity<SliceResponse<ChatMessageResponse>> getChatMessages(
return ResponseEntity.ok(response);
}

@GetMapping("rooms/{room-id}/partner")
public ResponseEntity<ChatParticipantResponse> getChatPartner(
@AuthorizedUser long siteUserId,
@PathVariable("room-id") Long roomId
) {
ChatParticipantResponse response = chatService.getChatPartner(siteUserId, roomId);
return ResponseEntity.ok(response);
}

@PutMapping("/rooms/{room-id}/read")
public ResponseEntity<Void> markChatMessagesAsRead(
@AuthorizedUser long siteUserId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,16 +84,6 @@ private ChatRoomResponse toChatRoomResponse(ChatRoom chatRoom, long siteUserId)
return ChatRoomResponse.of(chatRoom.getId(), lastChatMessage, lastReceivedTime, partner, unReadCount);
}

private ChatParticipant findPartner(ChatRoom chatRoom, long siteUserId) {
if (chatRoom.isGroup()) {
throw new CustomException(INVALID_CHAT_ROOM_STATE);
}
return chatRoom.getChatParticipants().stream()
.filter(participant -> participant.getSiteUserId() != siteUserId)
.findFirst()
.orElseThrow(() -> new CustomException(CHAT_PARTNER_NOT_FOUND));
}

@Transactional(readOnly = true)
public SliceResponse<ChatMessageResponse> getChatMessages(long siteUserId, long roomId, Pageable pageable) {
validateChatRoomParticipant(siteUserId, roomId);
Expand All @@ -107,6 +97,26 @@ public SliceResponse<ChatMessageResponse> getChatMessages(long siteUserId, long
return SliceResponse.of(content, chatMessages);
}

@Transactional(readOnly = true)
public ChatParticipantResponse getChatPartner(long siteUserId, Long roomId) {
ChatRoom chatRoom = chatRoomRepository.findById(roomId)
.orElseThrow(() -> new CustomException(INVALID_CHAT_ROOM_STATE));
ChatParticipant partnerParticipant = findPartner(chatRoom, siteUserId);
SiteUser siteUser = siteUserRepository.findById(partnerParticipant.getSiteUserId())
.orElseThrow(() -> new CustomException(USER_NOT_FOUND));
return ChatParticipantResponse.of(siteUser.getId(), siteUser.getNickname(), siteUser.getProfileImageUrl());
}

private ChatParticipant findPartner(ChatRoom chatRoom, long siteUserId) {
if (chatRoom.isGroup()) {
throw new CustomException(INVALID_CHAT_ROOM_STATE);
}
return chatRoom.getChatParticipants().stream()
.filter(participant -> participant.getSiteUserId() != siteUserId)
.findFirst()
.orElseThrow(() -> new CustomException(CHAT_PARTNER_NOT_FOUND));
}

public void validateChatRoomParticipant(long siteUserId, long roomId) {
boolean isParticipant = chatParticipantRepository.existsByChatRoomIdAndSiteUserId(roomId, siteUserId);
if (!isParticipant) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import com.example.solidconnection.chat.dto.ChatMessageResponse;
import com.example.solidconnection.chat.dto.ChatMessageSendRequest;
import com.example.solidconnection.chat.dto.ChatMessageSendResponse;
import com.example.solidconnection.chat.dto.ChatParticipantResponse;
import com.example.solidconnection.chat.dto.ChatRoomListResponse;
import com.example.solidconnection.chat.fixture.ChatAttachmentFixture;
import com.example.solidconnection.chat.fixture.ChatMessageFixture;
Expand Down Expand Up @@ -309,6 +310,28 @@ void setUp() {
}
}

@Nested
class 채팅방_파트너_정보를_조회한다 {

@Test
void 채팅방_파트너를_정상_조회한다() {
// given
ChatRoom chatRoom = chatRoomFixture.채팅방(false);
chatParticipantFixture.참여자(user.getId(), chatRoom);
chatParticipantFixture.참여자(mentor1.getId(), chatRoom);

// when
ChatParticipantResponse response = chatService.getChatPartner(user.getId(), chatRoom.getId());

// then
assertAll(
() -> assertThat(response.partnerId()).isEqualTo(mentor1.getId()),
() -> assertThat(response.nickname()).isEqualTo(mentor1.getNickname()),
() -> assertThat(response.profileUrl()).isEqualTo(mentor1.getProfileImageUrl())
);
}
}

@Nested
class 채팅_메시지_읽음을_처리한다 {

Expand Down
Loading