Skip to content

Commit

Permalink
fix: 상대방 확인 로직 추가 및 리팩토링
Browse files Browse the repository at this point in the history
  • Loading branch information
koreaioi committed Nov 13, 2024
1 parent 0c5c7ef commit 80dfb7c
Showing 1 changed file with 28 additions and 7 deletions.
35 changes: 28 additions & 7 deletions src/main/java/com/leets/X/domain/chat/service/ChattingService.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import com.leets.X.domain.chat.repository.ChatMessageRepository;
import com.leets.X.domain.chat.repository.ChatRoomRepository;
import com.leets.X.domain.user.domain.User;
import com.leets.X.domain.user.service.UserService;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
Expand All @@ -26,31 +27,36 @@ public class ChattingService {
private final ChatRoomRepository chatRoomRepository;
private final ChatMessageRepository chatMessageRepository;
private final RedisListener redisMessageListener;
private final UserService userService;

public ChattingDto getChatRoom(Long roomId, Integer page, Integer size) {
public ChattingDto getChatRoom(Long roomId, Integer page, Integer size, String email) {
ChatRoom findRoom = validateChatRoom(roomId);
User user1 = findRoom.getUser1();
User user2 = findRoom.getUser2();
redisMessageListener.adaptMessageListener(findRoom.getId()); // 채팅방 내역 조회시 리스너 등록 추가 (운영 시 삭제)

List<ChatMessageResponseDto> chatMessageList = generateChatRoomMessages(roomId, page, size);
return new ChattingDto(user1.getId(), user2.getId(), user1.getCustomId(), user2.getCustomId(), chatMessageList);
}
User sender = userService.find(email);

return findOpponent(sender, findRoom.getUser1(), findRoom.getUser2(), chatMessageList);
}

public List<ChattingListResponseDto> getChattingList(Long userId) { // 추후 JWT 파싱으로 받아내기.
public List<ChattingListResponseDto> getChattingList(Long userId, String email) { // 추후 JWT 파싱으로 받아내기.
List<ChatRoom> chatRooms = validateChatRommList(userId);

return chatRooms.stream()
.map(chatRoom -> {
ChatMessage latestMessage = chatMessageRepository.findTopByRoomIdOrderByCreatedAtDesc(chatRoom.getId()).orElse(null);
LatestMessageDto latestMessageDto = (latestMessage != null)
? LatestMessageDto.of(latestMessage) : new LatestMessageDto("", null);
return ChattingListResponseDto.of(chatRoom, latestMessageDto);
User sender = userService.find(email);
return findOpponentToAllChat(chatRoom, sender,chatRoom.getUser1(), chatRoom.getUser2(), latestMessageDto);
})
.collect(Collectors.toList());
}

/*
* 리팩토링
* */

private List<ChatMessageResponseDto> generateChatRoomMessages(Long roomId, Integer page, Integer size) {
return chatMessageRepository.findByRoomIdOrderByCreatedAtDesc(
roomId, PageRequest.of(page- 1, size))
Expand All @@ -60,6 +66,21 @@ private List<ChatMessageResponseDto> generateChatRoomMessages(Long roomId, Integ
.collect(Collectors.toList());
}

private static ChattingDto findOpponent(User sender, User user1, User user2, List<ChatMessageResponseDto> chatMessageList) {
if(sender.equals(user1)) { // user1 이 본인
return ChattingDto.of(user1, user2, chatMessageList);
}else{
return ChattingDto.of(user2, user1, chatMessageList);
}
}

private static ChattingListResponseDto findOpponentToAllChat(ChatRoom chatRoom, User sender,User user1, User user2, LatestMessageDto latestMessageDto) {
if(sender.equals(user1)) { // user1 이 본인
return ChattingListResponseDto.of(chatRoom.getId(), user1, user2, latestMessageDto);
}else{
return ChattingListResponseDto.of(chatRoom.getId(), user2, user1, latestMessageDto);
}
}

private ChatRoom validateChatRoom(Long roomId) {
return chatRoomRepository.findById(roomId)
Expand Down

0 comments on commit 80dfb7c

Please sign in to comment.