Skip to content

Commit c4058f9

Browse files
Also delete Threads & add Sentry after finishing RefreshController
1 parent 7056f3e commit c4058f9

File tree

4 files changed

+27
-3
lines changed

4 files changed

+27
-3
lines changed

app/src/main/java/com/infomaniak/mail/data/cache/mailboxContent/RefreshController.kt

+3
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,9 @@ class RefreshController @Inject constructor(
102102
setupConfiguration(refreshMode, mailbox, folder, realm, okHttpClient, callbacks)
103103

104104
return refreshWithRunCatching(refreshThreadsJob!!).also { (threads, _) ->
105+
106+
ThreadController.deleteEmptyThreadsInFolder(folder.id, realm)
107+
105108
if (threads != null) {
106109
onStop?.invoke()
107110
SentryLog.d("API", "End of refreshing threads with mode: $refreshMode | (${folder.displayForSentry()})")

app/src/main/java/com/infomaniak/mail/data/cache/mailboxContent/ThreadController.kt

+22-1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import com.infomaniak.mail.data.models.thread.Thread.ThreadFilter
2828
import com.infomaniak.mail.di.IoDispatcher
2929
import com.infomaniak.mail.utils.ErrorCode
3030
import com.infomaniak.mail.utils.SearchUtils.Companion.convertToSearchThreads
31+
import com.infomaniak.mail.utils.SentryDebug
3132
import io.realm.kotlin.MutableRealm
3233
import io.realm.kotlin.Realm
3334
import io.realm.kotlin.TypedRealm
@@ -214,6 +215,12 @@ class ThreadController @Inject constructor(
214215
private fun getThreadQuery(uid: String, realm: TypedRealm): RealmSingleQuery<Thread> {
215216
return realm.query<Thread>("${Thread::uid.name} == $0", uid).first()
216217
}
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+
}
217224
//endregion
218225

219226
//region Get data
@@ -313,7 +320,21 @@ class ThreadController @Inject constructor(
313320
}
314321

315322
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+
}
317338
}
318339
//endregion
319340
}

app/src/main/java/com/infomaniak/mail/ui/main/folder/ThreadListAdapter.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ class ThreadListAdapter @Inject constructor(
187187
// If we are trying to display an empty Thread, don't. Just delete it.
188188
if (thread.messages.isEmpty()) {
189189
// TODO: Find why we are sometimes displaying empty Threads, and fix it instead of just deleting them.
190-
// It's possibly because we are out of sync, and the situation will resolve itself shortly? ¯\_(ツ)_/¯
190+
// It's possibly because we are out of sync, and the situation will resolve by itself shortly?
191191
threadListAdapterCallback?.deleteThreadInRealm?.invoke(thread.uid)
192192
SentryDebug.sendEmptyThread(thread, "No Message in the Thread when displaying it in ThreadList")
193193
return

app/src/main/java/com/infomaniak/mail/utils/SentryDebug.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ object SentryDebug {
118118
// TODO: Added the 04/09/23. It's not supposed to be possible, but we never know…
119119
// If this doesn't trigger after a certain amount of time, you can remove it.
120120
//
121-
// Also added in ThreadListAdapter the 31/05/24.
121+
// Also added in ThreadListAdapter & ThreadController the 04/06/24.
122122
fun sendEmptyThread(thread: Thread, message: String) = with(thread) {
123123
Sentry.withScope { scope ->
124124
scope.setExtra("currentUserId", "${AccountUtils.currentUserId}")

0 commit comments

Comments
 (0)