Skip to content

Commit

Permalink
[feat] 나에게 보내기 api 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
qogustj committed Aug 4, 2024
1 parent 22ad057 commit 3381c8c
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<String> checkNewMailsAndGetContent(Member member, String platformHost, String platformId, String platformPassword, PlatformType platformType) {
List<String> newMailContents = new ArrayList<>();

Expand All @@ -52,21 +57,28 @@ public List<String> 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);
Expand Down Expand Up @@ -114,25 +126,29 @@ public String getTextFromMultipart(Multipart multipart) throws MessagingExceptio
public void scheduleMailCheck() {
List<Member> users = memberReader.findAll();
for (Member member : users) {
try {
List<String> 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<String> 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);
}
}
}
}
Expand Down
7 changes: 7 additions & 0 deletions src/main/java/com/Nunbody/domain/member/domain/Member.java
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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<MailHeader> mail = new ArrayList<>();
Expand All @@ -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;
}
}

0 comments on commit 3381c8c

Please sign in to comment.