diff --git a/src/backend/chatting-server/src/main/java/chattingserver/config/kafka/Producers.java b/src/backend/chatting-server/src/main/java/chattingserver/config/kafka/Producers.java index 9ba3f751..fc19a579 100644 --- a/src/backend/chatting-server/src/main/java/chattingserver/config/kafka/Producers.java +++ b/src/backend/chatting-server/src/main/java/chattingserver/config/kafka/Producers.java @@ -5,6 +5,7 @@ import chattingserver.dto.RoomMessageDto; import chattingserver.dto.response.ChatMessageResponseDto; import chattingserver.dto.response.RoomResponseDto; +import chattingserver.dto.response.UserListResponseDto; import chattingserver.service.ChatMessageService; import chattingserver.service.RoomService; import chattingserver.util.constant.MessageType; @@ -40,7 +41,7 @@ public void sendMessage(ChatMessageDto chatMessageDto) { if (chatMessageDto.getMessageType() == MessageType.ENTRANCE) { log.info("producers.sendMessage.if MessageType == ENTRANCE"); RoomResponseDto roomResponseDto = roomService.getRoomInfo(chatMessageDto.getRoomId()); - List receivers = roomResponseDto.getUsers().stream().map(User::getUid).collect(Collectors.toList()); + List receivers = roomResponseDto.getUsers().stream().map(UserListResponseDto::getUid).collect(Collectors.toList()); receivers.remove(chatMessageDto.getSenderId()); sendRoomMessage(RoomMessageDto.builder() .receivers(receivers) @@ -67,6 +68,7 @@ public void sendRoomMessage(RoomMessageDto roomMessageDto) { completableFuture.whenComplete((result, ex) -> { if (ex == null) { log.info("메시지 전송 성공=[" + roomMessageDto.getRoomResponseDto().getId() + "] with offset=[" + result.getRecordMetadata().offset() + "]"); + log.info("roomMessageDto={}", roomMessageDto.toString()); } else { log.info("메시지 전송 불가=[" + roomMessageDto.getRoomResponseDto().getId() + "] 원인 : " + ex.getMessage()); } diff --git a/src/backend/chatting-server/src/main/java/chattingserver/controller/ChatController.java b/src/backend/chatting-server/src/main/java/chattingserver/controller/ChatController.java index 1688a092..7a6121f2 100644 --- a/src/backend/chatting-server/src/main/java/chattingserver/controller/ChatController.java +++ b/src/backend/chatting-server/src/main/java/chattingserver/controller/ChatController.java @@ -2,8 +2,8 @@ import chattingserver.config.kafka.Producers; import chattingserver.dto.ChatMessageDto; -import chattingserver.dto.request.UserEntranceRequestDto; import chattingserver.dto.response.CommonAPIMessage; +import chattingserver.dto.response.ReEnterResponseDto; import chattingserver.service.ChatMessageService; import chattingserver.service.RoomService; import chattingserver.util.constant.ErrorCode; @@ -23,6 +23,7 @@ import org.springframework.messaging.handler.annotation.MessageMapping; import org.springframework.web.bind.annotation.*; + @Tag(name = "chat", description = "채팅 API") @RequiredArgsConstructor @RestController @@ -62,14 +63,21 @@ public void sendMessage(@Valid @RequestBody ChatMessageDto chatMessageDto) { log.info("메시지 전송 완료 - message={}", chatMessageDto); } - @Operation(summary = "채팅방 새 메시지 조회") + @Operation(summary = "참여중인 채팅방 재입장, 새 메시지 조회") @ApiResponses(value = { - @ApiResponse(responseCode = "200", description = "채팅방 새 메시지 조회 성공", content = @Content(schema = @Schema(implementation = CommonAPIMessage.class)))}) - @GetMapping("/api/v1/new-message/{roomId}/{readMsgId}") - public ResponseEntity newMessagesAtRoom(@PathVariable String roomId, @PathVariable String readMsgId) { + @ApiResponse(responseCode = "200", description = "채팅방 입장, 새 메시지 조회 성공", content = @Content(schema = @Schema(implementation = CommonAPIMessage.class)))}) + @GetMapping("/api/v1/rooms/joined/{roomId}") + public ResponseEntity newMessagesAtRoom(@PathVariable String roomId, @RequestParam String readMsgId) { CommonAPIMessage apiMessage = new CommonAPIMessage(); apiMessage.setMessage(CommonAPIMessage.ResultEnum.success); - apiMessage.setData(chatMessageService.getNewMessages(roomId, readMsgId)); + + ReEnterResponseDto responseDto = ReEnterResponseDto.builder() + .beforeMessages(chatMessageService.getMessagesBefore(roomId, readMsgId)) + .newMessages(chatMessageService.getNewMessages(roomId, readMsgId)) + .build(); + + apiMessage.setData(responseDto); + return new ResponseEntity<>(apiMessage, HttpStatus.OK); } @@ -99,9 +107,4 @@ public void join(ChatMessageDto message) { producers.sendMessage(chatMessageService.join(message)); } - @PostMapping("/api/v1/permanent-leave/{roomId}") - public void permanentLeaving(@PathVariable String roomId, @RequestBody UserEntranceRequestDto userDto) { - - producers.sendMessage(chatMessageService.permanentLeaving(roomId, userDto)); - } } diff --git a/src/backend/chatting-server/src/main/java/chattingserver/controller/RoomController.java b/src/backend/chatting-server/src/main/java/chattingserver/controller/RoomController.java index 8f8f722f..59d60616 100644 --- a/src/backend/chatting-server/src/main/java/chattingserver/controller/RoomController.java +++ b/src/backend/chatting-server/src/main/java/chattingserver/controller/RoomController.java @@ -5,9 +5,12 @@ import chattingserver.dto.RoomMessageDto; import chattingserver.dto.request.ReadMessageUpdateRequestDto; import chattingserver.dto.request.RoomCreateRequestDto; +import chattingserver.dto.request.UserEntranceRequestDto; import chattingserver.dto.response.CommonAPIMessage; import chattingserver.dto.response.JoinedRoomResponseDto; import chattingserver.dto.response.RoomResponseDto; +import chattingserver.dto.response.UserListResponseDto; +import chattingserver.service.ChatMessageService; import chattingserver.service.RoomService; import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.media.Content; @@ -33,6 +36,7 @@ public class RoomController { private final RoomService roomService; + private final ChatMessageService chatMessageService; private final Producers producers; @Operation(summary = "채팅방 생성 API", description = "신규 채팅방 생성", responses = { @@ -44,7 +48,7 @@ public ResponseEntity groupCreation(@Valid @RequestBody RoomCr RoomResponseDto roomResponseDto = roomService.create(roomCreateRequestDto); producers.sendRoomMessage(RoomMessageDto.builder() - .receivers(roomResponseDto.getUsers().stream().map(User::getUid).collect(Collectors.toList())) + .receivers(roomResponseDto.getUsers().stream().map(UserListResponseDto::getUid).collect(Collectors.toList())) .roomResponseDto(roomResponseDto) .build()); @@ -55,17 +59,18 @@ public ResponseEntity groupCreation(@Valid @RequestBody RoomCr return new ResponseEntity<>(apiMessage, HttpStatus.CREATED); } - @Operation(summary = "채팅방 나가기", description = "그룹 채팅방에서 유저 삭제", responses = { + @Operation(summary = "채팅방 영구적으로 나가기", description = "그룹 채팅방에서 유저 삭제", responses = { @ApiResponse(responseCode = "200", description = "삭제 성공", content = @Content(schema = @Schema(implementation = CommonAPIMessage.class)))}) - @DeleteMapping("/exit/{roomId}") - public ResponseEntity outOfTheRoom(@PathVariable(value = "roomId") String roomId, @RequestParam Long uid) { - if (roomService.exitRoom(roomId, uid)) { + @PostMapping("/exit/{roomId}") + public ResponseEntity outOfTheRoom(@PathVariable(value = "roomId") String roomId, @RequestBody UserEntranceRequestDto userDto) { + producers.sendMessage(chatMessageService.permanentLeaving(roomId, userDto)); + if (roomService.exitRoom(roomId, userDto.getUid())) { return new ResponseEntity<>(new CommonAPIMessage(CommonAPIMessage.ResultEnum.success, new HashMap() {{ put("roomId", roomId); }}), HttpStatus.OK); } return new ResponseEntity<>(new CommonAPIMessage(CommonAPIMessage.ResultEnum.failed, new HashMap() {{ - log.error("exitRoom 채팅방 나가기 실패 roomId: {}, userId: {}", roomId, uid); + log.error("exitRoom 채팅방 나가기 실패 roomId: {}, userId: {}", roomId, userDto.getUid()); put("roomId", roomId); }}), HttpStatus.BAD_REQUEST); } @@ -82,7 +87,7 @@ public ResponseEntity chatRoomInfo(@PathVariable(value = "room @Operation(summary = "모든 채팅방 정보 조회 API", description = "모든 채팅방 정보 조회", responses = { @ApiResponse(responseCode = "200", description = "조회 성공", content = @Content(schema = @Schema(implementation = RoomResponseDto.class)))}) - @GetMapping("/rooms") + @GetMapping("/") public ResponseEntity getAllChatRoomInfos() { CommonAPIMessage apiMessage = new CommonAPIMessage(); apiMessage.setMessage(CommonAPIMessage.ResultEnum.success); @@ -101,7 +106,7 @@ public ResponseEntity myChatRooms(@RequestParam(required = tru return new ResponseEntity<>(apiMessage,HttpStatus.OK); } - @Operation(summary = "채팅방 리스트 조회", description = "특정 유저가 참여할 수 있는 채팅방 리스트 조회", responses = { + @Operation(summary = "참여 가능한 채팅방 리스트 조회", description = "특정 유저가 참여할 수 있는 채팅방 리스트 조회", responses = { @ApiResponse(responseCode = "200", description = "조회 성공", content = @Content(schema = @Schema(implementation = CommonAPIMessage.class)))}) @GetMapping("/unjoined") public ResponseEntity unjoinedChatRooms(@RequestParam(required = true) Long uid) { @@ -112,8 +117,8 @@ public ResponseEntity unjoinedChatRooms(@RequestParam(required return new ResponseEntity<>(apiMessage, HttpStatus.OK); } - @Operation(summary = "마지막 읽은 메시지 id 저장") - @PutMapping("/last-message") + @Operation(summary = "채팅방 잠시 나가기, 마지막 읽은 메시지 id 저장", description = "잠시 나가기 (완전히 나가기 아님)") + @PutMapping("/leave") public ResponseEntity updateLastReadMsgId(@RequestBody ReadMessageUpdateRequestDto readMessageUpdateRequestDto){ return new ResponseEntity<>(roomService.updateLastReadMsgId(readMessageUpdateRequestDto), HttpStatus.OK); } diff --git a/src/backend/chatting-server/src/main/java/chattingserver/domain/chat/LastMessage.java b/src/backend/chatting-server/src/main/java/chattingserver/domain/chat/LastMessage.java index 197e87f1..9e80d5c2 100644 --- a/src/backend/chatting-server/src/main/java/chattingserver/domain/chat/LastMessage.java +++ b/src/backend/chatting-server/src/main/java/chattingserver/domain/chat/LastMessage.java @@ -13,6 +13,8 @@ public class LastMessage { private String messageId; private Long senderId; + private String nickName; + private String senderProfileImage; private String content; private LocalDateTime createdAt; } diff --git a/src/backend/chatting-server/src/main/java/chattingserver/domain/room/Playlist.java b/src/backend/chatting-server/src/main/java/chattingserver/domain/room/Playlist.java index 29483eac..3029c128 100644 --- a/src/backend/chatting-server/src/main/java/chattingserver/domain/room/Playlist.java +++ b/src/backend/chatting-server/src/main/java/chattingserver/domain/room/Playlist.java @@ -13,4 +13,8 @@ public class Playlist { private String id; private String name; private List musics; + + public Music getFirstMusic() { + return musics.get(0); // 무조건 있음 신뢰 + } } diff --git a/src/backend/chatting-server/src/main/java/chattingserver/domain/room/Room.java b/src/backend/chatting-server/src/main/java/chattingserver/domain/room/Room.java index b1e362e0..4f8cef2c 100644 --- a/src/backend/chatting-server/src/main/java/chattingserver/domain/room/Room.java +++ b/src/backend/chatting-server/src/main/java/chattingserver/domain/room/Room.java @@ -20,6 +20,7 @@ public class Room { private String id; private String roomName; private Playlist playlist; + private String thumbnailImage; private List users; private LocalDateTime createdAt; diff --git a/src/backend/chatting-server/src/main/java/chattingserver/dto/request/RoomCreateRequestDto.java b/src/backend/chatting-server/src/main/java/chattingserver/dto/request/RoomCreateRequestDto.java index ec391b71..55398417 100644 --- a/src/backend/chatting-server/src/main/java/chattingserver/dto/request/RoomCreateRequestDto.java +++ b/src/backend/chatting-server/src/main/java/chattingserver/dto/request/RoomCreateRequestDto.java @@ -18,4 +18,8 @@ public class RoomCreateRequestDto { private String nickName; @NotBlank private Playlist playlist; + + public String getThumbnailImage() { + return this.playlist.getFirstMusic().getThumbnail(); + } } diff --git a/src/backend/chatting-server/src/main/java/chattingserver/dto/request/UserEntranceRequestDto.java b/src/backend/chatting-server/src/main/java/chattingserver/dto/request/UserEntranceRequestDto.java index d6abc976..91c931a8 100644 --- a/src/backend/chatting-server/src/main/java/chattingserver/dto/request/UserEntranceRequestDto.java +++ b/src/backend/chatting-server/src/main/java/chattingserver/dto/request/UserEntranceRequestDto.java @@ -1,5 +1,6 @@ package chattingserver.dto.request; +import jakarta.validation.constraints.NotBlank; import lombok.*; @Builder @@ -9,7 +10,9 @@ @ToString public class UserEntranceRequestDto { + @NotBlank private Long uid; + @NotBlank private String nickName; private String profileImage; } diff --git a/src/backend/chatting-server/src/main/java/chattingserver/dto/response/JoinedRoomResponseDto.java b/src/backend/chatting-server/src/main/java/chattingserver/dto/response/JoinedRoomResponseDto.java index a698dce5..7675b595 100644 --- a/src/backend/chatting-server/src/main/java/chattingserver/dto/response/JoinedRoomResponseDto.java +++ b/src/backend/chatting-server/src/main/java/chattingserver/dto/response/JoinedRoomResponseDto.java @@ -13,6 +13,7 @@ public class JoinedRoomResponseDto { private String roomId; private String roomName; + private String thumbnailImage; private List users; private LastMessage lastMessage; } diff --git a/src/backend/chatting-server/src/main/java/chattingserver/dto/response/ReEnterResponseDto.java b/src/backend/chatting-server/src/main/java/chattingserver/dto/response/ReEnterResponseDto.java new file mode 100644 index 00000000..f612dc0c --- /dev/null +++ b/src/backend/chatting-server/src/main/java/chattingserver/dto/response/ReEnterResponseDto.java @@ -0,0 +1,15 @@ +package chattingserver.dto.response; + +import lombok.*; + +import java.util.List; + +@Builder +@Getter +@NoArgsConstructor +@AllArgsConstructor +@ToString +public class ReEnterResponseDto { + private List beforeMessages; + private List newMessages; +} diff --git a/src/backend/chatting-server/src/main/java/chattingserver/dto/response/RoomResponseDto.java b/src/backend/chatting-server/src/main/java/chattingserver/dto/response/RoomResponseDto.java index 6dc9a05f..de54e53a 100644 --- a/src/backend/chatting-server/src/main/java/chattingserver/dto/response/RoomResponseDto.java +++ b/src/backend/chatting-server/src/main/java/chattingserver/dto/response/RoomResponseDto.java @@ -16,8 +16,9 @@ public class RoomResponseDto { private String id; private String roomName; private long userCount; - private List users; + private List users; private Playlist playlist; + private String thumbnailImage; private LocalDateTime createdAt; } \ No newline at end of file diff --git a/src/backend/chatting-server/src/main/java/chattingserver/dto/response/UserListResponseDto.java b/src/backend/chatting-server/src/main/java/chattingserver/dto/response/UserListResponseDto.java new file mode 100644 index 00000000..f816c897 --- /dev/null +++ b/src/backend/chatting-server/src/main/java/chattingserver/dto/response/UserListResponseDto.java @@ -0,0 +1,17 @@ +package chattingserver.dto.response; + +import lombok.*; + +import java.time.LocalDateTime; + +@Getter +@Builder +@ToString +@AllArgsConstructor +@NoArgsConstructor + +public class UserListResponseDto { + private Long uid; + private String nickName; + private String profileImage; +} \ No newline at end of file diff --git a/src/backend/chatting-server/src/main/java/chattingserver/repository/ChatMessageRepositoryCustom.java b/src/backend/chatting-server/src/main/java/chattingserver/repository/ChatMessageRepositoryCustom.java index 87c016f9..e6923e33 100644 --- a/src/backend/chatting-server/src/main/java/chattingserver/repository/ChatMessageRepositoryCustom.java +++ b/src/backend/chatting-server/src/main/java/chattingserver/repository/ChatMessageRepositoryCustom.java @@ -15,4 +15,6 @@ public interface ChatMessageRepositoryCustom { List getNewMessages(String roomId, String readMsgId); ChatMessage getLastMessage(String roomId); + List findPreviousMessages(String roomId, String readMsgId, int limit); + } diff --git a/src/backend/chatting-server/src/main/java/chattingserver/repository/ChatMessageRepositoryImpl.java b/src/backend/chatting-server/src/main/java/chattingserver/repository/ChatMessageRepositoryImpl.java index f6c94a74..3bb5b713 100644 --- a/src/backend/chatting-server/src/main/java/chattingserver/repository/ChatMessageRepositoryImpl.java +++ b/src/backend/chatting-server/src/main/java/chattingserver/repository/ChatMessageRepositoryImpl.java @@ -74,4 +74,12 @@ public ChatMessage getLastMessage(String roomId) { } return chatMessage; } + + @Override + public List findPreviousMessages(String roomId, String readMsgId, int limit) { + Query query = new Query(); + query.addCriteria(Criteria.where("roomId").is(roomId).and("_id").lt(readMsgId)); // 이전 메시지만 가져오도록 쿼리 설정 + query.limit(20); // 최대 20개의 메시지 가져오도록 제한 설정 + return mongoTemplate.find(query, ChatMessage.class); + } } diff --git a/src/backend/chatting-server/src/main/java/chattingserver/repository/ChatroomDummyInitializer.java b/src/backend/chatting-server/src/main/java/chattingserver/repository/ChatroomDummyInitializer.java index 29d36c19..7f46bdb2 100644 --- a/src/backend/chatting-server/src/main/java/chattingserver/repository/ChatroomDummyInitializer.java +++ b/src/backend/chatting-server/src/main/java/chattingserver/repository/ChatroomDummyInitializer.java @@ -29,14 +29,14 @@ public void run(ApplicationArguments args) throws Exception { User dummyUser1 = User.builder() .uid(1L) .nickName("유저닉네임1") - .profileImage("userprofileimage1.url") + .profileImage("https://cdn.pixabay.com/photo/2015/10/05/22/37/blank-profile-picture-973460_960_720.png") .enteredAt(LocalDateTime.now()) .build(); User dummyUser2 = User.builder() .uid(2L) .nickName("유저닉네임2") - .profileImage("userprofileimage2.url") + .profileImage("") .enteredAt(LocalDateTime.now()) .build(); @@ -48,14 +48,14 @@ public void run(ApplicationArguments args) throws Exception { .title("음원1") .artist("아티스트1") .playtime("12:34") - .thumbnail("thumbnail-1.url") + .thumbnail("https://marketplace.canva.com/EAFHtE880QY/1/0/1600w/canva-blue-and-red-modern-music-youtube-thumbnail-cBAYqTj4TLk.jpg") .build(); Music dummyMusic2 = Music.builder() .title("음원2") .artist("아티스트2") .playtime("01:23") - .thumbnail("thumbnail-2.url") + .thumbnail("https://marketplace.canva.com/EAFOwKVP0Ck/1/0/1600w/canva-blue-orange-colorful-aesthetic-minimalist-lofi-music-youtube-thumbnail-ETT-krmERlk.jpg") .build(); List dummyMusics = new ArrayList<>(); @@ -73,7 +73,9 @@ public void run(ApplicationArguments args) throws Exception { Room dummyRoom = Room.builder() .roomName(dummyPlaylist.getName()) .playlist(dummyPlaylist) + .thumbnailImage(dummyPlaylist.getFirstMusic().getThumbnail()) .users(users) + .createdAt(LocalDateTime.now()) .build(); roomRepository.save(dummyRoom); @@ -105,14 +107,14 @@ public void run(ApplicationArguments args) throws Exception { User dummyUser3 = User.builder() .uid(3L) .nickName("유저닉네임3") - .profileImage("userprofileimage3.url") + .profileImage("https://t4.ftcdn.net/jpg/03/64/21/11/360_F_364211147_1qgLVxv1Tcq0Ohz3FawUfrtONzz8nq3e.jpg") .enteredAt(LocalDateTime.now()) .build(); User dummyUser4 = User.builder() .uid(3L) .nickName("유저닉네임4") - .profileImage("userprofileimage24url") + .profileImage("https://images.pexels.com/photos/771742/pexels-photo-771742.jpeg?cs=srgb&dl=pexels-mohamed-abdelghaffar-771742.jpg&fm=jpg") .enteredAt(LocalDateTime.now()) .build(); @@ -122,15 +124,15 @@ public void run(ApplicationArguments args) throws Exception { users2.add(dummyUser1); Music dummyMusic3 = Music.builder() - .title("음원1") - .artist("아티스트1") + .title("음원3") + .artist("아티스트3") .playtime("12:34") - .thumbnail("thumbnail-1.url") + .thumbnail("https://content.wepik.com/statics/13638236/preview-page0.jpg") .build(); List dummyMusics2 = new ArrayList<>(); - dummyMusics.add(dummyMusic1); - dummyMusics.add(dummyMusic3); + dummyMusics2.add(dummyMusic1); + dummyMusics2.add(dummyMusic3); Playlist dummyPlaylist2 = Playlist.builder() @@ -139,11 +141,16 @@ public void run(ApplicationArguments args) throws Exception { .musics(dummyMusics2) .build(); + log.info("dummyPlaylist2={}", dummyPlaylist2.toString()); + log.info("dummyPlaylist2 firstMusic={}", dummyPlaylist2.getFirstMusic().toString()); + // room build Room dummyRoom2 = Room.builder() .roomName(dummyPlaylist2.getName()) .playlist(dummyPlaylist2) + .thumbnailImage(dummyPlaylist2.getFirstMusic().getThumbnail()) .users(users2) + .createdAt(LocalDateTime.now()) .build(); roomRepository.save(dummyRoom2); diff --git a/src/backend/chatting-server/src/main/java/chattingserver/repository/RoomRepositoryImpl.java b/src/backend/chatting-server/src/main/java/chattingserver/repository/RoomRepositoryImpl.java index 772edc7a..63135945 100644 --- a/src/backend/chatting-server/src/main/java/chattingserver/repository/RoomRepositoryImpl.java +++ b/src/backend/chatting-server/src/main/java/chattingserver/repository/RoomRepositoryImpl.java @@ -33,7 +33,7 @@ public UpdateResult updateLastReadMsgId(ReadMessageUpdateRequestDto requestDto) .andOperator(Criteria.where("users").elemMatch(Criteria.where("uid").is(requestDto.getUid()))) ); - Update update = new Update().set("users.$.lastReadMsgId", requestDto.getMessageId()); + Update update = new Update().set("users.$.lastReadMessageId", requestDto.getMessageId()); return mongoTemplate.updateFirst(query, update, Room.class); } diff --git a/src/backend/chatting-server/src/main/java/chattingserver/service/ChatMessageService.java b/src/backend/chatting-server/src/main/java/chattingserver/service/ChatMessageService.java index b2c2dc82..5d749507 100644 --- a/src/backend/chatting-server/src/main/java/chattingserver/service/ChatMessageService.java +++ b/src/backend/chatting-server/src/main/java/chattingserver/service/ChatMessageService.java @@ -62,10 +62,10 @@ public ChatMessageDto saveChatMessage(ChatMessageDto chatMessageDto) { return entityToResponseDtoConverter.convertMessage(message); } - public Object getNewMessages(String roomId, String readMsgId) { + public List getNewMessages(String roomId, String readMsgId) { List messages = chatMessageRepository.getNewMessages(roomId, readMsgId); - log.info("신규 메시지 조회 성공 roomId={}, readMsgId={}", roomId, readMsgId); - return messages.stream().map(entityToResponseDtoConverter::convertMessage).collect(Collectors.toList()); + log.info("신규 메시지 조회 성공 roomId={}, readMsgId={}, messages={}", roomId, readMsgId, messages); + return messages.stream().map(entityToResponseDtoConverter::convertToResponseMessage).collect(Collectors.toList()); } public List getAllMessagesAtRoom(String roomId) { @@ -157,4 +157,11 @@ public ChatMessageDto permanentLeaving(String roomId, UserEntranceRequestDto use } + + public List getMessagesBefore(String roomId, String readMsgId) { + List messages = chatMessageRepository.findPreviousMessages(roomId, readMsgId, SIZE); + log.info("이전 메시지 조회 성공 roomId={}, readMsgId={}, messages={}", roomId, readMsgId, messages); + return messages.stream().map(entityToResponseDtoConverter::convertToResponseMessage).collect(Collectors.toList()); + } + } diff --git a/src/backend/chatting-server/src/main/java/chattingserver/service/RoomService.java b/src/backend/chatting-server/src/main/java/chattingserver/service/RoomService.java index 808e27bb..139f51a6 100644 --- a/src/backend/chatting-server/src/main/java/chattingserver/service/RoomService.java +++ b/src/backend/chatting-server/src/main/java/chattingserver/service/RoomService.java @@ -38,23 +38,29 @@ public RoomResponseDto getRoomInfo(String roomId) { return foundRoom.map(entityToResponseDtoConverter::convertRoom).orElse(null); } - public List findJoinedRoomsByUid(Long uid){ + public List findJoinedRoomsByUid(Long uid) { List roomList = roomRepository.findJoinedRoomsByUid(uid); log.info("참여한 방 리스트 조회 성공 uid={}", uid); List myRoomsDto = new ArrayList<>(); - for(Room room : roomList){ + for (Room room : roomList) { String roomId = room.getId(); ChatMessage message = chatMessageRepository.getLastMessage(roomId); LastMessage lastMessage = LastMessage.builder() - .messageId(message.getId()).senderId(message.getSenderId()) - .content(message.getContent()).createdAt(message.getCreatedAt()) + .messageId(message.getId()) + .senderId(message.getSenderId()) + .nickName(message.getNickName()) + .senderProfileImage(message.getSenderProfileImage()) + .content(message.getContent()) + .createdAt(message.getCreatedAt()) .build(); myRoomsDto.add(JoinedRoomResponseDto.builder() - .roomId(roomId).roomName(room.getRoomName()) + .roomId(roomId) + .roomName(room.getRoomName()) + .thumbnailImage(room.getThumbnailImage()) .users(room.getUsers()) .lastMessage(lastMessage) .build()); @@ -73,8 +79,9 @@ public List findUnjoinedRooms(Long uid) { .map(room -> RoomResponseDto.builder() .id(room.getId()) .roomName(room.getRoomName()) + .thumbnailImage(room.getThumbnailImage()) .userCount(room.getUsers().size()) - .users(room.getUsers()) + .users(room.getUsers().stream().map(entityToResponseDtoConverter::convertUser).collect(Collectors.toList())) .playlist(room.getPlaylist()) .build()) .collect(Collectors.toList()); @@ -96,6 +103,7 @@ public RoomResponseDto create(RoomCreateRequestDto roomCreateRequestDto) { Room room = Room.builder() .roomName(roomCreateRequestDto.getPlaylist().getName()) .playlist(roomCreateRequestDto.getPlaylist()) + .thumbnailImage(roomCreateRequestDto.getThumbnailImage()) .users(users) .build(); @@ -118,7 +126,8 @@ public boolean exitRoom(String roomId, Long uid) { public CommonAPIMessage updateLastReadMsgId(ReadMessageUpdateRequestDto requestDto) { UpdateResult updateResult = roomRepository.updateLastReadMsgId(requestDto); - if(updateResult.getModifiedCount() == 0) return new CommonAPIMessage(CommonAPIMessage.ResultEnum.failed, updateResult.getModifiedCount()); + if (updateResult.getModifiedCount() == 0) + return new CommonAPIMessage(CommonAPIMessage.ResultEnum.failed, updateResult.getModifiedCount()); return new CommonAPIMessage(CommonAPIMessage.ResultEnum.success, updateResult.getModifiedCount()); } diff --git a/src/backend/chatting-server/src/main/java/chattingserver/util/converter/EntityToResponseDtoConverter.java b/src/backend/chatting-server/src/main/java/chattingserver/util/converter/EntityToResponseDtoConverter.java index a56f8ad3..0ffd336b 100644 --- a/src/backend/chatting-server/src/main/java/chattingserver/util/converter/EntityToResponseDtoConverter.java +++ b/src/backend/chatting-server/src/main/java/chattingserver/util/converter/EntityToResponseDtoConverter.java @@ -2,9 +2,13 @@ import chattingserver.domain.chat.ChatMessage; import chattingserver.domain.room.Room; +import chattingserver.domain.room.User; import chattingserver.dto.ChatMessageDto; import chattingserver.dto.response.ChatMessageResponseDto; import chattingserver.dto.response.RoomResponseDto; +import chattingserver.dto.response.UserListResponseDto; + +import java.util.stream.Collectors; public class EntityToResponseDtoConverter { @@ -13,9 +17,17 @@ public RoomResponseDto convertRoom(Room room) { .id(room.getId()) .roomName(room.getRoomName()) .userCount(room.getUsers().size()) - .users(room.getUsers()) + .users(room.getUsers().stream().map(this::convertUser).collect(Collectors.toList())) .playlist(room.getPlaylist()) -// .thumbnail(room.getPlaylist().getMusics().get(0)) // 플레이리스트에 곡 무조건 존재함을 신뢰 + .thumbnailImage(room.getThumbnailImage()) // 플레이리스트에 곡 무조건 존재함을 신뢰 + .build(); + } + + public UserListResponseDto convertUser(User user) { + return UserListResponseDto.builder() + .uid(user.getUid()) + .nickName(user.getNickName()) + .profileImage(user.getProfileImage()) .build(); }