Skip to content

Commit

Permalink
fix : 알람 수정
Browse files Browse the repository at this point in the history
- 알람 서비스 이관 (api -> infra)
  • Loading branch information
KJBig committed Oct 2, 2024
1 parent 006874b commit 0e378ec
Show file tree
Hide file tree
Showing 14 changed files with 90 additions and 70 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.sluv.api.comment.service;

import com.sluv.api.alarm.service.CommentAlarmService;
import com.sluv.api.comment.dto.reponse.CommentResponse;
import com.sluv.api.comment.dto.reponse.SubCommentPageResponse;
import com.sluv.api.comment.dto.request.CommentPostRequest;
Expand All @@ -17,14 +16,16 @@
import com.sluv.domain.user.exception.UserNotMatchedException;
import com.sluv.domain.user.service.UserDomainService;
import com.sluv.infra.ai.AiModelService;
import java.util.List;
import com.sluv.infra.alarm.service.CommentAlarmService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Slf4j
@RequiredArgsConstructor
Expand Down Expand Up @@ -113,7 +114,7 @@ public void postComment(Long userId, Long questionId, CommentPostRequest request
commentImgHelper.saveCommentImg(request, comment);

aiModelService.censorComment(comment);
commentAlarmService.sendAlarmAboutComment(user.getId(), comment.getId(), user);
commentAlarmService.sendAlarmAboutComment(user.getId(), comment.getId());

}

