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

채팅 알림 메시지 형식 수정 #651

Merged
merged 9 commits into from
Oct 16, 2024
Merged
4 changes: 4 additions & 0 deletions backend/src/main/java/mouda/backend/bet/domain/Bet.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ public long getId() {
return betDetails.getId();
}

public String getTitle() {
return betDetails.getTitle();
}

public long timeDifferenceInMinutesWithNow() {
return betDetails.timeDifferenceInMinutesWithNow();
}
Expand Down
29 changes: 18 additions & 11 deletions backend/src/main/java/mouda/backend/chat/business/ChatService.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package mouda.backend.chat.business;

import java.time.LocalDate;
import java.time.LocalTime;
import java.util.List;

import org.springframework.stereotype.Service;
Expand All @@ -17,6 +19,7 @@
import mouda.backend.chat.presentation.request.LastReadChatRequest;
import mouda.backend.chat.presentation.request.PlaceConfirmRequest;
import mouda.backend.chat.presentation.response.ChatFindUnloadedResponse;
import mouda.backend.chat.util.DateTimeFormatter;
import mouda.backend.darakbangmember.domain.DarakbangMember;
import mouda.backend.moim.domain.Moim;
import mouda.backend.moim.implement.finder.MoimFinder;
Expand All @@ -42,10 +45,11 @@ public void createChat(
) {
ChatRoom chatRoom = chatRoomFinder.read(darakbangId, chatRoomId, darakbangMember);

chatWriter.append(chatRoom.getId(), request.content(), darakbangMember);
Moim moim = moimFinder.read(chatRoom.getTargetId(), darakbangId);
String content = request.content();
chatWriter.append(chatRoom.getId(), content, darakbangMember);

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

@Transactional(readOnly = true)
Expand All @@ -63,27 +67,30 @@ public ChatFindUnloadedResponse findUnloadedChats(
public void confirmPlace(long darakbangId, long chatRoomId, PlaceConfirmRequest request,
DarakbangMember darakbangMember) {
ChatRoom chatRoom = chatRoomFinder.readMoimChatRoom(darakbangId, chatRoomId);
String place = request.place();

Moim moim = moimFinder.read(chatRoom.getTargetId(), darakbangId);
moimWriter.confirmPlace(moim, darakbangMember, request.place());
moimWriter.confirmPlace(moim, darakbangMember, place);

chatWriter.appendPlaceTypeChat(chatRoom.getId(), request.place(), darakbangMember);
chatWriter.appendPlaceTypeChat(chatRoom.getId(), place, darakbangMember);

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

public void confirmDateTime(long darakbangId, long chatRoomId, DateTimeConfirmRequest request,
DarakbangMember darakbangMember) {
ChatRoom chatRoom = chatRoomFinder.readMoimChatRoom(darakbangId, chatRoomId);

Moim moim = moimFinder.read(chatRoom.getTargetId(), darakbangId);
moimWriter.confirmDateTime(moim, darakbangMember, request.date(), request.time());
LocalDate date = request.date();
LocalTime time = request.time();
moimWriter.confirmDateTime(moim, darakbangMember, date, time);

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

chatNotificationSender.sendChatNotification(moim, darakbangMember, NotificationType.MOIM_TIME_CONFIRMED,
chatRoomId);
chatNotificationSender.sendChatNotification(darakbangId, chatRoom, DateTimeFormatter.formatDateTime(date, time),
darakbangMember, NotificationType.MOIM_TIME_CONFIRMED);
}

public void updateLastReadChat(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,24 @@

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

import lombok.RequiredArgsConstructor;
import mouda.backend.bet.domain.Bet;
import mouda.backend.bet.implement.BetFinder;
import mouda.backend.chat.domain.ChatRoom;
import mouda.backend.chat.domain.ChatRoomType;
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.moim.implement.finder.MoimFinder;
import mouda.backend.notification.domain.NotificationEvent;
import mouda.backend.notification.domain.NotificationType;
import mouda.backend.notification.domain.Recipient;
import mouda.backend.notification.exception.NotificationErrorMessage;
import mouda.backend.notification.exception.NotificationException;

@Component
@EnableConfigurationProperties(UrlConfig.class)
Expand All @@ -23,23 +31,78 @@ public class ChatNotificationSender {
private final UrlConfig urlConfig;
private final ChatRecipientFinder chatRecipientFinder;
private final ApplicationEventPublisher eventPublisher;
private final MoimFinder moimFinder;
private final BetFinder betFinder;

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);
public void sendChatNotification(
long darakbangId, ChatRoom chatRoom, String content, DarakbangMember sender, NotificationType notificationType
Copy link
Contributor

Choose a reason for hiding this comment

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

DarakbangMember이 알림을 받는 사람일까요? 그렇다면 receiver가 맞을 것 같습니다 😀

) {
ChatRoomType chatRoomType = chatRoom.getType();
long chatRoomId = chatRoom.getId();
if (chatRoomType == ChatRoomType.BET) {
Bet bet = betFinder.find(darakbangId, chatRoom.getTargetId());
Copy link
Contributor

Choose a reason for hiding this comment

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

read로 바꾸면 좋을 것 같아용

sendBetNotification(bet, content, sender, notificationType);
return;
}
Moim moim = moimFinder.read(chatRoom.getTargetId(), darakbangId);
sendMoimNotification(moim, content, sender, notificationType, chatRoomId);
}

private void sendBetNotification(
Bet bet, String content, DarakbangMember sender, NotificationType notificationType
) {
List<Recipient> recipients = chatRecipientFinder.getBetChatNotificationRecipients(bet.getId(), sender);
long darakbangId = bet.getDarakbangId();
long chatRoomId = bet.getId();

publishEvent(notificationType, bet.getTitle(), content, sender, recipients, darakbangId, chatRoomId);
}

private void sendMoimNotification(
Moim moim, String content, DarakbangMember sender, NotificationType notificationType, long chatRoomId
) {
List<Recipient> recipients = chatRecipientFinder.getMoimChatNotificationRecipients(moim.getId(), sender);
long darakbangId = moim.getDarakbangId();

publishEvent(notificationType, moim.getTitle(), content, sender, recipients, darakbangId, chatRoomId);
}

private void publishEvent(
NotificationType notificationType, String title, String content, DarakbangMember sender,
List<Recipient> recipients, long darakbangId, long chatRoomId
) {
NotificationEvent notificationEvent = NotificationEvent.chatEvent(
notificationType,
title,
ChatNotificationMessage.create(content, sender, notificationType),
urlConfig.getChatRoomUrl(darakbangId, chatRoomId),
recipients,
darakbangId,
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());
static class ChatNotificationMessage {

public static String create(String content, DarakbangMember sender, NotificationType type) {
if (type.isConfirmedType()) {
return confirmedChatMessage(content, type);
}
if (type == NotificationType.NEW_CHAT) {
return sender.getNickname() + ": " + content;
}
throw new NotificationException(
HttpStatus.BAD_REQUEST, NotificationErrorMessage.NOT_ALLOWED_NOTIFICATION_TYPE
);
}

return new NotificationEvent(
notificationType, moim.getTitle(), message, urlConfig.getChatRoomUrl(moim.getDarakbangId(), chatRoomId), recipients, moim.getDarakbangId(), chatRoomId);
private static String confirmedChatMessage(String content, NotificationType type) {
if (type == NotificationType.MOIM_PLACE_CONFIRMED) {
return "장소가 '" + content + "' 로 확정되었어요!";
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
return "장소가 '" + content + "' 로 확정되었어요!";
return "장소가 '" + content + "' (으)로 확정되었어요!";

}
return "시간이 '" + content + "' 로 확정되었어요!";
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package mouda.backend.chat.util;

import java.time.LocalDate;
import java.time.LocalTime;

public class DateTimeFormatter {

public static String formatDateTime(LocalDate date, LocalTime time) {
return date.getMonth().getValue() + "월 "
+ date.getDayOfMonth() + "일 "
+ time.getHour() + "시 "
+ time.getMinute() + "분";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public void createChat(long darakbangId, OldChatCreateRequest oldChatCreateReque
Chat chat = oldChatCreateRequest.toEntity(moim, darakbangMember);
chatWriter.save(chat);

oldChatNotificationSender.sendChatNotification(moim, darakbangMember, NotificationType.NEW_CHAT);
oldChatNotificationSender.sendChatNotification(chat, 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);

oldChatNotificationSender.sendChatNotification(moim, darakbangMember, NotificationType.MOIM_PLACE_CONFIRMED);
oldChatNotificationSender.sendChatNotification(chat, 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);

oldChatNotificationSender.sendChatNotification(moim, darakbangMember, NotificationType.MOIM_TIME_CONFIRMED);
oldChatNotificationSender.sendChatNotification(chat, darakbangMember, NotificationType.MOIM_TIME_CONFIRMED);
}

public OldChatPreviewResponses findChatPreview(long darakbangId, DarakbangMember darakbangMember) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package mouda.backend.moim.implement.finder;

import java.util.List;
import java.util.Objects;
import java.util.stream.Stream;

import org.springframework.stereotype.Component;

import lombok.RequiredArgsConstructor;
import mouda.backend.bet.entity.BetDarakbangMemberEntity;
import mouda.backend.bet.infrastructure.BetDarakbangMemberRepository;
import mouda.backend.darakbangmember.domain.DarakbangMember;
import mouda.backend.moim.domain.Chamyo;
import mouda.backend.moim.infrastructure.ChamyoRepository;
Expand All @@ -16,13 +18,31 @@
public class ChatRecipientFinder {

private final ChamyoRepository chamyoRepository;
private final BetDarakbangMemberRepository betDarakbangMemberRepository;

public List<Recipient> getChatNotificationRecipients(long moimId, DarakbangMember darakbangMember) {
public List<Recipient> getMoimChatNotificationRecipients(long moimId, DarakbangMember sender) {
List<Chamyo> chamyos = chamyoRepository.findAllByMoimId(moimId);

return chamyos.stream()
.filter(chamyo -> !Objects.equals(chamyo.getDarakbangMember().getId(), darakbangMember.getId()))
.map(chamyo -> new Recipient(chamyo.getDarakbangMember().getMemberId(), chamyo.getDarakbangMember().getId()))
Stream<DarakbangMember> darakbangMemberStream = chamyos.stream()
.map(Chamyo::getDarakbangMember);

return getNotificationRecipients(darakbangMemberStream, sender);
}

public List<Recipient> getBetChatNotificationRecipients(long betId, DarakbangMember sender) {
List<BetDarakbangMemberEntity> members = betDarakbangMemberRepository.findAllByDarakbangMemberId(
betId);

Stream<DarakbangMember> darakbangMemberStream = members.stream()
.map(BetDarakbangMemberEntity::getDarakbangMember);

return getNotificationRecipients(darakbangMemberStream, sender);
}

public List<Recipient> getNotificationRecipients(Stream<DarakbangMember> memberStream, DarakbangMember sender) {
return memberStream
.filter(darakbangMember -> darakbangMember.isNotSameMemberWith(sender))
.map(darakbangMember -> new Recipient(darakbangMember.getMemberId(), darakbangMember.getId()))
.toList();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.util.List;

import org.springframework.context.ApplicationEventPublisher;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;

import mouda.backend.common.config.UrlConfig;
Expand All @@ -11,23 +12,47 @@
import mouda.backend.notification.domain.NotificationEvent;
import mouda.backend.notification.domain.NotificationType;
import mouda.backend.notification.domain.Recipient;
import mouda.backend.notification.exception.NotificationErrorMessage;
import mouda.backend.notification.exception.NotificationException;

@Component
public class ChamyoNotificationSender extends AbstractMoimNotificationSender {

private final ChamyoRecipientFinder chamyoRecipientFinder;
private final ApplicationEventPublisher eventPublisher;

public ChamyoNotificationSender(UrlConfig urlConfig, ChamyoRecipientFinder chamyoRecipientFinder, ApplicationEventPublisher eventPublisher) {
public ChamyoNotificationSender(UrlConfig urlConfig, ChamyoRecipientFinder chamyoRecipientFinder,
ApplicationEventPublisher eventPublisher) {
super(urlConfig);
this.chamyoRecipientFinder = chamyoRecipientFinder;
this.eventPublisher = eventPublisher;
}

public void sendChamyoNotification(long moimId, DarakbangMember updatedMember, NotificationType notificationType) {
List<Recipient> recipients = chamyoRecipientFinder.getChamyoNotificationRecipients(moimId, updatedMember);
NotificationEvent notificationEvent = new NotificationEvent(notificationType, updatedMember.getDarakbang().getName(), notificationType.createMessage(updatedMember.getNickname()), getMoimUrl(updatedMember.getDarakbang().getId(), moimId), recipients);
NotificationEvent notificationEvent = NotificationEvent.nonChatEvent(
notificationType,
updatedMember.getDarakbang().getName(),
ChamyoNotificationMessage.create(updatedMember.getNickname(), notificationType),
getMoimUrl(updatedMember.getDarakbang().getId(), moimId),
recipients
);

eventPublisher.publishEvent(notificationEvent);
}

static class ChamyoNotificationMessage {

public static String create(String updatedMemberName, NotificationType type) {
if (type == NotificationType.NEW_MOIMEE_JOINED) {
return updatedMemberName + "님이 모임에 참여했어요!";
}
if (type == NotificationType.MOIMEE_LEFT) {
return updatedMemberName + "님이 참여를 취소했어요!";
}
throw new NotificationException(
HttpStatus.BAD_REQUEST, NotificationErrorMessage.NOT_ALLOWED_NOTIFICATION_TYPE
);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.util.List;

import org.springframework.context.ApplicationEventPublisher;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;

import mouda.backend.common.config.UrlConfig;
Expand All @@ -14,6 +15,8 @@
import mouda.backend.notification.domain.NotificationEvent;
import mouda.backend.notification.domain.NotificationType;
import mouda.backend.notification.domain.Recipient;
import mouda.backend.notification.exception.NotificationErrorMessage;
import mouda.backend.notification.exception.NotificationException;

@Component
public class CommentNotificationSender extends AbstractMoimNotificationSender {
Expand All @@ -38,12 +41,31 @@ public void sendCommentNotification(Comment comment, DarakbangMember author) {

private void sendNotification(CommentRecipient commentRecipient, Comment comment, DarakbangMember author) {
NotificationType notificationType = commentRecipient.getNotificationType();
String message = notificationType.createMessage(author.getNickname());
List<Recipient> recipients = commentRecipient.getRecipients();
Moim moim = comment.getMoim();
NotificationEvent notificationEvent = new NotificationEvent(notificationType, moim.getTitle(), message,
getMoimUrl(moim.getDarakbangId(), moim.getId()), recipients);
NotificationEvent notificationEvent = NotificationEvent.nonChatEvent(
notificationType,
moim.getTitle(),
CommentNotificationMessage.create(author.getNickname(), notificationType),
getMoimUrl(moim.getDarakbangId(), moim.getId()),
recipients
);

eventPublisher.publishEvent(notificationEvent);
}

static class CommentNotificationMessage {

public static String create(String author, NotificationType type) {
if (type == NotificationType.NEW_COMMENT) {
return author + "님이 댓글을 남겼어요!";
}
if (type == NotificationType.NEW_REPLY) {
return author + "님이 답글을 남겼어요!";
}
throw new NotificationException(
HttpStatus.BAD_REQUEST, NotificationErrorMessage.NOT_ALLOWED_NOTIFICATION_TYPE
);
}
}
}
Loading