From ee61c83409fdac72315ea6e1caa6f4b8dd268ae6 Mon Sep 17 00:00:00 2001 From: Hinton Date: Thu, 24 Oct 2024 15:21:18 -0700 Subject: [PATCH] Add support for conflicts --- .../vault/datasource/disk/VaultDiskSource.kt | 6 ++++++ .../vault/datasource/disk/VaultDiskSourceImpl.kt | 16 ++++++++++++++++ .../data/vault/manager/CipherManagerImpl.kt | 6 +++--- 3 files changed, 25 insertions(+), 3 deletions(-) diff --git a/app/src/main/java/com/x8bit/bitwarden/data/vault/datasource/disk/VaultDiskSource.kt b/app/src/main/java/com/x8bit/bitwarden/data/vault/datasource/disk/VaultDiskSource.kt index 4e2b5efe1..3cec0b7ca 100644 --- a/app/src/main/java/com/x8bit/bitwarden/data/vault/datasource/disk/VaultDiskSource.kt +++ b/app/src/main/java/com/x8bit/bitwarden/data/vault/datasource/disk/VaultDiskSource.kt @@ -3,6 +3,7 @@ package com.x8bit.bitwarden.data.vault.datasource.disk import com.bitwarden.vault.Cipher import com.x8bit.bitwarden.data.vault.datasource.network.model.OfflineCipherJson import com.x8bit.bitwarden.data.vault.datasource.network.model.SyncResponseJson +import com.x8bit.bitwarden.data.vault.datasource.sdk.model.OfflineCipher import kotlinx.coroutines.flow.Flow /** @@ -16,6 +17,11 @@ interface VaultDiskSource { */ suspend fun saveOfflineCipher(userId: String, cipher: Cipher) + /** + * Saves an offline cipher to the offline data source for the given [userId]. + */ + suspend fun updateOfflineCipher(userId: String, cipher: OfflineCipher) + /** * Saves a cipher to the data source for the given [userId]. */ diff --git a/app/src/main/java/com/x8bit/bitwarden/data/vault/datasource/disk/VaultDiskSourceImpl.kt b/app/src/main/java/com/x8bit/bitwarden/data/vault/datasource/disk/VaultDiskSourceImpl.kt index 512fafbdb..71cca2d06 100644 --- a/app/src/main/java/com/x8bit/bitwarden/data/vault/datasource/disk/VaultDiskSourceImpl.kt +++ b/app/src/main/java/com/x8bit/bitwarden/data/vault/datasource/disk/VaultDiskSourceImpl.kt @@ -17,6 +17,7 @@ import com.x8bit.bitwarden.data.vault.datasource.disk.entity.OfflineCipherEntity import com.x8bit.bitwarden.data.vault.datasource.disk.entity.SendEntity import com.x8bit.bitwarden.data.vault.datasource.network.model.OfflineCipherJson import com.x8bit.bitwarden.data.vault.datasource.network.model.SyncResponseJson +import com.x8bit.bitwarden.data.vault.datasource.sdk.model.OfflineCipher import com.x8bit.bitwarden.data.vault.repository.util.toOfflineCipher import com.x8bit.bitwarden.data.vault.repository.util.toOfflineCipherJson import com.x8bit.bitwarden.data.vault.repository.util.toSdkCipherJson @@ -72,6 +73,21 @@ class VaultDiskSourceImpl( ) } + override suspend fun updateOfflineCipher(userId: String, cipher: OfflineCipher) { + offlineCiphersDao.insertCiphers( + ciphers = listOf( + OfflineCipherEntity( + id = cipher.id!!, + userId = userId, + cipherType = json.encodeToString(cipher.type), + cipherJson = json.encodeToString( + cipher.toOfflineCipherJson(cipher.id) + ), + ), + ), + ) + } + override suspend fun saveCipher(userId: String, cipher: SyncResponseJson.Cipher) { ciphersDao.insertCiphers( ciphers = listOf( diff --git a/app/src/main/java/com/x8bit/bitwarden/data/vault/manager/CipherManagerImpl.kt b/app/src/main/java/com/x8bit/bitwarden/data/vault/manager/CipherManagerImpl.kt index e18017e9f..99529c0ee 100644 --- a/app/src/main/java/com/x8bit/bitwarden/data/vault/manager/CipherManagerImpl.kt +++ b/app/src/main/java/com/x8bit/bitwarden/data/vault/manager/CipherManagerImpl.kt @@ -71,7 +71,7 @@ class CipherManagerImpl( // Device went online // TODO: We need to add support for non active users! - vaultDiskSource.getOfflineCiphers(activeUserId!!) + vaultDiskSource.getOfflineCiphers(activeUserId!!).map { it.filter { it.mergeConflict == false } } } else { flowOf(listOf()) } @@ -91,7 +91,7 @@ class CipherManagerImpl( vaultDiskSource.deleteOfflineCipher(userId = userId, cipherId = c.id) } .fold( - onFailure = { CreateCipherResult.Error }, + onFailure = { vaultDiskSource.updateOfflineCipher(userId = userId, c.toOfflineCipher().copy(mergeConflict = true)) }, onSuccess = { CreateCipherResult.Success }, ) else -> ciphersService.updateCipher( @@ -115,7 +115,7 @@ class CipherManagerImpl( } } .fold( - onFailure = { UpdateCipherResult.Error(errorMessage = null) }, + onFailure = { vaultDiskSource.updateOfflineCipher(userId = userId, c.toOfflineCipher().copy(mergeConflict = true)) }, onSuccess = { it }, ) }