Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check before trying to refresh messages for a specific mailbox if this one has already been open once #1955

Merged
merged 1 commit into from
Jul 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ 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,
Expand All @@ -69,16 +70,6 @@ class FetchMessagesManager @Inject constructor(
// 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 @@ -87,16 +78,6 @@ class FetchMessagesManager @Inject constructor(
// We only want to display Notifications about Mailboxes that the User opened at least once.
// 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 @@ -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 specific 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.
return@runCatching 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
Loading