@@ -235,6 +235,10 @@ class ThreadController @Inject constructor(
235235 // region Edit data
236236 fun upsertThread (thread : Thread , realm : MutableRealm ): Thread = realm.copyToRealm(thread, UpdatePolicy .ALL )
237237
238+ private fun updateThread (threadUid : String , realm : MutableRealm , onUpdate : (Thread ? ) -> Unit ) {
239+ onUpdate(getThread(threadUid, realm))
240+ }
241+
238242 /* *
239243 * Asynchronously fetches heavy data for a list of messages within a given mailbox and realm.
240244 *
@@ -266,6 +270,8 @@ class ThreadController @Inject constructor(
266270 }
267271
268272 realm.writeBlocking {
273+ var hasAttachmentsInThread = false
274+
269275 messages.forEach { localMessage ->
270276
271277 if (localMessage.isFullyDownloaded()) return @forEach
@@ -284,6 +290,9 @@ class ThreadController @Inject constructor(
284290 latestCalendarEventResponse = localMessage.latestCalendarEventResponse,
285291 messageIds = localMessage.messageIds,
286292 )
293+
294+ if (remoteMessage.hasAttachments) hasAttachmentsInThread = true
295+
287296 MessageController .upsertMessage(remoteMessage, realm = this )
288297 }
289298 } else {
@@ -294,6 +303,9 @@ class ThreadController @Inject constructor(
294303 // This `runCatching / onFailure` is here only to catch `OutOfMemoryError` when trying to deserialize very big Body
295304 handleFailure(localMessage.uid)
296305 }
306+
307+ // TODO: Remove this when the API returns the good value for `has_attachments`.
308+ verifyAttachmentsValues(hasAttachmentsInThread, messages, this @writeBlocking)
297309 }
298310 }
299311
@@ -309,6 +321,16 @@ class ThreadController @Inject constructor(
309321 fun deleteSearchThreads (realm : MutableRealm ) = with (realm) {
310322 delete(query<Thread >(" ${Thread ::isFromSearch.name} == true" ).find())
311323 }
324+
325+ private fun verifyAttachmentsValues (hasAttachmentsInThread : Boolean , messages : List <Message >, realm : MutableRealm ) {
326+ messages.flatMapTo(mutableSetOf ()) { it.threads }.forEach { thread ->
327+ if (thread.hasAttachments != hasAttachmentsInThread) {
328+ updateThread(thread.uid, realm) {
329+ it?.hasAttachments = hasAttachmentsInThread
330+ }
331+ }
332+ }
333+ }
312334 // endregion
313335 }
314336}
0 commit comments