From 5e6354854d716b30d106aa09d39736cef8d5e165 Mon Sep 17 00:00:00 2001 From: Luke Pulverenti Date: Tue, 30 Dec 2014 14:16:01 -0500 Subject: [PATCH] implement sync item removals --- MediaBrowser.Model/Sync/SyncJobItemQuery.cs | 10 +++- .../Localization/Server/server.json | 1 + .../Sync/SyncJobProcessor.cs | 10 ++-- .../Sync/SyncManager.cs | 54 +++++++++++++++++-- .../Sync/SyncRepository.cs | 7 +-- .../MediaBrowser.WebDashboard.csproj | 6 --- 6 files changed, 66 insertions(+), 22 deletions(-) diff --git a/MediaBrowser.Model/Sync/SyncJobItemQuery.cs b/MediaBrowser.Model/Sync/SyncJobItemQuery.cs index d8fa348514..f2cf781a3f 100644 --- a/MediaBrowser.Model/Sync/SyncJobItemQuery.cs +++ b/MediaBrowser.Model/Sync/SyncJobItemQuery.cs @@ -1,4 +1,5 @@ - +using System.Collections.Generic; + namespace MediaBrowser.Model.Sync { public class SyncJobItemQuery @@ -27,6 +28,11 @@ namespace MediaBrowser.Model.Sync /// Gets or sets the status. /// /// The status. - public SyncJobItemStatus? Status { get; set; } + public List Statuses { get; set; } + + public SyncJobItemQuery() + { + Statuses = new List(); + } } } diff --git a/MediaBrowser.Server.Implementations/Localization/Server/server.json b/MediaBrowser.Server.Implementations/Localization/Server/server.json index 839c22ecf2..64b183b7e6 100644 --- a/MediaBrowser.Server.Implementations/Localization/Server/server.json +++ b/MediaBrowser.Server.Implementations/Localization/Server/server.json @@ -19,6 +19,7 @@ "TitleMediaBrowser": "Media Browser", "ThisWizardWillGuideYou": "This wizard will help guide you through the setup process. To begin, please select your preferred language.", "TellUsAboutYourself": "Tell us about yourself", + "ButtonQuickStartGuide": "Quick start guide", "LabelYourFirstName": "Your first name:", "MoreUsersCanBeAddedLater": "More users can be added later within the Dashboard.", "UserProfilesIntro": "Media Browser includes built-in support for user profiles, enabling each user to have their own display settings, playstate and parental controls.", diff --git a/MediaBrowser.Server.Implementations/Sync/SyncJobProcessor.cs b/MediaBrowser.Server.Implementations/Sync/SyncJobProcessor.cs index 6f3e926c0b..44e95f07d6 100644 --- a/MediaBrowser.Server.Implementations/Sync/SyncJobProcessor.cs +++ b/MediaBrowser.Server.Implementations/Sync/SyncJobProcessor.cs @@ -62,7 +62,7 @@ namespace MediaBrowser.Server.Implementations.Sync // Respect ItemLimit, if set if (job.ItemLimit.HasValue) { - if (jobItems.Count >= job.ItemLimit.Value) + if (jobItems.Count(j => j.Status != SyncJobItemStatus.RemovedFromDevice && j.Status != SyncJobItemStatus.Failed) >= job.ItemLimit.Value) { break; } @@ -310,9 +310,10 @@ namespace MediaBrowser.Server.Implementations.Sync { await EnsureSyncJobs(cancellationToken).ConfigureAwait(false); + // If it already has a converting status then is must have been aborted during conversion var result = _syncRepo.GetJobItems(new SyncJobItemQuery { - Status = SyncJobItemStatus.Queued + Statuses = new List { SyncJobItemStatus.Queued, SyncJobItemStatus.Converting } }); var jobItems = result.Items; @@ -327,10 +328,7 @@ namespace MediaBrowser.Server.Implementations.Sync cancellationToken.ThrowIfCancellationRequested(); - if (item.Status == SyncJobItemStatus.Queued) - { - await ProcessJobItem(item, cancellationToken).ConfigureAwait(false); - } + await ProcessJobItem(item, cancellationToken).ConfigureAwait(false); var job = _syncRepo.GetJob(item.JobId); await UpdateJobStatus(job).ConfigureAwait(false); diff --git a/MediaBrowser.Server.Implementations/Sync/SyncManager.cs b/MediaBrowser.Server.Implementations/Sync/SyncManager.cs index 9cb697c812..199c6a9751 100644 --- a/MediaBrowser.Server.Implementations/Sync/SyncManager.cs +++ b/MediaBrowser.Server.Implementations/Sync/SyncManager.cs @@ -392,7 +392,7 @@ namespace MediaBrowser.Server.Implementations.Sync var jobItemResult = GetJobItems(new SyncJobItemQuery { TargetId = targetId, - Status = SyncJobItemStatus.Transferring + Statuses = new List { SyncJobItemStatus.Transferring } }); return jobItemResult.Items.Select(GetJobItemInfo) @@ -404,21 +404,65 @@ namespace MediaBrowser.Server.Implementations.Sync var jobItemResult = GetJobItems(new SyncJobItemQuery { TargetId = request.TargetId, - Status = SyncJobItemStatus.Synced + Statuses = new List { SyncJobItemStatus.Synced } }); + var response = new SyncDataResponse(); + foreach (var jobItem in jobItemResult.Items) { - if (!request.LocalItemIds.Contains(jobItem.ItemId, StringComparer.OrdinalIgnoreCase)) + if (request.LocalItemIds.Contains(jobItem.ItemId, StringComparer.OrdinalIgnoreCase)) + { + var job = _repo.GetJob(jobItem.JobId); + var user = _userManager.GetUserById(job.UserId); + + 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 _repo.Update(jobItem).ConfigureAwait(false); } } - - var response = new SyncDataResponse(); + response.ItemIdsToRemove = response.ItemIdsToRemove.Distinct(StringComparer.OrdinalIgnoreCase).ToList(); + return response; } + + private bool IsLibraryItemAvailable(BaseItem item) + { + if (item == null) + { + return false; + } + + // TODO: Make sure it hasn't been deleted + + return true; + } } } diff --git a/MediaBrowser.Server.Implementations/Sync/SyncRepository.cs b/MediaBrowser.Server.Implementations/Sync/SyncRepository.cs index a10e9a7e68..323e3f964f 100644 --- a/MediaBrowser.Server.Implementations/Sync/SyncRepository.cs +++ b/MediaBrowser.Server.Implementations/Sync/SyncRepository.cs @@ -467,10 +467,11 @@ namespace MediaBrowser.Server.Implementations.Sync cmd.Parameters.Add(cmd, "@TargetId", DbType.String).Value = query.TargetId; } - if (query.Status.HasValue) + if (query.Statuses.Count > 0) { - whereClauses.Add("Status=@Status"); - cmd.Parameters.Add(cmd, "@Status", DbType.String).Value = query.Status.Value.ToString(); + var statuses = string.Join(",", query.Statuses.Select(i => "'" + i.ToString() + "'").ToArray()); + + whereClauses.Add(string.Format("Status in ({0})", statuses)); } var whereTextWithoutPaging = whereClauses.Count == 0 ? diff --git a/MediaBrowser.WebDashboard/MediaBrowser.WebDashboard.csproj b/MediaBrowser.WebDashboard/MediaBrowser.WebDashboard.csproj index dc552c5185..166d3ce67d 100644 --- a/MediaBrowser.WebDashboard/MediaBrowser.WebDashboard.csproj +++ b/MediaBrowser.WebDashboard/MediaBrowser.WebDashboard.csproj @@ -216,15 +216,9 @@ PreserveNewest - - PreserveNewest - PreserveNewest - - PreserveNewest - PreserveNewest