Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Temp fix attachments #1894

Merged
merged 4 commits into from
Jun 7, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
}
}
Loading