public async Task <IEnumerable <Login> > GetAllAsync() { var attachmentData = await _attachmentRepository.GetAllByUserIdAsync(_authService.UserId); var attachmentDict = attachmentData.GroupBy(a => a.LoginId).ToDictionary(g => g.Key, g => g.ToList()); var data = await _loginRepository.GetAllByUserIdAsync(_authService.UserId); var logins = data.Select(f => new Login(f, attachmentDict.ContainsKey(f.Id) ? attachmentDict[f.Id] : null)); return(logins); }
public async Task <IEnumerable <Cipher> > GetAllAsync() { if (!_deviceInfoService.IsExtension && _appSettingsService.ClearCiphersCache) { CachedCiphers = null; _appSettingsService.ClearCiphersCache = false; } if (_deviceInfoService.IsExtension && _appSettingsService.ClearExtensionCiphersCache) { CachedCiphers = null; _appSettingsService.ClearExtensionCiphersCache = false; } if (CachedCiphers != null) { return(CachedCiphers); } var attachmentData = await _attachmentRepository.GetAllByUserIdAsync(_authService.UserId); var attachmentDict = attachmentData.GroupBy(a => a.LoginId).ToDictionary(g => g.Key, g => g.ToList()); var data = await _cipherRepository.GetAllByUserIdAsync(_authService.UserId); CachedCiphers = data .Select(f => new Cipher(f, attachmentDict.ContainsKey(f.Id) ? attachmentDict[f.Id] : null)) .ToList(); return(CachedCiphers); }
public async Task <IEnumerable <Cipher> > GetAllAsync() { if (CachedCiphers != null) { return(CachedCiphers); } var attachmentData = await _attachmentRepository.GetAllByUserIdAsync(_authService.UserId); var attachmentDict = attachmentData.GroupBy(a => a.LoginId).ToDictionary(g => g.Key, g => g.ToList()); var data = await _cipherRepository.GetAllByUserIdAsync(_authService.UserId); CachedCiphers = data .Select(f => new Cipher(f, attachmentDict.ContainsKey(f.Id) ? attachmentDict[f.Id] : null)) .ToList(); return(CachedCiphers); }
private async Task SyncCiphersAsync(IDictionary <string, CipherResponse> serverCiphers) { if (!_authService.IsAuthenticated) { return; } var localCiphers = (await _cipherService.GetAllAsync() .ConfigureAwait(false)) .GroupBy(s => s.Id) .Select(s => s.First()) .ToDictionary(s => s.Id); var localAttachments = (await _attachmentRepository.GetAllByUserIdAsync(_authService.UserId) .ConfigureAwait(false)) .GroupBy(a => a.LoginId) .ToDictionary(g => g.Key); var cipherCollections = new List <CipherCollectionData>(); foreach (var serverCipher in serverCiphers) { if (!_authService.IsAuthenticated) { return; } var collectionForThisCipher = serverCipher.Value.CollectionIds?.Select(cid => new CipherCollectionData { CipherId = serverCipher.Value.Id, CollectionId = cid, UserId = _authService.UserId }).ToList(); if (collectionForThisCipher != null && collectionForThisCipher.Any()) { cipherCollections.AddRange(collectionForThisCipher); } try { var localCipher = localCiphers.ContainsKey(serverCipher.Value.Id) ? localCiphers[serverCipher.Value.Id] : null; var data = new CipherData(serverCipher.Value, _authService.UserId); await _cipherService.UpsertDataAsync(data).ConfigureAwait(false); if (serverCipher.Value.Attachments != null) { 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)) { foreach (var attachment in localAttachments[localCipher.Id] .Where(a => !serverCipher.Value.Attachments.Any(sa => sa.Id == a.Id))) { try { await _cipherService.DeleteAttachmentDataAsync(attachment.Id).ConfigureAwait(false); } catch (SQLite.SQLiteException) { } } } } catch (SQLite.SQLiteException) { } } foreach (var cipher in localCiphers.Where(local => !serverCiphers.ContainsKey(local.Key))) { try { await _cipherService.DeleteDataAsync(cipher.Value.Id).ConfigureAwait(false); } catch (SQLite.SQLiteException) { } } try { await _cipherCollectionRepository.DeleteByUserIdAsync(_authService.UserId).ConfigureAwait(false); } catch (SQLite.SQLiteException) { } foreach (var cipherCollection in cipherCollections) { try { await _cipherCollectionRepository.InsertAsync(cipherCollection).ConfigureAwait(false); } catch (SQLite.SQLiteException) { } } }
private async Task SyncLoginsAsync(IDictionary <string, CipherResponse> serverLogins) { if (!_authService.IsAuthenticated) { return; } var localLogins = (await _loginRepository.GetAllByUserIdAsync(_authService.UserId) .ConfigureAwait(false)) .GroupBy(s => s.Id) .Select(s => s.First()) .ToDictionary(s => s.Id); var localAttachments = (await _attachmentRepository.GetAllByUserIdAsync(_authService.UserId) .ConfigureAwait(false)) .GroupBy(a => a.LoginId) .ToDictionary(g => g.Key); foreach (var serverLogin in serverLogins) { if (!_authService.IsAuthenticated) { return; } try { var localLogin = localLogins.ContainsKey(serverLogin.Value.Id) ? localLogins[serverLogin.Value.Id] : null; var data = new LoginData(serverLogin.Value, _authService.UserId); await _loginRepository.UpsertAsync(data).ConfigureAwait(false); if (serverLogin.Value.Attachments != null) { foreach (var attachment in serverLogin.Value.Attachments) { var attachmentData = new AttachmentData(attachment, data.Id); await _attachmentRepository.UpsertAsync(attachmentData).ConfigureAwait(false); } } if (localLogin != null && localAttachments != null && localAttachments.ContainsKey(localLogin.Id)) { foreach (var attachment in localAttachments[localLogin.Id] .Where(a => !serverLogin.Value.Attachments.Any(sa => sa.Id == a.Id))) { try { await _attachmentRepository.DeleteAsync(attachment.Id).ConfigureAwait(false); } catch (SQLite.SQLiteException) { } } } } catch (SQLite.SQLiteException) { } } foreach (var login in localLogins.Where(localLogin => !serverLogins.ContainsKey(localLogin.Key))) { try { await _loginRepository.DeleteAsync(login.Value.Id).ConfigureAwait(false); } catch (SQLite.SQLiteException) { } } }