Skip to content

Commit

Permalink
Check before trying to refresh messages for a specific mailbox if thi…
Browse files Browse the repository at this point in the history
…s one has already been open once
  • Loading branch information
tevincent committed Jul 11, 2024
1 parent 831d992 commit f2f53b8
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import com.infomaniak.mail.data.cache.mailboxContent.MessageController
import com.infomaniak.mail.data.cache.mailboxContent.RefreshController
import com.infomaniak.mail.data.cache.mailboxContent.RefreshController.RefreshMode
import com.infomaniak.mail.data.cache.mailboxContent.ThreadController
import com.infomaniak.mail.data.models.Folder
import com.infomaniak.mail.data.models.Folder.FolderRole
import com.infomaniak.mail.data.models.mailbox.Mailbox
import com.infomaniak.mail.data.models.thread.Thread
Expand All @@ -51,12 +52,14 @@ class FetchMessagesManager @Inject constructor(

private lateinit var coroutineScope: CoroutineScope

//We can arrive here for mailboxes we did not opened yet. That's why we check before doing anything.
suspend fun execute(
scope: CoroutineScope,
userId: Int,
mailbox: Mailbox,
sentryMessageUid: String? = null,
mailboxContentRealm: Realm? = null,
mailboxFolder: Folder? = null,
) {
coroutineScope = scope

Expand All @@ -65,19 +68,11 @@ class FetchMessagesManager @Inject constructor(
if (mailbox.notificationsIsDisabled(notificationManagerCompat)) return

val realm = mailboxContentRealm ?: RealmDatabase.newMailboxContentInstance(userId, mailbox.mailboxId)
val folder = FolderController.getFolder(FolderRole.INBOX, realm) ?: run {
val folder = mailboxFolder ?: FolderController.getFolder(FolderRole.INBOX, realm) ?: run {
// If we can't find the INBOX in Realm, it means the user never opened this Mailbox.
// We don't want to display Notifications in this case.
// We can leave safely.
// But if a user never opened this Mailbox, we shouldn't have register it to receive Notifications. So this shouldn't happen.
SentryDebug.sendFailedNotification(
reason = "No Folder in Realm",
sentryLevel = SentryLevel.WARNING,
userId = userId,
mailboxId = mailbox.mailboxId,
messageUid = sentryMessageUid,
mailbox = mailbox,
)

realm.close()
return
Expand All @@ -88,14 +83,6 @@ class FetchMessagesManager @Inject constructor(
// If we don't have any cursor for this Mailbox's INBOX, it means it was never opened.
// We can leave safely.
// But if a user never opened this Mailbox, we shouldn't have register it to receive Notifications. So this shouldn't happen.
SentryDebug.sendFailedNotification(
reason = "Folder's cursor is null",
sentryLevel = SentryLevel.WARNING,
userId = userId,
mailboxId = mailbox.mailboxId,
messageUid = sentryMessageUid,
mailbox = mailbox,
)

realm.close()
return
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,8 @@ class SyncMailboxesWorker @AssistedInject constructor(
SentryLog.d(TAG, "Work launched")

AccountUtils.getAllUsersSync().forEach { user ->
mailboxController.getMailboxes(user.id).forEach { mailbox ->
fetchMessagesManager.execute(scope = this, user.id, mailbox)
}
val mailboxes = mailboxController.getMailboxes(user.id)
mailboxes.forEach { mailbox -> fetchMessagesManager.execute(scope = this, user.id, mailbox) }
}

SentryLog.d(TAG, "Work finished")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,11 @@ import com.google.firebase.messaging.RemoteMessage
import com.infomaniak.lib.core.utils.SentryLog
import com.infomaniak.mail.MainApplication
import com.infomaniak.mail.data.LocalSettings
import com.infomaniak.mail.data.cache.RealmDatabase
import com.infomaniak.mail.data.cache.mailboxContent.FolderController
import com.infomaniak.mail.data.cache.mailboxInfo.MailboxController
import com.infomaniak.mail.data.models.AppSettings
import com.infomaniak.mail.data.models.Folder.FolderRole
import com.infomaniak.mail.utils.AccountUtils
import com.infomaniak.mail.utils.PlayServicesUtils
import dagger.hilt.android.AndroidEntryPoint
Expand Down Expand Up @@ -80,12 +83,15 @@ class KMailFirebaseMessagingService : FirebaseMessagingService() {
SentryLog.d(TAG, "onMessageReceived: mailboxId=$mailboxId")
SentryLog.d(TAG, "onMessageReceived: messageUid=$messageUid")

if (mainApplication.isAppInBackground) {
processMessageInBackground(userId, mailboxId, messageUid)
} else {
processMessageInForeground(userId, mailboxId)
// This is to avoid doing some processing when we never opened a spcific mailbox
val realm = RealmDatabase.newMailboxContentInstance(userId, mailboxId)
FolderController.getFolder(FolderRole.INBOX, realm)?.cursor?.let {
if (mainApplication.isAppInBackground) {
processMessageInBackground(userId, mailboxId, messageUid)
} else {
processMessageInForeground(userId, mailboxId)
}
}

}

private fun processMessageInForeground(userId: Int, mailboxId: Int) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,20 +78,25 @@ class ProcessMessageNotificationsWorker @AssistedInject constructor(
)
return@withContext Result.success()
}

val mailboxContentRealm = RealmDatabase.newMailboxContentInstance(userId, mailbox.mailboxId)

MessageController.getMessage(messageUid, mailboxContentRealm)?.let {
// If the Message is already in Realm, it means we already fetched it when we received a previous Notification.
// So we've already shown it in a previous batch of Notifications.
// We can leave safely.
return@withContext Result.success()
}
return@withContext runCatching {
MessageController.getMessage(messageUid, mailboxContentRealm)?.let {
// If the Message is already in Realm, it means we already fetched it when we received a previous Notification.
// So we've already shown it in a previous batch of Notifications.
// We can leave safely.
Result.success()
}

fetchMessagesManager.execute(scope = this, userId, mailbox, messageUid, mailboxContentRealm)
fetchMessagesManager.execute(scope = this, userId, mailbox, messageUid, mailboxContentRealm)

SentryLog.i(TAG, "Work finished")
return@withContext Result.success()
SentryLog.i(TAG, "Work finished")
Result.success()
}.getOrElse {
Result.failure()
}.also {
mailboxContentRealm.close()
}
}

@Singleton
Expand Down

0 comments on commit f2f53b8

Please sign in to comment.