Add support for conflicts

This commit is contained in:
Hinton 2024-10-24 15:21:18 -07:00
parent b30e52245f
commit ee61c83409
No known key found for this signature in database
GPG key ID: 5F7295599C5D965C
3 changed files with 25 additions and 3 deletions

View file

@ -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].
*/

View file

@ -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(

View file

@ -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<OfflineCipherJson>())
}
@ -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 },
)
}