예제 #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();
                }
            }
        }
예제 #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;

                var libraryItem = _libraryManager.GetItemById(jobItem.ItemId);

                if (request.SyncJobItemIds.Contains(jobItem.Id, 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
                        _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
                {
                    // 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;
        }