intro historyId in gmail

This commit is contained in:
heyethereum
2024-08-17 22:38:00 +08:00
parent b8ee29ce9a
commit 9f4771f16d
4 changed files with 95 additions and 48 deletions

View File

@@ -7,6 +7,7 @@ import lombok.Data;
import lombok.NoArgsConstructor;
import org.hibernate.annotations.UuidGenerator;
import java.math.BigInteger;
import java.time.OffsetDateTime;
import java.util.UUID;
@@ -33,7 +34,7 @@ public class GmailEmailEntity {
private String threadId;
@Column(name = "history_id")
private Long historyId;
private BigInteger historyId;
@Column(name= "subject")
private String subject;

View File

@@ -33,6 +33,7 @@ import com.safeqr.app.gmail.repository.GmailEmailRespository;
import com.safeqr.app.gmail.repository.GmailUrlsRespository;
import com.safeqr.app.qrcode.model.QRCodeModel;
import com.safeqr.app.qrcode.service.QRCodeTypeService;
import com.safeqr.app.user.entity.UserEntity;
import com.safeqr.app.user.service.UserService;
import com.safeqr.app.utils.DateParsingUtils;
import org.apache.commons.codec.binary.Base64;
@@ -52,6 +53,7 @@ import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.lang.Thread;
import java.math.BigInteger;
import java.net.ConnectException;
import java.net.URI;
import java.net.URISyntaxException;
@@ -148,22 +150,36 @@ public class GmailService {
public ScannedGmailResponseDto getEmail(String userId, String accessToken, String refreshToken) throws IOException {
Gmail service = refreshAndGetGmailService(accessToken, refreshToken);
logger.info("Gmail service initialized: {}", service);
List<EmailMessage> emailMessagesList = new ArrayList<>();
String meUserId = "me";
String nextPageToken = null;
UserEntity userEntity = userService.getUserByIdForGmail(userId);
BigInteger historyId = userEntity.getGmailHistoryId();
// Fetch history if historyId is not 0 (Default db value)
if (historyId.compareTo(BigInteger.ZERO) != 0) {
logger.info("HistoryId from db: {}", historyId);
ListHistoryResponse historyResponse = service.users().history().list(meUserId)
.setStartHistoryId(historyId)
.execute();
List<History> historyList = historyResponse.getHistory();
if (historyList != null) {
for (History history : historyList) {
logger.info("History Response - History Id: {}, Message Id: {}", history.getId(), history.getMessages().get(0).getId());
List<Message> messages = history.getMessages();
for (Message message : messages) {
EmailMessage emailMessage = processMessage(service, meUserId, message);
if (emailMessage != null) {
emailMessagesList.add(emailMessage);
saveEmailMessageAndScanQRCode(userId, emailMessage);
}
}
}
}
} else {
// Fetching email messages with page token and setting max results, Default value is 100.
do {
// ListHistoryResponse historyResponse = service.users().history().list(meUserId)
// .setStartHistoryId(BigInteger.valueOf(689335))
// .execute();
//
// List<History> historyList = historyResponse.getHistory();
//
// for (History history : historyList) {
// logger.info("History Id: {}, Message Id: {}, Message Snippet: {}", history.getId(), history.getMessages().get(0).getId(), history.getMessages().get(0).getHistoryId());
// }
ListMessagesResponse listResponse = fetchMessages(service, meUserId, nextPageToken);
List<Message> messages = listResponse.getMessages();
nextPageToken = listResponse.getNextPageToken();
@@ -178,12 +194,21 @@ public class GmailService {
}
}
} while (nextPageToken != null);
}
// Update user's history id.
// TODO: Update user's history id.
BigInteger latestHistoryId = getLatestHistoryId(service, meUserId);
if (latestHistoryId != null) {
userEntity.setGmailHistoryId(latestHistoryId);
userService.updateUserEntity(userEntity);
}
return new ScannedGmailResponseDto(emailMessagesList);
}
private BigInteger getLatestHistoryId(Gmail service, String userId) throws IOException {
Profile profile = service.users().getProfile(userId).execute();
return profile.getHistoryId();
}
// Save email message to database and scan QR code.
private void saveEmailMessageAndScanQRCode(String userId, EmailMessage emailMessage) {
GmailEmailEntity gmailEmailEntity = saveEmailMessage(userId, emailMessage);
@@ -207,7 +232,7 @@ public class GmailService {
.userId(userId)
.messageId(emailMessage.getMessageId())
.threadId(emailMessage.getThreadId())
.historyId(Long.valueOf(emailMessage.getHistoryId()))
.historyId(new BigInteger(emailMessage.getHistoryId()))
.subject(emailMessage.getSubject())
.dateReceived(dateReceived)
.active(emailMessage.getActive())
@@ -351,7 +376,8 @@ public class GmailService {
.execute();
}
// Processing email message and returning EmailMessage object if it has a valid QR code.
private EmailMessage processMessage(Gmail service, String userId, Message message) throws IOException {
private EmailMessage processMessage(Gmail service, String userId, Message message) {
try {
message = service.users().messages().get(userId, message.getId()).setFormat("full").execute();
List<MessagePart> parts = message.getPayload().getParts();
Set<String> attachmentIds = new HashSet<>();
@@ -374,6 +400,18 @@ public class GmailService {
processImageUrls(imageUrls, emailMessage);
return emailMessage.hasQRCodes() ? emailMessage : null;
} catch (GoogleJsonResponseException e) {
if (e.getStatusCode() == 404) {
logger.warn("Message with ID {} not found. It may have been deleted.", message.getId());
return null;
} else {
logger.error("Error processing message with ID {}: {}", message.getId(), e.getMessage());
throw new RuntimeException("Error processing Gmail message", e);
}
} catch (IOException e) {
logger.error("IO error processing message with ID {}: {}", message.getId(), e.getMessage());
throw new RuntimeException("IO error processing Gmail message", e);
}
}
// Process all the attachments.
private void processAttachments(Gmail service, String messageId, List<MessagePart> parts, Set<String> attachmentIds, EmailMessage emailMessage) throws IOException {

View File

@@ -8,6 +8,7 @@ import jakarta.persistence.Table;
import lombok.*;
import org.hibernate.annotations.Type;
import java.math.BigInteger;
import java.time.OffsetDateTime;
import java.util.List;
@@ -35,5 +36,5 @@ public class UserEntity {
private String status;
@Column(name = "gmail_history_id")
private Long gmailHistoryId;
private BigInteger gmailHistoryId = BigInteger.ZERO;
}

View File

@@ -54,6 +54,13 @@ public class UserService {
.status(userEntity.getStatus())
.build();
}
public UserEntity getUserByIdForGmail(String userId){
return userRepository.findById(userId)
.orElseThrow(()-> new ResourceNotFoundExceptions("User id not found: " + userId));
}
public UserEntity updateUserEntity(UserEntity userEntity) {
return userRepository.save(userEntity);
}
public List<ScannedHistoriesDto> getUserScannedHistories(String userId) {
return scanHistoryRepository.findAllQRCodesByUserId(userId);
}