Skip to content

Commit

Permalink
Merge pull request #130 from Modagbul/feat/alarm_minsu
Browse files Browse the repository at this point in the history
푸시알림 보내기와 alarm_history 합치기 및 fcmService 의존성 제거
  • Loading branch information
minsu20 authored Dec 10, 2023
2 parents d174475 + b8446c6 commit b130f81
Show file tree
Hide file tree
Showing 20 changed files with 183 additions and 104 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import com.moing.backend.domain.member.domain.entity.Member;
import com.moing.backend.domain.team.domain.entity.Team;
import com.moing.backend.domain.teamMember.domain.service.TeamMemberGetService;
import com.moing.backend.global.config.fcm.dto.event.FcmEvent;
import com.moing.backend.global.config.fcm.dto.event.MultiFcmEvent;
import com.moing.backend.global.response.BaseServiceResponse;
import lombok.RequiredArgsConstructor;
import net.minidev.json.JSONObject;
Expand All @@ -29,8 +29,6 @@ public class SendBoardAlarmUseCase {

private final TeamMemberGetService teamMemberGetService;
private final ApplicationEventPublisher eventPublisher;
private final SaveMultiAlarmHistoryUseCase saveAlarmHistory;
private final AlarmHistoryMapper alarmHistoryMapper;

public void sendNewUploadAlarm(BaseServiceResponse baseServiceResponse, Board board) {
Member member = baseServiceResponse.getMember();
Expand All @@ -42,10 +40,7 @@ public void sendNewUploadAlarm(BaseServiceResponse baseServiceResponse, Board bo
Optional<List<MemberIdAndToken>> memberIdAndTokens = teamMemberGetService.getMemberInfoExceptMe(team.getTeamId(), member.getMemberId());
if (memberIdAndTokens.isPresent() && !memberIdAndTokens.get().isEmpty()) {
//알림 보내기
List<String> fcmTokens = alarmHistoryMapper.getFcmTokens(memberIdAndTokens);
eventPublisher.publishEvent(new FcmEvent(title, body, fcmTokens));
//알림 저장하기
saveAlarmHistory.saveAlarmHistories(memberIdAndTokens, createIdInfo(team.getTeamId(), board.getBoardId()), title, body, team.getName(), AlarmType.NEW_UPLOAD, PagePath.NOTICE_PATH.getValue());
eventPublisher.publishEvent(new MultiFcmEvent(title, body, memberIdAndTokens.get(), createIdInfo(team.getTeamId(), board.getBoardId()), team.getName(), AlarmType.NEW_UPLOAD, PagePath.NOTICE_PATH.getValue()));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ public class BoardComment extends BaseTimeEntity {
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "board_comment_id")
private Long boardCommentId;

@Column(nullable = false, length = 300)
private String content;

@ManyToOne(fetch = FetchType.LAZY)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,43 +1,50 @@
package com.moing.backend.domain.fire.application.service;

import com.moing.backend.domain.history.domain.entity.AlarmType;
import com.moing.backend.domain.member.domain.entity.Member;
import com.moing.backend.global.config.fcm.dto.request.SingleRequest;
import com.moing.backend.domain.mission.domain.entity.Mission;
import com.moing.backend.domain.mission.domain.entity.constant.MissionType;
import com.moing.backend.domain.team.domain.entity.Team;
import com.moing.backend.domain.teamMember.domain.service.TeamMemberGetService;
import com.moing.backend.global.config.fcm.dto.event.SingleFcmEvent;
import com.moing.backend.global.config.fcm.service.FcmService;
import lombok.RequiredArgsConstructor;
import net.minidev.json.JSONObject;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;

import java.util.Random;

import static com.moing.backend.domain.history.domain.entity.PagePath.MISSION_PATH;
import static com.moing.backend.global.config.fcm.constant.FireThrowMessage.*;
import static java.lang.Math.random;

@Service
@Transactional
@RequiredArgsConstructor
public class FireThrowAlarmUseCase {

private final FcmService fcmService;
private final ApplicationEventPublisher eventPublisher;
private final TeamMemberGetService teamMemberGetService;

public void sendFireThrowAlarm(Member throwMember, Member receiveMember) {
public void sendFireThrowAlarm(Member throwMember, Member receiveMember, Team team, Mission mission) {

Random random = new Random(System.currentTimeMillis());
int randomNum = random.nextInt(2);

String title = getTitle(throwMember.getNickName(), receiveMember.getNickName(), randomNum);
String message = getMessage(throwMember.getNickName(), receiveMember.getNickName(), randomNum);
String idInfo = createIdInfo(mission.getType() == MissionType.REPEAT, mission.getTeam().getTeamId(), mission.getId());

SingleRequest singleRequest = new SingleRequest(receiveMember.getFcmToken(), title, message);

fcmService.sendSingleDevice(singleRequest);
eventPublisher.publishEvent(new SingleFcmEvent(receiveMember.getFcmToken(), title, message, receiveMember.getMemberId(), idInfo, team.getName(), AlarmType.FIRE, MISSION_PATH.getValue()));
}

public String getMessage(String pusher, String receiver, int num) {

switch (num) {
case 0: return pusher + "님이" + receiver + NEW_FIRE_THROW_MESSAGE1.getMessage();
case 1: return receiver + "님! " + pusher + NEW_FIRE_THROW_MESSAGE2.getMessage();
case 0: return pusher + "님이 " + receiver + NEW_FIRE_THROW_MESSAGE1.getMessage();
case 1: return receiver + "님! " + pusher + NEW_FIRE_THROW_MESSAGE2.getMessage();
}
return pusher + "님이" + receiver + NEW_FIRE_THROW_MESSAGE1.getMessage();
}
Expand All @@ -53,5 +60,13 @@ public String getTitle(String pusher, String receiver, int num) {
return NEW_FIRE_THROW_TITLE1.getMessage();
}

private String createIdInfo(boolean isRepeated, Long teamId, Long missionId) {
JSONObject jo = new JSONObject();
jo.put("isRepeated", isRepeated);
jo.put("teamId", teamId);
jo.put("missionId", missionId);
return jo.toJSONString();
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,17 @@
import com.moing.backend.domain.fire.exception.NoAuthThrowFireException;
import com.moing.backend.domain.member.domain.entity.Member;
import com.moing.backend.domain.member.domain.service.MemberGetService;
import com.moing.backend.domain.mission.domain.entity.Mission;
import com.moing.backend.domain.mission.domain.service.MissionQueryService;
import com.moing.backend.domain.missionArchive.domain.service.MissionArchiveQueryService;
import com.moing.backend.global.config.fcm.dto.request.SingleRequest;
import com.moing.backend.global.config.fcm.service.FcmService;
import com.moing.backend.domain.team.domain.entity.Team;
import com.moing.backend.domain.team.domain.service.TeamGetService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

import static java.lang.Math.random;

@Service
@Transactional
@RequiredArgsConstructor
Expand All @@ -28,15 +28,17 @@ public class FireThrowUseCase {
public final FireSaveService fireSaveService;
public final FireQueryService fireQueryService;
private final MemberGetService memberGetService;

private final FireThrowAlarmUseCase fireThrowAlarmUseCase;

private final MissionArchiveQueryService missionArchiveQueryService;
private final MissionQueryService missionQueryService;
private final TeamGetService teamGetService;

public FireThrowRes createFireThrow(String userId, Long receiveMemberId) {
public FireThrowRes createFireThrow(String userId, Long receiveMemberId, Long missionId, Long teamId) {

Member throwMember = memberGetService.getMemberBySocialId(userId);
Member receiveMember = memberGetService.getMemberByMemberId(receiveMemberId);
Team team = teamGetService.getTeamByTeamId(teamId);
Mission mission = missionQueryService.findMissionById(missionId);

// 나에게 던질 수 없음
if (throwMember.equals(receiveMember)) {
Expand All @@ -48,7 +50,7 @@ public FireThrowRes createFireThrow(String userId, Long receiveMemberId) {
throw new NoAuthThrowFireException();
}

fireThrowAlarmUseCase.sendFireThrowAlarm(throwMember, receiveMember);
fireThrowAlarmUseCase.sendFireThrowAlarm(throwMember, receiveMember, team, mission);

return FireMapper.mapToFireThrowRes(fireSaveService.save(Fire.builder()
.throwMemberId(throwMember.getMemberId())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import com.moing.backend.domain.fire.application.dto.res.FireReceiveRes;
import com.moing.backend.domain.fire.application.dto.res.FireThrowRes;
import com.moing.backend.domain.fire.application.service.FireThrowUseCase;
import com.moing.backend.domain.mission.application.dto.res.MissionReadRes;
import com.moing.backend.global.config.security.dto.User;
import com.moing.backend.global.response.SuccessResponse;
import lombok.AllArgsConstructor;
Expand All @@ -15,7 +14,6 @@

import static com.moing.backend.domain.fire.presentation.constant.FireResponseMessage.GET_RECEIVERS_SUCCESS;
import static com.moing.backend.domain.fire.presentation.constant.FireResponseMessage.THROW_FIRE_SUCCESS;
import static com.moing.backend.domain.mission.presentation.constant.MissionResponseMessage.READ_MISSION_SUCCESS;

@RestController
@AllArgsConstructor
Expand All @@ -32,8 +30,8 @@ public class FireController {

@PostMapping("/{receiveMemberId}")
public ResponseEntity<SuccessResponse<FireThrowRes>> throwFire (@AuthenticationPrincipal User user, @PathVariable("teamId") Long teamId,
@PathVariable("receiveMemberId") Long receiveMemberId) {
return ResponseEntity.ok(SuccessResponse.create(THROW_FIRE_SUCCESS.getMessage(), this.fireThrowUseCase.createFireThrow(user.getSocialId(),receiveMemberId)));
@PathVariable("receiveMemberId") Long receiveMemberId, @PathVariable("missionId") Long missionId) {
return ResponseEntity.ok(SuccessResponse.create(THROW_FIRE_SUCCESS.getMessage(), this.fireThrowUseCase.createFireThrow(user.getSocialId(), receiveMemberId, missionId, teamId)));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

@Component
Expand All @@ -28,18 +27,25 @@ public AlarmHistory toAlarmHistory(AlarmType type, String path, String idInfo, L
.build();
}

public List<String> getFcmTokens(Optional<List<MemberIdAndToken>> memberIdAndTokens) {
return memberIdAndTokens.map(list -> list.stream()
.map(MemberIdAndToken::getFcmToken)
.collect(Collectors.toList()))
.orElse(Collections.emptyList());
public static List<String> getFcmTokens(List<MemberIdAndToken> memberIdAndTokens) {
if (memberIdAndTokens == null || memberIdAndTokens.isEmpty()) {
return Collections.emptyList();
}

return memberIdAndTokens.stream()
.map(MemberIdAndToken::getFcmToken)
.collect(Collectors.toList());
}

public List<Long> getMemberIds(Optional<List<MemberIdAndToken>> memberIdAndTokens) {
return memberIdAndTokens.map(list -> list.stream()
.map(MemberIdAndToken::getMemberId)
.collect(Collectors.toList()))
.orElse(Collections.emptyList());

public static List<Long> getMemberIds(List<MemberIdAndToken> memberIdAndTokens) {
if (memberIdAndTokens == null || memberIdAndTokens.isEmpty()) {
return Collections.emptyList();
}

return memberIdAndTokens.stream()
.map(MemberIdAndToken::getMemberId)
.collect(Collectors.toList());
}

public List<AlarmHistory> getAlarmHistories(String idInfo, List<Long> memberIds, String title, String body, String teamName, AlarmType alarmType, String path) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.moing.backend.domain.history.application.service;

import com.moing.backend.domain.history.application.dto.response.MemberIdAndToken;
import com.moing.backend.domain.history.application.mapper.AlarmHistoryMapper;
import com.moing.backend.domain.history.domain.entity.AlarmHistory;
import com.moing.backend.domain.history.domain.entity.AlarmType;
Expand All @@ -10,7 +9,6 @@
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

@Service
@RequiredArgsConstructor
Expand All @@ -20,8 +18,7 @@ public class SaveMultiAlarmHistoryUseCase {
private final AlarmHistorySaveService alarmHistorySaveService;

@Async
public void saveAlarmHistories(Optional<List<MemberIdAndToken>> memberIdAndTokens, String idInfo, String title, String body, String name, AlarmType alarmType, String path) {
List<Long> memberIds = alarmHistoryMapper.getMemberIds(memberIdAndTokens);
public void saveAlarmHistories(List<Long> memberIds, String idInfo, String title, String body, String name, AlarmType alarmType, String path) {
List<AlarmHistory> alarmHistories = alarmHistoryMapper.getAlarmHistories(idInfo, memberIds, title, body, name, alarmType, path);
alarmHistorySaveService.saveAlarmHistories(alarmHistories);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,9 @@
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;

import java.util.Random;

import static com.moing.backend.global.config.fcm.constant.RemindMissionTitle.*;
import static java.lang.Math.random;

@Service
@Transactional
Expand Down
Original file line number Diff line number Diff line change
@@ -1,25 +1,23 @@
package com.moing.backend.domain.mission.application.service;

import com.moing.backend.domain.board.domain.entity.Board;
import com.moing.backend.domain.history.application.dto.response.MemberIdAndToken;
import com.moing.backend.domain.history.domain.entity.AlarmType;
import com.moing.backend.domain.history.domain.entity.PagePath;
import com.moing.backend.domain.member.domain.entity.Member;
import com.moing.backend.domain.mission.domain.entity.Mission;
import com.moing.backend.domain.team.domain.entity.Team;
import com.moing.backend.domain.teamMember.domain.service.TeamMemberGetService;
import com.moing.backend.global.config.fcm.dto.event.FcmEvent;
import com.moing.backend.global.config.fcm.dto.request.MultiRequest;
import com.moing.backend.global.config.fcm.service.FcmService;
import com.moing.backend.global.response.BaseServiceResponse;
import com.moing.backend.global.config.fcm.dto.event.MultiFcmEvent;
import lombok.RequiredArgsConstructor;
import net.minidev.json.JSONObject;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.EventListener;
import java.util.List;
import java.util.Optional;

import static com.moing.backend.global.config.fcm.constant.NewMissionTitle.NEW_SINGLE_MISSION_COMING;
import static com.moing.backend.global.config.fcm.constant.NewUploadTitle.UPLOAD_NOTICE_NEW_TITLE;

@Service
@Transactional
Expand All @@ -34,10 +32,18 @@ public void sendNewMissionUploadAlarm(Member member, Mission mission) {
String title = team.getName() + " " + NEW_SINGLE_MISSION_COMING.getTitle();
String message = mission.getTitle();

Optional<List<String>> fcmTokens = teamMemberGetService.getFcmTokensExceptMe(team.getTeamId(), member.getMemberId());
if (fcmTokens.isPresent() && !fcmTokens.get().isEmpty()) {
eventPublisher.publishEvent(new FcmEvent(title, message, fcmTokens.get()));
Optional<List<MemberIdAndToken>> memberIdAndTokens = teamMemberGetService.getMemberInfoExceptMe(team.getTeamId(), member.getMemberId());
if (memberIdAndTokens.isPresent() && !memberIdAndTokens.get().isEmpty()) {
eventPublisher.publishEvent(new MultiFcmEvent(title, message, memberIdAndTokens.get(), createIdInfo(team.getTeamId(), mission.getId()), team.getName(), AlarmType.NEW_UPLOAD, PagePath.MISSION_PATH.getValue()));
}
}

private String createIdInfo(Long teamId, Long missionId) {
JSONObject jo = new JSONObject();
jo.put("isRepeated", false);
jo.put("teamId", teamId);
jo.put("missionId", missionId);
return jo.toJSONString();
}
}

Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
package com.moing.backend.domain.team.application.service;

import com.moing.backend.domain.history.domain.entity.AlarmType;
import com.moing.backend.domain.team.application.dto.response.GetLeaderInfoResponse;
import com.moing.backend.domain.team.domain.service.TeamGetService;
import com.moing.backend.domain.team.domain.service.TeamUpdateService;
import com.moing.backend.global.config.fcm.FcmConfig;
import com.moing.backend.global.config.fcm.dto.event.FcmEvent;
import com.moing.backend.global.config.fcm.dto.request.SingleRequest;
import com.moing.backend.global.config.fcm.service.FcmService;
import com.moing.backend.global.config.fcm.dto.event.SingleFcmEvent;
import lombok.RequiredArgsConstructor;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.Collections;
import java.util.List;

import static com.moing.backend.domain.history.domain.entity.PagePath.HOME_PATH;
import static com.moing.backend.global.config.fcm.constant.ApproveTeamMessage.APPROVE_TEAM_MESSAGE;

@Service
Expand All @@ -24,15 +22,16 @@ public class ApproveTeamUseCase {

private final TeamUpdateService teamUpdateService;
private final TeamGetService teamGetService;
private final FcmService fcmService;
private final ApplicationEventPublisher eventPublisher;

public void approveTeams(List<Long> teamIds){
teamUpdateService.updateTeamStatus(true, teamIds);
List<GetLeaderInfoResponse> leaderInfos=teamGetService.getLeaderInfoResponses(teamIds);
for(GetLeaderInfoResponse info: leaderInfos){
String title=APPROVE_TEAM_MESSAGE.title(info.getLeaderName(), info.getTeamName());
String body= APPROVE_TEAM_MESSAGE.body();
fcmService.sendSingleDevice(new SingleRequest(info.getLeaderFcmToken(), title, body));

eventPublisher.publishEvent(new SingleFcmEvent(info.getLeaderFcmToken(), title, body, info.getLeaderId(), "", info.getTeamName(), AlarmType.APPROVE_TEAM, HOME_PATH.getValue()));
}
}
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
package com.moing.backend.domain.team.application.service;

import com.moing.backend.domain.history.domain.entity.AlarmType;
import com.moing.backend.domain.team.application.dto.response.GetLeaderInfoResponse;
import com.moing.backend.domain.team.domain.service.TeamGetService;
import com.moing.backend.domain.team.domain.service.TeamUpdateService;
import com.moing.backend.global.config.fcm.dto.event.FcmEvent;
import com.moing.backend.global.config.fcm.dto.event.SingleFcmEvent;
import lombok.RequiredArgsConstructor;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.Collections;
import java.util.List;

import static com.moing.backend.domain.history.domain.entity.PagePath.HOME_PATH;
import static com.moing.backend.global.config.fcm.constant.ApproveTeamMessage.REJECT_TEAM_MESSAGE;

@Service
Expand All @@ -29,7 +30,8 @@ public void rejectTeams(List<Long> teamIds) {
for (GetLeaderInfoResponse info : leaderInfos) {
String title = REJECT_TEAM_MESSAGE.title(info.getLeaderName(), info.getTeamName());
String body = REJECT_TEAM_MESSAGE.body();
eventPublisher.publishEvent(new FcmEvent(title, body, Collections.singletonList(info.getLeaderFcmToken())));

eventPublisher.publishEvent(new SingleFcmEvent(info.getLeaderFcmToken(), title, body, info.getLeaderId(), "", info.getTeamName(), AlarmType.REJECT_TEAM, HOME_PATH.getValue()));
}
}
}
Loading

0 comments on commit b130f81

Please sign in to comment.