Added emailFolderName, onError and onEmailReceived to FetchEmailsOptions, so that also fetchAllEmails() can set a progress listener for each received email
This commit is contained in:
parent
bb3f468c48
commit
31ba07d5e9
|
@ -34,21 +34,18 @@ open class EmailsFetcher(
|
||||||
protected val log by logger()
|
protected val log by logger()
|
||||||
|
|
||||||
|
|
||||||
open fun listenForNewEmails(account: EmailAccount, downloadMessageBody: Boolean = false, emailFolderName: String = "INBOX",
|
open fun listenForNewEmails(account: EmailAccount, options: FetchEmailsOptions) = runBlocking {
|
||||||
onError: ((FetchEmailsError) -> Unit)? = null, emailReceived: (EmailWithInvoice) -> Unit) = runBlocking {
|
|
||||||
try {
|
try {
|
||||||
connect(account) { store ->
|
connect(account) { store ->
|
||||||
val folder = store.getFolder(emailFolderName)
|
val folder = store.getFolder(options.emailFolderName)
|
||||||
folder.open(Folder.READ_ONLY)
|
folder.open(Folder.READ_ONLY)
|
||||||
|
|
||||||
val status = FetchEmailsStatus(FetchEmailsOptions(downloadMessageBody), onError = onError)
|
val status = FetchEmailsStatus(options)
|
||||||
|
|
||||||
folder.addMessageCountListener(object : MessageCountAdapter() {
|
folder.addMessageCountListener(object : MessageCountAdapter() {
|
||||||
override fun messagesAdded(event: MessageCountEvent) {
|
override fun messagesAdded(event: MessageCountEvent) {
|
||||||
event.messages.forEach { message ->
|
event.messages.forEach { message ->
|
||||||
findEInvoice(message, status)?.let {
|
findEInvoice(message, status)
|
||||||
emailReceived(it)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
@ -59,7 +56,7 @@ open class EmailsFetcher(
|
||||||
}
|
}
|
||||||
} catch (e: Throwable) {
|
} catch (e: Throwable) {
|
||||||
log.error(e) { "Listening to new emails of '${account.username}' failed" }
|
log.error(e) { "Listening to new emails of '${account.username}' failed" }
|
||||||
onError?.invoke(FetchEmailsError(FetchEmailsErrorType.ListenForNewEmails, null, e))
|
options.onError?.invoke(FetchEmailsError(FetchEmailsErrorType.ListenForNewEmails, null, e))
|
||||||
}
|
}
|
||||||
|
|
||||||
log.info { "Stopped listening to new emails of '${account.username}'" }
|
log.info { "Stopped listening to new emails of '${account.username}'" }
|
||||||
|
@ -82,13 +79,13 @@ open class EmailsFetcher(
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
open fun fetchAllEmails(account: EmailAccount, downloadMessageBody: Boolean = false, emailFolderName: String = "INBOX"): FetchEmailsResult {
|
open fun fetchAllEmails(account: EmailAccount, options: FetchEmailsOptions = FetchEmailsOptions()): FetchEmailsResult {
|
||||||
try {
|
try {
|
||||||
return connect(account) { store ->
|
return connect(account) { store ->
|
||||||
val inbox = store.getFolder(emailFolderName)
|
val inbox = store.getFolder(options.emailFolderName)
|
||||||
inbox.open(Folder.READ_ONLY)
|
inbox.open(Folder.READ_ONLY)
|
||||||
|
|
||||||
val status = FetchEmailsStatus(FetchEmailsOptions(downloadMessageBody))
|
val status = FetchEmailsStatus(options)
|
||||||
|
|
||||||
val emails = fetchAllEmailsInFolder(inbox, status).also {
|
val emails = fetchAllEmailsInFolder(inbox, status).also {
|
||||||
inbox.close(false)
|
inbox.close(false)
|
||||||
|
@ -132,13 +129,17 @@ open class EmailsFetcher(
|
||||||
}
|
}
|
||||||
|
|
||||||
if (attachmentsWithEInvoice.isNotEmpty()) {
|
if (attachmentsWithEInvoice.isNotEmpty()) {
|
||||||
return EmailWithInvoice(
|
val email = EmailWithInvoice(
|
||||||
message.from?.joinToString(), message.subject ?: "",
|
message.from?.joinToString(), message.subject ?: "",
|
||||||
message.sentDate?.let { map(it) }, map(message.receivedDate), message.messageNumber,
|
message.sentDate?.let { map(it) }, map(message.receivedDate), message.messageNumber,
|
||||||
parts.any { it.mediaType == "application/pgp-encrypted" },
|
parts.any { it.mediaType == "application/pgp-encrypted" },
|
||||||
getPlainTextBody(parts, status), getHtmlBody(parts, status),
|
getPlainTextBody(parts, status), getHtmlBody(parts, status),
|
||||||
attachmentsWithEInvoice
|
attachmentsWithEInvoice
|
||||||
)
|
)
|
||||||
|
|
||||||
|
status.options.emailReceived(email)
|
||||||
|
|
||||||
|
return email
|
||||||
}
|
}
|
||||||
|
|
||||||
return null
|
return null
|
||||||
|
|
|
@ -1,5 +1,13 @@
|
||||||
package net.codinux.invoicing.email
|
package net.codinux.invoicing.email
|
||||||
|
|
||||||
data class FetchEmailsOptions(
|
data class FetchEmailsOptions(
|
||||||
val downloadMessageBody: Boolean = false
|
val downloadMessageBody: Boolean = false,
|
||||||
)
|
val emailFolderName: String = "INBOX",
|
||||||
|
|
||||||
|
val onError: ((FetchEmailsError) -> Unit)? = null,
|
||||||
|
val onEmailReceived: ((EmailWithInvoice) -> Unit)? = null
|
||||||
|
) {
|
||||||
|
fun emailReceived(email: EmailWithInvoice) {
|
||||||
|
onEmailReceived?.invoke(email)
|
||||||
|
}
|
||||||
|
}
|
|
@ -6,8 +6,7 @@ import jakarta.mail.Part
|
||||||
|
|
||||||
data class FetchEmailsStatus(
|
data class FetchEmailsStatus(
|
||||||
val options: FetchEmailsOptions,
|
val options: FetchEmailsOptions,
|
||||||
val messageSpecificErrors: MutableList<FetchEmailsError> = mutableListOf(),
|
val messageSpecificErrors: MutableList<FetchEmailsError> = mutableListOf()
|
||||||
val onError: ((FetchEmailsError) -> Unit)? = null
|
|
||||||
) {
|
) {
|
||||||
|
|
||||||
fun addError(type: FetchEmailsErrorType, parts: Collection<Part>, error: Throwable) =
|
fun addError(type: FetchEmailsErrorType, parts: Collection<Part>, error: Throwable) =
|
||||||
|
@ -22,7 +21,7 @@ data class FetchEmailsStatus(
|
||||||
fun addError(error: FetchEmailsError) {
|
fun addError(error: FetchEmailsError) {
|
||||||
messageSpecificErrors.add(error)
|
messageSpecificErrors.add(error)
|
||||||
|
|
||||||
onError?.invoke(error)
|
options.onError?.invoke(error)
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun getMessage(part: Part): Message? {
|
private fun getMessage(part: Part): Message? {
|
||||||
|
|
|
@ -25,7 +25,7 @@ class EmailsFetcherTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun fetchAllEmails() {
|
fun fetchAllEmails() {
|
||||||
val result = underTest.fetchAllEmails(emailAccount, true)
|
val result = underTest.fetchAllEmails(emailAccount, FetchEmailsOptions(true))
|
||||||
|
|
||||||
assertThat(result.emails).isNotEmpty()
|
assertThat(result.emails).isNotEmpty()
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue