Update UserStateJson with avatarColor and securityStamp from sync (#265)

This commit is contained in:
Brian Yencho
2024-06-20 17:08:07 +01:00
committed by Álison Fernandes
parent 58eb4d4107
commit feba8595b3
5 changed files with 193 additions and 42 deletions
@@ -0,0 +1,33 @@
package com.x8bit.bitwarden.data.auth.repository.util
import com.x8bit.bitwarden.data.auth.datasource.disk.model.UserStateJson
import com.x8bit.bitwarden.data.vault.datasource.network.model.SyncResponseJson
/**
* Updates the given [UserStateJson] with the data from the [syncResponse] to return a new
* [UserStateJson]. The original will be returned if the sync response does not match any accounts
* in the [UserStateJson].
*/
@Suppress("ReturnCount")
fun UserStateJson.toUpdatedUserStateJson(
syncResponse: SyncResponseJson,
): UserStateJson {
val userId = syncResponse.profile?.id ?: return this
val account = this.accounts[userId] ?: return this
val profile = account.profile
// TODO: Update additional missing UserStateJson properties (BIT-916)
val updatedProfile = profile
.copy(
avatarColorHex = syncResponse.profile.avatarColor,
stamp = syncResponse.profile.securityStamp,
)
val updatedAccount = account.copy(profile = updatedProfile)
return this
.copy(
accounts = accounts
.toMutableMap()
.apply {
replace(userId, updatedAccount)
},
)
}
@@ -5,6 +5,7 @@ import com.bitwarden.core.FolderView
import com.bitwarden.core.InitCryptoRequest
import com.x8bit.bitwarden.data.auth.datasource.disk.AuthDiskSource
import com.x8bit.bitwarden.data.auth.repository.util.toSdkParams
import com.x8bit.bitwarden.data.auth.repository.util.toUpdatedUserStateJson
import com.x8bit.bitwarden.data.platform.datasource.network.util.isNoConnectionError
import com.x8bit.bitwarden.data.platform.manager.dispatcher.DispatcherManager
import com.x8bit.bitwarden.data.platform.repository.model.DataState
@@ -85,6 +86,13 @@ class VaultRepositoryImpl constructor(
.sync()
.fold(
onSuccess = { syncResponse ->
// Update user information with additional information from sync response
authDiskSource.userState = authDiskSource
.userState
?.toUpdatedUserStateJson(
syncResponse = syncResponse,
)
storeUserKeyAndPrivateKey(
userKey = syncResponse.profile?.key,
privateKey = syncResponse.profile?.privateKey,