mirror of
https://github.com/bitwarden/android.git
synced 2026-06-07 23:58:03 -05:00
Saves changes to database instead of syncing (#517)
This commit is contained in:
committed by
Álison Fernandes
parent
5469874c95
commit
6acfb10709
@@ -8,21 +8,41 @@ import kotlinx.coroutines.flow.Flow
|
||||
*/
|
||||
interface VaultDiskSource {
|
||||
|
||||
/**
|
||||
* Saves a cipher to the data source for the given [userId].
|
||||
*/
|
||||
suspend fun saveCipher(userId: String, cipher: SyncResponseJson.Cipher)
|
||||
|
||||
/**
|
||||
* Retrieves all ciphers from the data source for a given [userId].
|
||||
*/
|
||||
fun getCiphers(userId: String): Flow<List<SyncResponseJson.Cipher>>
|
||||
|
||||
/**
|
||||
* Saves a collection to the data source for the given [userId].
|
||||
*/
|
||||
suspend fun saveCollection(userId: String, collection: SyncResponseJson.Collection)
|
||||
|
||||
/**
|
||||
* Retrieves all collections from the data source for a given [userId].
|
||||
*/
|
||||
fun getCollections(userId: String): Flow<List<SyncResponseJson.Collection>>
|
||||
|
||||
/**
|
||||
* Saves a folder to the data source for the given [userId].
|
||||
*/
|
||||
suspend fun saveFolder(userId: String, folder: SyncResponseJson.Folder)
|
||||
|
||||
/**
|
||||
* Retrieves all folders from the data source for a given [userId].
|
||||
*/
|
||||
fun getFolders(userId: String): Flow<List<SyncResponseJson.Folder>>
|
||||
|
||||
/**
|
||||
* Saves a send to the data source for the given [userId].
|
||||
*/
|
||||
suspend fun saveSend(userId: String, send: SyncResponseJson.Send)
|
||||
|
||||
/**
|
||||
* Retrieves all sends from the data source for a given [userId].
|
||||
*/
|
||||
|
||||
@@ -36,6 +36,19 @@ class VaultDiskSourceImpl(
|
||||
private val forceFolderFlow = bufferedMutableSharedFlow<List<SyncResponseJson.Folder>>()
|
||||
private val forceSendFlow = bufferedMutableSharedFlow<List<SyncResponseJson.Send>>()
|
||||
|
||||
override suspend fun saveCipher(userId: String, cipher: SyncResponseJson.Cipher) {
|
||||
ciphersDao.insertCiphers(
|
||||
ciphers = listOf(
|
||||
CipherEntity(
|
||||
id = cipher.id,
|
||||
userId = userId,
|
||||
cipherType = json.encodeToString(cipher.type),
|
||||
cipherJson = json.encodeToString(cipher),
|
||||
),
|
||||
),
|
||||
)
|
||||
}
|
||||
|
||||
override fun getCiphers(
|
||||
userId: String,
|
||||
): Flow<List<SyncResponseJson.Cipher>> =
|
||||
@@ -50,6 +63,20 @@ class VaultDiskSourceImpl(
|
||||
},
|
||||
)
|
||||
|
||||
override suspend fun saveCollection(userId: String, collection: SyncResponseJson.Collection) {
|
||||
collectionsDao.insertCollection(
|
||||
collection = CollectionEntity(
|
||||
userId = userId,
|
||||
id = collection.id,
|
||||
name = collection.name,
|
||||
organizationId = collection.organizationId,
|
||||
shouldHidePasswords = collection.shouldHidePasswords,
|
||||
externalId = collection.externalId,
|
||||
isReadOnly = collection.isReadOnly,
|
||||
),
|
||||
)
|
||||
}
|
||||
|
||||
override fun getCollections(
|
||||
userId: String,
|
||||
): Flow<List<SyncResponseJson.Collection>> =
|
||||
@@ -71,6 +98,17 @@ class VaultDiskSourceImpl(
|
||||
},
|
||||
)
|
||||
|
||||
override suspend fun saveFolder(userId: String, folder: SyncResponseJson.Folder) {
|
||||
foldersDao.insertFolder(
|
||||
folder = FolderEntity(
|
||||
userId = userId,
|
||||
id = folder.id,
|
||||
name = folder.name,
|
||||
revisionDate = folder.revisionDate,
|
||||
),
|
||||
)
|
||||
}
|
||||
|
||||
override fun getFolders(
|
||||
userId: String,
|
||||
): Flow<List<SyncResponseJson.Folder>> =
|
||||
@@ -89,6 +127,19 @@ class VaultDiskSourceImpl(
|
||||
},
|
||||
)
|
||||
|
||||
override suspend fun saveSend(userId: String, send: SyncResponseJson.Send) {
|
||||
sendsDao.insertSends(
|
||||
sends = listOf(
|
||||
SendEntity(
|
||||
userId = userId,
|
||||
id = send.id,
|
||||
sendType = json.encodeToString(send.type),
|
||||
sendJson = json.encodeToString(send),
|
||||
),
|
||||
),
|
||||
)
|
||||
}
|
||||
|
||||
override fun getSends(
|
||||
userId: String,
|
||||
): Flow<List<SyncResponseJson.Send>> =
|
||||
|
||||
@@ -370,10 +370,11 @@ class VaultRepositoryImpl(
|
||||
.onCompletion { willSyncAfterUnlock = false }
|
||||
.first()
|
||||
|
||||
override suspend fun createCipher(cipherView: CipherView): CreateCipherResult =
|
||||
vaultSdkSource
|
||||
override suspend fun createCipher(cipherView: CipherView): CreateCipherResult {
|
||||
val userId = requireNotNull(activeUserId)
|
||||
return vaultSdkSource
|
||||
.encryptCipher(
|
||||
userId = requireNotNull(activeUserId),
|
||||
userId = userId,
|
||||
cipherView = cipherView,
|
||||
)
|
||||
.flatMap { cipher ->
|
||||
@@ -387,18 +388,20 @@ class VaultRepositoryImpl(
|
||||
CreateCipherResult.Error
|
||||
},
|
||||
onSuccess = {
|
||||
sync()
|
||||
vaultDiskSource.saveCipher(userId = userId, cipher = it)
|
||||
CreateCipherResult.Success
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
override suspend fun updateCipher(
|
||||
cipherId: String,
|
||||
cipherView: CipherView,
|
||||
): UpdateCipherResult =
|
||||
vaultSdkSource
|
||||
): UpdateCipherResult {
|
||||
val userId = requireNotNull(activeUserId)
|
||||
return vaultSdkSource
|
||||
.encryptCipher(
|
||||
userId = requireNotNull(activeUserId),
|
||||
userId = userId,
|
||||
cipherView = cipherView,
|
||||
)
|
||||
.flatMap { cipher ->
|
||||
@@ -416,35 +419,39 @@ class VaultRepositoryImpl(
|
||||
}
|
||||
|
||||
is UpdateCipherResponseJson.Success -> {
|
||||
sync()
|
||||
vaultDiskSource.saveCipher(userId = userId, cipher = response.cipher)
|
||||
UpdateCipherResult.Success
|
||||
}
|
||||
}
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
override suspend fun createSend(sendView: SendView): CreateSendResult =
|
||||
vaultSdkSource
|
||||
override suspend fun createSend(sendView: SendView): CreateSendResult {
|
||||
val userId = requireNotNull(activeUserId)
|
||||
return vaultSdkSource
|
||||
.encryptSend(
|
||||
userId = requireNotNull(activeUserId),
|
||||
userId = userId,
|
||||
sendView = sendView,
|
||||
)
|
||||
.flatMap { send -> sendsService.createSend(body = send.toEncryptedNetworkSend()) }
|
||||
.fold(
|
||||
onFailure = { CreateSendResult.Error },
|
||||
onSuccess = {
|
||||
sync()
|
||||
vaultDiskSource.saveSend(userId = userId, send = it)
|
||||
CreateSendResult.Success
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
override suspend fun updateSend(
|
||||
sendId: String,
|
||||
sendView: SendView,
|
||||
): UpdateSendResult =
|
||||
vaultSdkSource
|
||||
): UpdateSendResult {
|
||||
val userId = requireNotNull(activeUserId)
|
||||
return vaultSdkSource
|
||||
.encryptSend(
|
||||
userId = requireNotNull(activeUserId),
|
||||
userId = userId,
|
||||
sendView = sendView,
|
||||
)
|
||||
.flatMap { send ->
|
||||
@@ -462,12 +469,13 @@ class VaultRepositoryImpl(
|
||||
}
|
||||
|
||||
is UpdateSendResponseJson.Success -> {
|
||||
sync()
|
||||
vaultDiskSource.saveSend(userId = userId, send = response.send)
|
||||
UpdateSendResult.Success
|
||||
}
|
||||
}
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
// TODO: This is temporary. Eventually this needs to be based on the presence of various
|
||||
// user keys but this will likely require SDK updates to support this (BIT-1190).
|
||||
|
||||
Reference in New Issue
Block a user