Expand All @@ -137,8 +138,8 @@ public void postSubComment(Long userId, Long questionId, Long commentId, Comment
commentImgHelper.saveCommentImg(request, comment);

aiModelService.censorComment(comment);
commentAlarmService.sendAlarmAboutComment(user.getId(), comment.getId(), user);
commentAlarmService.sendAlarmAboutSubComment(user.getId(), comment.getId(), user);
commentAlarmService.sendAlarmAboutComment(user.getId(), comment.getId());
commentAlarmService.sendAlarmAboutSubComment(user.getId(), comment.getId());
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package com.sluv.api.item.service;

import com.sluv.api.alarm.service.ItemAlarmService;
import com.sluv.api.item.dto.ItemEditReqDto;
import com.sluv.domain.item.entity.Item;
import com.sluv.domain.item.entity.ItemEditReq;
import com.sluv.domain.item.service.ItemDomainService;
import com.sluv.domain.item.service.ItemEditReqDomainService;
import com.sluv.domain.user.entity.User;
import com.sluv.domain.user.service.UserDomainService;
import com.sluv.infra.alarm.service.ItemAlarmService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
Expand All @@ -26,6 +26,6 @@ public void postItemEdit(Long userId, Long itemId, ItemEditReqDto dto) {
User user = userDomainService.findById(userId);
ItemEditReq itemEditReq = itemEditReqDomainService.saveItemEditReq(user, item, dto.getReason(),
dto.getContent());
itemAlarmService.sendAlarmAboutItemEdit(item.getId(), itemEditReq.getId(), user);
itemAlarmService.sendAlarmAboutItemEdit(user.getId(), item.getId(), itemEditReq.getId());
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.sluv.api.item.service;


import com.sluv.api.alarm.service.ItemAlarmService;
import com.sluv.api.brand.dto.response.BrandSearchResponse;
import com.sluv.api.celeb.dto.response.CelebSearchResponse;
import com.sluv.api.common.response.PaginationResponse;
Expand Down Expand Up @@ -34,6 +33,7 @@
import com.sluv.domain.user.service.FollowDomainService;
import com.sluv.domain.user.service.UserDomainService;
import com.sluv.infra.ai.AiModelService;
import com.sluv.infra.alarm.service.ItemAlarmService;
import com.sluv.infra.cache.CacheService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
Expand Down Expand Up @@ -301,7 +301,7 @@ public void postItemLike(Long userId, Long itemId) {
boolean likeExist = itemLikeDomainService.existsByUserIdAndItemId(user.getId(), itemId);
if (!likeExist) {
itemLikeDomainService.saveItemLike(item, user);
itemAlarmService.sendAlarmAboutItemLike(user.getId(), itemId);
itemAlarmService.sendAlarmAboutItemLike(user.getId(), item.getId());
} else {
itemLikeDomainService.deleteByUserIdAndItemId(user.getId(), itemId);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.sluv.api.question.service;

import com.sluv.api.alarm.service.QuestionAlarmService;
import com.sluv.api.celeb.dto.response.CelebChipResponse;
import com.sluv.api.common.response.PaginationResponse;
import com.sluv.api.question.dto.*;
Expand All @@ -26,6 +25,7 @@
import com.sluv.domain.question.service.*;
import com.sluv.domain.user.entity.User;
import com.sluv.domain.user.service.UserDomainService;
import com.sluv.infra.alarm.service.QuestionAlarmService;
import com.sluv.infra.cache.CacheService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package com.sluv.api.user.service;

import com.sluv.api.alarm.service.UserAlarmService;
import com.sluv.api.common.response.PaginationResponse;
import com.sluv.domain.user.dto.UserSearchInfoDto;
import com.sluv.domain.user.entity.Follow;
import com.sluv.domain.user.entity.User;
import com.sluv.domain.user.service.FollowDomainService;
import com.sluv.domain.user.service.UserDomainService;
import com.sluv.infra.alarm.service.UserAlarmService;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.sluv.domain.question.entity.QuestionBuy;
import com.sluv.domain.question.repository.DailyHotQuestionRepository;
import com.sluv.domain.question.repository.QuestionRepository;
import com.sluv.infra.alarm.service.QuestionAlarmService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
Expand All @@ -21,7 +22,7 @@ public class QuestionScheduler {

private final DailyHotQuestionRepository dailyHotQuestionRepository;
private final QuestionRepository questionRepository;
// private final QuestionAlarmService questionAlarmService;
private final QuestionAlarmService questionAlarmService;

private static final int VOTE_END_CHECK_PERIOD = 60000; // 1분

Expand Down Expand Up @@ -57,7 +58,7 @@ public void checkQuestionVoteEnd() {
log.info("QuestionVoteEnd Check Time: {}", Calendar.getInstance().getTime());
List<QuestionBuy> endTimeBetweenNow = questionRepository.getEndTimeBetweenNow(VOTE_END_CHECK_PERIOD);
endTimeBetweenNow.forEach(questionBuy -> System.out.println("dd: " + questionBuy.getId()));
// endTimeBetweenNow.forEach(questionBuy -> questionAlarmService.sendAlarmAboutQuestionVote(questionBuy.getId()));
endTimeBetweenNow.forEach(questionBuy -> questionAlarmService.sendAlarmAboutQuestionVote(questionBuy.getId()));

}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.sluv.infra.firebase;
package com.sluv.infra.alarm.firebase;

import com.google.firebase.messaging.FirebaseMessaging;
import com.google.firebase.messaging.FirebaseMessagingException;
Expand All @@ -9,7 +9,7 @@
import com.sluv.domain.user.entity.User;
import com.sluv.domain.user.exception.UserNotFoundException;
import com.sluv.domain.user.repository.UserRepository;
import com.sluv.infra.firebase.exception.FcmConnectException;
import com.sluv.infra.alarm.firebase.exception.FcmConnectException;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.sluv.infra.firebase.exception;
package com.sluv.infra.alarm.firebase.exception;

import com.sluv.common.exception.HttpStatusCode;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.sluv.infra.firebase.exception;
package com.sluv.infra.alarm.firebase.exception;

import com.sluv.common.exception.HttpStatusCode;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.sluv.infra.firebase.exception;
package com.sluv.infra.alarm.firebase.exception;

import com.sluv.common.exception.ApplicationException;
import com.sluv.common.exception.HttpStatusCode;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.sluv.api.alarm.service;
package com.sluv.infra.alarm.service;

import com.sluv.domain.alarm.dto.AlarmElement;
import com.sluv.domain.alarm.enums.AlarmMessage;
Expand All @@ -8,7 +8,7 @@
import com.sluv.domain.comment.service.CommentDomainService;
import com.sluv.domain.user.entity.User;
import com.sluv.domain.user.service.UserDomainService;
import com.sluv.infra.firebase.FcmNotificationService;
import com.sluv.infra.alarm.firebase.FcmNotificationService;
import lombok.RequiredArgsConstructor;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
Expand All @@ -30,44 +30,53 @@ public class CommentAlarmService {

@Transactional
@Async("alarmThreadPoolExecutor")
public void sendAlarmAboutCommentLike(Long userId, Long commentId) {
User user = userDomainService.findById(userId);
public void sendAlarmAboutCommentLike(Long senderId, Long commentId) {
Comment comment = commentDomainService.findById(commentId);
String message = AlarmMessage.getMessageWithUserName(user.getNickname(), AlarmMessage.COMMENT_LIKE);
sendMessageTypeComment(comment.getUser().getId(), comment, message, user);

if (!senderId.equals(comment.getUser().getId())) {
User sender = userDomainService.findById(senderId);
String message = AlarmMessage.getMessageWithUserName(sender.getNickname(), AlarmMessage.COMMENT_LIKE);
sendMessageTypeComment(comment.getUser(), comment, message, sender);
}
}

@Transactional
@Async("alarmThreadPoolExecutor")
public void sendAlarmAboutComment(Long userId, Long commentId, User sender) {
User user = userDomainService.findById(userId);
public void sendAlarmAboutComment(Long senderId, Long commentId) {
Comment comment = commentDomainService.findById(commentId);
String message = AlarmMessage.getMessageWithUserName(user.getNickname(), AlarmMessage.QUESTION_COMMENT);
sendMessageTypeComment(comment.getQuestion().getUser().getId(), comment, message, sender);

if (!senderId.equals(comment.getUser().getId())) {
User sender = userDomainService.findById(senderId);
String message = AlarmMessage.getMessageWithUserName(sender.getNickname(), AlarmMessage.QUESTION_COMMENT);
sendMessageTypeComment(comment.getQuestion().getUser(), comment, message, sender);
}
}

@Transactional
@Async("alarmThreadPoolExecutor")
public void sendAlarmAboutSubComment(Long userId, Long commentId, User sender) {
User user = userDomainService.findById(userId);
public void sendAlarmAboutSubComment(Long senderId, Long commentId) {
Comment comment = commentDomainService.findById(commentId);
String message = AlarmMessage.getMessageWithUserName(user.getNickname(), AlarmMessage.COMMENT_SUB);
sendMessageTypeComment(comment.getUser().getId(), comment, message, sender);

if (!senderId.equals(comment.getUser().getId())) {
User sender = userDomainService.findById(senderId);
String message = AlarmMessage.getMessageWithUserName(sender.getNickname(), AlarmMessage.COMMENT_SUB);
sendMessageTypeComment(comment.getUser(), comment, message, sender);
}
}

@Transactional
@Async("alarmThreadPoolExecutor")
public void sendAlarmAboutReportByAI(Long commentId, User sender) {
Comment comment = commentDomainService.findById(commentId);
String message = AlarmMessage.COMMENT_REPORT_BY_AI.getMessage();
sendMessageTypeComment(comment.getUser().getId(), comment, message, sender);
sendMessageTypeComment(comment.getUser(), comment, message, sender);
}

private void sendMessageTypeComment(Long receiverId, Comment comment, String message, User sender) {
private void sendMessageTypeComment(User receiver, Comment comment, String message, User sender) {
AlarmElement alarmElement = AlarmElement.of(null, comment.getQuestion(), comment, sender);
alarmDomainService.saveAlarm(comment.getUser(), ALARM_TITLE, message, AlarmType.COMMENT, alarmElement);
alarmDomainService.saveAlarm(receiver, ALARM_TITLE, message, AlarmType.COMMENT, alarmElement);
fcmNotificationService.sendFCMNotification(
receiverId, ALARM_TITLE, message, AlarmType.COMMENT, getIdsAboutComment(comment)
receiver.getId(), ALARM_TITLE, message, AlarmType.COMMENT, getIdsAboutComment(comment)
);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.sluv.api.alarm.service;
package com.sluv.infra.alarm.service;

import com.sluv.domain.alarm.dto.AlarmElement;
import com.sluv.domain.alarm.enums.AlarmMessage;
Expand All @@ -10,7 +10,7 @@
import com.sluv.domain.user.entity.User;
import com.sluv.domain.user.service.FollowDomainService;
import com.sluv.domain.user.service.UserDomainService;
import com.sluv.infra.firebase.FcmNotificationService;
import com.sluv.infra.alarm.firebase.FcmNotificationService;
import lombok.RequiredArgsConstructor;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
Expand All @@ -34,51 +34,58 @@ public class ItemAlarmService {

@Transactional
@Async("alarmThreadPoolExecutor")
public void sendAlarmAboutItemLike(Long userId, Long itemId) {
User user = userDomainService.findById(userId);
public void sendAlarmAboutItemLike(Long senderId, Long itemId) {
Item item = itemDomainService.findById(itemId);
String message = AlarmMessage.getMessageWithUserName(user.getNickname(), AlarmMessage.ITEM_LIKE);
sendMessageTypeItem(user, item, message, user);

if (!senderId.equals(item.getUser().getId())) {
User sender = userDomainService.findById(senderId);
String message = AlarmMessage.getMessageWithUserName(sender.getNickname(), AlarmMessage.ITEM_LIKE);
sendMessageTypeItem(item.getUser(), item, message, sender);
}
}

@Transactional
@Async("alarmThreadPoolExecutor")
public void sendAlarmAboutItemEdit(Long itemId, Long itemEditReqId, User sender) {
public void sendAlarmAboutItemEdit(Long senderId, Long itemId, Long itemEditReqId) {
Item item = itemDomainService.findById(itemId);
String message = AlarmMessage.ITEM_EDIT.getMessage();
sendMessageTypeItemEdit(itemEditReqId, item, message, sender);

if (!senderId.equals(item.getUser().getId())) {
User sender = userDomainService.findById(senderId);
String message = AlarmMessage.ITEM_EDIT.getMessage();
sendMessageTypeItemEdit(itemEditReqId, item, message, sender);
}
}

@Transactional
@Async("alarmThreadPoolExecutor")
public void sendAlarmAboutFollowItem(Long userId, Long itemId) {
User user = userDomainService.findById(userId);
public void sendAlarmAboutFollowItem(Long followeeId, Long itemId) {
User followee = userDomainService.findById(followeeId);
Item item = itemDomainService.findById(itemId);
List<User> follower = followDomainService.getAllFollower(userId)
List<User> follower = followDomainService.getAllFollower(followeeId)
.stream()
.map(Follow::getFollower)
.toList();

String message = AlarmMessage.getMessageWithUserName(user.getNickname(), AlarmMessage.USER_FOLLOW_ITEM);
sendMulticastMessageTypeItem(follower, item, message, user);
String message = AlarmMessage.getMessageWithUserName(followee.getNickname(), AlarmMessage.USER_FOLLOW_ITEM);
sendMulticastMessageTypeItem(follower, item, message, followee);
}

private void sendMessageTypeItem(User user, Item item, String message, User sender) {
private void sendMessageTypeItem(User receiver, Item item, String message, User sender) {
AlarmElement alarmElement = AlarmElement.of(item, null, null, sender);
alarmDomainService.saveAlarm(user, ALARM_TITLE, message, AlarmType.ITEM, alarmElement);
alarmDomainService.saveAlarm(receiver, ALARM_TITLE, message, AlarmType.ITEM, alarmElement);
fcmNotificationService.sendFCMNotification(
item.getUser().getId(), ALARM_TITLE, message, AlarmType.ITEM, getIdAboutItem(item.getId())
receiver.getId(), ALARM_TITLE, message, AlarmType.ITEM, getIdAboutItem(item.getId())
);
}

private void sendMulticastMessageTypeItem(List<User> follower, Item item, String message, User sender) {
private void sendMulticastMessageTypeItem(List<User> follower, Item item, String message, User followee) {

List<Long> followerIds = follower
.stream()
.map(User::getId)
.toList();

AlarmElement alarmElement = AlarmElement.of(item, null, null, sender);
AlarmElement alarmElement = AlarmElement.of(item, null, null, followee);
alarmDomainService.saveAllAlarm(follower, ALARM_TITLE, message, AlarmType.ITEM, alarmElement);
fcmNotificationService.sendFCMNotificationMulticast(
followerIds, ALARM_TITLE, message, AlarmType.ITEM, getIdAboutItem(item.getId())
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.sluv.api.alarm.service;
package com.sluv.infra.alarm.service;

import com.sluv.domain.alarm.dto.AlarmElement;
import com.sluv.domain.alarm.enums.AlarmMessage;
Expand All @@ -8,7 +8,7 @@
import com.sluv.domain.question.service.QuestionDomainService;
import com.sluv.domain.user.entity.User;
import com.sluv.domain.user.service.UserDomainService;
import com.sluv.infra.firebase.FcmNotificationService;
import com.sluv.infra.alarm.firebase.FcmNotificationService;
import lombok.RequiredArgsConstructor;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
Expand All @@ -30,18 +30,20 @@ public class QuestionAlarmService {

@Transactional
@Async("alarmThreadPoolExecutor")
public void sendAlarmAboutQuestionLike(Long userId, Long questionId) {
User user = userDomainService.findById(userId);
public void sendAlarmAboutQuestionLike(Long senderId, Long questionId) {
Question question = questionDomainService.findById(questionId);
String message = AlarmMessage.getMessageWithUserName(user.getNickname(), AlarmMessage.QUESTION_LIKE);
sendMessageTypeQuestion(user, question, message, user, AlarmType.QUESTION);
if (!senderId.equals(question.getUser().getId())) {
User sender = userDomainService.findById(senderId);
String message = AlarmMessage.getMessageWithUserName(sender.getNickname(), AlarmMessage.QUESTION_LIKE);
sendMessageTypeQuestion(question.getUser(), question, message, sender, AlarmType.QUESTION);
}
}

private void sendMessageTypeQuestion(User user, Question question, String message, User sender, AlarmType type) {
private void sendMessageTypeQuestion(User receiver, Question question, String message, User sender, AlarmType type) {
AlarmElement alarmElement = AlarmElement.of(null, question, null, sender);
alarmDomainService.saveAlarm(user, ALARM_TITLE, message, type, alarmElement);
alarmDomainService.saveAlarm(receiver, ALARM_TITLE, message, type, alarmElement);
fcmNotificationService.sendFCMNotification(
question.getUser().getId(), ALARM_TITLE, message, type,
receiver.getId(), ALARM_TITLE, message, type,
getIdAboutQuestion(question.getId())
);
}
Expand Down
Loading

0 comments on commit 0e378ec

Please sign in to comment.