From a190af69e1658bbf95ce3bbe51e3a519cb01eec0 Mon Sep 17 00:00:00 2001 From: NicolasBourdin88 Date: Wed, 5 Jun 2024 14:23:17 +0200 Subject: [PATCH 1/4] Add logic to fix attachments --- .../cache/mailboxContent/ThreadController.kt | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/app/src/main/java/com/infomaniak/mail/data/cache/mailboxContent/ThreadController.kt b/app/src/main/java/com/infomaniak/mail/data/cache/mailboxContent/ThreadController.kt index 386dfb0d05..4a9a471df6 100644 --- a/app/src/main/java/com/infomaniak/mail/data/cache/mailboxContent/ThreadController.kt +++ b/app/src/main/java/com/infomaniak/mail/data/cache/mailboxContent/ThreadController.kt @@ -235,6 +235,10 @@ class ThreadController @Inject constructor( //region Edit data fun upsertThread(thread: Thread, realm: MutableRealm): Thread = realm.copyToRealm(thread, UpdatePolicy.ALL) + private fun updateThread(threadUid: String, realm: MutableRealm, onUpdate: (Thread?) -> Unit) { + onUpdate(getThread(threadUid, realm)) + } + /** * Asynchronously fetches heavy data for a list of messages within a given mailbox and realm. * @@ -266,6 +270,8 @@ class ThreadController @Inject constructor( } realm.writeBlocking { + var hasAttachmentsInThread = false + messages.forEach { localMessage -> if (localMessage.isFullyDownloaded()) return@forEach @@ -284,6 +290,9 @@ class ThreadController @Inject constructor( latestCalendarEventResponse = localMessage.latestCalendarEventResponse, messageIds = localMessage.messageIds, ) + + if (remoteMessage.hasAttachments) hasAttachmentsInThread = true + MessageController.upsertMessage(remoteMessage, realm = this) } } else { @@ -294,6 +303,16 @@ class ThreadController @Inject constructor( // This `runCatching / onFailure` is here only to catch `OutOfMemoryError` when trying to deserialize very big Body handleFailure(localMessage.uid) } + + if (!hasAttachmentsInThread) { + messages.flatMapTo(mutableSetOf()) { it.threads }.forEach { thread -> + if (thread.hasAttachments) { + updateThread(thread.uid, realm = this@writeBlocking) { + it?.hasAttachments = false + } + } + } + } } } From f1e8f1eca7f29f206f86c02e1fdb753b60a3ddb1 Mon Sep 17 00:00:00 2001 From: NicolasBourdin88 Date: Wed, 5 Jun 2024 14:23:47 +0200 Subject: [PATCH 2/4] Add comments to prevent that this fix is temporary --- .../mail/data/cache/mailboxContent/ThreadController.kt | 1 + 1 file changed, 1 insertion(+) diff --git a/app/src/main/java/com/infomaniak/mail/data/cache/mailboxContent/ThreadController.kt b/app/src/main/java/com/infomaniak/mail/data/cache/mailboxContent/ThreadController.kt index 4a9a471df6..e02a98e90a 100644 --- a/app/src/main/java/com/infomaniak/mail/data/cache/mailboxContent/ThreadController.kt +++ b/app/src/main/java/com/infomaniak/mail/data/cache/mailboxContent/ThreadController.kt @@ -304,6 +304,7 @@ class ThreadController @Inject constructor( handleFailure(localMessage.uid) } + // TODO: Remove this when the API returns the good value for `has_attachments`. if (!hasAttachmentsInThread) { messages.flatMapTo(mutableSetOf()) { it.threads }.forEach { thread -> if (thread.hasAttachments) { From cc3948b892f6361a3855fa9d8147208adaf388ae Mon Sep 17 00:00:00 2001 From: NicolasBourdin88 Date: Thu, 6 Jun 2024 13:23:43 +0200 Subject: [PATCH 3/4] Put logic in a function --- .../cache/mailboxContent/ThreadController.kt | 22 +++++++++++-------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/app/src/main/java/com/infomaniak/mail/data/cache/mailboxContent/ThreadController.kt b/app/src/main/java/com/infomaniak/mail/data/cache/mailboxContent/ThreadController.kt index e02a98e90a..4cea11cf7b 100644 --- a/app/src/main/java/com/infomaniak/mail/data/cache/mailboxContent/ThreadController.kt +++ b/app/src/main/java/com/infomaniak/mail/data/cache/mailboxContent/ThreadController.kt @@ -305,15 +305,7 @@ class ThreadController @Inject constructor( } // TODO: Remove this when the API returns the good value for `has_attachments`. - if (!hasAttachmentsInThread) { - messages.flatMapTo(mutableSetOf()) { it.threads }.forEach { thread -> - if (thread.hasAttachments) { - updateThread(thread.uid, realm = this@writeBlocking) { - it?.hasAttachments = false - } - } - } - } + verifyAttachmentsValues(hasAttachmentsInThread, messages, this@writeBlocking) } } @@ -329,6 +321,18 @@ class ThreadController @Inject constructor( fun deleteSearchThreads(realm: MutableRealm) = with(realm) { delete(query("${Thread::isFromSearch.name} == true").find()) } + + private fun verifyAttachmentsValues(hasAttachmentsInThread: Boolean, messages: List, realm: MutableRealm) { + if (!hasAttachmentsInThread) { + messages.flatMapTo(mutableSetOf()) { it.threads }.forEach { thread -> + if (thread.hasAttachments) { + updateThread(thread.uid, realm) { + it?.hasAttachments = false + } + } + } + } + } //endregion } } From e2c6c222d5c728947ba9be7a01c1e5497d3e5d69 Mon Sep 17 00:00:00 2001 From: NicolasBourdin88 Date: Fri, 7 Jun 2024 10:46:25 +0200 Subject: [PATCH 4/4] Manage another case of the bug The API can also send that they're are not attachments but they're is --- .../mail/data/cache/mailboxContent/ThreadController.kt | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/app/src/main/java/com/infomaniak/mail/data/cache/mailboxContent/ThreadController.kt b/app/src/main/java/com/infomaniak/mail/data/cache/mailboxContent/ThreadController.kt index 4cea11cf7b..77fcd92e11 100644 --- a/app/src/main/java/com/infomaniak/mail/data/cache/mailboxContent/ThreadController.kt +++ b/app/src/main/java/com/infomaniak/mail/data/cache/mailboxContent/ThreadController.kt @@ -323,12 +323,10 @@ class ThreadController @Inject constructor( } private fun verifyAttachmentsValues(hasAttachmentsInThread: Boolean, messages: List, realm: MutableRealm) { - if (!hasAttachmentsInThread) { - messages.flatMapTo(mutableSetOf()) { it.threads }.forEach { thread -> - if (thread.hasAttachments) { - updateThread(thread.uid, realm) { - it?.hasAttachments = false - } + messages.flatMapTo(mutableSetOf()) { it.threads }.forEach { thread -> + if (thread.hasAttachments != hasAttachmentsInThread) { + updateThread(thread.uid, realm) { + it?.hasAttachments = hasAttachmentsInThread } } }