From c40eb9d3db85d463c77a755d6b3375a3cfbb5fe3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Gonc=CC=A7alves?= Date: Tue, 28 May 2024 21:31:28 +0100 Subject: [PATCH] PM-2572 Fix pr comments --- src/Core/Services/CipherService.cs | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/src/Core/Services/CipherService.cs b/src/Core/Services/CipherService.cs index aa3aa4522..13ec23209 100644 --- a/src/Core/Services/CipherService.cs +++ b/src/Core/Services/CipherService.cs @@ -578,8 +578,7 @@ namespace Bit.Core.Services //If the cipher doesn't have a key, we update it if(await ShouldUseCipherKeyEncryptionAsync() && cipherView.Key == null) { - cipher = await EncryptAsync(cipherView); - await UpdateAndUpsertAsync(() => _apiService.PutCipherAsync(cipherView.Id, new CipherRequest(cipher))); + await UpdateAndUpsertAsync(cipherView, cipher => _apiService.PutCipherAsync(cipherView.Id, new CipherRequest(cipher))); cipher = await GetAsync(cipherView.Id); cipherView = await cipher.DecryptAsync(); } @@ -596,8 +595,15 @@ namespace Bit.Core.Services await Task.WhenAll(attachmentTasks); cipherView.OrganizationId = organizationId; cipherView.CollectionIds = collectionIds; - cipher = await EncryptAsync(cipherView); - await UpdateAndUpsertAsync(() => _apiService.PutShareCipherAsync(cipherView.Id, new CipherShareRequest(cipher)), collectionIds); + await UpdateAndUpsertAsync(cipherView, cipher => _apiService.PutShareCipherAsync(cipherView.Id, new CipherShareRequest(cipher)), collectionIds); + + async Task UpdateAndUpsertAsync(CipherView cipherView, Func> callPutCipherApi, HashSet collectionIds = null) + { + var cipher = await EncryptAsync(cipherView); + var response = await callPutCipherApi(cipher); + var data = new CipherData(response, await _stateService.GetActiveUserIdAsync(), collectionIds); + await UpsertAsync(data); + } } public async Task SaveAttachmentRawWithServerAsync(Cipher cipher, CipherView cipherView, string filename, byte[] data) @@ -1360,13 +1366,6 @@ namespace Bit.Core.Services } } - private async Task UpdateAndUpsertAsync(Func> callPutCipherApi, HashSet collectionIds = null) - { - var response = await callPutCipherApi(); - var data = new CipherData(response, await _stateService.GetActiveUserIdAsync(), collectionIds); - await UpsertAsync(data); - } - private class CipherLocaleComparer : IComparer { private readonly II18nService _i18nService;