@@ -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
@@ -159,6 +160,12 @@ class ThreadController @Inject constructor(
159
160
}
160
161
}
161
162
}
163
+
164
+ fun deleteThread (threadUid : String ) {
165
+ mailboxContentRealm().writeBlocking {
166
+ delete(getThreadQuery(threadUid, realm = this ))
167
+ }
168
+ }
162
169
// endregion
163
170
164
171
companion object {
@@ -208,6 +215,12 @@ class ThreadController @Inject constructor(
208
215
private fun getThreadQuery (uid : String , realm : TypedRealm ): RealmSingleQuery <Thread > {
209
216
return realm.query<Thread >(" ${Thread ::uid.name} == $0" , uid).first()
210
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
+ }
211
224
// endregion
212
225
213
226
// region Get data
@@ -319,7 +332,22 @@ class ThreadController @Inject constructor(
319
332
}
320
333
321
334
fun deleteSearchThreads (realm : MutableRealm ) = with (realm) {
322
- 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
+ }
323
351
}
324
352
325
353
private fun verifyAttachmentsValues (hasAttachmentsInThread : Boolean , messages : List <Message >, realm : MutableRealm ) {
0 commit comments