From 43099b2064aeef6562885bc28af51c28918331a5 Mon Sep 17 00:00:00 2001 From: Louis CAD Date: Wed, 6 May 2026 17:51:26 +0200 Subject: [PATCH 1/2] fix: Re-check up-to-date checks for workers scheduling on user token update This way, if the last update files are removed before a token update, we will reschedule the worker to send an update with the new token. --- .../back/internal/deviceinfo/DeviceInfoUpdateManager.kt | 4 +++- .../src/main/kotlin/NotificationsRegistrationManager.kt | 4 +++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/CrossAppLogin/Back/src/main/kotlin/com/infomaniak/core/crossapplogin/back/internal/deviceinfo/DeviceInfoUpdateManager.kt b/CrossAppLogin/Back/src/main/kotlin/com/infomaniak/core/crossapplogin/back/internal/deviceinfo/DeviceInfoUpdateManager.kt index 76fac1ac5..fb0a8a8b5 100644 --- a/CrossAppLogin/Back/src/main/kotlin/com/infomaniak/core/crossapplogin/back/internal/deviceinfo/DeviceInfoUpdateManager.kt +++ b/CrossAppLogin/Back/src/main/kotlin/com/infomaniak/core/crossapplogin/back/internal/deviceinfo/DeviceInfoUpdateManager.kt @@ -100,7 +100,9 @@ object DeviceInfoUpdateManager : AssociatedUserDataCleanable { Dispatchers.Default { val crossAppLogin = CrossAppLogin.forContext(context = appCtx, coroutineScope = this) crossAppLogin.sharedDeviceIdFlow.collectLatest { currentCrossAppDeviceId -> - val userIdsFlow = UserDatabase().userDao().allUsers.map { users -> users.map { it.id } }.distinctUntilChanged() + val userIdsFlow = UserDatabase().userDao().allUsers.map { users -> + users.map { it.id to it.apiToken } + }.distinctUntilChanged().map { it.map { (id, _) -> id } } userIdsFlow.collect { userIds -> val isEverythingUpToDate = userIds.allConcurrent { isUpToDate(currentCrossAppDeviceId, it.toLong()) } if (isEverythingUpToDate) return@collect diff --git a/Notifications/Registration/src/main/kotlin/NotificationsRegistrationManager.kt b/Notifications/Registration/src/main/kotlin/NotificationsRegistrationManager.kt index 6448bab65..aba3c3c20 100644 --- a/Notifications/Registration/src/main/kotlin/NotificationsRegistrationManager.kt +++ b/Notifications/Registration/src/main/kotlin/NotificationsRegistrationManager.kt @@ -110,7 +110,9 @@ object NotificationsRegistrationManager : AssociatedUserDataCleanable { emit(token) }.stateIn(this) - val userIdsFlow = UserDatabase().userDao().allUsers.map { users -> users.map { it.id } }.distinctUntilChanged() + val userIdsFlow = UserDatabase().userDao().allUsers.map { users -> + users.map { it.id to it.apiToken } + }.distinctUntilChanged().map { it.map { (id, _) -> id } } latestFcmToken.collectLatest { fcmToken -> userIdsFlow.collectLatest { userIds -> From a948ed2b7cfec6f54eb8c63323dff2c360de165a Mon Sep 17 00:00:00 2001 From: Louis CAD Date: Thu, 7 May 2026 09:51:01 +0200 Subject: [PATCH 2/2] chore: Add comment + TODO comment about code to de-duplicate --- .../back/internal/deviceinfo/DeviceInfoUpdateManager.kt | 6 ++++++ .../src/main/kotlin/NotificationsRegistrationManager.kt | 4 ++++ 2 files changed, 10 insertions(+) diff --git a/CrossAppLogin/Back/src/main/kotlin/com/infomaniak/core/crossapplogin/back/internal/deviceinfo/DeviceInfoUpdateManager.kt b/CrossAppLogin/Back/src/main/kotlin/com/infomaniak/core/crossapplogin/back/internal/deviceinfo/DeviceInfoUpdateManager.kt index fb0a8a8b5..6d6dd0f64 100644 --- a/CrossAppLogin/Back/src/main/kotlin/com/infomaniak/core/crossapplogin/back/internal/deviceinfo/DeviceInfoUpdateManager.kt +++ b/CrossAppLogin/Back/src/main/kotlin/com/infomaniak/core/crossapplogin/back/internal/deviceinfo/DeviceInfoUpdateManager.kt @@ -100,9 +100,15 @@ object DeviceInfoUpdateManager : AssociatedUserDataCleanable { Dispatchers.Default { val crossAppLogin = CrossAppLogin.forContext(context = appCtx, coroutineScope = this) crossAppLogin.sharedDeviceIdFlow.collectLatest { currentCrossAppDeviceId -> + + // We need to check for updates when the set of users changes, or when any of their token changes. + // When the token is refreshed, the corresponding file is supposed to have been cleared, + // so that the up-to-date check triggers a new schedule. + //TODO[Core-associated-user-data]: Factorize this duplicate code, and possibly the common logic. val userIdsFlow = UserDatabase().userDao().allUsers.map { users -> users.map { it.id to it.apiToken } }.distinctUntilChanged().map { it.map { (id, _) -> id } } + userIdsFlow.collect { userIds -> val isEverythingUpToDate = userIds.allConcurrent { isUpToDate(currentCrossAppDeviceId, it.toLong()) } if (isEverythingUpToDate) return@collect diff --git a/Notifications/Registration/src/main/kotlin/NotificationsRegistrationManager.kt b/Notifications/Registration/src/main/kotlin/NotificationsRegistrationManager.kt index aba3c3c20..1b75fdd94 100644 --- a/Notifications/Registration/src/main/kotlin/NotificationsRegistrationManager.kt +++ b/Notifications/Registration/src/main/kotlin/NotificationsRegistrationManager.kt @@ -110,6 +110,10 @@ object NotificationsRegistrationManager : AssociatedUserDataCleanable { emit(token) }.stateIn(this) + // We need to check for updates when the set of users changes, or when any of their token changes. + // When the token is refreshed, the corresponding file is supposed to have been cleared, + // so that the up-to-date check triggers a new schedule. + //TODO[Core-associated-user-data]: Factorize this duplicate code, and possibly the common logic. val userIdsFlow = UserDatabase().userDao().allUsers.map { users -> users.map { it.id to it.apiToken } }.distinctUntilChanged().map { it.map { (id, _) -> id } }