Skip to content

Commit

Permalink
Merge pull request #63 from 28th-meetup/feat/chat-1121
Browse files Browse the repository at this point in the history
채팅 기능 업데이트
  • Loading branch information
eckrin authored Nov 21, 2023
2 parents 1995b13 + e562b60 commit 38bdfbf
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ public class MessageDto {
private String senderName;
private String roomId;
private String message;
private String sentTime;

// 대화 조회
public MessageDto(Message message) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class MessageResponseDto {
private String roomId;
private String receiver;
private String message;
private String createdAt;
private String lastMessageSentTime; // (채팅방 조회시) 메세지 마지막 전송 시각

// 채팅방 생성
public MessageResponseDto(Room room) {
Expand Down Expand Up @@ -46,6 +46,6 @@ public void setLatestMessageContent(String message) {
}

public void setLatestMessageCreatedAt(String createdAt) {
this.createdAt = createdAt;
this.lastMessageSentTime = createdAt;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,8 @@ public Message(String senderName, Room room, String message) {
this.room = room;
this.message = message;
}

public void updateSentTime(String sentTime) {
this.sentTime = sentTime;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ public interface MessageRepository extends JpaRepository<Message, Long> {
List<Message> findTop100ByRoomOrderById(Room room);

Message findTopByRoomOrderById(Room room);
Message findTopByRoomOrderBySentTimeDesc(Room room);
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;
Expand All @@ -29,14 +31,15 @@ public class MessageService {
private final ObjectMapper objectMapper;

// 메세지 저장
@Transactional
public void saveMessage(MessageDto messageDto) {
Room room = roomRepository.findByRoomId(messageDto.getRoomId()).orElseThrow(
()->new RoomNotExistsException("채팅방이 더 이상 존재하지 않습니다.")
);

// DB 저장
Message message = new Message(messageDto.getSenderName(), room, messageDto.getMessage());
messageRepository.save(message);
Message message = messageRepository.save(new Message(messageDto.getSenderName(), room, messageDto.getMessage()));
message.updateSentTime(message.getCreatedAt().toString());

// 1. 직렬화
redisTemplateMessage.setValueSerializer(new GenericJackson2JsonRedisSerializer(objectMapper));
Expand All @@ -49,6 +52,7 @@ public void saveMessage(MessageDto messageDto) {
}

// 6. 대화 조회 - Redis & DB (TLB 캐시전략 유사)
@Transactional
public List<MessageDto> loadMessage(String roomId) {
List<MessageDto> messageList = new ArrayList<>();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.springframework.data.redis.listener.ChannelTopic;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
Expand Down Expand Up @@ -60,6 +61,7 @@ private void init() {
}

// 채팅방 생성
@Transactional
public MessageResponseDto createRoom(MessageRequestDto dto, String email) {
User sender = userRepository.findByEmail(email).orElseThrow(()->new InvalidEmailException("회원정보가 존재하지 않습니다."));
// User receiver = userRepository.findById(dto.getReceiverId()).orElseThrow(()->new InvalidEmailException("수신 회원정보가 존재하지 않습니다."));
Expand All @@ -82,6 +84,7 @@ public MessageResponseDto createRoom(MessageRequestDto dto, String email) {
}

// 7. 사용자 관련 채팅방 전체 조회 (생성, 수신)
@Transactional
public List<MessageResponseDto> findAllRoomByUser(String email) {
User user = userRepository.findByEmail(email).orElseThrow(()->new InvalidEmailException("회원정보가 존재하지 않습니다."));
List<Room> messageRooms = roomRepository.findByUserOrReceiverName(user, user.getUsername()); // sender & receiver 모두 해당 채팅방 조회 가능 (1:1 대화)
Expand All @@ -99,7 +102,7 @@ public List<MessageResponseDto> findAllRoomByUser(String email) {
messageRoom.getReceiverName());

// 8. 가장 최신 메시지 & 생성 시간 조회
Message latestMessage = messageRepository.findTopByRoomOrderById(messageRoom);
Message latestMessage = messageRepository.findTopByRoomOrderBySentTimeDesc(messageRoom);
if (latestMessage != null) {
messageRoomDto.setLatestMessageCreatedAt(latestMessage.getSentTime());
messageRoomDto.setLatestMessageContent(latestMessage.getMessage());
Expand All @@ -116,7 +119,7 @@ public List<MessageResponseDto> findAllRoomByUser(String email) {
messageRoom.getReceiverName());

// 가장 최신 메시지 & 생성 시간 조회
Message latestMessage = messageRepository.findTopByRoomOrderById(messageRoom);
Message latestMessage = messageRepository.findTopByRoomOrderBySentTimeDesc(messageRoom);
if (latestMessage != null) {
messageRoomDto.setLatestMessageCreatedAt(latestMessage.getSentTime());
messageRoomDto.setLatestMessageContent(latestMessage.getMessage());
Expand All @@ -130,6 +133,7 @@ public List<MessageResponseDto> findAllRoomByUser(String email) {
}

// 사용자 관련 채팅방 선택 조회
@Transactional
public RoomDto findRoom(String roomId, String email) {
Room room = roomRepository.findByRoomId(roomId).orElseThrow(
()->new RoomNotExistsException("채팅방 정보를 찾을 수 없습니다.")
Expand Down Expand Up @@ -163,6 +167,7 @@ public RoomDto findRoom(String roomId, String email) {
}

// 10. 채팅방 삭제
@Transactional
public void deleteRoom(Long id, String email) {
User user = userRepository.findByEmail(email).orElseThrow(()->new InvalidEmailException("회원정보가 존재하지 않습니다."));

Expand Down

0 comments on commit 38bdfbf

Please sign in to comment.