Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BE] feat: 채팅 알림에 모임 정보(모임 사진, 모임 이름) 추가 #706

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,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 @@ -28,19 +30,22 @@ public class ChatCommandService {
private static final String EMPTY_CONTENT = "";

private final MemberRepository memberRepository;
private final ClubRepository clubRepository;
private final ChatRoomRepository chatRoomRepository;
private final ChatMessageRepository chatMessageRepository;
private final NotificationService notificationService;
private final SimpMessagingTemplate template;

public ChatCommandService(
MemberRepository memberRepository,
ClubRepository clubRepository,
ChatRoomRepository chatRoomRepository,
ChatMessageRepository chatMessageRepository,
NotificationService notificationService,
SimpMessagingTemplate template
) {
this.memberRepository = memberRepository;
this.clubRepository = clubRepository;
this.chatRoomRepository = chatRoomRepository;
this.chatMessageRepository = chatMessageRepository;
this.notificationService = notificationService;
Expand Down Expand Up @@ -71,8 +76,11 @@ 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);
template.convertAndSend(TOPIC_CHAT_PREFIX + 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
Loading