From 8e461b43a594650162ee1c1c1926adcc0c6c20ba Mon Sep 17 00:00:00 2001 From: dankito Date: Thu, 28 Nov 2024 04:06:02 +0100 Subject: [PATCH] Implemented option downloadOnlyMessagesNewerThan --- .../net/codinux/invoicing/email/EmailsFetcher.kt | 11 ++++++++++- .../net/codinux/invoicing/email/FetchEmailsOptions.kt | 7 +++++++ .../invoicing/email/ListenForNewMailsOptions.kt | 2 +- 3 files changed, 18 insertions(+), 2 deletions(-) diff --git a/e-invoice-domain/src/main/kotlin/net/codinux/invoicing/email/EmailsFetcher.kt b/e-invoice-domain/src/main/kotlin/net/codinux/invoicing/email/EmailsFetcher.kt index 893c551..522b0be 100644 --- a/e-invoice-domain/src/main/kotlin/net/codinux/invoicing/email/EmailsFetcher.kt +++ b/e-invoice-domain/src/main/kotlin/net/codinux/invoicing/email/EmailsFetcher.kt @@ -146,6 +146,15 @@ open class EmailsFetcher( } protected open fun getEmail(message: Message, status: FetchEmailsStatus): Email? { + val date = map(message.sentDate ?: message.receivedDate) + status.options.minMessageDate?.let { minDate -> + if (date.isBefore(minDate)) { + log.debug { "Ignoring message $message with date $date as it is before downloadOnlyMessagesNewerThan date $minDate" } + return null + } + } + + val imapMessage = message as? IMAPMessage val messageId = status.folder.getUID(message) @@ -162,7 +171,7 @@ open class EmailsFetcher( val email = Email( messageId, - sender, message.subject ?: "", map(message.sentDate ?: message.receivedDate), + sender, message.subject ?: "", date, message.getRecipients(Message.RecipientType.TO).orEmpty().map { map(it) }, message.getRecipients(Message.RecipientType.CC).orEmpty().map { map(it) }, message.getRecipients(Message.RecipientType.BCC).orEmpty().map { map(it) }, (message.replyTo?.firstOrNull() as? InternetAddress)?.let { if (it.address != sender?.address) map(it) else null }, // only set replyTo if it differs from sender diff --git a/e-invoice-domain/src/main/kotlin/net/codinux/invoicing/email/FetchEmailsOptions.kt b/e-invoice-domain/src/main/kotlin/net/codinux/invoicing/email/FetchEmailsOptions.kt index d135ff3..2004e98 100644 --- a/e-invoice-domain/src/main/kotlin/net/codinux/invoicing/email/FetchEmailsOptions.kt +++ b/e-invoice-domain/src/main/kotlin/net/codinux/invoicing/email/FetchEmailsOptions.kt @@ -2,6 +2,9 @@ package net.codinux.invoicing.email import net.codinux.invoicing.email.model.Email import java.io.File +import java.time.Instant +import java.time.LocalDate +import java.time.ZoneId open class FetchEmailsOptions( /** @@ -15,6 +18,7 @@ open class FetchEmailsOptions( * and the HTML message body ignored / not downloaded. Reduces process time about 50 % (if no attachments get downloaded). */ val downloadOnlyPlainTextOrHtmlMessageBody: Boolean = false, + val downloadOnlyMessagesNewerThan: LocalDate? = null, /** * Set the extension (without the dot) of files that should be downloaded. @@ -34,6 +38,9 @@ open class FetchEmailsOptions( val DefaultAttachmentsDownloadDirectory: File = File(System.getProperty("java.io.tmpdir"), "eInvoices").also { it.mkdirs() } } + + val minMessageDate: Instant? by lazy { downloadOnlyMessagesNewerThan?.atStartOfDay(ZoneId.systemDefault())?.toInstant() } + fun emailReceived(email: Email) { onEmailReceived?.invoke(email) } diff --git a/e-invoice-domain/src/main/kotlin/net/codinux/invoicing/email/ListenForNewMailsOptions.kt b/e-invoice-domain/src/main/kotlin/net/codinux/invoicing/email/ListenForNewMailsOptions.kt index 9764e48..66b26bc 100644 --- a/e-invoice-domain/src/main/kotlin/net/codinux/invoicing/email/ListenForNewMailsOptions.kt +++ b/e-invoice-domain/src/main/kotlin/net/codinux/invoicing/email/ListenForNewMailsOptions.kt @@ -20,7 +20,7 @@ open class ListenForNewMailsOptions( onEmailReceived: (Email) -> Unit ) : FetchEmailsOptions( null, - downloadMessageBody, downloadOnlyPlainTextOrHtmlMessageBody, + downloadMessageBody, downloadOnlyPlainTextOrHtmlMessageBody, null, downloadAttachmentsWithExtensions, attachmentsDownloadDirectory, emailFolderName, connectTimeoutSeconds, onError, onEmailReceived ) \ No newline at end of file