Exemplo n.º 1
0
        private void SetUserAccess(SyncDataRequest request, SyncDataResponse response, List <string> itemIds)
        {
            var users = request.OfflineUserIds
                        .Select(_userManager.GetUserById)
                        .Where(i => i != null)
                        .ToList();

            foreach (var itemId in itemIds)
            {
                var item = _libraryManager.GetItemById(itemId);

                if (item != null)
                {
                    response.ItemUserAccess[itemId] = users
                                                      .Where(i => IsUserVisible(item, i))
                                                      .Select(i => i.Id.ToString("N"))
                                                      .OrderBy(i => i)
                                                      .ToList();
                }
            }
        }
Exemplo n.º 2
0
        private async Task <SyncDataResponse> SyncDataUsingSyncJobItemIds(SyncDataRequest request)
        {
            var jobItemResult = GetJobItems(new SyncJobItemQuery
            {
                TargetId = request.TargetId,
                Statuses = new[] { SyncJobItemStatus.Synced }
            });

            var response = new SyncDataResponse();

            foreach (var jobItem in jobItemResult.Items)
            {
                var requiresSaving   = false;
                var removeFromDevice = false;

                if (request.SyncJobItemIds.Contains(jobItem.Id, StringComparer.OrdinalIgnoreCase))
                {
                    var libraryItem = _libraryManager.GetItemById(jobItem.ItemId);

                    var job  = _repo.GetJob(jobItem.JobId);
                    var user = _userManager.GetUserById(job.UserId);

                    if (jobItem.IsMarkedForRemoval)
                    {
                        // Tell the device to remove it since it has been marked for removal
                        _logger.Info("Adding ItemIdsToRemove {0} because IsMarkedForRemoval is set.", jobItem.Id);
                        removeFromDevice = true;
                    }
                    else if (user == null)
                    {
                        // Tell the device to remove it since the user is gone now
                        _logger.Info("Adding ItemIdsToRemove {0} because the user is no longer valid.", jobItem.Id);
                        removeFromDevice = true;
                    }
                    else if (!IsLibraryItemAvailable(libraryItem))
                    {
                        // Tell the device to remove it since it's no longer available
                        _logger.Info("Adding ItemIdsToRemove {0} because it is no longer available.", jobItem.Id);
                        removeFromDevice = true;
                    }
                    else if (job.UnwatchedOnly)
                    {
                        if (libraryItem is Video && libraryItem.IsPlayed(user))
                        {
                            // Tell the device to remove it since it has been played
                            _logger.Info("Adding ItemIdsToRemove {0} because it has been marked played.", jobItem.Id);
                            removeFromDevice = true;
                        }
                    }
                    else if (libraryItem != null && libraryItem.DateModified.Ticks != jobItem.ItemDateModifiedTicks && jobItem.ItemDateModifiedTicks > 0)
                    {
                        _logger.Info("Setting status to Queued for {0} because the media has been modified since the original sync.", jobItem.ItemId);
                        jobItem.Status   = SyncJobItemStatus.Queued;
                        jobItem.Progress = 0;
                        requiresSaving   = true;
                    }
                }
                else
                {
                    // Content is no longer on the device
                    if (jobItem.IsMarkedForRemoval)
                    {
                        jobItem.Status = SyncJobItemStatus.RemovedFromDevice;
                    }
                    else
                    {
                        _logger.Info("Setting status to Queued for {0} because it is no longer on the device.", jobItem.Id);
                        jobItem.Status   = SyncJobItemStatus.Queued;
                        jobItem.Progress = 0;
                    }
                    requiresSaving = true;
                }

                if (removeFromDevice)
                {
                    response.ItemIdsToRemove.Add(jobItem.Id);
                    jobItem.IsMarkedForRemoval = true;
                    requiresSaving             = true;
                }

                if (requiresSaving)
                {
                    await UpdateSyncJobItemInternal(jobItem).ConfigureAwait(false);
                }
            }

            // Now check each item that's on the device
            foreach (var syncJobItemId in request.SyncJobItemIds)
            {
                // See if it's already marked for removal
                if (response.ItemIdsToRemove.Contains(syncJobItemId, StringComparer.OrdinalIgnoreCase))
                {
                    continue;
                }

                // If there isn't a sync job for this item, mark it for removal
                if (!jobItemResult.Items.Any(i => string.Equals(syncJobItemId, i.Id, StringComparison.OrdinalIgnoreCase)))
                {
                    response.ItemIdsToRemove.Add(syncJobItemId);
                }
            }

            response.ItemIdsToRemove = response.ItemIdsToRemove.Distinct(StringComparer.OrdinalIgnoreCase).ToList();

            return(response);
        }
Exemplo n.º 3
0
        public async Task <SyncDataResponse> SyncData(SyncDataRequest request)
        {
            var jobItemResult = GetJobItems(new SyncJobItemQuery
            {
                TargetId = request.TargetId,
                Statuses = new List <SyncJobItemStatus> {
                    SyncJobItemStatus.Synced
                }
            });

            var response = new SyncDataResponse();

            foreach (var jobItem in jobItemResult.Items)
            {
                if (request.LocalItemIds.Contains(jobItem.ItemId, StringComparer.OrdinalIgnoreCase))
                {
                    var job  = _repo.GetJob(jobItem.JobId);
                    var user = _userManager.GetUserById(job.UserId);

                    if (jobItem.IsMarkedForRemoval)
                    {
                        // Tell the device to remove it since it has been marked for removal
                        response.ItemIdsToRemove.Add(jobItem.ItemId);
                    }
                    else if (user == null)
                    {
                        // Tell the device to remove it since the user is gone now
                        response.ItemIdsToRemove.Add(jobItem.ItemId);
                    }
                    else if (job.UnwatchedOnly)
                    {
                        var libraryItem = _libraryManager.GetItemById(jobItem.ItemId);

                        if (IsLibraryItemAvailable(libraryItem))
                        {
                            if (libraryItem.IsPlayed(user) && libraryItem is Video)
                            {
                                // Tell the device to remove it since it has been played
                                response.ItemIdsToRemove.Add(jobItem.ItemId);
                            }
                        }
                        else
                        {
                            // Tell the device to remove it since it's no longer available
                            response.ItemIdsToRemove.Add(jobItem.ItemId);
                        }
                    }
                }
                else
                {
                    // Content is no longer on the device
                    jobItem.Status = SyncJobItemStatus.RemovedFromDevice;
                    await UpdateSyncJobItemInternal(jobItem).ConfigureAwait(false);
                }
            }

            // Now check each item that's on the device
            foreach (var itemId in request.LocalItemIds)
            {
                // See if it's already marked for removal
                if (response.ItemIdsToRemove.Contains(itemId, StringComparer.OrdinalIgnoreCase))
                {
                    continue;
                }

                // If there isn't a sync job for this item, mark it for removal
                if (!jobItemResult.Items.Any(i => string.Equals(itemId, i.ItemId, StringComparison.OrdinalIgnoreCase)))
                {
                    response.ItemIdsToRemove.Add(itemId);
                }
            }

            response.ItemIdsToRemove = response.ItemIdsToRemove.Distinct(StringComparer.OrdinalIgnoreCase).ToList();

            var itemsOnDevice = request.LocalItemIds
                                .Except(response.ItemIdsToRemove)
                                .ToList();

            SetUserAccess(request, response, itemsOnDevice);

            return(response);
        }