From af1d353b984e1b0f803bd2f84585777f9e434357 Mon Sep 17 00:00:00 2001 From: Heo Namjun Date: Thu, 17 Mar 2022 19:09:04 +0900 Subject: [PATCH] [feat] changenoti message close #532 --- .../comment/controller/CommentController.java | 2 +- .../controller/CommentControllerImpl.java | 16 ++--- .../controller/ReCommentController.java | 2 +- .../controller/ReCommentControllerImpl.java | 26 +++----- .../api/domain/notification/DTO/NotiDTO.java | 4 ++ .../controller/NotificationController.java | 3 +- .../NotificationControllerImpl.java | 17 ++++- .../api/domain/notification/domain/Noti.java | 6 +- .../repository/NotificationRepository.java | 5 +- .../service/NotificationService.java | 38 ++++++++--- .../api/global/firebase/FCMInitializer.java | 39 ----------- .../firebase/controller/FCMController.java | 46 ------------- .../firebase/request/NotificationRequest.java | 24 ------- .../global/firebase/service/FCMService.java | 22 ------- .../firebase/service/FCMServiceImpl.java | 64 ------------------- .../handler/AdminAccessHandler.java | 1 + 16 files changed, 77 insertions(+), 238 deletions(-) delete mode 100644 server/api/src/main/java/com/blind/api/global/firebase/FCMInitializer.java delete mode 100644 server/api/src/main/java/com/blind/api/global/firebase/controller/FCMController.java delete mode 100644 server/api/src/main/java/com/blind/api/global/firebase/request/NotificationRequest.java delete mode 100644 server/api/src/main/java/com/blind/api/global/firebase/service/FCMService.java delete mode 100644 server/api/src/main/java/com/blind/api/global/firebase/service/FCMServiceImpl.java diff --git a/server/api/src/main/java/com/blind/api/domain/comment/controller/CommentController.java b/server/api/src/main/java/com/blind/api/domain/comment/controller/CommentController.java index b2427b28..3c670f39 100644 --- a/server/api/src/main/java/com/blind/api/domain/comment/controller/CommentController.java +++ b/server/api/src/main/java/com/blind/api/domain/comment/controller/CommentController.java @@ -32,5 +32,5 @@ void deleteComment(@RequestParam("commentId") @Positive (message = "{invalid.req CommentResponseDTO findCommentByUserId (@SortDefault.SortDefaults({@SortDefault(sort = "id", direction = Sort.Direction.DESC)}) Pageable pageable, HttpServletRequest request); - CommentListResponseDTO findCommentByPost (@RequestParam("boardId") @Positive (message = "{invalid.request}")Long boardId, @RequestParam("postId") @Positive (message = "{invalid.request}")Long postId, HttpServletRequest request); + CommentListResponseDTO findCommentByPost (@RequestParam("postId") @Positive (message = "{invalid.request}")Long postId, HttpServletRequest request); } diff --git a/server/api/src/main/java/com/blind/api/domain/comment/controller/CommentControllerImpl.java b/server/api/src/main/java/com/blind/api/domain/comment/controller/CommentControllerImpl.java index 5fa85be4..8f476b08 100644 --- a/server/api/src/main/java/com/blind/api/domain/comment/controller/CommentControllerImpl.java +++ b/server/api/src/main/java/com/blind/api/domain/comment/controller/CommentControllerImpl.java @@ -42,17 +42,13 @@ public void saveComment(Long boardId, Long postId, CommentRequestDTO requestDTO, Post post = postService.findById(postId); if (post.getCommentCnt() >= 1000) throw new BusinessException("{invalid.request}"); - User author = userService.findById(post.getAuthorId()); + User postAuthor = userService.findById(post.getAuthorId()); commentService.save(boardId,post, user, requestDTO.getContent()); postService.updateComment(postId, 1L); - if (author.getId() != user.getId()) { - if (author.getIsChecked() == false) { - Noti noti = notificationService.findByUserAndPost(author, post); - if (noti != null) - notificationService.delete(noti.getId()); - } - notificationService.save(author, post, "post", post.getTitle(), requestDTO.getContent()); - userService.setCheck(author); + notificationService.checkNoti(postAuthor, user, post); + if (postAuthor.getId() != user.getId()) { + notificationService.save(postAuthor, post, "post", post.getTitle(), requestDTO.getContent()); + userService.setCheck(postAuthor); } } @@ -88,7 +84,7 @@ public CommentResponseDTO findCommentByUserId (Pageable pageable, HttpServletReq } @RequestMapping(value={"/comment"}, method = RequestMethod.GET) - public CommentListResponseDTO findCommentByPost (Long boardId, Long postId, HttpServletRequest request){ + public CommentListResponseDTO findCommentByPost (Long postId, HttpServletRequest request){ Post post = postService.findById(postId); User user = tokenService.findUserByAccessToken(HeaderUtil.getAccessToken(request)); HashMap commentDTOHashMap = new LinkedHashMap<>(1001, 1); diff --git a/server/api/src/main/java/com/blind/api/domain/comment/controller/ReCommentController.java b/server/api/src/main/java/com/blind/api/domain/comment/controller/ReCommentController.java index d7df860a..aaf71720 100644 --- a/server/api/src/main/java/com/blind/api/domain/comment/controller/ReCommentController.java +++ b/server/api/src/main/java/com/blind/api/domain/comment/controller/ReCommentController.java @@ -8,5 +8,5 @@ import javax.validation.Valid; public interface ReCommentController { - void createReComment(@RequestParam(value = "commentId")Long targetCmmtId, @RequestBody @Valid CommentRequestDTO content, HttpServletRequest request); + void saveReComment(@RequestParam(value = "commentId")Long targetCmmtId, @RequestBody @Valid CommentRequestDTO content, HttpServletRequest request); } diff --git a/server/api/src/main/java/com/blind/api/domain/comment/controller/ReCommentControllerImpl.java b/server/api/src/main/java/com/blind/api/domain/comment/controller/ReCommentControllerImpl.java index 38eafc5c..48262202 100644 --- a/server/api/src/main/java/com/blind/api/domain/comment/controller/ReCommentControllerImpl.java +++ b/server/api/src/main/java/com/blind/api/domain/comment/controller/ReCommentControllerImpl.java @@ -33,7 +33,7 @@ public class ReCommentControllerImpl implements ReCommentController { private final NotificationService notificationService; @RequestMapping(value = "/recomment", method = RequestMethod.POST) - public void createReComment(Long targetCmmtId, CommentRequestDTO requestDTO, HttpServletRequest request) { + public void saveReComment(Long targetCmmtId, CommentRequestDTO requestDTO, HttpServletRequest request) { Comment targetCmmt = commentService.findCommentById(targetCmmtId); Comment rootCmmt = targetCmmt; while (rootCmmt.getRootCommentId() != null) @@ -43,26 +43,18 @@ public void createReComment(Long targetCmmtId, CommentRequestDTO requestDTO, Htt Post post = rootCmmt.getPost(); reCommentService.save(targetCmmt, rootCmmtId, user.getId(), requestDTO.getContent()); postService.updateComment(rootCmmt.getPost().getId(), 1L); - User rtAuthor = userService.findById(rootCmmt.getAuthorId()); + User targetAuthor = userService.findById(rootCmmt.getTargetAuthorId()); User pstAuthor = userService.findById(post.getAuthorId()); if (pstAuthor.getId() != user.getId()) { - if (pstAuthor.getIsChecked() == false) { - Noti noti = notificationService.findByUserAndPost(pstAuthor, post); - if (noti != null) - notificationService.delete(noti.getId()); - } + notificationService.checkNoti(pstAuthor, user, post); notificationService.save(pstAuthor, post, "post", post.getTitle(), requestDTO.getContent()); userService.setCheck(pstAuthor); } - if (rtAuthor.getId() != user.getId()) { - if (rtAuthor.getIsChecked() == false) { - Noti noti = notificationService.findByUserAndPost(rtAuthor, post); - if (noti != null) - notificationService.delete(noti.getId()); - } - notificationService.save(rtAuthor, post, "comment", rootCmmt.getContent(), requestDTO.getContent()); - userService.setCheck(rtAuthor); - } + if (targetAuthor.getId() != user.getId()) { + notificationService.checkNoti(targetAuthor, user, post); + notificationService.save(targetAuthor, post, "comment", rootCmmt.getContent(), requestDTO.getContent()); + userService.setCheck(targetAuthor); } - } \ No newline at end of file + } +} \ No newline at end of file diff --git a/server/api/src/main/java/com/blind/api/domain/notification/DTO/NotiDTO.java b/server/api/src/main/java/com/blind/api/domain/notification/DTO/NotiDTO.java index 2e7789c3..e921c831 100644 --- a/server/api/src/main/java/com/blind/api/domain/notification/DTO/NotiDTO.java +++ b/server/api/src/main/java/com/blind/api/domain/notification/DTO/NotiDTO.java @@ -4,6 +4,8 @@ import lombok.Getter; import lombok.Setter; +import java.time.LocalDateTime; + @Getter @Setter public class NotiDTO { @@ -12,6 +14,7 @@ public class NotiDTO { private String contentType; private String title; private String content; + private LocalDateTime modifiedDate; public NotiDTO(Noti noti) { this.id = noti.getId(); @@ -19,5 +22,6 @@ public NotiDTO(Noti noti) { this.contentType = noti.getContentType(); this.title = noti.getTitle(); this.content = noti.getContent(); + this.modifiedDate = noti.getModifiedDate(); } } diff --git a/server/api/src/main/java/com/blind/api/domain/notification/controller/NotificationController.java b/server/api/src/main/java/com/blind/api/domain/notification/controller/NotificationController.java index 05a43cd1..ee6fa905 100644 --- a/server/api/src/main/java/com/blind/api/domain/notification/controller/NotificationController.java +++ b/server/api/src/main/java/com/blind/api/domain/notification/controller/NotificationController.java @@ -1,5 +1,6 @@ package com.blind.api.domain.notification.controller; +import com.blind.api.domain.notification.DTO.NotiDTO; import com.blind.api.domain.notification.domain.Noti; import org.springframework.web.bind.annotation.RequestParam; @@ -8,6 +9,6 @@ public interface NotificationController { - List getNoti(HttpServletRequest request); + List getNoti(HttpServletRequest request); void delNoti(HttpServletRequest request, @RequestParam("id") Long id); } diff --git a/server/api/src/main/java/com/blind/api/domain/notification/controller/NotificationControllerImpl.java b/server/api/src/main/java/com/blind/api/domain/notification/controller/NotificationControllerImpl.java index 14c56eed..4fe732f6 100644 --- a/server/api/src/main/java/com/blind/api/domain/notification/controller/NotificationControllerImpl.java +++ b/server/api/src/main/java/com/blind/api/domain/notification/controller/NotificationControllerImpl.java @@ -1,5 +1,6 @@ package com.blind.api.domain.notification.controller; +import com.blind.api.domain.notification.DTO.NotiDTO; import com.blind.api.domain.notification.domain.Noti; import com.blind.api.domain.notification.service.NotificationService; import com.blind.api.domain.security.jwt.v1.service.TokenService; @@ -12,6 +13,7 @@ import org.springframework.web.bind.annotation.RestController; import javax.servlet.http.HttpServletRequest; +import java.util.ArrayList; import java.util.List; @RestController @@ -22,10 +24,15 @@ public class NotificationControllerImpl implements NotificationController { private final TokenService tokenService; @RequestMapping(value = {"/notification"}, method= RequestMethod.GET) - public List getNoti(HttpServletRequest request){ + public List getNoti(HttpServletRequest request){ User user = tokenService.findUserByAccessToken(HeaderUtil.getAccessToken(request)); + List notiList = notificationService.getNoti(user); + List dtoList = new ArrayList<>(); + notiList.stream().forEach(noti -> { + dtoList.add(new NotiDTO(noti)); + }); userService.delCheck(user); - return notificationService.getNoti(user); + return dtoList; } @RequestMapping(value = {"/notification"}, method= RequestMethod.DELETE) @@ -33,4 +40,10 @@ public void delNoti(HttpServletRequest request, Long id){ User user = tokenService.findUserByAccessToken(HeaderUtil.getAccessToken(request)); notificationService.delete(id); } + + @RequestMapping(value = {"/notification/all"}, method= RequestMethod.DELETE) + public void delAllNoti(HttpServletRequest request, Long id){ + User user = tokenService.findUserByAccessToken(HeaderUtil.getAccessToken(request)); + notificationService.deleteByUser(user); + } } diff --git a/server/api/src/main/java/com/blind/api/domain/notification/domain/Noti.java b/server/api/src/main/java/com/blind/api/domain/notification/domain/Noti.java index e7d1a042..8a3447ed 100644 --- a/server/api/src/main/java/com/blind/api/domain/notification/domain/Noti.java +++ b/server/api/src/main/java/com/blind/api/domain/notification/domain/Noti.java @@ -2,6 +2,7 @@ import com.blind.api.domain.post.v2.domain.Post; import com.blind.api.domain.user.v2.domain.User; +import com.blind.api.global.entity.BaseTimeEntity; import com.fasterxml.jackson.annotation.JsonIgnore; import lombok.Builder; import lombok.Getter; @@ -16,7 +17,7 @@ @Setter @NoArgsConstructor @DynamicInsert -public class Noti { +public class Noti extends BaseTimeEntity { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @Column(name = "id") @@ -40,6 +41,9 @@ public class Noti { @Column(name = "content") private String content; + @Column(name = "count", columnDefinition = "Integer default 1") + private Long count; + @Builder public Noti(User user, Post post, String contentType, String title, String content) { this.user = user; diff --git a/server/api/src/main/java/com/blind/api/domain/notification/repository/NotificationRepository.java b/server/api/src/main/java/com/blind/api/domain/notification/repository/NotificationRepository.java index 192d3e2b..1981d7cc 100644 --- a/server/api/src/main/java/com/blind/api/domain/notification/repository/NotificationRepository.java +++ b/server/api/src/main/java/com/blind/api/domain/notification/repository/NotificationRepository.java @@ -4,12 +4,15 @@ import com.blind.api.domain.post.v2.domain.Post; import com.blind.api.domain.user.v2.domain.User; import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Modifying; +import org.springframework.data.jpa.repository.Query; import java.util.List; import java.util.Optional; public interface NotificationRepository extends JpaRepository { - List findByUser(User user); + List findByUserOrderByCreatedDateDesc(User user); void deleteById(Long id); Noti findByUserAndPost(User user, Post post); + void deleteAllByUser(User user); } diff --git a/server/api/src/main/java/com/blind/api/domain/notification/service/NotificationService.java b/server/api/src/main/java/com/blind/api/domain/notification/service/NotificationService.java index a640f714..4168e662 100644 --- a/server/api/src/main/java/com/blind/api/domain/notification/service/NotificationService.java +++ b/server/api/src/main/java/com/blind/api/domain/notification/service/NotificationService.java @@ -9,7 +9,6 @@ import javax.transaction.Transactional; import java.util.List; -import java.util.Optional; @Service @AllArgsConstructor @@ -18,18 +17,25 @@ public class NotificationService { @Transactional public Noti save(User user, Post post, String contentType, String title, String content) { - return notificationRepository.save(Noti.builder() - .user(user) - .post(post) - .contentType(contentType) - .title(title) - .content(content) - .build()); + Noti noti = notificationRepository.findByUserAndPost(user, post); + if (noti != null) { + noti = notificationRepository.save(Noti.builder() + .user(user) + .post(post) + .contentType(contentType) + .title(title) + .content(content) + .build()); + } else { + noti.setContent(content); + noti.setCount(noti.getCount() + 1L); + } + return noti; } @Transactional public List getNoti(User user) { - return notificationRepository.findByUser(user); + return notificationRepository.findByUserOrderByCreatedDateDesc(user); } @Transactional @@ -40,4 +46,18 @@ public void delete(Long id) { public Noti findByUserAndPost(User user, Post post) { return notificationRepository.findByUserAndPost(user, post); } + + @Transactional + public void deleteByUser(User user) { + notificationRepository.deleteAllByUser(user); + } + + @Transactional + public void checkNoti(User rootUser, User authorUser, Post post) { + if (rootUser.getIsChecked() == true) { + Noti noti = notificationRepository.findByUserAndPost(rootUser, post); + if (noti != null) + notificationRepository.deleteById(post.getId()); + } + } } diff --git a/server/api/src/main/java/com/blind/api/global/firebase/FCMInitializer.java b/server/api/src/main/java/com/blind/api/global/firebase/FCMInitializer.java deleted file mode 100644 index 1e8f31da..00000000 --- a/server/api/src/main/java/com/blind/api/global/firebase/FCMInitializer.java +++ /dev/null @@ -1,39 +0,0 @@ -/* -package com.blind.api.global.firebase; - -import com.google.auth.oauth2.GoogleCredentials; -import com.google.firebase.FirebaseApp; -import com.google.firebase.FirebaseOptions; - -import lombok.extern.slf4j.Slf4j; -import org.springframework.beans.factory.annotation.Value; -import org.springframework.core.io.ClassPathResource; -import org.springframework.stereotype.Component; - -import javax.annotation.PostConstruct; -import java.io.IOException; -import java.io.InputStream; - -@Slf4j -@Component -public class FCMInitializer { - - @Value("${fcm.certification}") - private String googleApplicationCredentials; - - @PostConstruct - public void initialize() throws IOException { - ClassPathResource resource = new ClassPathResource(googleApplicationCredentials); - - try (InputStream is = resource.getInputStream()) { - FirebaseOptions options = FirebaseOptions.builder() - .setCredentials(GoogleCredentials.fromStream(is)) - .build(); - - if (FirebaseApp.getApps().isEmpty()) { - FirebaseApp.initializeApp(options); - log.info("FirebaseApp initialization complete"); - } - } - } -}*/ diff --git a/server/api/src/main/java/com/blind/api/global/firebase/controller/FCMController.java b/server/api/src/main/java/com/blind/api/global/firebase/controller/FCMController.java deleted file mode 100644 index 3cc006f6..00000000 --- a/server/api/src/main/java/com/blind/api/global/firebase/controller/FCMController.java +++ /dev/null @@ -1,46 +0,0 @@ -/* -package com.blind.api.global.firebase.controller; - -import com.blind.api.domain.user.v2.domain.User; -import com.blind.api.global.firebase.request.NotificationRequest; -import com.blind.api.global.firebase.service.FCMService; -import com.blind.api.global.utils.HeaderUtil; -import com.google.cloud.firestore.Firestore; -import com.google.firebase.FirebaseApp; -import com.google.firebase.auth.FirebaseAuth; -import com.google.firebase.auth.FirebaseToken; -import com.google.firebase.messaging.FirebaseMessaging; -import com.google.firebase.messaging.Message; -import lombok.AllArgsConstructor; -import org.springframework.stereotype.Controller; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RequestMethod; - -import javax.servlet.http.HttpServletRequest; - -@Controller -@AllArgsConstructor -public class FCMController { - private final FCMService fcmService; - - @RequestMapping(value={"/noti"}, method= RequestMethod.GET) - public Message getUserInfo(HttpServletRequest request) { - FirebaseApp.initializeApp(); - Firestore - FirebaseToken decodedToken = FirebaseAuth.getInstance().verifyIdToken(idToken); - String uid = decodedToken.getUid(); - - firebase.auth().currentUser.getIdToken(*/ -/* forceRefresh *//* - true).then(function(idToken) { - // Send token to your backend via HTTPS - // ... - }).catch(function(error) { - // Handle error - }); - - NotificationRequest notificationRequest = new NotificationRequest("알림", "내용", "") - return fcmService.send(NotificationRequest.builder().build()); - } -} -*/ diff --git a/server/api/src/main/java/com/blind/api/global/firebase/request/NotificationRequest.java b/server/api/src/main/java/com/blind/api/global/firebase/request/NotificationRequest.java deleted file mode 100644 index 8004c890..00000000 --- a/server/api/src/main/java/com/blind/api/global/firebase/request/NotificationRequest.java +++ /dev/null @@ -1,24 +0,0 @@ -/* -package com.blind.api.global.firebase.request; - -import lombok.Builder; -import lombok.Getter; -import lombok.NoArgsConstructor; -import lombok.ToString; - -@Getter -@ToString -@NoArgsConstructor -public class NotificationRequest { - private String title; - private String message; - private String token; - - @Builder - public NotificationRequest(String title, String message, String token) { - this.title = title; - this.message = message; - this.token = token; - } -} -*/ diff --git a/server/api/src/main/java/com/blind/api/global/firebase/service/FCMService.java b/server/api/src/main/java/com/blind/api/global/firebase/service/FCMService.java deleted file mode 100644 index 9900eb11..00000000 --- a/server/api/src/main/java/com/blind/api/global/firebase/service/FCMService.java +++ /dev/null @@ -1,22 +0,0 @@ -/* -package com.blind.api.global.firebase.service; - -import com.blind.api.domain.user.v2.domain.User; -import com.blind.api.global.firebase.request.NotificationRequest; -import com.google.firebase.messaging.Message; - -import java.util.concurrent.ExecutionException; - -public interface FCMService { - - void saveToken(User user); - - String getToken(User user); - - void deleteToken(User user); - - boolean hasKey(User user); - public Message send(final NotificationRequest notificationRequest) throws InterruptedException, ExecutionException; - } - -*/ diff --git a/server/api/src/main/java/com/blind/api/global/firebase/service/FCMServiceImpl.java b/server/api/src/main/java/com/blind/api/global/firebase/service/FCMServiceImpl.java deleted file mode 100644 index 58303aa5..00000000 --- a/server/api/src/main/java/com/blind/api/global/firebase/service/FCMServiceImpl.java +++ /dev/null @@ -1,64 +0,0 @@ -/* -package com.blind.api.global.firebase.service; - -import com.blind.api.domain.user.v2.domain.User; -import com.blind.api.global.firebase.request.NotificationRequest; -import com.google.firebase.FirebaseApp; -import com.google.firebase.messaging.FirebaseMessaging; -import com.google.firebase.messaging.Message; -import com.google.firebase.messaging.WebpushConfig; -import com.google.firebase.messaging.WebpushNotification; -import lombok.extern.slf4j.Slf4j; -import lombok.AllArgsConstructor; -import org.springframework.stereotype.Service; - -import javax.transaction.Transactional; -import java.util.concurrent.ExecutionException; - -@Service -@AllArgsConstructor -@Slf4j -public class FCMServiceImpl implements FCMService { - - private final FirebaseApp firebaseApp; - - @Override - @Transactional - public void saveToken(User user) { - user.setFcmToken(String.valueOf(firebaseApp.getOptions().hashCode())); - } - - @Override - @Transactional - public String getToken(User user) { - return null; - } - - @Override - @Transactional - public void deleteToken(User user) { - - } - - @Override - @Transactional - public boolean hasKey(User user) { - return false; - } - - @Transactional - public Message send(final NotificationRequest notificationRequest) throws InterruptedException, ExecutionException { - Message message = Message.builder() - .setToken(notificationRequest.getToken()) - .setWebpushConfig(WebpushConfig.builder().putHeader("ttl", "300") - .setNotification(new WebpushNotification(notificationRequest.getTitle(), - notificationRequest.getMessage())) - .build()) - .build(); - - String response = FirebaseMessaging.getInstance().sendAsync(message).get(); - log.info("Sent message: " + response); - return message; - } -} -*/ diff --git a/server/api/src/main/java/com/blind/api/web/management/handler/AdminAccessHandler.java b/server/api/src/main/java/com/blind/api/web/management/handler/AdminAccessHandler.java index 2e69d10d..e6b96b3e 100644 --- a/server/api/src/main/java/com/blind/api/web/management/handler/AdminAccessHandler.java +++ b/server/api/src/main/java/com/blind/api/web/management/handler/AdminAccessHandler.java @@ -43,6 +43,7 @@ public Object doCheckAdmin(ProceedingJoinPoint joinPoint) throws Throwable{ Method method = signature.getMethod(); Object[] args = joinPoint.getArgs(); + /* Request 정보 가져오기 */ HttpServletRequest request = ((ServletRequestAttributes)RequestContextHolder.currentRequestAttributes()).getRequest(); HttpServletResponse response = ((ServletRequestAttributes)RequestContextHolder.currentRequestAttributes()).getResponse();