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

fix: Refresh User & Mailboxes before fetching Notifications #2094

Merged
merged 3 commits into from
Nov 6, 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
12 changes: 12 additions & 0 deletions app/src/main/java/com/infomaniak/mail/utils/NotificationUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import com.infomaniak.lib.core.utils.SentryLog
import com.infomaniak.lib.core.utils.clearStack
import com.infomaniak.mail.R
import com.infomaniak.mail.data.LocalSettings
import com.infomaniak.mail.data.api.ApiRepository
import com.infomaniak.mail.data.cache.mailboxInfo.MailboxController
import com.infomaniak.mail.data.models.draft.Draft.DraftAction
import com.infomaniak.mail.data.models.draft.Draft.DraftMode
Expand Down Expand Up @@ -306,6 +307,17 @@ class NotificationUtils @Inject constructor(
addAction(replyAction)
}

suspend fun updateUserAndMailboxes(mailboxController: MailboxController, tag: String) {
// Refresh User
AccountUtils.updateCurrentUser()

// Refresh Mailboxes
SentryLog.d(tag, "Refresh mailboxes from remote")
with(ApiRepository.getMailboxes()) {
if (isSuccess()) mailboxController.updateMailboxes(data!!)
}
}

companion object : NotificationUtilsCore() {

private val TAG: String = NotificationUtils::class.java.simpleName
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import com.infomaniak.mail.data.cache.mailboxInfo.MailboxController
import com.infomaniak.mail.di.IoDispatcher
import com.infomaniak.mail.utils.AccountUtils
import com.infomaniak.mail.utils.FetchMessagesManager
import com.infomaniak.mail.utils.NotificationUtils
import com.infomaniak.mail.utils.PlayServicesUtils
import dagger.assisted.Assisted
import dagger.assisted.AssistedInject
Expand All @@ -48,12 +49,16 @@ class SyncMailboxesWorker @AssistedInject constructor(
@Assisted params: WorkerParameters,
private val fetchMessagesManager: FetchMessagesManager,
private val mailboxController: MailboxController,
private val notificationUtils: NotificationUtils,
@IoDispatcher private val ioDispatcher: CoroutineDispatcher,
) : BaseCoroutineWorker(appContext, params) {

override suspend fun launchWork(): Result = withContext(ioDispatcher) {
SentryLog.d(TAG, "Work launched")

// Refresh current User and its Mailboxes
notificationUtils.updateUserAndMailboxes(mailboxController, TAG)

AccountUtils.getAllUsersSync().forEach { user ->
mailboxController.getMailboxes(user.id).forEach { mailbox ->
fetchMessagesManager.execute(scope = this, user.id, mailbox)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import com.infomaniak.mail.data.cache.mailboxContent.MessageController
import com.infomaniak.mail.data.cache.mailboxInfo.MailboxController
import com.infomaniak.mail.di.IoDispatcher
import com.infomaniak.mail.utils.FetchMessagesManager
import com.infomaniak.mail.utils.NotificationUtils
import com.infomaniak.mail.utils.SentryDebug
import com.infomaniak.mail.workers.BaseProcessMessageNotificationsWorker
import dagger.assisted.Assisted
Expand All @@ -45,6 +46,7 @@ class ProcessMessageNotificationsWorker @AssistedInject constructor(
@Assisted params: WorkerParameters,
private val fetchMessagesManager: FetchMessagesManager,
private val mailboxController: MailboxController,
private val notificationUtils: NotificationUtils,
@IoDispatcher private val ioDispatcher: CoroutineDispatcher,
) : BaseProcessMessageNotificationsWorker(appContext, params) {

Expand All @@ -63,21 +65,20 @@ class ProcessMessageNotificationsWorker @AssistedInject constructor(
SentryDebug.sendFailedNotification("No messageUid in Notification", SentryLevel.ERROR, userId, mailboxId)
return@withContext Result.success()
}

// Refresh current User and its Mailboxes
notificationUtils.updateUserAndMailboxes(mailboxController, TAG)

val mailbox = mailboxController.getMailbox(userId, mailboxId) ?: run {
// If the Mailbox doesn't exist in Realm, it's POSSIBLY because the user recently added
// this new Mailbox on its account, via the Infomaniak WebMail or somewhere else.
// We need to wait until the user opens the app again to fetch this new Mailbox.
// Then, we'll be able to handle Notifications for this Mailbox.
// Until then, we can leave safely.
SentryDebug.sendFailedNotification(
reason = "Received Notif: no Mailbox in Realm",
sentryLevel = SentryLevel.ERROR,
userId = userId,
mailboxId = mailboxId,
messageUid = messageUid,
)
// If the Mailbox doesn't exist in Realm, it's either because :
// - The Mailbox isn't attached to this User anymore.
// - The user POSSIBLY recently added this new Mailbox on its account, via the Infomaniak
// WebMail or somewhere else. We need to wait until the user opens the app again to
// fetch this new Mailbox. Then, we'll be able to handle Notifications for this Mailbox.
// Either way, we can leave safely.
return@withContext Result.success()
}

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

return@withContext runCatching {
Expand Down Expand Up @@ -119,6 +120,8 @@ class ProcessMessageNotificationsWorker @AssistedInject constructor(
}

companion object {
private const val TAG = "ProcessMessageNotificationsWorker"

private const val USER_ID_KEY = "userIdKey"
private const val MAILBOX_ID_KEY = "mailboxIdKey"
private const val MESSAGE_UID_KEY = "messageUidKey"
Expand Down
Loading