From 4986722c7305ebce02c37c80363a909d372ff7a3 Mon Sep 17 00:00:00 2001 From: Luke Pulverenti Date: Fri, 5 Apr 2013 15:02:48 -0400 Subject: [PATCH] made display preferences uniquely identifiable --- MediaBrowser.Api/DisplayPreferencesService.cs | 20 ++++------ .../Entities/CollectionFolder.cs | 3 +- MediaBrowser.Controller/Entities/Folder.cs | 12 +++++- MediaBrowser.Controller/Library/DtoBuilder.cs | 2 +- .../Library/IUserManager.cs | 9 ++--- .../IDisplayPreferencesRepository.cs | 7 +--- .../Entities/DisplayPreferences.cs | 2 +- .../Library/UserManager.cs | 38 +++++++++--------- .../SQLiteDisplayPreferencesRepository.cs | 39 ++++++------------- .../LibraryExplorer.xaml.cs | 12 +++--- Nuget/MediaBrowser.Common.Internal.nuspec | 4 +- Nuget/MediaBrowser.Common.nuspec | 2 +- Nuget/MediaBrowser.Server.Core.nuspec | 4 +- 13 files changed, 68 insertions(+), 86 deletions(-) diff --git a/MediaBrowser.Api/DisplayPreferencesService.cs b/MediaBrowser.Api/DisplayPreferencesService.cs index 1ea71c545e..99b37444cf 100644 --- a/MediaBrowser.Api/DisplayPreferencesService.cs +++ b/MediaBrowser.Api/DisplayPreferencesService.cs @@ -12,7 +12,7 @@ namespace MediaBrowser.Api /// /// Class UpdateDisplayPreferences /// - [Route("/Users/{UserId}/DisplayPreferences/{Id}", "POST")] + [Route("/DisplayPreferences/{DisplayPreferencesId}", "POST")] [Api(("Updates a user's display preferences for an item"))] public class UpdateDisplayPreferences : DisplayPreferences, IReturnVoid { @@ -20,17 +20,14 @@ namespace MediaBrowser.Api /// Gets or sets the id. /// /// The id. - [ApiMember(Name = "Id", Description = "Item Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "POST")] - public Guid Id { get; set; } + [ApiMember(Name = "DisplayPreferencesId", Description = "DisplayPreferences Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "POST")] + public Guid DisplayPreferencesId { get; set; } } - [Route("/Users/{UserId}/DisplayPreferences/{Id}", "GET")] + [Route("/DisplayPreferences/{Id}", "GET")] [Api(("Gets a user's display preferences for an item"))] public class GetDisplayPreferences : IReturn { - [ApiMember(Name = "UserId", Description = "User Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "POST")] - public Guid UserId { get; set; } - /// /// Gets or sets the id. /// @@ -70,7 +67,7 @@ namespace MediaBrowser.Api /// The request. public object Get(GetDisplayPreferences request) { - var task = _userManager.GetDisplayPreferences(request.UserId, request.Id); + var task = _userManager.GetDisplayPreferences(request.Id); return ToOptimizedResult(task.Result); } @@ -84,15 +81,12 @@ namespace MediaBrowser.Api // We need to parse this manually because we told service stack not to with IRequiresRequestStream // https://code.google.com/p/servicestack/source/browse/trunk/Common/ServiceStack.Text/ServiceStack.Text/Controller/PathInfo.cs var pathInfo = PathInfo.Parse(RequestContext.PathInfo); - var userId = new Guid(pathInfo.GetArgumentValue(1)); - var displayPreferencesId = new Guid(pathInfo.GetArgumentValue(3)); - - var user = _userManager.GetUserById(userId); + var displayPreferencesId = new Guid(pathInfo.GetArgumentValue(1)); // Serialize to json and then back so that the core doesn't see the request dto type var displayPreferences = _jsonSerializer.DeserializeFromString(_jsonSerializer.SerializeToString(request)); - var task = _userManager.SaveDisplayPreferences(user.Id, displayPreferencesId, displayPreferences, CancellationToken.None); + var task = _userManager.SaveDisplayPreferences(displayPreferences, CancellationToken.None); Task.WaitAll(task); } diff --git a/MediaBrowser.Controller/Entities/CollectionFolder.cs b/MediaBrowser.Controller/Entities/CollectionFolder.cs index c64f001c11..42c112752e 100644 --- a/MediaBrowser.Controller/Entities/CollectionFolder.cs +++ b/MediaBrowser.Controller/Entities/CollectionFolder.cs @@ -1,6 +1,5 @@ using MediaBrowser.Common.Extensions; using MediaBrowser.Model.Entities; -using MediaBrowser.Model.Tasks; using System; using System.Collections.Concurrent; using System.Collections.Generic; @@ -35,7 +34,7 @@ namespace MediaBrowser.Controller.Entities /// Allow different display preferences for each collection folder /// /// The display prefs id. - public override Guid DisplayPreferencesId + protected override Guid DisplayPreferencesId { get { diff --git a/MediaBrowser.Controller/Entities/Folder.cs b/MediaBrowser.Controller/Entities/Folder.cs index 05ee87defa..ce8bf679ea 100644 --- a/MediaBrowser.Controller/Entities/Folder.cs +++ b/MediaBrowser.Controller/Entities/Folder.cs @@ -65,7 +65,7 @@ namespace MediaBrowser.Controller.Entities /// /// The display prefs id. [IgnoreDataMember] - public virtual Guid DisplayPreferencesId + protected virtual Guid DisplayPreferencesId { get { @@ -74,6 +74,16 @@ namespace MediaBrowser.Controller.Entities } } + /// + /// Gets the display preferences id. + /// + /// The user id. + /// Guid. + public Guid GetDisplayPreferencesId(Guid userId) + { + return (userId + DisplayPreferencesId.ToString()).GetMD5(); + } + #region Indexing /// diff --git a/MediaBrowser.Controller/Library/DtoBuilder.cs b/MediaBrowser.Controller/Library/DtoBuilder.cs index fbaa59f34f..d54563e41b 100644 --- a/MediaBrowser.Controller/Library/DtoBuilder.cs +++ b/MediaBrowser.Controller/Library/DtoBuilder.cs @@ -174,7 +174,7 @@ namespace MediaBrowser.Controller.Library if (item.IsFolder && fields.Contains(ItemFields.DisplayPreferencesId)) { - dto.DisplayPreferencesId = ((Folder)item).DisplayPreferencesId.ToString(); + dto.DisplayPreferencesId = ((Folder) item).GetDisplayPreferencesId(user.Id).ToString(); } if (item.IsFolder) diff --git a/MediaBrowser.Controller/Library/IUserManager.cs b/MediaBrowser.Controller/Library/IUserManager.cs index 8eee8e447c..1c276ffebd 100644 --- a/MediaBrowser.Controller/Library/IUserManager.cs +++ b/MediaBrowser.Controller/Library/IUserManager.cs @@ -190,23 +190,20 @@ namespace MediaBrowser.Controller.Library /// The user data id. /// Task{DisplayPreferences}. Task GetUserData(Guid userId, Guid userDataId); - + /// /// Gets the display preferences. /// - /// The user id. /// The display preferences id. /// DisplayPreferences. - Task GetDisplayPreferences(Guid userId, Guid displayPreferencesId); + Task GetDisplayPreferences(Guid displayPreferencesId); /// /// Saves display preferences for an item /// - /// The user id. - /// The display preferences id. /// The display preferences. /// The cancellation token. /// Task. - Task SaveDisplayPreferences(Guid userId, Guid displayPreferencesId, DisplayPreferences displayPreferences, CancellationToken cancellationToken); + Task SaveDisplayPreferences(DisplayPreferences displayPreferences, CancellationToken cancellationToken); } } diff --git a/MediaBrowser.Controller/Persistence/IDisplayPreferencesRepository.cs b/MediaBrowser.Controller/Persistence/IDisplayPreferencesRepository.cs index eb43c8ca5b..9774bb68e6 100644 --- a/MediaBrowser.Controller/Persistence/IDisplayPreferencesRepository.cs +++ b/MediaBrowser.Controller/Persistence/IDisplayPreferencesRepository.cs @@ -13,20 +13,17 @@ namespace MediaBrowser.Controller.Persistence /// /// Saves display preferences for an item /// - /// The user id. - /// The display preferences id. /// The display preferences. /// The cancellation token. /// Task. - Task SaveDisplayPreferences(Guid userId, Guid displayPreferencesId, DisplayPreferences displayPreferences, + Task SaveDisplayPreferences(DisplayPreferences displayPreferences, CancellationToken cancellationToken); /// /// Gets the display preferences. /// - /// The user id. /// The display preferences id. /// Task{DisplayPreferences}. - Task GetDisplayPreferences(Guid userId, Guid displayPreferencesId); + Task GetDisplayPreferences(Guid displayPreferencesId); } } diff --git a/MediaBrowser.Model/Entities/DisplayPreferences.cs b/MediaBrowser.Model/Entities/DisplayPreferences.cs index 7331da86d7..31edab621b 100644 --- a/MediaBrowser.Model/Entities/DisplayPreferences.cs +++ b/MediaBrowser.Model/Entities/DisplayPreferences.cs @@ -32,7 +32,7 @@ namespace MediaBrowser.Model.Entities /// /// The user id. [ProtoMember(1)] - public Guid UserId { get; set; } + public Guid Id { get; set; } /// /// Gets or sets the type of the view. /// diff --git a/MediaBrowser.Server.Implementations/Library/UserManager.cs b/MediaBrowser.Server.Implementations/Library/UserManager.cs index c5e4de2bc1..a122e2c6d6 100644 --- a/MediaBrowser.Server.Implementations/Library/UserManager.cs +++ b/MediaBrowser.Server.Implementations/Library/UserManager.cs @@ -102,7 +102,7 @@ namespace MediaBrowser.Server.Implementations.Library /// /// The _user data /// - private readonly ConcurrentDictionary> _displayPreferences = new ConcurrentDictionary>(); + private readonly ConcurrentDictionary> _displayPreferences = new ConcurrentDictionary>(); private readonly ConcurrentDictionary> _userData = new ConcurrentDictionary>(); @@ -168,51 +168,51 @@ namespace MediaBrowser.Server.Implementations.Library /// /// Gets the display preferences. /// - /// The user id. /// The display preferences id. /// DisplayPreferences. - public Task GetDisplayPreferences(Guid userId, Guid displayPreferencesId) + public Task GetDisplayPreferences(Guid displayPreferencesId) { - var key = userId + displayPreferencesId.ToString(); - - return _displayPreferences.GetOrAdd(key, keyName => RetrieveDisplayPreferences(userId, displayPreferencesId)); + return _displayPreferences.GetOrAdd(displayPreferencesId, keyName => RetrieveDisplayPreferences(displayPreferencesId)); } /// /// Retrieves the display preferences. /// - /// The user id. /// The display preferences id. /// DisplayPreferences. - private async Task RetrieveDisplayPreferences(Guid userId, Guid displayPreferencesId) + private async Task RetrieveDisplayPreferences(Guid displayPreferencesId) { - var displayPreferences = await Kernel.Instance.DisplayPreferencesRepository.GetDisplayPreferences(userId, displayPreferencesId).ConfigureAwait(false); + var displayPreferences = await Kernel.Instance.DisplayPreferencesRepository.GetDisplayPreferences(displayPreferencesId).ConfigureAwait(false); - return displayPreferences ?? new DisplayPreferences(); + return displayPreferences ?? new DisplayPreferences { Id = displayPreferencesId }; } /// /// Saves display preferences for an item /// - /// The user id. - /// The display preferences id. /// The display preferences. /// The cancellation token. /// Task. - public async Task SaveDisplayPreferences(Guid userId, Guid displayPreferencesId, DisplayPreferences displayPreferences, CancellationToken cancellationToken) + public async Task SaveDisplayPreferences(DisplayPreferences displayPreferences, CancellationToken cancellationToken) { - var key = userId + displayPreferencesId.ToString(); - + if (displayPreferences == null) + { + throw new ArgumentNullException("displayPreferences"); + } + if (displayPreferences.Id == Guid.Empty) + { + throw new ArgumentNullException("displayPreferences.Id"); + } + try { - await Kernel.Instance.DisplayPreferencesRepository.SaveDisplayPreferences(userId, displayPreferencesId, - displayPreferences, + await Kernel.Instance.DisplayPreferencesRepository.SaveDisplayPreferences(displayPreferences, cancellationToken).ConfigureAwait(false); var newValue = Task.FromResult(displayPreferences); // Once it succeeds, put it into the dictionary to make it available to everyone else - _displayPreferences.AddOrUpdate(key, newValue, delegate { return newValue; }); + _displayPreferences.AddOrUpdate(displayPreferences.Id, newValue, delegate { return newValue; }); } catch (Exception ex) { @@ -232,7 +232,7 @@ namespace MediaBrowser.Server.Implementations.Library { if (id == Guid.Empty) { - throw new ArgumentNullException(); + throw new ArgumentNullException("id"); } return Users.FirstOrDefault(u => u.Id == id); diff --git a/MediaBrowser.Server.Implementations/Sqlite/SQLiteDisplayPreferencesRepository.cs b/MediaBrowser.Server.Implementations/Sqlite/SQLiteDisplayPreferencesRepository.cs index 8a15d4028e..5f6ac39ee5 100644 --- a/MediaBrowser.Server.Implementations/Sqlite/SQLiteDisplayPreferencesRepository.cs +++ b/MediaBrowser.Server.Implementations/Sqlite/SQLiteDisplayPreferencesRepository.cs @@ -78,8 +78,8 @@ namespace MediaBrowser.Server.Implementations.Sqlite string[] queries = { - "create table if not exists displaypreferences (id GUID, userId GUID, data BLOB)", - "create unique index if not exists displaypreferencesindex on displaypreferences (id, userId)", + "create table if not exists displaypreferences (id GUID, data BLOB)", + "create unique index if not exists displaypreferencesindex on displaypreferences (id)", "create table if not exists schema_version (table_name primary key, version)", //pragmas "pragma temp_store = memory" @@ -91,29 +91,23 @@ namespace MediaBrowser.Server.Implementations.Sqlite /// /// Save the display preferences associated with an item in the repo /// - /// The user id. - /// The display preferences id. /// The display preferences. /// The cancellation token. /// Task. /// item - public Task SaveDisplayPreferences(Guid userId, Guid displayPreferencesId, DisplayPreferences displayPreferences, CancellationToken cancellationToken) + public Task SaveDisplayPreferences(DisplayPreferences displayPreferences, CancellationToken cancellationToken) { if (displayPreferences == null) { throw new ArgumentNullException("displayPreferences"); } - if (cancellationToken == null) - { - throw new ArgumentNullException("cancellationToken"); - } - if (userId == Guid.Empty) + if (displayPreferences.Id == Guid.Empty) { - throw new ArgumentNullException("userId"); + throw new ArgumentNullException("displayPreferences.Id"); } - if (displayPreferencesId == Guid.Empty) + if (cancellationToken == null) { - throw new ArgumentNullException("displayPreferencesId"); + throw new ArgumentNullException("cancellationToken"); } cancellationToken.ThrowIfCancellationRequested(); @@ -125,10 +119,9 @@ namespace MediaBrowser.Server.Implementations.Sqlite cancellationToken.ThrowIfCancellationRequested(); var cmd = connection.CreateCommand(); - cmd.CommandText = "replace into displaypreferences (id, userId, data) values (@1, @2, @3)"; - cmd.AddParam("@1", displayPreferencesId); - cmd.AddParam("@2", userId); - cmd.AddParam("@3", serialized); + cmd.CommandText = "replace into displaypreferences (id, data) values (@1, @3)"; + cmd.AddParam("@1", displayPreferences.Id); + cmd.AddParam("@2", serialized); QueueCommand(cmd); }); } @@ -136,30 +129,22 @@ namespace MediaBrowser.Server.Implementations.Sqlite /// /// Gets the display preferences. /// - /// The user id. /// The display preferences id. /// Task{DisplayPreferences}. /// item - public async Task GetDisplayPreferences(Guid userId, Guid displayPreferencesId) + public async Task GetDisplayPreferences(Guid displayPreferencesId) { - if (userId == Guid.Empty) - { - throw new ArgumentNullException("userId"); - } if (displayPreferencesId == Guid.Empty) { throw new ArgumentNullException("displayPreferencesId"); } var cmd = connection.CreateCommand(); - cmd.CommandText = "select data from displaypreferences where id = @id and userId=@userId"; + cmd.CommandText = "select data from displaypreferences where id = @id"; var idParam = cmd.Parameters.Add("@id", DbType.Guid); idParam.Value = displayPreferencesId; - var userIdParam = cmd.Parameters.Add("@userId", DbType.Guid); - userIdParam.Value = userId; - using (var reader = await cmd.ExecuteReaderAsync(CommandBehavior.SequentialAccess | CommandBehavior.SingleResult | CommandBehavior.SingleRow).ConfigureAwait(false)) { if (reader.Read()) diff --git a/MediaBrowser.ServerApplication/LibraryExplorer.xaml.cs b/MediaBrowser.ServerApplication/LibraryExplorer.xaml.cs index 8811de9db6..c72cf03407 100644 --- a/MediaBrowser.ServerApplication/LibraryExplorer.xaml.cs +++ b/MediaBrowser.ServerApplication/LibraryExplorer.xaml.cs @@ -91,7 +91,7 @@ namespace MediaBrowser.ServerApplication var currentFolder = folder; Task.Factory.StartNew(() => { - var prefs = ddlProfile.SelectedItem != null ? _userManager.GetDisplayPreferences((ddlProfile.SelectedItem as User).Id, currentFolder.DisplayPreferencesId).Result ?? new DisplayPreferences { SortBy = ItemSortBy.SortName } : new DisplayPreferences { SortBy = ItemSortBy.SortName }; + var prefs = ddlProfile.SelectedItem != null ? _userManager.GetDisplayPreferences(currentFolder.GetDisplayPreferencesId((ddlProfile.SelectedItem as User).Id)).Result ?? new DisplayPreferences { SortBy = ItemSortBy.SortName } : new DisplayPreferences { SortBy = ItemSortBy.SortName }; var node = new TreeViewItem { Tag = currentFolder }; var subChildren = currentFolder.GetChildren(CurrentUser, prefs.IndexBy); @@ -144,7 +144,7 @@ namespace MediaBrowser.ServerApplication var subFolder = item as Folder; if (subFolder != null) { - var prefs = _userManager.GetDisplayPreferences(user.Id, subFolder.DisplayPreferencesId).Result; + var prefs = _userManager.GetDisplayPreferences(subFolder.GetDisplayPreferencesId(user.Id)).Result; AddChildren(node, OrderBy(subFolder.GetChildren(user), user, prefs.SortBy), user); node.Header = item.Name + " (" + node.Items.Count + ")"; @@ -201,8 +201,8 @@ namespace MediaBrowser.ServerApplication var prefs = await - _userManager.GetDisplayPreferences((ddlProfile.SelectedItem as User).Id, - folder.DisplayPreferencesId); + _userManager.GetDisplayPreferences(folder.GetDisplayPreferencesId((ddlProfile.SelectedItem as User).Id)); + ddlIndexBy.SelectedItem = prefs != null ? prefs.IndexBy ?? LocalizedStrings.Instance.GetString("NoneDispPref") : LocalizedStrings.Instance.GetString("NoneDispPref"); @@ -360,7 +360,7 @@ namespace MediaBrowser.ServerApplication var folder = treeItem != null ? treeItem.Tag as Folder : null; - var prefs = folder != null ? _userManager.GetDisplayPreferences(CurrentUser.Id, folder.DisplayPreferencesId).Result : new DisplayPreferences {SortBy = ItemSortBy.SortName}; + var prefs = folder != null ? _userManager.GetDisplayPreferences(folder.GetDisplayPreferencesId(CurrentUser.Id)).Result : new DisplayPreferences {SortBy = ItemSortBy.SortName}; if (folder != null && prefs.IndexBy != ddlIndexBy.SelectedItem as string) { //grab UI context so we can update within the below task @@ -401,7 +401,7 @@ namespace MediaBrowser.ServerApplication var folder = treeItem != null ? treeItem.Tag as Folder : null; - var prefs = folder != null ? _userManager.GetDisplayPreferences(CurrentUser.Id, folder.DisplayPreferencesId).Result : new DisplayPreferences(); + var prefs = folder != null ? _userManager.GetDisplayPreferences(folder.GetDisplayPreferencesId(CurrentUser.Id)).Result : new DisplayPreferences(); if (folder != null && prefs.SortBy != ddlSortBy.SelectedItem as string) { //grab UI context so we can update within the below task diff --git a/Nuget/MediaBrowser.Common.Internal.nuspec b/Nuget/MediaBrowser.Common.Internal.nuspec index 892848c163..b89c8f9f2f 100644 --- a/Nuget/MediaBrowser.Common.Internal.nuspec +++ b/Nuget/MediaBrowser.Common.Internal.nuspec @@ -2,7 +2,7 @@ MediaBrowser.Common.Internal - 3.0.66 + 3.0.67 MediaBrowser.Common.Internal Luke ebr,Luke,scottisafool @@ -12,7 +12,7 @@ Contains common components shared by Media Browser Theatre and Media Browser Server. Not intended for plugin developer consumption. Copyright © Media Browser 2013 - + diff --git a/Nuget/MediaBrowser.Common.nuspec b/Nuget/MediaBrowser.Common.nuspec index 7fae829c10..f628fee30d 100644 --- a/Nuget/MediaBrowser.Common.nuspec +++ b/Nuget/MediaBrowser.Common.nuspec @@ -2,7 +2,7 @@ MediaBrowser.Common - 3.0.66 + 3.0.67 MediaBrowser.Common Media Browser Team ebr,Luke,scottisafool diff --git a/Nuget/MediaBrowser.Server.Core.nuspec b/Nuget/MediaBrowser.Server.Core.nuspec index c2c9b33f26..6058d6e04c 100644 --- a/Nuget/MediaBrowser.Server.Core.nuspec +++ b/Nuget/MediaBrowser.Server.Core.nuspec @@ -2,7 +2,7 @@ MediaBrowser.Server.Core - 3.0.66 + 3.0.67 Media Browser.Server.Core Media Browser Team ebr,Luke,scottisafool @@ -12,7 +12,7 @@ Contains core components required to build plugins for Media Browser Server. Copyright © Media Browser 2013 - +