Skip to content

Commit

Permalink
[BE] feat: 채팅 알림에 모임 정보(모임 사진, 모임 이름) 추가 (#718)
Browse files Browse the repository at this point in the history
  • Loading branch information
takoyakimchi authored Oct 23, 2024
1 parent 4cbc0db commit 1f37c3b
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
import com.happy.friendogly.chat.dto.response.ChatMessageSocketResponse;
import com.happy.friendogly.chat.repository.ChatMessageRepository;
import com.happy.friendogly.chat.repository.ChatRoomRepository;
import com.happy.friendogly.club.domain.Club;
import com.happy.friendogly.club.repository.ClubRepository;
import com.happy.friendogly.exception.FriendoglyException;
import com.happy.friendogly.member.domain.Member;
import com.happy.friendogly.member.repository.MemberRepository;
Expand All @@ -29,19 +31,22 @@ public class ChatCommandService {
private final MemberRepository memberRepository;
private final ChatRoomRepository chatRoomRepository;
private final ChatMessageRepository chatMessageRepository;
private final ClubRepository clubRepository;
private final NotificationService notificationService;
private final ChatTemplate chatTemplate;

public ChatCommandService(
MemberRepository memberRepository,
ChatRoomRepository chatRoomRepository,
ChatMessageRepository chatMessageRepository,
ClubRepository clubRepository,
NotificationService notificationService,
ChatTemplate chatTemplate
) {
this.memberRepository = memberRepository;
this.chatRoomRepository = chatRoomRepository;
this.chatMessageRepository = chatMessageRepository;
this.clubRepository = clubRepository;
this.notificationService = notificationService;
this.chatTemplate = chatTemplate;
}
Expand Down Expand Up @@ -70,9 +75,10 @@ public void sendLeave(Long senderMemberId, Long chatRoomId) {
}

private void sendAndSave(MessageType messageType, String content, ChatRoom chatRoom, Member senderMember) {
ChatMessageSocketResponse chat = new ChatMessageSocketResponse(messageType, content, senderMember,
LocalDateTime.now());
notificationService.sendChatNotification(chatRoom.getId(), chat);
ChatMessageSocketResponse chat = new ChatMessageSocketResponse(
messageType, content, senderMember, LocalDateTime.now());
Club club = clubRepository.getByChatRoomId(chatRoom.getId());
notificationService.sendChatNotification(chatRoom.getId(), chat, club);
chatTemplate.convertAndSend(chatRoom.getId(), chat);
chatMessageRepository.save(new ChatMessage(chatRoom, messageType, senderMember, content));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.happy.friendogly.notification.service;

import com.happy.friendogly.chat.dto.response.ChatMessageSocketResponse;
import com.happy.friendogly.club.domain.Club;
import java.util.List;
import org.springframework.context.annotation.Profile;
import org.springframework.stereotype.Service;
Expand All @@ -20,7 +21,7 @@ public void sendFootprintNotification(String title, String content, List<String>
}

@Override
public void sendChatNotification(Long chatRoomId, ChatMessageSocketResponse response) {
public void sendChatNotification(Long chatRoomId, ChatMessageSocketResponse response, Club club) {

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import com.google.firebase.messaging.FirebaseMessagingException;
import com.google.firebase.messaging.MulticastMessage;
import com.happy.friendogly.chat.dto.response.ChatMessageSocketResponse;
import com.happy.friendogly.club.domain.Club;
import com.happy.friendogly.exception.FriendoglyException;
import com.happy.friendogly.notification.domain.NotificationType;
import com.happy.friendogly.notification.repository.DeviceTokenRepository;
Expand Down Expand Up @@ -55,7 +56,7 @@ public void sendFootprintNotification(String title, String content, List<String>
}

@Override
public void sendChatNotification(Long chatRoomId, ChatMessageSocketResponse response) {
public void sendChatNotification(Long chatRoomId, ChatMessageSocketResponse response, Club club) {
List<String> receiverTokens = deviceTokenRepository
.findAllByChatRoomIdWithoutMine(chatRoomId, response.senderMemberId());

Expand All @@ -66,7 +67,9 @@ public void sendChatNotification(Long chatRoomId, ChatMessageSocketResponse resp
"senderName", response.senderName(),
"content", response.content(),
"createdAt", response.createdAt().toString(),
"profilePictureUrl", response.profilePictureUrl()
"profilePictureUrl", response.profilePictureUrl(),
"clubPictureUrl", club.getImageUrl(),
"clubTitle", club.getTitle().getValue()
);

sendNotificationWithType(CHAT, "채팅", data, receiverTokens);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.happy.friendogly.notification.service;

import com.happy.friendogly.chat.dto.response.ChatMessageSocketResponse;
import com.happy.friendogly.club.domain.Club;
import java.util.List;

public interface NotificationService {
Expand All @@ -9,7 +10,7 @@ public interface NotificationService {

void sendFootprintNotification(String title, String content, List<String> receiverTokens);

void sendChatNotification(Long chatRoomId, ChatMessageSocketResponse response);
void sendChatNotification(Long chatRoomId, ChatMessageSocketResponse response, Club club);

void sendPlaygroundJoinNotification(String title, String content, List<String> receiverTokens);
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,15 @@
import com.happy.friendogly.chat.domain.ChatMessage;
import com.happy.friendogly.chat.domain.ChatRoom;
import com.happy.friendogly.chat.dto.request.ChatMessageSocketRequest;
import com.happy.friendogly.club.domain.Club;
import com.happy.friendogly.member.domain.Member;
import com.happy.friendogly.pet.domain.Gender;
import com.happy.friendogly.pet.domain.Pet;
import com.happy.friendogly.pet.domain.SizeType;
import com.happy.friendogly.support.ServiceTest;
import java.time.LocalDate;
import java.util.List;
import java.util.Set;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
Expand All @@ -28,8 +34,14 @@ class ChatCommandServiceTest extends ServiceTest {
@BeforeEach
void setUp() {
member = memberRepository.save(new Member("트레", "abcdef12", "https://image.com/image.jpg"));
chatRoom = chatRoomRepository.save(ChatRoom.createGroup(5));
chatRoom.addMember(member);
Pet pet = petRepository.save(
new Pet(member, "asda", "asfdfdsa", LocalDate.now().minusYears(1), SizeType.SMALL, Gender.MALE,
"https://image.com"));
Club club = clubRepository.save(Club.create(
"모임", "모임입니다.", "서울특별시", "구구구", "동동동",
5, member, Set.of(Gender.MALE), Set.of(SizeType.SMALL), "https://image.com", List.of(pet)
));
chatRoom = club.getChatRoom();
}

@DisplayName("채팅 입장 메시지를 DB에 저장한다.")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@
import com.happy.friendogly.chat.dto.request.SaveChatRoomRequest;
import com.happy.friendogly.chat.dto.response.SaveChatRoomResponse;
import com.happy.friendogly.club.domain.Club;
import com.happy.friendogly.club.dto.request.SaveClubMemberRequest;
import com.happy.friendogly.club.service.ClubCommandService;
import com.happy.friendogly.member.domain.Member;
import com.happy.friendogly.pet.domain.Gender;
import com.happy.friendogly.pet.domain.Pet;
import com.happy.friendogly.pet.domain.SizeType;
import com.happy.friendogly.support.ServiceTest;
Expand Down Expand Up @@ -79,12 +81,22 @@ void save_AlreadyExists() {
@Test
void leave() {
// given
ChatRoom chatRoom = chatRoomRepository.save(ChatRoom.createGroup(5));
Member member3 = memberRepository.save(new Member("john", "aaa111ab", "https://image.com"));
Pet pet = petRepository.save(
new Pet(member1, "asda", "asfdfdsa", LocalDate.now().minusYears(1), SizeType.SMALL, Gender.MALE,
"https://image.com"));

Club club = clubRepository.save(Club.create(
"모임", "모임입니다.", "서울특별시", "구구구", "동동동",
5, member1, Set.of(Gender.MALE), Set.of(SizeType.SMALL), "https://image.com", List.of(pet)
));

chatRoom.addMember(member1);
chatRoom.addMember(member2);
chatRoom.addMember(member3);
Pet pet2 = petRepository.save(
new Pet(member2, "asda", "asfdfdsa", LocalDate.now().minusYears(1), SizeType.SMALL, Gender.MALE,
"https://image.com"));

clubCommandService.joinClub(club.getId(), member2.getId(), new SaveClubMemberRequest(List.of(pet2.getId())));

ChatRoom chatRoom = club.getChatRoom();

// when
chatRoomCommandService.leave(member2.getId(), chatRoom.getId());
Expand All @@ -93,8 +105,7 @@ void leave() {
ChatRoom foundChatRoom = chatRoomRepository.getById(chatRoom.getId());
assertAll(
() -> assertThat(foundChatRoom.containsMember(member1)).isTrue(),
() -> assertThat(foundChatRoom.containsMember(member2)).isFalse(),
() -> assertThat(foundChatRoom.containsMember(member3)).isTrue()
() -> assertThat(foundChatRoom.containsMember(member2)).isFalse()
);
}

Expand Down

0 comments on commit 1f37c3b

Please sign in to comment.