Implemented option downloadOnlyMessagesNewerThan

This commit is contained in:
dankito 2024-11-28 04:06:02 +01:00
parent 7df4b944ee
commit 8e461b43a5
3 changed files with 18 additions and 2 deletions

View File

@ -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

View File

@ -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)
}

View File

@ -20,7 +20,7 @@ open class ListenForNewMailsOptions(
onEmailReceived: (Email) -> Unit
) : FetchEmailsOptions(
null,
downloadMessageBody, downloadOnlyPlainTextOrHtmlMessageBody,
downloadMessageBody, downloadOnlyPlainTextOrHtmlMessageBody, null,
downloadAttachmentsWithExtensions, attachmentsDownloadDirectory,
emailFolderName, connectTimeoutSeconds, onError, onEmailReceived
)