Skip to content

Commit

Permalink
[FEAT] 채팅 알림 기능 구현 (#82)
Browse files Browse the repository at this point in the history
* fix(CommentService): 도와줄래요? 게시글 알림 url 수정

* style(ChatMessageController): 컨벤션 수정 및 TODO 주석 제거

* feat(ChatMessageService): 채팅 알림 전송 기능 추가
  • Loading branch information
Profile-exe authored Jul 21, 2024
1 parent 7a5a9ce commit 8929bef
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,30 @@
import econo.buddybridge.chat.chatmessage.dto.ChatMessageResDto;
import econo.buddybridge.chat.chatmessage.service.ChatMessageService;
import econo.buddybridge.utils.session.SessionUtils;
import java.util.Map;
import lombok.RequiredArgsConstructor;
import org.springframework.messaging.handler.annotation.*;
import org.springframework.messaging.handler.annotation.DestinationVariable;
import org.springframework.messaging.handler.annotation.Header;
import org.springframework.messaging.handler.annotation.MessageMapping;
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.messaging.handler.annotation.SendTo;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
@RequiredArgsConstructor
public class ChatMessageController {

private final ChatMessageService chatMessageService;

@MessageMapping("/chat/{matching-id}") // 메시지 보내기 // /api/app/chat/{room-id} - pub
@SendTo("/api/queue/chat/{matching-id}") // 구독 경로 - sub
public ChatMessageResDto sendMessage(
@DestinationVariable("matching-id") Long matchingId,
@Payload ChatMessageReqDto chatMessageReqDto,
@Header ("simpSessionAttributes") Map<String, Object> attributes
){
@Header("simpSessionAttributes") Map<String, Object> attributes
) {
Object memberIdObject = attributes.get("memberId");
Long senderId = SessionUtils.validateMemberId(memberIdObject);
return chatMessageService.save(senderId,chatMessageReqDto,matchingId);
return chatMessageService.save(senderId, chatMessageReqDto, matchingId);
}


//TODO: 채팅 메시지 삭제(업데이트로 MessageType: DELETE로 변경)
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import econo.buddybridge.matching.repository.MatchingRepository;
import econo.buddybridge.member.entity.Member;
import econo.buddybridge.member.repository.MemberRepository;
import econo.buddybridge.notification.entity.NotificationType;
import econo.buddybridge.notification.service.EmitterService;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
Expand All @@ -18,14 +20,16 @@
@Service
@RequiredArgsConstructor
public class ChatMessageService {

private final MemberRepository memberRepository;
private final MatchingRepository matchingRepository;
private final ChatMessageRepository chatMessageRepository;
private final EmitterService emitterService;

@Transactional // 메시지 저장
public ChatMessageResDto save(Long senderId, ChatMessageReqDto chatMessageReqDto, Long matchingId) {
Member sender = memberRepository.findById(senderId)
.orElseThrow(()->new IllegalArgumentException("존재하지 않는 사용자입니다."));
.orElseThrow(() -> new IllegalArgumentException("존재하지 않는 사용자입니다."));

Matching matching = matchingRepository.findById(matchingId)
.orElseThrow(() -> new IllegalArgumentException("존재하지 않는 매칭입니다."));
Expand All @@ -37,6 +41,17 @@ public ChatMessageResDto save(Long senderId, ChatMessageReqDto chatMessageReqDto
.messageType(chatMessageReqDto.messageType())
.build();

Long receiverId = getReceiverId(sender.getId(), matching.getId());
Member receiver = memberRepository.findById(receiverId)
.orElseThrow(() -> new IllegalArgumentException("존재하지 않는 사용자입니다."));

emitterService.send( // 채팅을 받는 사용자에게 알림 전송
receiver,
sender.getName() + "님이 메시지를 보냈습니다. - " + chatMessage.getContent(),
"/chat/" + matching.getId(),
NotificationType.CHAT
);

chatMessageRepository.save(chatMessage);

return ChatMessageResDto.builder()
Expand All @@ -48,10 +63,23 @@ public ChatMessageResDto save(Long senderId, ChatMessageReqDto chatMessageReqDto
.build();
}

private Long getReceiverId(Long senderId, Long matchingId) {
Matching matching = matchingRepository.findById(matchingId)
.orElseThrow(() -> new IllegalArgumentException("존재하지 않는 매칭입니다."));

if (matching.getGiver().getId().equals(senderId)) {
return matching.getTaker().getId();
} else if (matching.getTaker().getId().equals(senderId)) {
return matching.getGiver().getId();
} else {
throw new IllegalArgumentException("매칭에 속하지 않은 사용자입니다.");
}
}

@Transactional // 마지막 메시지 조회
public ChatMessageResDto getLastChatMessage(Long matchingId){
public ChatMessageResDto getLastChatMessage(Long matchingId) {
List<ChatMessage> chatMessageList = chatMessageRepository.findLastMessageByMatchingId(matchingId, PageRequest.of(0, 1));
if(chatMessageList.isEmpty()) {
if (chatMessageList.isEmpty()) {
throw new IllegalArgumentException("마지막 메시지가 존재하지 않습니다.");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public Long createComment(CommentReqDto commentReqDto, Long postId, Long memberI
private String getCommentNotificationUrl(PostType postType, Long postId) {
switch (postType) {
case TAKER -> { // 도와줄래요? 게시글
return "/help-me" + postId;
return "/help-me/" + postId;
}
case GIVER -> { // 도와줄게요! 게시글
return "/help-you/" + postId;
Expand Down

0 comments on commit 8929bef

Please sign in to comment.