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] refactor: 알림 서비스 다른 도메인 의존성 제거 #740

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from
Open
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 @@ -4,20 +4,20 @@
import static com.happy.friendogly.chatsocket.domain.MessageType.ENTER;
import static com.happy.friendogly.chatsocket.domain.MessageType.LEAVE;

import com.happy.friendogly.chatsocket.template.ChatTemplate;
import com.happy.friendogly.chatmessage.domain.ChatMessage;
import com.happy.friendogly.chatmessage.repository.ChatMessageRepository;
import com.happy.friendogly.chatroom.domain.ChatRoom;
import com.happy.friendogly.chatroom.repository.ChatRoomRepository;
import com.happy.friendogly.chatsocket.domain.MessageType;
import com.happy.friendogly.chatsocket.dto.request.ChatMessageSocketRequest;
import com.happy.friendogly.chatsocket.dto.response.ChatMessageSocketResponse;
import com.happy.friendogly.chatmessage.repository.ChatMessageRepository;
import com.happy.friendogly.chatroom.repository.ChatRoomRepository;
import com.happy.friendogly.chatsocket.template.ChatTemplate;
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;
import com.happy.friendogly.notification.service.NotificationService;
import com.happy.friendogly.notification.service.ChatNotificationService;
import java.time.LocalDateTime;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
Expand All @@ -32,22 +32,22 @@ public class ChatSocketCommandService {
private final ClubRepository clubRepository;
private final ChatRoomRepository chatRoomRepository;
private final ChatMessageRepository chatMessageRepository;
private final NotificationService notificationService;
private final ChatNotificationService chatNotificationService;
private final ChatTemplate template;

public ChatSocketCommandService(
MemberRepository memberRepository,
ClubRepository clubRepository,
ChatRoomRepository chatRoomRepository,
ChatMessageRepository chatMessageRepository,
NotificationService notificationService,
ChatNotificationService chatNotificationService,
ChatTemplate template
) {
this.memberRepository = memberRepository;
this.clubRepository = clubRepository;
this.chatRoomRepository = chatRoomRepository;
this.chatMessageRepository = chatMessageRepository;
this.notificationService = notificationService;
this.chatNotificationService = chatNotificationService;
this.template = template;
}

Expand Down Expand Up @@ -79,7 +79,7 @@ private void sendAndSave(MessageType messageType, String content, ChatRoom chatR
messageType, content, senderMember, LocalDateTime.now());
Club club = clubRepository.getByChatRoomId(chatRoom.getId());

notificationService.sendChatNotification(chatRoom.getId(), chat, club);
chatNotificationService.sendChatNotification(chatRoom.getId(), chat, club);
template.convertAndSend(chatRoom.getId(), chat);
chatMessageRepository.save(new ChatMessage(chatRoom, messageType, senderMember, content));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package com.happy.friendogly.notification.service;

import static com.happy.friendogly.notification.domain.NotificationType.CHAT;

import com.happy.friendogly.chatsocket.dto.response.ChatMessageSocketResponse;
import com.happy.friendogly.club.domain.Club;
import com.happy.friendogly.notification.repository.DeviceTokenRepository;
import java.util.List;
import java.util.Map;
import org.springframework.stereotype.Service;

@Service
public class ChatNotificationService {

private final DeviceTokenRepository deviceTokenRepository;
private final NotificationService notificationService;

public ChatNotificationService(
DeviceTokenRepository deviceTokenRepository,
NotificationService notificationService
) {
this.deviceTokenRepository = deviceTokenRepository;
this.notificationService = notificationService;
}

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

Map<String, String> data = Map.of(
"chatRoomId", chatRoomId.toString(),
"messageType", response.messageType().toString(),
"senderMemberId", response.senderMemberId().toString(),
"senderName", response.senderName(),
"content", response.content(),
"createdAt", response.createdAt().toString(),
"profilePictureUrl", response.profilePictureUrl() == null ? "" : response.profilePictureUrl(),
"clubPictureUrl", club.getImageUrl() == null ? "" : club.getImageUrl(),
Comment on lines +37 to +38
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

image

아하 이런 문제가 있었군요 !!!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

테스트가 터져서 확인해보니 해당부분 문제가 보이더라구요~!

"clubTitle", club.getTitle().getValue()
);

notificationService.sendNotification("채팅", data, CHAT, receiverTokens);
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package com.happy.friendogly.notification.service;

import com.happy.friendogly.chatsocket.dto.response.ChatMessageSocketResponse;
import com.happy.friendogly.club.domain.Club;
import com.happy.friendogly.notification.domain.NotificationType;
import java.util.List;
import java.util.Map;
import org.springframework.context.annotation.Profile;
import org.springframework.stereotype.Service;

Expand All @@ -11,22 +11,22 @@
public class FakeNotificationService implements NotificationService {

@Override
public void sendFootprintNotification(String title, String content, String receiverToken) {
public void sendNotification(
String title,
String content,
NotificationType notificationType,
List<String> receiverTokens
) {

}

@Override
public void sendFootprintNotification(String title, String content, List<String> receiverTokens) {

}

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

}

@Override
public void sendPlaygroundJoinNotification(String title, String content, List<String> receiverTokens) {
public void sendNotification(
String title,
Map<String, String> contents,
NotificationType notificationType,
List<String> receiverTokens
) {

}
}
Original file line number Diff line number Diff line change
@@ -1,19 +1,13 @@
package com.happy.friendogly.notification.service;

import static com.happy.friendogly.notification.domain.NotificationType.CHAT;
import static com.happy.friendogly.notification.domain.NotificationType.FOOTPRINT;
import static com.happy.friendogly.notification.domain.NotificationType.PLAYGROUND;
import static org.springframework.http.HttpStatus.INTERNAL_SERVER_ERROR;

import com.google.firebase.FirebaseApp;
import com.google.firebase.messaging.FirebaseMessaging;
import com.google.firebase.messaging.FirebaseMessagingException;
import com.google.firebase.messaging.MulticastMessage;
import com.happy.friendogly.chatsocket.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;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
Expand All @@ -27,72 +21,31 @@
public class FcmNotificationService implements NotificationService {

private final FirebaseMessaging firebaseMessaging;
private final DeviceTokenRepository deviceTokenRepository;

public FcmNotificationService(
@Autowired FirebaseApp firebaseApp,
DeviceTokenRepository deviceTokenRepository
) {
public FcmNotificationService(@Autowired FirebaseApp firebaseApp) {
this.firebaseMessaging = FirebaseMessaging.getInstance(firebaseApp);
this.deviceTokenRepository = deviceTokenRepository;
}

@Override
public void sendFootprintNotification(String title, String content, String receiverToken) {
Map<String, String> data = Map.of(
"body", content
);

sendNotificationWithType(FOOTPRINT, title, data, List.of(receiverToken));
}

@Override
public void sendFootprintNotification(String title, String content, List<String> receiverTokens) {
Map<String, String> data = Map.of(
"body", content
);

sendNotificationWithType(FOOTPRINT, title, data, receiverTokens);
}

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

Map<String, String> data = Map.of(
"chatRoomId", chatRoomId.toString(),
"messageType", response.messageType().toString(),
"senderMemberId", response.senderMemberId().toString(),
"senderName", response.senderName(),
"content", response.content(),
"createdAt", response.createdAt().toString(),
"profilePictureUrl", response.profilePictureUrl(),
"clubPictureUrl", club.getImageUrl(),
"clubTitle", club.getTitle().getValue()
);

sendNotificationWithType(CHAT, "채팅", data, receiverTokens);
public void sendNotification(
String title,
String content,
NotificationType notificationType,
List<String> receiverTokens
) {
sendNotification(title, Map.of("body", content), notificationType, receiverTokens);
}

@Override
public void sendPlaygroundJoinNotification(String title, String content, List<String> receiverTokens) {
Map<String, String> data = Map.of(
"body", content
);

sendNotificationWithType(PLAYGROUND, title, data, receiverTokens);
}

private void sendNotificationWithType(
NotificationType notificationType,
public void sendNotification(
String title,
Map<String, String> data,
Map<String, String> contents,
NotificationType notificationType,
List<String> receiverTokens
) {
if (!receiverTokens.isEmpty()) {
MulticastMessage message = MulticastMessage.builder()
.putAllData(data)
.putAllData(contents)
.putData("type", notificationType.toString())
.putData("title", title)
.addAllTokens(receiverTokens)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.happy.friendogly.notification.service;

import static com.happy.friendogly.notification.domain.NotificationType.FOOTPRINT;

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

static import는 제거하는게 어떨까요?
클래스 내부에 선언된 private 상수인지, 공통적으로 사용하는 enum인지 알아보기 어려워지는 것 같습니다.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

동의합니다

import com.happy.friendogly.footprint.domain.Footprint;
import com.happy.friendogly.footprint.domain.WalkStatus;
import com.happy.friendogly.notification.domain.DeviceToken;
Expand Down Expand Up @@ -35,28 +37,31 @@ public void sendWalkNotificationToMe(Long memberId, WalkStatus currentWalkStatus
return;
}

notificationService.sendFootprintNotification(
notificationService.sendNotification(
DEFAULT_TITLE,
content,
deviceTokenRepository.getByMemberId(memberId).getDeviceToken()
FOOTPRINT,
List.of(deviceTokenRepository.getByMemberId(memberId).getDeviceToken())
);
}

public void sendWalkComingNotification(String comingMemberName, List<Footprint> nearFootprints) {
List<String> nearDeviceTokens = toDeviceToken(nearFootprints);

notificationService.sendFootprintNotification(
notificationService.sendNotification(
DEFAULT_TITLE,
"내 산책 장소에 " + comingMemberName + "님도 산책온대요!",
FOOTPRINT,
nearDeviceTokens
);
}

public void sendWalkStartNotificationToNear(String startMemberName, List<Footprint> nearFootprints) {
List<String> nearDeviceTokens = toDeviceToken(nearFootprints);

notificationService.sendFootprintNotification(DEFAULT_TITLE,
notificationService.sendNotification(DEFAULT_TITLE,
"내 산책장소에 " + startMemberName + "님이 산책을 시작했어요!",
FOOTPRINT,
nearDeviceTokens
);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
package com.happy.friendogly.notification.service;

import com.happy.friendogly.chatsocket.dto.response.ChatMessageSocketResponse;
import com.happy.friendogly.club.domain.Club;
import com.happy.friendogly.notification.domain.NotificationType;
import java.util.List;
import java.util.Map;

public interface NotificationService {

void sendFootprintNotification(String title, String content, String receiverToken);
void sendNotification(
String title,
String content,
NotificationType notificationType,
List<String> receiverTokens
);

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

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

void sendPlaygroundJoinNotification(String title, String content, List<String> receiverTokens);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

오오 추상 메서드가 많이 줄어들었네요

void sendNotification(
String title,
Map<String, String> contents,
NotificationType notificationType,
List<String> receiverTokens
);
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.happy.friendogly.notification.service;

import static com.happy.friendogly.notification.domain.NotificationType.PLAYGROUND;

import com.happy.friendogly.notification.domain.DeviceToken;
import com.happy.friendogly.notification.repository.DeviceTokenRepository;
import com.happy.friendogly.playground.domain.PlaygroundMember;
Expand All @@ -15,19 +17,25 @@ public class PlaygroundNotificationService {
private final DeviceTokenRepository deviceTokenRepository;
private final NotificationService notificationService;

public PlaygroundNotificationService(DeviceTokenRepository deviceTokenRepository,
NotificationService notificationService) {
public PlaygroundNotificationService(
DeviceTokenRepository deviceTokenRepository,
NotificationService notificationService
) {
this.deviceTokenRepository = deviceTokenRepository;
this.notificationService = notificationService;
}

public void sendJoinNotification(String newParticipatingMember,
List<PlaygroundMember> existingParticipatingMembers) {
public void sendJoinNotification(
String newParticipatingMember,
List<PlaygroundMember> existingParticipatingMembers
) {
List<String> deviceTokens = toDeviceToken(existingParticipatingMembers);
String content = newParticipatingMember + "님이 놀이터에 참여했습니다";

notificationService.sendPlaygroundJoinNotification(DEFAULT_TITLE,
notificationService.sendNotification(
DEFAULT_TITLE,
Comment on lines +34 to +35
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

예전에 작성된 코드같긴 한데
하는 김에 DEFAULT_TITLE을 static으로 두면 어떨까요?

content,
PLAYGROUND,
deviceTokens
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.google.firebase.FirebaseApp;
import com.google.firebase.messaging.FirebaseMessaging;
import com.google.firebase.messaging.FirebaseMessagingException;
import com.happy.friendogly.notification.domain.NotificationType;
import java.util.List;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
Expand All @@ -31,7 +32,7 @@ class FcmNotificationServiceTest {
@Test
void sendFootprintNotification() throws FirebaseMessagingException {
// when
fcmNotificationService.sendFootprintNotification("title", "content", List.of());
fcmNotificationService.sendNotification("title", "content", NotificationType.FOOTPRINT, List.of());

// then
verify(firebaseMessaging, never()).sendEachForMulticast(any());
Expand Down
Loading