diff --git a/src/App/Abstractions/Services/ICipherService.cs b/src/App/Abstractions/Services/ICipherService.cs index dead4d00f..9f5713e8b 100644 --- a/src/App/Abstractions/Services/ICipherService.cs +++ b/src/App/Abstractions/Services/ICipherService.cs @@ -3,6 +3,7 @@ using System.Threading.Tasks; using Bit.App.Models; using Bit.App.Models.Api; using System; +using Bit.App.Models.Data; namespace Bit.App.Abstractions { @@ -13,9 +14,13 @@ namespace Bit.App.Abstractions Task> GetAllAsync(bool favorites); Task, IEnumerable>> GetAllAsync(string uriString); Task> SaveAsync(Cipher cipher); + Task UpsertDataAsync(CipherData cipher); Task DeleteAsync(string id); + Task DeleteDataAsync(string id); Task DownloadAndDecryptAttachmentAsync(string url, string orgId = null); Task> EncryptAndSaveAttachmentAsync(Cipher cipher, byte[] data, string fileName); + Task UpsertAttachmentDataAsync(IEnumerable attachments); Task DeleteAttachmentAsync(Cipher cipher, string attachmentId); + Task DeleteAttachmentDataAsync(string attachmentId); } } diff --git a/src/App/Services/CipherService.cs b/src/App/Services/CipherService.cs index cdb247e51..22f31fb41 100644 --- a/src/App/Services/CipherService.cs +++ b/src/App/Services/CipherService.cs @@ -236,17 +236,8 @@ namespace Bit.App.Services if(response.Succeeded) { var data = new CipherData(response.Result, _authService.UserId); - if(cipher.Id == null) - { - await _cipherRepository.InsertAsync(data); - cipher.Id = data.Id; - } - else - { - await _cipherRepository.UpdateAsync(data); - } - - _cachedCiphers = null; + await UpsertDataAsync(data); + cipher.Id = data.Id; } else if(response.StatusCode == System.Net.HttpStatusCode.Forbidden || response.StatusCode == System.Net.HttpStatusCode.Unauthorized) @@ -257,13 +248,18 @@ namespace Bit.App.Services return response; } + public async Task UpsertDataAsync(CipherData cipher) + { + await _cipherRepository.UpsertAsync(cipher); + _cachedCiphers = null; + } + public async Task DeleteAsync(string id) { var response = await _cipherApiRepository.DeleteAsync(id); if(response.Succeeded) { - await _cipherRepository.DeleteAsync(id); - _cachedCiphers = null; + await DeleteDataAsync(id); } else if(response.StatusCode == System.Net.HttpStatusCode.Forbidden || response.StatusCode == System.Net.HttpStatusCode.Unauthorized) @@ -274,6 +270,12 @@ namespace Bit.App.Services return response; } + public async Task DeleteDataAsync(string id) + { + await _cipherRepository.DeleteAsync(id); + _cachedCiphers = null; + } + public async Task DownloadAndDecryptAttachmentAsync(string url, string orgId = null) { using(var client = new HttpClient()) @@ -318,10 +320,7 @@ namespace Bit.App.Services if(response.Succeeded) { var attachmentData = response.Result.Attachments.Select(a => new AttachmentData(a, cipher.Id)); - foreach(var attachment in attachmentData) - { - await _attachmentRepository.UpsertAsync(attachment); - } + await UpsertAttachmentDataAsync(attachmentData); cipher.Attachments = response.Result.Attachments.Select(a => new Attachment(a)); } else if(response.StatusCode == System.Net.HttpStatusCode.Forbidden @@ -333,12 +332,21 @@ namespace Bit.App.Services return response; } + public async Task UpsertAttachmentDataAsync(IEnumerable attachments) + { + foreach(var attachment in attachments) + { + await _attachmentRepository.UpsertAsync(attachment); + } + _cachedCiphers = null; + } + public async Task DeleteAttachmentAsync(Cipher cipher, string attachmentId) { var response = await _cipherApiRepository.DeleteAttachmentAsync(cipher.Id, attachmentId); if(response.Succeeded) { - await _attachmentRepository.DeleteAsync(attachmentId); + await DeleteAttachmentDataAsync(attachmentId); } else if(response.StatusCode == System.Net.HttpStatusCode.Forbidden || response.StatusCode == System.Net.HttpStatusCode.Unauthorized) @@ -349,6 +357,12 @@ namespace Bit.App.Services return response; } + public async Task DeleteAttachmentDataAsync(string attachmentId) + { + await _attachmentRepository.DeleteAsync(attachmentId); + _cachedCiphers = null; + } + private Tuple InfoFromMobileAppUri(string mobileAppUriString) { if(UriIsAndroidApp(mobileAppUriString)) diff --git a/src/App/Services/SyncService.cs b/src/App/Services/SyncService.cs index ae3f6ffdc..d6d78f4f3 100644 --- a/src/App/Services/SyncService.cs +++ b/src/App/Services/SyncService.cs @@ -20,7 +20,7 @@ namespace Bit.App.Services private readonly ISettingsApiRepository _settingsApiRepository; private readonly ISyncApiRepository _syncApiRepository; private readonly IFolderRepository _folderRepository; - private readonly ICipherRepository _cipherRepository; + private readonly ICipherService _cipherService; private readonly IAttachmentRepository _attachmentRepository; private readonly ISettingsRepository _settingsRepository; private readonly IAuthService _authService; @@ -35,7 +35,7 @@ namespace Bit.App.Services ISettingsApiRepository settingsApiRepository, ISyncApiRepository syncApiRepository, IFolderRepository folderRepository, - ICipherRepository cipherRepository, + ICipherService cipherService, IAttachmentRepository attachmentRepository, ISettingsRepository settingsRepository, IAuthService authService, @@ -49,7 +49,7 @@ namespace Bit.App.Services _settingsApiRepository = settingsApiRepository; _syncApiRepository = syncApiRepository; _folderRepository = folderRepository; - _cipherRepository = cipherRepository; + _cipherService = cipherService; _attachmentRepository = attachmentRepository; _settingsRepository = settingsRepository; _authService = authService; @@ -78,18 +78,15 @@ namespace Bit.App.Services try { var cipherData = new CipherData(cipher.Result, _authService.UserId); - await _cipherRepository.UpsertAsync(cipherData).ConfigureAwait(false); + await _cipherService.UpsertDataAsync(cipherData).ConfigureAwait(false); var localAttachments = (await _attachmentRepository.GetAllByCipherIdAsync(cipherData.Id) .ConfigureAwait(false)); if(cipher.Result.Attachments != null) { - foreach(var attachment in cipher.Result.Attachments) - { - var attachmentData = new AttachmentData(attachment, cipherData.Id); - await _attachmentRepository.UpsertAsync(attachmentData).ConfigureAwait(false); - } + var attachmentData = cipher.Result.Attachments.Select(a => new AttachmentData(a, cipherData.Id)); + await _cipherService.UpsertAttachmentDataAsync(attachmentData).ConfigureAwait(false); } if(localAttachments != null) @@ -99,7 +96,7 @@ namespace Bit.App.Services { try { - await _attachmentRepository.DeleteAsync(attachment.Id).ConfigureAwait(false); + await _cipherService.DeleteAttachmentDataAsync(attachment.Id).ConfigureAwait(false); } catch(SQLite.SQLiteException) { } } @@ -178,7 +175,7 @@ namespace Bit.App.Services try { - await _cipherRepository.DeleteAsync(id).ConfigureAwait(false); + await _cipherService.DeleteDataAsync(id).ConfigureAwait(false); SyncCompleted(true); return true; } @@ -359,7 +356,7 @@ namespace Bit.App.Services return; } - var localCiphers = (await _cipherRepository.GetAllByUserIdAsync(_authService.UserId) + var localCiphers = (await _cipherService.GetAllAsync() .ConfigureAwait(false)) .GroupBy(s => s.Id) .Select(s => s.First()) @@ -383,15 +380,12 @@ namespace Bit.App.Services localCiphers[serverCipher.Value.Id] : null; var data = new CipherData(serverCipher.Value, _authService.UserId); - await _cipherRepository.UpsertAsync(data).ConfigureAwait(false); + await _cipherService.UpsertDataAsync(data).ConfigureAwait(false); if(serverCipher.Value.Attachments != null) { - foreach(var attachment in serverCipher.Value.Attachments) - { - var attachmentData = new AttachmentData(attachment, data.Id); - await _attachmentRepository.UpsertAsync(attachmentData).ConfigureAwait(false); - } + var attachmentData = serverCipher.Value.Attachments.Select(a => new AttachmentData(a, data.Id)); + await _cipherService.UpsertAttachmentDataAsync(attachmentData).ConfigureAwait(false); } if(localCipher != null && localAttachments != null && localAttachments.ContainsKey(localCipher.Id)) @@ -401,7 +395,7 @@ namespace Bit.App.Services { try { - await _attachmentRepository.DeleteAsync(attachment.Id).ConfigureAwait(false); + await _cipherService.DeleteAttachmentDataAsync(attachment.Id).ConfigureAwait(false); } catch(SQLite.SQLiteException) { } } @@ -414,7 +408,7 @@ namespace Bit.App.Services { try { - await _cipherRepository.DeleteAsync(cipher.Value.Id).ConfigureAwait(false); + await _cipherService.DeleteDataAsync(cipher.Value.Id).ConfigureAwait(false); } catch(SQLite.SQLiteException) { } }