Skip to content

Commit

Permalink
Merge pull request #1894 from Infomaniak/fix-temp-attachments
Browse files Browse the repository at this point in the history
Temp fix attachments
  • Loading branch information
NicolasBourdin88 committed Jun 7, 2024
2 parents aafc8c5 + e2c6c22 commit 0e29e22
Showing 1 changed file with 22 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -266,6 +270,8 @@ class ThreadController @Inject constructor(
}

realm.writeBlocking {
var hasAttachmentsInThread = false

messages.forEach { localMessage ->

if (localMessage.isFullyDownloaded()) return@forEach
Expand All @@ -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 {
Expand All @@ -294,6 +303,9 @@ class ThreadController @Inject constructor(
// This `runCatching / onFailure` is here only to catch `OutOfMemoryError` when trying to deserialize very big Body
handleFailure(localMessage.uid)
}

// TODO: Remove this when the API returns the good value for `has_attachments`.
verifyAttachmentsValues(hasAttachmentsInThread, messages, this@writeBlocking)
}
}

Expand All @@ -309,6 +321,16 @@ class ThreadController @Inject constructor(
fun deleteSearchThreads(realm: MutableRealm) = with(realm) {
delete(query<Thread>("${Thread::isFromSearch.name} == true").find())
}

private fun verifyAttachmentsValues(hasAttachmentsInThread: Boolean, messages: List<Message>, realm: MutableRealm) {
messages.flatMapTo(mutableSetOf()) { it.threads }.forEach { thread ->
if (thread.hasAttachments != hasAttachmentsInThread) {
updateThread(thread.uid, realm) {
it?.hasAttachments = hasAttachmentsInThread
}
}
}
}
//endregion
}
}

0 comments on commit 0e29e22

Please sign in to comment.