Skip to content

Commit 24043e3

Browse files
Also delete Threads & add Sentry after finishing RefreshController
1 parent b7d9d42 commit 24043e3

File tree

4 files changed

+28
-3
lines changed

4 files changed

+28
-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

+23-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
@@ -325,7 +332,22 @@ class ThreadController @Inject constructor(
325332
}
326333

327334
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+
}
329351
}
330352

331353
private fun verifyAttachmentsValues(hasAttachmentsInThread: Boolean, messages: List<Message>, realm: MutableRealm) {

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ class ThreadListAdapter @Inject constructor(
195195
// If we are trying to display an empty Thread, don't. Just delete it.
196196
if (thread.messages.isEmpty()) {
197197
// TODO: Find why we are sometimes displaying empty Threads, and fix it instead of just deleting them.
198-
// It's possibly because we are out of sync, and the situation will resolve itself shortly? ¯\_(ツ)_/¯
198+
// It's possibly because we are out of sync, and the situation will resolve by itself shortly?
199199
threadListAdapterCallback?.deleteThreadInRealm?.invoke(thread.uid)
200200
SentryDebug.sendEmptyThread(thread, "No Message in the Thread when displaying it in ThreadList")
201201
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)