@@ -28,6 +28,7 @@ import com.infomaniak.mail.data.models.thread.Thread.ThreadFilter
28
28
import com.infomaniak.mail.di.IoDispatcher
29
29
import com.infomaniak.mail.utils.ErrorCode
30
30
import com.infomaniak.mail.utils.SearchUtils.Companion.convertToSearchThreads
31
+ import com.infomaniak.mail.utils.SentryDebug
31
32
import io.realm.kotlin.MutableRealm
32
33
import io.realm.kotlin.Realm
33
34
import io.realm.kotlin.TypedRealm
@@ -214,6 +215,12 @@ class ThreadController @Inject constructor(
214
215
private fun getThreadQuery (uid : String , realm : TypedRealm ): RealmSingleQuery <Thread > {
215
216
return realm.query<Thread >(" ${Thread ::uid.name} == $0" , uid).first()
216
217
}
218
+
219
+ private fun getEmptyThreadsInFolderQuery (folderId : String , realm : TypedRealm ): RealmQuery <Thread > {
220
+ val noMessages = " ${Thread ::messages.name} .@size == $0"
221
+ return FolderController .getFolder(folderId, realm)?.threads?.query(noMessages, 0 )
222
+ ? : realm.query<Thread >(" $noMessages AND ${Thread ::folderId.name} == $1" , 0 , folderId)
223
+ }
217
224
// endregion
218
225
219
226
// region Get data
@@ -325,7 +332,22 @@ class ThreadController @Inject constructor(
325
332
}
326
333
327
334
fun deleteSearchThreads (realm : MutableRealm ) = with (realm) {
328
- delete(query<Thread >(" ${Thread ::isFromSearch.name} == true" ).find())
335
+ delete(query<Thread >(" ${Thread ::isFromSearch.name} == true" ))
336
+ }
337
+
338
+ fun deleteEmptyThreadsInFolder (folderId : String , realm : Realm ) {
339
+ realm.writeBlocking {
340
+ val emptyThreadsQuery = getEmptyThreadsInFolderQuery(folderId, realm = this )
341
+ val emptyThreads = emptyThreadsQuery.find()
342
+ // TODO: Find why we are sometimes displaying empty Threads, and fix it instead of just deleting them.
343
+ // It's possibly because we are out of sync, and the situation will resolve by itself shortly?
344
+ if (emptyThreads.isNotEmpty()) {
345
+ emptyThreads.forEach {
346
+ SentryDebug .sendEmptyThread(it, " No Message in a Thread when refreshing a Folder" )
347
+ }
348
+ delete(emptyThreadsQuery)
349
+ }
350
+ }
329
351
}
330
352
331
353
private fun verifyAttachmentsValues (hasAttachmentsInThread : Boolean , messages : List <Message >, realm : MutableRealm ) {
0 commit comments