@@ -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
@@ -313,7 +320,21 @@ class ThreadController @Inject constructor(
313
320
}
314
321
315
322
fun deleteSearchThreads (realm : MutableRealm ) = with (realm) {
316
- delete(query<Thread >(" ${Thread ::isFromSearch.name} == true" ).find())
323
+ delete(query<Thread >(" ${Thread ::isFromSearch.name} == true" ))
324
+ }
325
+
326
+ fun deleteEmptyThreadsInFolder (folderId : String , realm : Realm ) {
327
+ realm.writeBlocking {
328
+ val emptyThreads = getEmptyThreadsInFolderQuery(folderId, realm = this ).find()
329
+ // TODO: Find why we are sometimes displaying empty Threads, and fix it instead of just deleting them.
330
+ // It's possibly because we are out of sync, and the situation will resolve by itself shortly?
331
+ if (emptyThreads.isNotEmpty()) {
332
+ emptyThreads.forEach {
333
+ SentryDebug .sendEmptyThread(it, " No Message in a Thread when refreshing a Folder" )
334
+ }
335
+ delete(emptyThreads)
336
+ }
337
+ }
317
338
}
318
339
// endregion
319
340
}
0 commit comments