Implemented configuring if message body should be downloaded
This commit is contained in:
parent
82bbf8cc8d
commit
4cc03a0036
|
@ -33,7 +33,7 @@ class MailReader(
|
||||||
private val log by logger()
|
private val log by logger()
|
||||||
|
|
||||||
|
|
||||||
fun listenForNewReceivedEInvoices(account: MailAccount, emailFolderName: String = "INBOX", eInvoiceReceived: (MailWithInvoice) -> Unit) = runBlocking {
|
fun listenForNewReceivedEInvoices(account: MailAccount, downloadMessageBody: Boolean = false, emailFolderName: String = "INBOX", eInvoiceReceived: (MailWithInvoice) -> Unit) = runBlocking {
|
||||||
try {
|
try {
|
||||||
connect(account) { store ->
|
connect(account) { store ->
|
||||||
val folder = store.getFolder(emailFolderName)
|
val folder = store.getFolder(emailFolderName)
|
||||||
|
@ -42,7 +42,7 @@ class MailReader(
|
||||||
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)?.let {
|
findEInvoice(message, downloadMessageBody)?.let {
|
||||||
eInvoiceReceived(it)
|
eInvoiceReceived(it)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -77,13 +77,13 @@ class MailReader(
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
fun listAllMessagesWithEInvoice(account: MailAccount, emailFolderName: String = "INBOX"): List<MailWithInvoice> {
|
fun listAllMessagesWithEInvoice(account: MailAccount, downloadMessageBody: Boolean = false, emailFolderName: String = "INBOX"): List<MailWithInvoice> {
|
||||||
try {
|
try {
|
||||||
connect(account) { store ->
|
connect(account) { store ->
|
||||||
val inbox = store.getFolder(emailFolderName)
|
val inbox = store.getFolder(emailFolderName)
|
||||||
inbox.open(Folder.READ_ONLY)
|
inbox.open(Folder.READ_ONLY)
|
||||||
|
|
||||||
listAllMessagesWithEInvoiceInFolder(inbox).also {
|
listAllMessagesWithEInvoiceInFolder(inbox, downloadMessageBody).also {
|
||||||
inbox.close(false)
|
inbox.close(false)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -94,7 +94,7 @@ class MailReader(
|
||||||
return emptyList()
|
return emptyList()
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun listAllMessagesWithEInvoiceInFolder(folder: Folder): List<MailWithInvoice> = runBlocking {
|
private fun listAllMessagesWithEInvoiceInFolder(folder: Folder, downloadMessageBody: Boolean): List<MailWithInvoice> = runBlocking {
|
||||||
val messageCount = folder.messageCount
|
val messageCount = folder.messageCount
|
||||||
if (messageCount <= 0) {
|
if (messageCount <= 0) {
|
||||||
return@runBlocking emptyList()
|
return@runBlocking emptyList()
|
||||||
|
@ -103,7 +103,7 @@ class MailReader(
|
||||||
IntRange(1, messageCount).mapNotNull { messageNumber -> // message numbers start at 1
|
IntRange(1, messageCount).mapNotNull { messageNumber -> // message numbers start at 1
|
||||||
async(mailDispatcher) {
|
async(mailDispatcher) {
|
||||||
try {
|
try {
|
||||||
findEInvoice(folder.getMessage(messageNumber))
|
findEInvoice(folder.getMessage(messageNumber), downloadMessageBody)
|
||||||
} catch (e: Throwable) {
|
} catch (e: Throwable) {
|
||||||
log.error(e) { "Could not get message with messageNumber $messageNumber" }
|
log.error(e) { "Could not get message with messageNumber $messageNumber" }
|
||||||
null
|
null
|
||||||
|
@ -114,7 +114,7 @@ class MailReader(
|
||||||
.filterNotNull()
|
.filterNotNull()
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun findEInvoice(message: Message): MailWithInvoice? {
|
private fun findEInvoice(message: Message, downloadMessageBody: Boolean): MailWithInvoice? {
|
||||||
try {
|
try {
|
||||||
val parts = getAllMessageParts(message)
|
val parts = getAllMessageParts(message)
|
||||||
|
|
||||||
|
@ -127,7 +127,7 @@ class MailReader(
|
||||||
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), getHtmlBody(parts),
|
if (downloadMessageBody) getPlainTextBody(parts) else null, if (downloadMessageBody) getHtmlBody(parts) else null,
|
||||||
attachmentsWithEInvoice
|
attachmentsWithEInvoice
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue