Exemple #1
0
        public async Task <bool> SyncDeleteCipherAsync(SyncCipherNotification notification)
        {
            SyncStarted();
            if (await _stateService.IsAuthenticatedAsync())
            {
                await _cipherService.DeleteAsync(notification.Id);

                _messagingService.Send("syncedDeletedCipher", new Dictionary <string, string>
                {
                    ["cipherId"] = notification.Id
                });
                return(SyncCompleted(true));
            }
            return(SyncCompleted(false));
        }
Exemple #2
0
        public async Task <bool> SyncUpsertCipherAsync(SyncCipherNotification notification, bool isEdit)
        {
            SyncStarted();
            if (await _stateService.IsAuthenticatedAsync())
            {
                try
                {
                    var shouldUpdate = true;
                    var localCipher  = await _cipherService.GetAsync(notification.Id);

                    if (localCipher != null && localCipher.RevisionDate >= notification.RevisionDate)
                    {
                        shouldUpdate = false;
                    }

                    var checkCollections = false;
                    if (shouldUpdate)
                    {
                        if (isEdit)
                        {
                            shouldUpdate     = localCipher != null;
                            checkCollections = true;
                        }
                        else
                        {
                            if (notification.CollectionIds == null || notification.OrganizationId == null)
                            {
                                shouldUpdate = localCipher == null;
                            }
                            else
                            {
                                shouldUpdate     = false;
                                checkCollections = true;
                            }
                        }
                    }

                    if (!shouldUpdate && checkCollections && notification.OrganizationId != null &&
                        notification.CollectionIds != null && notification.CollectionIds.Any())
                    {
                        var collections = await _collectionService.GetAllAsync();

                        if (collections != null)
                        {
                            foreach (var c in collections)
                            {
                                if (notification.CollectionIds.Contains(c.Id))
                                {
                                    shouldUpdate = true;
                                    break;
                                }
                            }
                        }
                    }

                    if (shouldUpdate)
                    {
                        var remoteCipher = await _apiService.GetCipherAsync(notification.Id);

                        if (remoteCipher != null)
                        {
                            var userId = await _stateService.GetActiveUserIdAsync();

                            await _cipherService.UpsertAsync(new CipherData(remoteCipher, userId));

                            _messagingService.Send("syncedUpsertedCipher", new Dictionary <string, string>
                            {
                                ["cipherId"] = notification.Id
                            });
                            return(SyncCompleted(true));
                        }
                    }
                }
                catch (ApiException e)
                {
                    if (e.Error != null && e.Error.StatusCode == System.Net.HttpStatusCode.NotFound && isEdit)
                    {
                        await _cipherService.DeleteAsync(notification.Id);

                        _messagingService.Send("syncedDeletedCipher", new Dictionary <string, string>
                        {
                            ["cipherId"] = notification.Id
                        });
                        return(SyncCompleted(true));
                    }
                }
            }
            return(SyncCompleted(false));
        }