diff --git a/src/main/java/com/Nunbody/domain/Kakao/service/MailScheduleService.java b/src/main/java/com/Nunbody/domain/Kakao/service/MailScheduleService.java index a89667e..3aced01 100644 --- a/src/main/java/com/Nunbody/domain/Kakao/service/MailScheduleService.java +++ b/src/main/java/com/Nunbody/domain/Kakao/service/MailScheduleService.java @@ -6,15 +6,18 @@ import com.Nunbody.domain.Mail.repository.MailRepository; import com.Nunbody.domain.Mail.service.MailService; import com.Nunbody.domain.member.domain.Member; +import com.Nunbody.domain.member.repository.MemberRepository; import com.Nunbody.domain.member.service.MemberReader; import com.Nunbody.external.WindyfloMailClient; +import com.Nunbody.global.common.EncoderDecoder; import com.fasterxml.jackson.databind.JsonNode; import jakarta.mail.*; -import jakarta.transaction.Transactional; import lombok.RequiredArgsConstructor; +import org.joda.time.DateTime; import org.jsoup.Jsoup; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; import java.io.IOException; import java.text.ParseException; @@ -34,7 +37,9 @@ public class MailScheduleService { private final MailRepository mailRepository; private final WindyfloMailClient windyfloMailClient; private final KakaoService kakaoService; + private final MemberRepository memberRepository; + @Transactional public List checkNewMailsAndGetContent(Member member, String platformHost, String platformId, String platformPassword, PlatformType platformType) { List newMailContents = new ArrayList<>(); @@ -52,21 +57,28 @@ public List checkNewMailsAndGetContent(Member member, String platformHos } else if (object instanceof Multipart) { st = getTextFromMultipart((Multipart) message.getContent()); } - String latestStoredDate = mailRepository.findFirstByMemberIdAndPlatformTypeOrderByDateDesc(member.getId(), platformType) - .map(MailHeader::getDate) - .orElse(String.valueOf(new Date(0))); // 저장된 메일이 없으면 1970년 1월 1일로 설정 + Date latestStoredDate = member.getLastTime(); +// mailRepository.findFirstByMemberIdAndPlatformTypeOrderByDateDesc(member.getId(), platformType) +// .map(MailHeader::getDate) +// .orElse(String.valueOf(new Date(0))); // 저장된 메일이 없으면 1970년 1월 1일로 설정 Date messageDate = message.getReceivedDate(); - if (messageDate.after(parseDate(latestStoredDate))) { + if ((messageDate.compareTo(latestStoredDate) > 0)) { st = getMessageContent(message); + member.updateLastTime(messageDate); + memberRepository.save(member); + JsonNode jsonNode = windyfloMailClient.summaryMail(st); + DefaultMessageDto myMsg = DefaultMessageDto.of("text", jsonNode.get("text").asText() +" 이메일이 도착했어요 확인해주세요","https://www.richam.site/login","https://www.richam.site/","메일 확인하러가기"); + + kakaoService.sendMessage(member.getAccessToken(), myMsg); } else{ return null; } - JsonNode jsonNode = windyfloMailClient.summaryMail(st); - DefaultMessageDto myMsg = DefaultMessageDto.of("text", jsonNode.get("text").asText() +" 이메일이 도착했어요 확인해주세요","https://www.richam.site/login","https://www.richam.site/","메일 확인하러가기"); - - kakaoService.sendMessage(member.getAccessToken(), myMsg); +// JsonNode jsonNode = windyfloMailClient.summaryMail(st); +// DefaultMessageDto myMsg = DefaultMessageDto.of("text", jsonNode.get("text").asText() +" 이메일이 도착했어요 확인해주세요","https://www.richam.site/login","https://www.richam.site/","메일 확인하러가기"); +// +// kakaoService.sendMessage(member.getAccessToken(), myMsg); } catch (Exception e) { // log.error("Error in checkNewMailsAndGetContent for user: " + userId, e); @@ -114,25 +126,29 @@ public String getTextFromMultipart(Multipart multipart) throws MessagingExceptio public void scheduleMailCheck() { List users = memberReader.findAll(); for (Member member : users) { - try { - List newMailContents = checkNewMailsAndGetContent( - member, - "imap.naver.com", - member.getNaverId(), - member.getNaverPassword(), - PlatformType.NAVER - ); - String body = newMailContents.get(0); - - if (!newMailContents.isEmpty()) { +// Member member = memberReader.getMemberById(8L); + if (member.getAccessToken()!=null) { + try { + + List newMailContents = checkNewMailsAndGetContent( + member, + "imap.naver.com", + member.getNaverId(), + EncoderDecoder.decodeFromBase64(member.getNaverPassword()), + PlatformType.NAVER + ); + String body = newMailContents.get(0); + + if (!newMailContents.isEmpty()) { // log.info("New mails for user {}: {}", member.getId(), newMailContents.size()); - // 여기서 새 메일 내용을 처리할 수 있습니다. - for (String content : newMailContents) { + // 여기서 새 메일 내용을 처리할 수 있습니다. + for (String content : newMailContents) { // log.info("New mail content: {}", content.substring(0, Math.min(content.length(), 100)) + "..."); + } } - } - } catch (Exception e) { + } catch (Exception e) { // log.error("Failed to check mails for user: " + member.getId(), e); + } } } } diff --git a/src/main/java/com/Nunbody/domain/member/domain/Member.java b/src/main/java/com/Nunbody/domain/member/domain/Member.java index 9c3e4ff..a608457 100644 --- a/src/main/java/com/Nunbody/domain/member/domain/Member.java +++ b/src/main/java/com/Nunbody/domain/member/domain/Member.java @@ -6,7 +6,10 @@ import lombok.*; import jakarta.persistence.*; +import org.joda.time.DateTime; + import java.util.ArrayList; +import java.util.Date; import java.util.List; @NoArgsConstructor(access = AccessLevel.PROTECTED) @@ -29,6 +32,7 @@ public class Member extends BaseTimeEntity { private String gmailPassword; private String accessToken; private String refreshToken; + private Date lastTime; @OneToMany(mappedBy = "member") @Builder.Default private List mail = new ArrayList<>(); @@ -39,4 +43,7 @@ public void updateRefreshToken(String newRefreshToken) { public void updateAccessToken(String accessToken) { this.accessToken = accessToken; } + public void updateLastTime(Date lastTime) { + this.lastTime = lastTime; + } }