Esempio n. 1
0
        public async Task <bool> SyncCipherAsync(string id)
        {
            if (!_authService.IsAuthenticated)
            {
                return(false);
            }

            SyncStarted();

            var cipher = await _cipherApiRepository.GetByIdAsync(id).ConfigureAwait(false);

            if (!CheckSuccess(cipher))
            {
                return(false);
            }

            try
            {
                var existingCipher = await _cipherService.GetByIdAsync(cipher.Result.Id);

                var cipherData = new CipherData(cipher.Result, _authService.UserId);
                await _cipherService.UpsertDataAsync(cipherData, true, existingCipher == null).ConfigureAwait(false);

                var localAttachments = (await _attachmentRepository.GetAllByCipherIdAsync(cipherData.Id)
                                        .ConfigureAwait(false));

                if (cipher.Result.Attachments != null)
                {
                    var attachmentData = cipher.Result.Attachments.Select(a => new AttachmentData(a, cipherData.Id));
                    await _cipherService.UpsertAttachmentDataAsync(attachmentData).ConfigureAwait(false);
                }

                if (localAttachments != null)
                {
                    foreach (var attachment in localAttachments
                             .Where(a => !cipher.Result.Attachments.Any(sa => sa.Id == a.Id)))
                    {
                        try
                        {
                            await _cipherService.DeleteAttachmentDataAsync(attachment.Id).ConfigureAwait(false);
                        }
                        catch (SQLite.SQLiteException) { }
                    }
                }
            }
            catch (SQLite.SQLiteException)
            {
                SyncCompleted(false);
                return(false);
            }

            SyncCompleted(true);
            return(true);
        }