Skip to content

Commit

Permalink
Merge pull request #35 from Domitory-CheckMate/feature/31-chat
Browse files Browse the repository at this point in the history
[feat] 안읽은 채팅 수
  • Loading branch information
OJOJIN authored Jan 11, 2024
2 parents 56ec01a + e9988bd commit f99c384
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,15 @@ public void getChatList(@Header("simpSessionAttributes") Map<String, Object> sim
// 채팅방 입장하기
@MessageMapping("/room-enter")
public void enterRoom(@Header("simpSessionAttributes") Map<String, Object> simpSessionAttributes,
@Payload final ChatListRequestDto request) {
@Payload final ChatListRequestDto request) {
ChatRoomEnterResponseDto response = chatService.enterChatRoom(simpSessionAttributes, request);
sendingOperations.convertAndSend("/queue/chat/" + response.chatRoomId(), SocketBaseResponse.of(MessageType.ROOM_ENTER, response));
}

// 유저 총 안읽은 메세지
@MessageMapping("/not-read")
public void getNotReadChatCount(@Header("simpSessionAttributes") Map<String, Object> simpSessionAttributes) {
ChatTotalNotReadResponseDto response = chatService.getTotalNotReadCount(simpSessionAttributes);
sendingOperations.convertAndSend("/queue/user/" + simpSessionAttributes.get("userId"), SocketBaseResponse.of(MessageType.NOT_READ_COUNT, response));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ public enum MessageType {
NEW_CHAT_NOTIFICATION("NEW_CHAT_NOTIFICATION"),
ROOM_ENTER("ROOM_ENTER"),
ROOM_LIST("ROOM_LIST"),
CHAT_LIST("CHAT_LIST");
CHAT_LIST("CHAT_LIST"),
NOT_READ_COUNT("NOT_READ_COUNT");

private final String desc;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package org.gachon.checkmate.domain.chat.dto.response;

import lombok.Builder;

@Builder
public record ChatTotalNotReadResponseDto(
Long notReadCount

) {
public static ChatTotalNotReadResponseDto of(Long notReadCount) {
return ChatTotalNotReadResponseDto.builder()
.notReadCount(notReadCount)
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Slice;

import java.util.List;

public interface ChatCustomRepository {

Slice<Chat> findBeforeChatList(final String chatRoomId, final Pageable pageable);
Expand All @@ -14,4 +16,6 @@ public interface ChatCustomRepository {
ChatLastMessageDto findLastChatRoomContent(final String chatRoomId);

Long findUserNotReadCount(final String chatRoomId, final Long userId);

Long findUserNotReadTotalCount(final List<String> chatRoomId, final Long userId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,20 @@ public Long findUserNotReadCount(final String chatRoomId, final Long userId) {
return mongoTemplate.count(query, Chat.class);
}

/**
* 채팅방에 유저가 읽지않은 메시지의 수를 가져오는 메소드
*/
@Override
public Long findUserNotReadTotalCount(final List<String> chatRoomId, final Long userId) {
Query query = new Query();

query.addCriteria(Criteria.where("chatRoomId").in(chatRoomId)
.and("senderId").ne(userId)
.and("isRead").is(false));

return mongoTemplate.count(query, Chat.class);
}

private boolean hasNextPage(List<Chat> chats, int pageSize) {
if (chats.size() > pageSize) {
chats.remove(pageSize);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ public ChatListResponseDto getChatList(Map<String, Object> simpSessionAttributes
* 2. 이전에 유저가 읽지 않은 채팅을 모두 읽음처리 해줍니다.
*/
public ChatRoomEnterResponseDto enterChatRoom(Map<String, Object> simpSessionAttributes,
ChatListRequestDto request) {
ChatListRequestDto request) {
Long userId = getUserIdInAttributes(simpSessionAttributes);
String chatRoomId = getChatRoomId(request.otherUserId(), userId);

Expand All @@ -130,6 +130,18 @@ public ChatRoomEnterResponseDto enterChatRoom(Map<String, Object> simpSessionAtt
return ChatRoomEnterResponseDto.of(userId, chatRoomId);
}

/**
* 유저가 안읽은 메시지의 총 수를 받아옵니다.
*/
public ChatTotalNotReadResponseDto getTotalNotReadCount(Map<String, Object> simpSessionAttributes) {
Long userId = getUserIdInAttributes(simpSessionAttributes);
List<ChatRoom> chatRooms = getUserChatRoomsByUserId(userId);

Long userTotalNotReadCount = getUserTotalNotReadCount(chatRooms, userId);

return ChatTotalNotReadResponseDto.of(userTotalNotReadCount);
}

private void sendNotificationToOtherUser(Boolean isOtherUserInChatRoom, Chat savedChat, Long otherUserId) {
if(!isOtherUserInChatRoom) {
NewChatResponseDto notificationChat = NewChatResponseDto.of(savedChat);
Expand Down Expand Up @@ -163,6 +175,11 @@ private Long getUserNotReadCount(ChatRoom chatRoom, Long userId) {
return chatRepository.findUserNotReadCount(chatRoom.getId(), userId);
}


private Long getUserTotalNotReadCount(List<ChatRoom> chatRooms, Long userId) {
return chatRepository.findUserNotReadTotalCount(chatRooms.stream().map(ChatRoom::getId).toList(), userId);
}

private ChatLastMessageDto getLastChatRoomContent(ChatRoom chatRoom) {
return chatRepository.findLastChatRoomContent(chatRoom.getId());
}
Expand Down

0 comments on commit f99c384

Please sign in to comment.