From d285415e247ab343abe374586a60cf1b30860644 Mon Sep 17 00:00:00 2001 From: youngdon Date: Wed, 14 Feb 2024 21:10:35 +0900 Subject: [PATCH] =?UTF-8?q?[FEAT]=20fcm=20token=20user=20=EC=A0=80?= =?UTF-8?q?=EC=9E=A5=20=EB=A1=9C=EC=A7=81=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/wooyeon/yeon/chat/dto/FcmDto.java | 23 +++++++++++++ .../yeon/chat/service/RoomService.java | 28 ---------------- .../common/fcm/controller/FcmController.java | 5 +++ .../yeon/common/fcm/service/FcmService.java | 32 +++++++++++++++---- .../wooyeon/yeon/exception/ExceptionCode.java | 2 +- .../com/wooyeon/yeon/user/domain/User.java | 4 +++ .../java/com/wooyeon/yeon/user/UserTest.java | 21 ++++++------ 7 files changed, 69 insertions(+), 46 deletions(-) create mode 100644 src/main/java/com/wooyeon/yeon/chat/dto/FcmDto.java diff --git a/src/main/java/com/wooyeon/yeon/chat/dto/FcmDto.java b/src/main/java/com/wooyeon/yeon/chat/dto/FcmDto.java new file mode 100644 index 0000000..c755c9b --- /dev/null +++ b/src/main/java/com/wooyeon/yeon/chat/dto/FcmDto.java @@ -0,0 +1,23 @@ +package com.wooyeon.yeon.chat.dto; + +import lombok.Builder; +import lombok.Getter; +import org.springframework.http.HttpStatus; + +import javax.validation.constraints.NotNull; + +public class FcmDto { + + @Getter + @Builder + public static class Request { + @NotNull + private String fcmToken; + } + + @Getter + @Builder + public static class Response { + private HttpStatus status; + } +} diff --git a/src/main/java/com/wooyeon/yeon/chat/service/RoomService.java b/src/main/java/com/wooyeon/yeon/chat/service/RoomService.java index 1891b24..c2f2a87 100644 --- a/src/main/java/com/wooyeon/yeon/chat/service/RoomService.java +++ b/src/main/java/com/wooyeon/yeon/chat/service/RoomService.java @@ -103,34 +103,6 @@ public List searchMatchRoomList(String searchWord) { User loginUser = userRepository.findOptionalByEmail(securityService.getCurrentUserEmail()) .orElseThrow(() -> new WooyeonException(ExceptionCode.USER_NOT_FOUND)); - // 채팅방 내 검색 단어 포함 항목 조회 후 추가 -// List chatList = chatRepository.findAllByMessageContains(searchWord); - -// if (0 < chatList.size() && !chatList.isEmpty()) { -// for (Chat chat : chatList) { -// UserMatch userMatch = chat.getUserMatch(); -// -// Long matchUserId = getMatchUserId(userMatch, loginUser); -// -// User user = userRepository.findOptionalByUserId(matchUserId) -// .orElseThrow(() -> new IllegalArgumentException("User does not exist")); -// -// // 상대방 프로필 정보 조회 -// Profile profile = profileRepository.findById(user.getUserProfile().getId()) -// .orElseThrow(() -> new IllegalArgumentException("User Profile does not exist")); -// -// Optional profilePhoto = profilePhotoRepository.findByProfileId(profile.getId()); -// -// RoomDto.SearchRoomResponse response = RoomDto.SearchRoomResponse.builder() -// .matchId(userMatch.getMatchId()) -// .profilePhoto(!profilePhoto.isPresent() ? null : profilePhoto.get().getPhotoUrl()) -// .name(profile.getNickname()) -// .build(); -// -// searchRoomList.add(response); -// } -// } - List searchRoomList = new ArrayList<>(); // 이름이 같은 사람 조회 후 추가 diff --git a/src/main/java/com/wooyeon/yeon/common/fcm/controller/FcmController.java b/src/main/java/com/wooyeon/yeon/common/fcm/controller/FcmController.java index 1fd790c..a45f519 100644 --- a/src/main/java/com/wooyeon/yeon/common/fcm/controller/FcmController.java +++ b/src/main/java/com/wooyeon/yeon/common/fcm/controller/FcmController.java @@ -23,4 +23,9 @@ public String getAccessToken() throws IOException { public void sendNotificationByToken(@RequestBody FcmDto.Request request) throws IOException { fcmService.sendMessageTo(request); } + + @PostMapping("/save") + public com.wooyeon.yeon.chat.dto.FcmDto.Response saveFcmToken(com.wooyeon.yeon.chat.dto.FcmDto.Request request) { + return fcmService.saveFcmToken(request); + } } diff --git a/src/main/java/com/wooyeon/yeon/common/fcm/service/FcmService.java b/src/main/java/com/wooyeon/yeon/common/fcm/service/FcmService.java index 5a1f213..c66d6a4 100644 --- a/src/main/java/com/wooyeon/yeon/common/fcm/service/FcmService.java +++ b/src/main/java/com/wooyeon/yeon/common/fcm/service/FcmService.java @@ -3,20 +3,23 @@ import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; import com.google.auth.oauth2.GoogleCredentials; -import com.google.firebase.messaging.FirebaseMessaging; -import com.google.firebase.messaging.FirebaseMessagingException; -import com.google.firebase.messaging.Message; -import com.google.firebase.messaging.Notification; import com.wooyeon.yeon.common.fcm.dto.FcmDto; +import com.wooyeon.yeon.common.security.SecurityService; +import com.wooyeon.yeon.exception.ExceptionCode; +import com.wooyeon.yeon.exception.WooyeonException; import com.wooyeon.yeon.user.domain.User; import com.wooyeon.yeon.user.repository.UserRepository; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; -import okhttp3.*; +import okhttp3.MediaType; +import okhttp3.OkHttpClient; +import okhttp3.Request; +import okhttp3.RequestBody; import org.apache.http.HttpHeaders; -import org.springframework.beans.factory.annotation.Value; import org.springframework.core.io.ClassPathResource; +import org.springframework.http.HttpStatus; import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; import java.io.IOException; import java.util.List; @@ -26,6 +29,8 @@ @RequiredArgsConstructor public class FcmService { private final ObjectMapper objectMapper; + private final UserRepository userRepository; + private final SecurityService securityService; public String getAccessToken() throws IOException { String firebaseConfigPath = "fcm-secret.json"; @@ -103,4 +108,19 @@ public String makeMessage( return objectMapper.writeValueAsString(fcmMessage); } + + @Transactional + public com.wooyeon.yeon.chat.dto.FcmDto.Response saveFcmToken(com.wooyeon.yeon.chat.dto.FcmDto.Request request) { + + User loginUser = userRepository.findOptionalByEmail(securityService.getCurrentUserEmail()) + .orElseThrow(() -> new WooyeonException(ExceptionCode.LOGIN_USER_NOT_FOUND)); + + loginUser.setTargetToken(request.getFcmToken()); + + userRepository.save(loginUser); + + return com.wooyeon.yeon.chat.dto.FcmDto.Response.builder() + .status(HttpStatus.OK) + .build(); + } } diff --git a/src/main/java/com/wooyeon/yeon/exception/ExceptionCode.java b/src/main/java/com/wooyeon/yeon/exception/ExceptionCode.java index 62a097d..b13e519 100644 --- a/src/main/java/com/wooyeon/yeon/exception/ExceptionCode.java +++ b/src/main/java/com/wooyeon/yeon/exception/ExceptionCode.java @@ -20,7 +20,7 @@ public enum ExceptionCode { INVALID_REFRESH_TOKEN("Refresh Token 값이 유효하지 않습니다", "4007"), NOT_EXIST_REFRESH_TOKEN("Refresh Token 값이 존재하지 않습니다", "4008"), NOT_MATCH_REFRESH_TOKEN("DB 내 Refresh Token 값이 일치하지 않습니다", "4009"), - SECURITY_CONTEXT_IS_EMPTY("SECURITY CONTEXT USER 정보가 존재하지 않습니다", "4010"); + SECURITY_CONTEXT_IS_EMPTY("Security Context USER 정보가 존재하지 않습니다", "4010"); private final String description; private final String resultCode; diff --git a/src/main/java/com/wooyeon/yeon/user/domain/User.java b/src/main/java/com/wooyeon/yeon/user/domain/User.java index eeb4fe3..e2074c6 100644 --- a/src/main/java/com/wooyeon/yeon/user/domain/User.java +++ b/src/main/java/com/wooyeon/yeon/user/domain/User.java @@ -95,6 +95,10 @@ public void setProfile(Profile profile) { this.profile = profile; } + public void setTargetToken(String targetToken) { + this.targetToken = targetToken; + } + public void updateEmailAuth(boolean emailAuth) { this.emailAuth = emailAuth; } public void updatePhoneAuth(boolean phoneAuth) { this.phoneAuth = phoneAuth; } public Long getUserId() {return this.userId;} diff --git a/src/test/java/com/wooyeon/yeon/user/UserTest.java b/src/test/java/com/wooyeon/yeon/user/UserTest.java index 0b77583..6c163a0 100644 --- a/src/test/java/com/wooyeon/yeon/user/UserTest.java +++ b/src/test/java/com/wooyeon/yeon/user/UserTest.java @@ -1,6 +1,5 @@ package com.wooyeon.yeon.user; -import com.wooyeon.yeon.user.domain.Profile; import com.wooyeon.yeon.user.domain.User; import com.wooyeon.yeon.user.repository.UserRepository; import com.wooyeon.yeon.user.service.UserService; @@ -20,16 +19,16 @@ public class UserTest { @Autowired private UserService userService; // -// @Test -// public void createUser() { -// User user = User.builder() -// .email("match1@naver.com") -// .phone("01012345678") -// .password(passwordEncoder.encode("1234")) -// .build(); -// -// userRepository.save(user); -// } + @Test + public void createUser() { + User user = User.builder() + .email("young2@naver.com") + .phone("01012345678") + .password(passwordEncoder.encode("A!12345678")) + .build(); + + userRepository.save(user); + } // // passwordEncoder 사용 @Test