예제 #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);
        }
예제 #2
0
        public async Task <Cipher> GetByIdAsync(string id)
        {
            var data = await _cipherRepository.GetByIdAsync(id);

            if (data == null || data.UserId != _authService.UserId)
            {
                return(null);
            }

            var attachments = await _attachmentRepository.GetAllByCipherIdAsync(id);

            var cipher = new Cipher(data, attachments);

            return(cipher);
        }