Skip to content

Commit

Permalink
[BE] refactor: 알림 서비스 다른 도메인 의존성 제거 (#740)
Browse files Browse the repository at this point in the history
  • Loading branch information
ehtjsv2 authored Dec 3, 2024
1 parent 85e5177 commit c70acbf
Show file tree
Hide file tree
Showing 8 changed files with 117 additions and 101 deletions.
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(),
"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
Expand Up @@ -3,6 +3,7 @@
import com.happy.friendogly.footprint.domain.Footprint;
import com.happy.friendogly.footprint.domain.WalkStatus;
import com.happy.friendogly.notification.domain.DeviceToken;
import com.happy.friendogly.notification.domain.NotificationType;
import com.happy.friendogly.notification.repository.DeviceTokenRepository;
import java.util.List;
import java.util.Optional;
Expand All @@ -11,7 +12,8 @@
@Service
public class FootprintNotificationService {

private final String DEFAULT_TITLE = "반갑개";
private static final String DEFAULT_TITLE = "반갑개";

private final NotificationService notificationService;
private final DeviceTokenRepository deviceTokenRepository;

Expand All @@ -35,28 +37,31 @@ public void sendWalkNotificationToMe(Long memberId, WalkStatus currentWalkStatus
return;
}

notificationService.sendFootprintNotification(
notificationService.sendNotification(
DEFAULT_TITLE,
content,
deviceTokenRepository.getByMemberId(memberId).getDeviceToken()
NotificationType.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 + "님도 산책온대요!",
NotificationType.FOOTPRINT,
nearDeviceTokens
);
}

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

notificationService.sendFootprintNotification(DEFAULT_TITLE,
notificationService.sendNotification(DEFAULT_TITLE,
"내 산책장소에 " + startMemberName + "님이 산책을 시작했어요!",
NotificationType.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);
void sendNotification(
String title,
Map<String, String> contents,
NotificationType notificationType,
List<String> receiverTokens
);
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.happy.friendogly.notification.service;

import com.happy.friendogly.notification.domain.DeviceToken;
import com.happy.friendogly.notification.domain.NotificationType;
import com.happy.friendogly.notification.repository.DeviceTokenRepository;
import com.happy.friendogly.playground.domain.PlaygroundMember;
import java.util.List;
Expand All @@ -10,24 +11,30 @@
@Service
public class PlaygroundNotificationService {

private final String DEFAULT_TITLE = "반갑개";
private static final String DEFAULT_TITLE = "반갑개";

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,
content,
NotificationType.PLAYGROUND,
deviceTokens
);
}
Expand Down
Loading

0 comments on commit c70acbf

Please sign in to comment.