Skip to content

Commit

Permalink
Merge pull request #621 from woowacourse-teams/refactor/#564
Browse files Browse the repository at this point in the history
푸시 알림 기능 리팩토링 및 알림 선택 구독 기능 추가
  • Loading branch information
pricelees authored Oct 15, 2024
2 parents 6765ded + f60d06d commit 0c432a1
Show file tree
Hide file tree
Showing 134 changed files with 3,300 additions and 1,730 deletions.
14 changes: 11 additions & 3 deletions backend/src/main/java/mouda/backend/chat/business/ChatService.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import mouda.backend.chat.domain.Chats;
import mouda.backend.chat.implement.ChatRoomFinder;
import mouda.backend.chat.implement.ChatWriter;
import mouda.backend.chat.implement.sender.ChatNotificationSender;
import mouda.backend.chat.presentation.request.ChatCreateRequest;
import mouda.backend.chat.presentation.request.DateTimeConfirmRequest;
import mouda.backend.chat.presentation.request.LastReadChatRequest;
Expand All @@ -20,6 +21,7 @@
import mouda.backend.moim.domain.Moim;
import mouda.backend.moim.implement.finder.MoimFinder;
import mouda.backend.moim.implement.writer.MoimWriter;
import mouda.backend.notification.domain.NotificationType;

