diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index 9f4197d..a80b22c 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -1,6 +1,6 @@ distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-8.2.1-bin.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-8.6-bin.zip networkTimeout=10000 validateDistributionUrl=true zipStoreBase=GRADLE_USER_HOME diff --git a/src/main/java/com/Nunbody/domain/Mail/controller/GmailController.java b/src/main/java/com/Nunbody/domain/Mail/controller/GmailController.java index 555cc74..b00b19d 100644 --- a/src/main/java/com/Nunbody/domain/Mail/controller/GmailController.java +++ b/src/main/java/com/Nunbody/domain/Mail/controller/GmailController.java @@ -2,18 +2,18 @@ import com.Nunbody.domain.Mail.domain.MailBody; import com.Nunbody.domain.Mail.domain.MailHeader; -import com.Nunbody.domain.Mail.domain.MailList; import com.Nunbody.domain.Mail.domain.PlatformType; +import com.Nunbody.domain.Mail.repository.MailBodyRepository; import com.Nunbody.domain.Mail.repository.MailRepository; import com.Nunbody.domain.member.repository.MemberRepository; import lombok.RequiredArgsConstructor; import org.springframework.data.mongodb.core.MongoTemplate; +import org.springframework.http.ResponseEntity; import org.springframework.transaction.annotation.Transactional; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; import javax.mail.*; -import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeMultipart; import javax.mail.internet.MimeUtility; import java.io.IOException; @@ -37,10 +37,12 @@ public class GmailController { private static String platformHost ="imap.gmail.com"; private final Pattern pattern = Pattern.compile("<(.*?)>"); private final MemberRepository memberRepository; + private final MailBodyRepository mailBodyRepository; + public static List mailBodies = new ArrayList<>(); @GetMapping("/gmail") - public MailList getGmailMessages() { - List mailBodies = new ArrayList<>(); - MailList mailList = MailList.builder().memberId(userId).build(); + public ResponseEntity getGmailMessages() throws MessagingException, IOException { + +// MailList mailList = MailList.builder().memberId(userId).build(); Properties props = new Properties(); props.setProperty("mail.store.protocol", "imaps"); props.setProperty("mail.imaps.host", "imap.gmail.com"); @@ -53,7 +55,7 @@ public MailList getGmailMessages() { Session session = Session.getInstance(props); StringBuilder result = new StringBuilder(); - try { + Store store = session.getStore("imaps"); store.connect("imap.gmail.com", "qogustj50@gmail.com", "eqno csrt cmcm xnaa"); @@ -61,24 +63,20 @@ public MailList getGmailMessages() { Folder inbox = store.getFolder("INBOX"); inbox.open(Folder.READ_ONLY); - Message[] messages = inbox.getMessages(1, 10); // 최근 10개 메일 가져오기 + Message[] messages = inbox.getMessages(1, 9); // 최근 10개 메일 가져오기 MailHeader latestMail = mailRepository.findFirstByMemberIdAndPlatformTypeOrderByDateDesc(userId, platformType).orElse(null); - if (latestMail == null) { - reset(messages, userId, platformType, platformHost); - } else { +// if (latestMail == null) { +// reset(messages, userId, platformType, platformHost); +// } else { processNewMails(messages, userId, platformType, platformHost, latestMail, mailBodies); - } - - mongoTemplate.insertAll(mailBodies); +// } - } catch (Exception e) { -// log.error("Error in mailSetting", e); - throw new RuntimeException("Failed to process emails", e); - } +// mongoTemplate.insertAll(mailBodies); +mailBodyRepository.saveAll(mailBodies); - return mailList; + return ResponseEntity.ok(null); } @Transactional public void reset(Message[] messages, Long userId, PlatformType platformType, String platformHost) @@ -95,20 +93,20 @@ public void reset(Message[] messages, Long userId, PlatformType platformType, St } private void processNewMails(Message[] messages, Long userId, PlatformType platformType, String platformHost, MailHeader latestMail, List mailBodies) throws MessagingException, IOException { - LocalDateTime latestMailDate = LocalDateTime.parse(latestMail.getDate(), DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")); +// LocalDateTime latestMailDate = LocalDateTime.parse(latestMail.getDate(), DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")); ZoneId seoulZone = ZoneId.of("Asia/Seoul"); for (int i = messages.length - 1; i >= 0; i--) { Message message = messages[i]; LocalDateTime messageDate = LocalDateTime.ofInstant(message.getReceivedDate().toInstant(), seoulZone); - if (messageDate.isAfter(latestMailDate)) { +// if (!messageDate.isAfter(latestMailDate)) { MailHeader mailHeader = createMailHeader(message, userId, platformType); mailRepository.save(mailHeader); mailBodies.add(extractMailBody(platformHost, message, mailHeader.getId())); - } else { - break; // No need to check older messages - } +// } else { +// break; // No need to check older messages +// } } } private String handleMultipart(Multipart multipart) throws MessagingException, IOException { diff --git a/src/main/java/com/Nunbody/global/config/SpringSecurityConfig.java b/src/main/java/com/Nunbody/global/config/SpringSecurityConfig.java index d5366f8..1cc3863 100644 --- a/src/main/java/com/Nunbody/global/config/SpringSecurityConfig.java +++ b/src/main/java/com/Nunbody/global/config/SpringSecurityConfig.java @@ -24,7 +24,7 @@ public class SpringSecurityConfig { private final JwtAuthenticationEntryPoint jwtAuthenticationEntryPoint; private final JwtTokenProvider tokenProvider; private final CorsConfig corsConfig; - private static final String[] whiteList = {"/api/member/signup", "/api/member/signin", "/api/member/validate", "/api/mail/validate"}; + private static final String[] whiteList = {"/api/member/signup", "/api/member/signin", "/api/member/validate", "/api/mail/validate","/gmail"}; @Bean public WebSecurityCustomizer webSecurityCustomizer() {