-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #384 from twenty-three-23/feature/TT-486
TT-486
- Loading branch information
Showing
31 changed files
with
561 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -43,3 +43,6 @@ data.sql | |
|
||
### Logging File ### | ||
/logs/** | ||
|
||
### FCM Admin SDK ### | ||
**/src/main/resources/peech_fcm.json |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
src/main/java/com/twentythree/peech/analyzescript/application/AnalyzeScriptFacade.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package com.twentythree.peech.analyzescript.application; | ||
|
||
import com.twentythree.peech.analyzescript.domain.AnalyzeScriptPredictor; | ||
import com.twentythree.peech.fcm.application.NotificationService; | ||
import com.twentythree.peech.script.service.ScriptService; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.scheduling.annotation.Async; | ||
import org.springframework.stereotype.Service; | ||
|
||
@RequiredArgsConstructor | ||
@Service | ||
public class AnalyzeScriptFacade { | ||
|
||
private final AnalyzeScriptPredictor analyzeScriptPredictor; | ||
private final ScriptService scriptService; | ||
private final NotificationService notificationService; | ||
|
||
@Async | ||
public void analyzeScriptAndSave(Long userId, Long scriptId, String scriptContent) { | ||
analyzeScriptPredictor.requestAnalyzeScript(scriptContent) | ||
.thenAccept(result -> { | ||
scriptService.reflectAnalyzeResult(scriptId, result); | ||
notificationService.pushNotification(userId); | ||
}); | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
src/main/java/com/twentythree/peech/analyzescript/client/AnalyzeScriptGPT.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package com.twentythree.peech.analyzescript.client; | ||
|
||
import com.twentythree.peech.analyzescript.dto.request.GPTRequest; | ||
import com.twentythree.peech.common.dto.response.GPTResponse; | ||
import org.springframework.cloud.openfeign.FeignClient; | ||
import org.springframework.web.bind.annotation.PostMapping; | ||
|
||
@FeignClient( | ||
name = "analyzeScriptGPT", | ||
url = "https://api.openai.com/") | ||
public interface AnalyzeScriptGPT { | ||
|
||
@PostMapping("v1/chat/completions") | ||
GPTResponse analyzeScript(GPTRequest gptRequest); | ||
} |
46 changes: 46 additions & 0 deletions
46
src/main/java/com/twentythree/peech/analyzescript/domain/AnalyzeScriptPredictor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package com.twentythree.peech.analyzescript.domain; | ||
|
||
import com.twentythree.peech.analyzescript.client.AnalyzeScriptGPT; | ||
import com.twentythree.peech.analyzescript.dto.request.GPTRequest; | ||
import com.twentythree.peech.aop.annotation.Trace; | ||
import com.twentythree.peech.common.dto.Message; | ||
import com.twentythree.peech.common.dto.response.GPTResponse; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.scheduling.annotation.Async; | ||
import org.springframework.stereotype.Component; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.concurrent.CompletableFuture; | ||
|
||
@Async | ||
@RequiredArgsConstructor | ||
@Component | ||
public class AnalyzeScriptPredictor { | ||
|
||
@Value("${gpt.prompt.system}") | ||
private String gptSystemPrompt; | ||
|
||
private final AnalyzeScriptGPT analyzeScriptGPT; | ||
|
||
public CompletableFuture<String> requestAnalyzeScript(String scriptContent) { | ||
List<Message> messages = new ArrayList<>(); | ||
|
||
String systemPrompt = gptSystemPrompt; | ||
messages.add(new Message("system", systemPrompt)); | ||
|
||
String userPrompt = "아래의 글은 회사 면접을 위한 자기소개서입니다. 내용을 보고 자기소개서를 분석해주세요.\n"+ | ||
"자기소개서: " + | ||
scriptContent; | ||
messages.add(new Message("user", userPrompt)); | ||
|
||
GPTRequest gptRequest = new GPTRequest("gpt-4o-mini", messages); | ||
GPTResponse gptResponse = analyzeScriptGPT.analyzeScript(gptRequest); | ||
String result = gptResponse.getChoices().get(0).getMessage().getContent(); | ||
|
||
return CompletableFuture.completedFuture(result); | ||
} | ||
|
||
} |
14 changes: 14 additions & 0 deletions
14
src/main/java/com/twentythree/peech/analyzescript/dto/request/GPTRequest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package com.twentythree.peech.analyzescript.dto.request; | ||
|
||
import com.twentythree.peech.common.dto.Message; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
|
||
import java.util.List; | ||
|
||
@Getter | ||
@AllArgsConstructor | ||
public class GPTRequest { | ||
private String model; | ||
private List<Message> messages; | ||
} |
8 changes: 8 additions & 0 deletions
8
src/main/java/com/twentythree/peech/fcm/application/NotificationService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package com.twentythree.peech.fcm.application; | ||
|
||
import com.twentythree.peech.fcm.dto.request.RequestFCMTokenDTO; | ||
|
||
public interface NotificationService { | ||
void pushNotification(Long userId); | ||
void saveOrUpdateToken(RequestFCMTokenDTO fcmTokenDTO, Long userId); | ||
} |
58 changes: 58 additions & 0 deletions
58
src/main/java/com/twentythree/peech/fcm/application/NotificationServiceImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package com.twentythree.peech.fcm.application; | ||
|
||
|
||
import com.twentythree.peech.fcm.dto.request.RequestFCMTokenDTO; | ||
import com.twentythree.peech.fcm.entity.NotificationEntity; | ||
import com.twentythree.peech.fcm.event.FCMPushedEvent; | ||
import com.twentythree.peech.fcm.infra.NotificationRepository; | ||
import com.twentythree.peech.user.entity.UserEntity; | ||
import com.twentythree.peech.user.repository.UserRepository; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.context.ApplicationEventPublisher; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
import java.util.List; | ||
|
||
@RequiredArgsConstructor | ||
@Service | ||
public class NotificationServiceImpl implements NotificationService { | ||
|
||
private final NotificationRepository notificationRepository; | ||
private final UserRepository userRepository; | ||
private final ApplicationEventPublisher applicationEventPublisher; | ||
|
||
public void pushNotification(Long userId) { | ||
List<String> fcmTokenList = notificationRepository.findAllByUserId(userId); | ||
|
||
if(fcmTokenList.isEmpty()){ | ||
throw new IllegalStateException("FCM 토큰이 존재하지 않습니다."); | ||
} | ||
|
||
applicationEventPublisher.publishEvent(new FCMPushedEvent(fcmTokenList)); | ||
} | ||
|
||
@Transactional | ||
@Override | ||
public void saveOrUpdateToken(RequestFCMTokenDTO request, Long userId) { | ||
notificationRepository.findByDeviceId(request.getDeviceId()) | ||
.ifPresentOrElse( | ||
fcmToken -> updateFCMToken(fcmToken, request.getFcmToken()), | ||
|
||
() -> { | ||
UserEntity userEntity = userRepository.findById(userId) | ||
.orElseThrow(() -> new IllegalStateException("유저가 존재하지 않습니다.")); | ||
NotificationEntity newEntity = NotificationEntity | ||
.ofCreateFCMToken( | ||
userEntity, | ||
request.getDeviceId(), | ||
request.getFcmToken() | ||
); | ||
notificationRepository.save(newEntity); | ||
}); | ||
} | ||
|
||
private void updateFCMToken(NotificationEntity originEntity, String newToken){ | ||
originEntity.updateToken(newToken); | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
src/main/java/com/twentythree/peech/fcm/client/FCMClient.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package com.twentythree.peech.fcm.client; | ||
|
||
|
||
import com.google.firebase.messaging.FirebaseMessaging; | ||
import org.springframework.cloud.openfeign.FeignClient; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.web.bind.annotation.PostMapping; | ||
|
||
@FeignClient( | ||
name = "FCMClient", | ||
url = "https://fcm.googleapis.com/v1/projects/${fcm.project-id}/messages:send" | ||
) | ||
public interface FCMClient { | ||
|
||
@PostMapping(consumes = MediaType.APPLICATION_JSON_VALUE) | ||
void sendNotification( | ||
FirebaseMessaging message | ||
); | ||
|
||
|
||
} |
37 changes: 37 additions & 0 deletions
37
src/main/java/com/twentythree/peech/fcm/config/FcmConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package com.twentythree.peech.fcm.config; | ||
|
||
import com.google.auth.oauth2.GoogleCredentials; | ||
import com.google.firebase.FirebaseApp; | ||
import com.google.firebase.FirebaseOptions; | ||
import jakarta.annotation.PostConstruct; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.core.io.ClassPathResource; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
|
||
|
||
@Configuration | ||
public class FcmConfig { | ||
|
||
@Value("${fcm.key-path}") | ||
private String fcmKeyPath; | ||
private final Logger log = LoggerFactory.getLogger(this.getClass()); | ||
|
||
@PostConstruct | ||
private void init() throws IOException { | ||
if (FirebaseApp.getApps().isEmpty()) { | ||
log.info("FirebaseApp을 초기화합니다"); | ||
InputStream firebaseAccount = new ClassPathResource(fcmKeyPath).getInputStream(); | ||
|
||
FirebaseOptions options = FirebaseOptions.builder() | ||
.setCredentials(GoogleCredentials.fromStream(firebaseAccount)) | ||
.build(); | ||
|
||
FirebaseApp.initializeApp(options); | ||
} | ||
} | ||
|
||
} |
35 changes: 35 additions & 0 deletions
35
src/main/java/com/twentythree/peech/fcm/dto/FCMMessageDTO.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package com.twentythree.peech.fcm.dto; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Builder; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
|
||
@Builder | ||
@AllArgsConstructor | ||
@NoArgsConstructor | ||
@Getter | ||
public class FCMMessageDTO { | ||
|
||
private boolean validateOnly; | ||
private Message message; | ||
|
||
@Builder | ||
@AllArgsConstructor | ||
@NoArgsConstructor | ||
@Getter | ||
public static class Message { | ||
private Notification notification; | ||
private String token; | ||
} | ||
|
||
@Builder | ||
@AllArgsConstructor | ||
@NoArgsConstructor | ||
@Getter | ||
public static class Notification { | ||
private String title; | ||
private String body; | ||
private String image; | ||
} | ||
} |
Oops, something went wrong.