@Service
@Transactional
Expand All @@ -30,6 +32,7 @@ public class ChatService {
private final ChatWriter chatWriter;
private final MoimWriter moimWriter;
private final MoimFinder moimFinder;
private final ChatNotificationSender chatNotificationSender;

public void createChat(
long darakbangId,
Expand All @@ -40,7 +43,9 @@ public void createChat(
ChatRoom chatRoom = chatRoomFinder.read(darakbangId, chatRoomId, darakbangMember);

chatWriter.append(chatRoom.getId(), request.content(), darakbangMember);
// 알림을 발생한다.
Moim moim = moimFinder.read(chatRoom.getTargetId(), darakbangId);

chatNotificationSender.sendChatNotification(moim, darakbangMember, NotificationType.NEW_CHAT, chatRoomId);
}

@Transactional(readOnly = true)
Expand All @@ -63,7 +68,9 @@ public void confirmPlace(long darakbangId, long chatRoomId, PlaceConfirmRequest
moimWriter.confirmPlace(moim, darakbangMember, request.place());

chatWriter.appendPlaceTypeChat(chatRoom.getId(), request.place(), darakbangMember);
// notificationService.notifyToMembers(NotificationType.MOIM_PLACE_CONFIRMED, darakbangId, moim, darakbangMember);

chatNotificationSender.sendChatNotification(moim, darakbangMember, NotificationType.MOIM_PLACE_CONFIRMED,
chatRoomId);
}

public void confirmDateTime(long darakbangId, long chatRoomId, DateTimeConfirmRequest request,
Expand All @@ -75,7 +82,8 @@ public void confirmDateTime(long darakbangId, long chatRoomId, DateTimeConfirmRe

chatWriter.appendDateTimeTypeChat(chatRoom.getId(), request.date(), request.time(), darakbangMember);

// notificationService.notifyToMembers(NotificationType.MOIM_TIME_CONFIRMED, darakbangId, moim, darakbangMember);
chatNotificationSender.sendChatNotification(moim, darakbangMember, NotificationType.MOIM_TIME_CONFIRMED,
chatRoomId);
}

public void updateLastReadChat(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package mouda.backend.chat.implement.sender;

import java.util.List;

import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.stereotype.Component;

import lombok.RequiredArgsConstructor;
import mouda.backend.common.config.UrlConfig;
import mouda.backend.darakbangmember.domain.DarakbangMember;
import mouda.backend.moim.domain.Moim;
import mouda.backend.moim.implement.finder.ChatRecipientFinder;
import mouda.backend.notification.domain.NotificationEvent;
import mouda.backend.notification.domain.NotificationType;
import mouda.backend.notification.domain.Recipient;

@Component
@EnableConfigurationProperties(UrlConfig.class)
@RequiredArgsConstructor
public class ChatNotificationSender {

private final UrlConfig urlConfig;
private final ChatRecipientFinder chatRecipientFinder;
private final ApplicationEventPublisher eventPublisher;

public void sendChatNotification(Moim moim, DarakbangMember sender, NotificationType notificationType, long chatRoomId) {
List<Recipient> recipients = chatRecipientFinder.getChatNotificationRecipients(moim.getId(), sender);
NotificationEvent notificationEvent = createNotificationEvent(moim, sender, notificationType, recipients, chatRoomId);

eventPublisher.publishEvent(notificationEvent);
}

private NotificationEvent createNotificationEvent(Moim moim, DarakbangMember sender, NotificationType notificationType, List<Recipient> recipients, long chatRoomId) {
String message;
if (notificationType.isConfirmedType()) {
message = notificationType.createMessage(moim.getTitle());
} else {
message = notificationType.createMessage(sender.getNickname());
}

return new NotificationEvent(
notificationType, moim.getTitle(), message, urlConfig.getChatRoomUrl(moim.getDarakbangId(), chatRoomId), recipients, moim.getDarakbangId(), chatRoomId);
}
}
25 changes: 25 additions & 0 deletions backend/src/main/java/mouda/backend/common/config/UrlConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package mouda.backend.common.config;

import org.springframework.boot.context.properties.ConfigurationProperties;

import lombok.Getter;
import lombok.RequiredArgsConstructor;

@ConfigurationProperties(prefix = "url")
@Getter
@RequiredArgsConstructor
public class UrlConfig {

private final String base;
private final String moim;
private final String chat;
private final String chatroom;

public String getChatRoomUrl(long darakbangId, long chatRoomId) {
return base + String.format(chatroom, darakbangId, chatRoomId);
}

public String getMoimUrl(long darakbangId, long moimId) {
return base + String.format(moim, darakbangId, moimId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,14 @@ public DarakbangMember updateMyInfo(String nickname, String description, String
return this;
}

public boolean isSameMemberWith(DarakbangMember other) {
return id.equals(other.getId());
}

public boolean isNotSameMemberWith(DarakbangMember other) {
return !isSameMemberWith(other);
}

@Override
public boolean equals(Object o) {
if (this == o)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@
import mouda.backend.moim.domain.MoimRole;
import mouda.backend.moim.implement.finder.ChamyoFinder;
import mouda.backend.moim.implement.finder.MoimFinder;
import mouda.backend.moim.implement.sender.ChamyoNotificationSender;
import mouda.backend.moim.implement.writer.ChamyoWriter;
import mouda.backend.moim.implement.writer.MoimWriter;
import mouda.backend.moim.presentation.response.chamyo.ChamyoFindAllResponses;
import mouda.backend.moim.presentation.response.chamyo.MoimRoleFindResponse;
import mouda.backend.notification.business.NotificationService;
import mouda.backend.notification.domain.NotificationType;

@Service
Expand All @@ -28,7 +28,7 @@ public class ChamyoService {
private final MoimWriter moimWriter;
private final ChamyoFinder chamyoFinder;
private final ChamyoWriter chamyoWriter;
private final NotificationService notificationService;
private final ChamyoNotificationSender chamyoNotificationSender;

@Transactional(readOnly = true)
public MoimRoleFindResponse findMoimRole(Long darakbangId, Long moimId, DarakbangMember darakbangMember) {
Expand All @@ -48,23 +48,17 @@ public ChamyoFindAllResponses findAllChamyo(Long darakbangId, Long moimId) {

public void chamyoMoim(Long darakbangId, Long moimId, DarakbangMember darakbangMember) {
Moim moim = moimFinder.read(moimId, darakbangId);
chamyoWriter.saveAsMoimee(moim, darakbangMember);
Chamyo chamyo = chamyoWriter.saveAsMoimee(moim, darakbangMember);
moimWriter.updateMoimStatusIfFull(moim);

notificationService.notifyToMembers(NotificationType.NEW_MOIMEE_JOINED, darakbangId, moim, darakbangMember);
chamyoNotificationSender.sendChamyoNotification(moimId, darakbangMember, NotificationType.NEW_MOIMEE_JOINED);
}

public void cancelChamyo(Long darakbangId, Long moimId, DarakbangMember darakbangMember) {
Moim moim = moimFinder.read(moimId, darakbangId);
Chamyo chamyo = chamyoFinder.read(moim, darakbangMember);
chamyoWriter.delete(chamyo);

sendCancelNotification(darakbangId, darakbangMember, moim);
}

private void sendCancelNotification(Long darakbangId, DarakbangMember darakbangMember, Moim moim) {
if (moim.isCompleted()) {
notificationService.notifyToMembers(NotificationType.MOIMEE_LEFT, darakbangId, moim, darakbangMember);
}
chamyoNotificationSender.sendChamyoNotification(moimId, darakbangMember, NotificationType.MOIMEE_LEFT);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import mouda.backend.moim.implement.finder.ChatFinder;
import mouda.backend.moim.implement.finder.ChatRoomFinder;
import mouda.backend.moim.implement.finder.MoimFinder;
import mouda.backend.moim.implement.sender.OldChatNotificationSender;
import mouda.backend.moim.implement.validator.ChamyoValidator;
import mouda.backend.moim.implement.writer.ChatWriter;
import mouda.backend.moim.implement.writer.MoimWriter;
Expand All @@ -27,22 +28,21 @@
import mouda.backend.moim.presentation.request.chat.OldPlaceConfirmRequest;
import mouda.backend.moim.presentation.response.chat.OldChatFindUnloadedResponse;
import mouda.backend.moim.presentation.response.chat.OldChatPreviewResponses;
import mouda.backend.notification.business.NotificationService;
import mouda.backend.notification.domain.NotificationType;

@Transactional
@Service(value = "oldChatService")
@RequiredArgsConstructor
public class ChatService {

private final NotificationService notificationService;
private final MoimFinder moimFinder;
private final MoimWriter moimWriter;
private final ChatFinder chatFinder;
private final ChatWriter chatWriter;
private final ChamyoValidator chamyoValidator;
private final ChamyoFinder chamyoFinder;
private final ChatRoomFinder chatRoomFinder;
private final OldChatNotificationSender oldChatNotificationSender;

public void createChat(long darakbangId, OldChatCreateRequest oldChatCreateRequest, DarakbangMember darakbangMember) {
Moim moim = moimFinder.read(oldChatCreateRequest.moimId(), darakbangId);
Expand All @@ -51,7 +51,7 @@ public void createChat(long darakbangId, OldChatCreateRequest oldChatCreateReque
Chat chat = oldChatCreateRequest.toEntity(moim, darakbangMember);
chatWriter.save(chat);

notificationService.notifyToMembers(NotificationType.NEW_CHAT, darakbangId, moim, darakbangMember);
oldChatNotificationSender.sendChatNotification(moim, darakbangMember, NotificationType.NEW_CHAT);
}

@Transactional(readOnly = true)
Expand All @@ -76,7 +76,7 @@ public void confirmPlace(
Chat chat = request.toEntity(moim, darakbangMember);
chatWriter.save(chat);

notificationService.notifyToMembers(NotificationType.MOIM_PLACE_CONFIRMED, darakbangId, moim, darakbangMember);
oldChatNotificationSender.sendChatNotification(moim, darakbangMember, NotificationType.MOIM_PLACE_CONFIRMED);
}

public void confirmDateTime(
Expand All @@ -88,7 +88,7 @@ public void confirmDateTime(
Chat chat = request.toEntity(moim, darakbangMember);
chatWriter.save(chat);

notificationService.notifyToMembers(NotificationType.MOIM_TIME_CONFIRMED, darakbangId, moim, darakbangMember);
oldChatNotificationSender.sendChatNotification(moim, darakbangMember, NotificationType.MOIM_TIME_CONFIRMED);
}

public OldChatPreviewResponses findChatPreview(long darakbangId, DarakbangMember darakbangMember) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,46 +5,28 @@

import lombok.RequiredArgsConstructor;
import mouda.backend.darakbangmember.domain.DarakbangMember;
import mouda.backend.moim.domain.Comment;
import mouda.backend.moim.domain.Moim;
import mouda.backend.moim.domain.MoimRole;
import mouda.backend.moim.implement.finder.ChamyoFinder;
import mouda.backend.moim.implement.finder.CommentFinder;
import mouda.backend.moim.implement.finder.MoimFinder;
import mouda.backend.moim.implement.sender.CommentNotificationSender;
import mouda.backend.moim.implement.writer.CommentWriter;
import mouda.backend.moim.presentation.request.comment.CommentCreateRequest;
import mouda.backend.notification.business.NotificationService;
import mouda.backend.notification.domain.NotificationType;

@Transactional
@Service
@RequiredArgsConstructor
public class CommentService {

private final MoimFinder moimFinder;
private final ChamyoFinder chamyoFinder;
private final CommentFinder commentFinder;
private final CommentWriter commentWriter;
private final NotificationService notificationService;
private final CommentNotificationSender commentNotificationSender;

public void createComment(
Long darakbangId, Long moimId, DarakbangMember darakbangMember, CommentCreateRequest request
) {
Moim moim = moimFinder.read(moimId, darakbangId);
commentWriter.saveComment(moim, darakbangMember, request.parentId(), request.content());
Comment comment = commentWriter.saveComment(moim, darakbangMember, request.parentId(), request.content());

sendCommentNotification(moim, darakbangMember, request.parentId(), darakbangId);
}

private void sendCommentNotification(Moim moim, DarakbangMember author, Long parentId, Long darakbangId) {
if (chamyoFinder.readMoimRole(moim, author) == MoimRole.MOIMER) {
return;
}
if (parentId != null) {
Long parentCommentAuthorId = commentFinder.readMemberIdByParentId(parentId);
notificationService.notifyToMember(NotificationType.NEW_REPLY, darakbangId, moim, author,
parentCommentAuthorId);
}

notificationService.notifyToMembers(NotificationType.NEW_COMMENT, darakbangId, moim, author);
commentNotificationSender.sendCommentNotification(comment, darakbangMember);
}
}
27 changes: 13 additions & 14 deletions backend/src/main/java/mouda/backend/moim/business/MoimService.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@
import mouda.backend.moim.domain.ParentComment;
import mouda.backend.moim.implement.finder.CommentFinder;
import mouda.backend.moim.implement.finder.MoimFinder;
import mouda.backend.moim.implement.sender.MoimNotificationSender;
import mouda.backend.moim.implement.writer.MoimWriter;
import mouda.backend.moim.presentation.request.moim.MoimCreateRequest;
import mouda.backend.moim.presentation.request.moim.MoimEditRequest;
import mouda.backend.moim.presentation.response.comment.CommentResponses;
import mouda.backend.moim.presentation.response.moim.MoimDetailsFindResponse;
import mouda.backend.moim.presentation.response.moim.MoimFindAllResponses;
import mouda.backend.notification.business.NotificationService;
import mouda.backend.notification.domain.NotificationType;

@Transactional
Expand All @@ -30,7 +30,7 @@ public class MoimService {
private final MoimWriter moimWriter;
private final MoimFinder moimFinder;
private final CommentFinder commentFinder;
private final NotificationService notificationService;
private final MoimNotificationSender moimNotificationSender;

@Transactional(readOnly = true)
public MoimDetailsFindResponse findMoimDetails(long darakbangId, long moimId) {
Expand Down Expand Up @@ -63,40 +63,39 @@ public MoimFindAllResponses findZzimedMoim(DarakbangMember darakbangMember) {
return MoimFindAllResponses.toResponse(moimOverviews);
}

public void completeMoim(Long darakbangId, Long moimId, DarakbangMember darakbangMember) {
Moim moim = moimFinder.read(moimId, darakbangId);
moimWriter.completeMoim(moim, darakbangMember);

notificationService.notifyToMembers(NotificationType.MOIMING_COMPLETED, darakbangId, moim, darakbangMember);
}

public Moim createMoim(Long darakbangId, DarakbangMember darakbangMember, MoimCreateRequest moimCreateRequest) {
Moim moim = moimWriter.save(moimCreateRequest.toEntity(darakbangId), darakbangMember);

notificationService.notifyToMembers(NotificationType.MOIM_CREATED, darakbangId, moim, darakbangMember);

moimNotificationSender.sendMoimCreatedNotification(moim, darakbangMember, NotificationType.MOIM_CREATED);
return moim;
}

public void completeMoim(Long darakbangId, Long moimId, DarakbangMember darakbangMember) {
Moim moim = moimFinder.read(moimId, darakbangId);
moimWriter.completeMoim(moim, darakbangMember);

moimNotificationSender.sendMoimStatusChangedNotification(moim, NotificationType.MOIMING_COMPLETED);
}

public void cancelMoim(Long darakbangId, Long moimId, DarakbangMember darakbangMember) {
Moim moim = moimFinder.read(moimId, darakbangId);
moimWriter.cancelMoim(moim, darakbangMember);

notificationService.notifyToMembers(NotificationType.MOIM_CANCELLED, darakbangId, moim, darakbangMember);
moimNotificationSender.sendMoimStatusChangedNotification(moim, NotificationType.MOIM_CANCELLED);
}

public void reopenMoim(Long darakbangId, Long moimId, DarakbangMember darakbangMember) {
Moim moim = moimFinder.read(moimId, darakbangId);
moimWriter.reopenMoim(moim, darakbangMember);

notificationService.notifyToMembers(NotificationType.MOINING_REOPENED, darakbangId, moim, darakbangMember);
moimNotificationSender.sendMoimStatusChangedNotification(moim, NotificationType.MOINING_REOPENED);
}

public void editMoim(Long darakbangId, MoimEditRequest request, DarakbangMember darakbangMember) {
Moim moim = moimFinder.read(request.moimId(), darakbangId);
moimWriter.updateMoim(moim, darakbangMember, request.title(), request.date(), request.time(), request.place(),
request.maxPeople(), request.description());

notificationService.notifyToMembers(NotificationType.MOIM_MODIFIED, darakbangId, moim, darakbangMember);
moimNotificationSender.sendMoimStatusChangedNotification(moim, NotificationType.MOIM_MODIFIED);
}
}
4 changes: 2 additions & 2 deletions backend/src/main/java/mouda/backend/moim/domain/Comment.java
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,11 @@ private void validateMember(DarakbangMember darakbangMember) {
}
}

public boolean isParent() {
public boolean isComment() {
return parentId == null;
}

public boolean isChild() {
public boolean isReply() {
return parentId != null;
}

Expand Down
Loading

0 comments on commit 0c432a1

Please sign in to comment.