moved displaypreferences to usermanager to solve concurrency issues
This commit is contained in:
parent
dcc057f324
commit
b4dd8a2106
|
@ -104,13 +104,16 @@ namespace MediaBrowser.Api.Library
|
|||
/// </summary>
|
||||
private readonly IApplicationHost _appHost;
|
||||
private readonly ILibraryManager _libraryManager;
|
||||
private readonly IUserManager _userManager;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="LibraryService" /> class.
|
||||
/// </summary>
|
||||
/// <param name="appHost">The app host.</param>
|
||||
/// <param name="libraryManager">The library manager.</param>
|
||||
/// <param name="userManager">The user manager.</param>
|
||||
/// <exception cref="System.ArgumentNullException">appHost</exception>
|
||||
public LibraryService(IApplicationHost appHost, ILibraryManager libraryManager)
|
||||
public LibraryService(IApplicationHost appHost, ILibraryManager libraryManager, IUserManager userManager)
|
||||
{
|
||||
if (appHost == null)
|
||||
{
|
||||
|
@ -119,6 +122,7 @@ namespace MediaBrowser.Api.Library
|
|||
|
||||
_appHost = appHost;
|
||||
_libraryManager = libraryManager;
|
||||
_userManager = userManager;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
@ -133,7 +137,7 @@ namespace MediaBrowser.Api.Library
|
|||
// Get everything
|
||||
var fields = Enum.GetNames(typeof(ItemFields)).Select(i => (ItemFields)Enum.Parse(typeof(ItemFields), i, true));
|
||||
|
||||
var result = new DtoBuilder(Logger, _libraryManager).GetBaseItemDto(item, fields.ToList()).Result;
|
||||
var result = new DtoBuilder(Logger, _libraryManager, _userManager).GetBaseItemDto(item, fields.ToList()).Result;
|
||||
|
||||
return ToOptimizedResult(result);
|
||||
}
|
||||
|
@ -150,7 +154,7 @@ namespace MediaBrowser.Api.Library
|
|||
// Get everything
|
||||
var fields = Enum.GetNames(typeof(ItemFields)).Select(i => (ItemFields)Enum.Parse(typeof(ItemFields), i, true));
|
||||
|
||||
var result = new DtoBuilder(Logger, _libraryManager).GetBaseItemDto(item, fields.ToList()).Result;
|
||||
var result = new DtoBuilder(Logger, _libraryManager, _userManager).GetBaseItemDto(item, fields.ToList()).Result;
|
||||
|
||||
return ToOptimizedResult(result);
|
||||
}
|
||||
|
@ -167,7 +171,7 @@ namespace MediaBrowser.Api.Library
|
|||
// Get everything
|
||||
var fields = Enum.GetNames(typeof(ItemFields)).Select(i => (ItemFields)Enum.Parse(typeof(ItemFields), i, true));
|
||||
|
||||
var result = new DtoBuilder(Logger, _libraryManager).GetBaseItemDto(item, fields.ToList()).Result;
|
||||
var result = new DtoBuilder(Logger, _libraryManager, _userManager).GetBaseItemDto(item, fields.ToList()).Result;
|
||||
|
||||
return ToOptimizedResult(result);
|
||||
}
|
||||
|
@ -184,7 +188,7 @@ namespace MediaBrowser.Api.Library
|
|||
// Get everything
|
||||
var fields = Enum.GetNames(typeof(ItemFields)).Select(i => (ItemFields)Enum.Parse(typeof(ItemFields), i, true));
|
||||
|
||||
var result = new DtoBuilder(Logger, _libraryManager).GetBaseItemDto(item, fields.ToList()).Result;
|
||||
var result = new DtoBuilder(Logger, _libraryManager, _userManager).GetBaseItemDto(item, fields.ToList()).Result;
|
||||
|
||||
return ToOptimizedResult(result);
|
||||
}
|
||||
|
|
|
@ -149,7 +149,7 @@ namespace MediaBrowser.Api.UserLibrary
|
|||
return null;
|
||||
}
|
||||
|
||||
var dto = await new DtoBuilder(Logger, LibraryManager).GetBaseItemDto(item, user, fields).ConfigureAwait(false);
|
||||
var dto = await new DtoBuilder(Logger, LibraryManager, UserManager).GetBaseItemDto(item, user, fields).ConfigureAwait(false);
|
||||
|
||||
dto.ChildCount = stub.Item2();
|
||||
|
||||
|
|
|
@ -179,7 +179,7 @@ namespace MediaBrowser.Api.UserLibrary
|
|||
|
||||
var fields = GetItemFields(request).ToList();
|
||||
|
||||
var dtoBuilder = new DtoBuilder(Logger, _libraryManager);
|
||||
var dtoBuilder = new DtoBuilder(Logger, _libraryManager, _userManager);
|
||||
|
||||
var returnItems = await Task.WhenAll(pagedItems.Select(i => dtoBuilder.GetBaseItemDto(i, user, fields))).ConfigureAwait(false);
|
||||
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
using MediaBrowser.Controller.Entities;
|
||||
using System.Threading;
|
||||
using MediaBrowser.Controller.Entities;
|
||||
using MediaBrowser.Controller.Entities.Movies;
|
||||
using MediaBrowser.Controller.Library;
|
||||
using MediaBrowser.Model.Connectivity;
|
||||
|
@ -385,7 +386,7 @@ namespace MediaBrowser.Api.UserLibrary
|
|||
|
||||
var movie = (Movie)item;
|
||||
|
||||
var dtoBuilder = new DtoBuilder(Logger, _libraryManager);
|
||||
var dtoBuilder = new DtoBuilder(Logger, _libraryManager, _userManager);
|
||||
|
||||
var items = movie.SpecialFeatures.Select(i => dtoBuilder.GetBaseItemDto(i, user, fields)).AsParallel().Select(t => t.Result).ToList();
|
||||
|
||||
|
@ -406,7 +407,7 @@ namespace MediaBrowser.Api.UserLibrary
|
|||
// Get everything
|
||||
var fields = Enum.GetNames(typeof(ItemFields)).Select(i => (ItemFields)Enum.Parse(typeof(ItemFields), i, true)).ToList();
|
||||
|
||||
var dtoBuilder = new DtoBuilder(Logger, _libraryManager);
|
||||
var dtoBuilder = new DtoBuilder(Logger, _libraryManager, _userManager);
|
||||
|
||||
var items = item.LocalTrailers.Select(i => dtoBuilder.GetBaseItemDto(i, user, fields)).AsParallel().Select(t => t.Result).ToList();
|
||||
|
||||
|
@ -427,7 +428,7 @@ namespace MediaBrowser.Api.UserLibrary
|
|||
// Get everything
|
||||
var fields = Enum.GetNames(typeof(ItemFields)).Select(i => (ItemFields)Enum.Parse(typeof(ItemFields), i, true)).ToList();
|
||||
|
||||
var dtoBuilder = new DtoBuilder(Logger, _libraryManager);
|
||||
var dtoBuilder = new DtoBuilder(Logger, _libraryManager, _userManager);
|
||||
|
||||
var result = dtoBuilder.GetBaseItemDto(item, user, fields).Result;
|
||||
|
||||
|
@ -443,7 +444,7 @@ namespace MediaBrowser.Api.UserLibrary
|
|||
// Get everything
|
||||
var fields = Enum.GetNames(typeof(ItemFields)).Select(i => (ItemFields)Enum.Parse(typeof(ItemFields), i, true)).ToList();
|
||||
|
||||
var dtoBuilder = new DtoBuilder(Logger, _libraryManager);
|
||||
var dtoBuilder = new DtoBuilder(Logger, _libraryManager, _userManager);
|
||||
|
||||
var result = dtoBuilder.GetBaseItemDto(item, user, fields).Result;
|
||||
|
||||
|
@ -486,7 +487,7 @@ namespace MediaBrowser.Api.UserLibrary
|
|||
// Serialize to json and then back so that the core doesn't see the request dto type
|
||||
var displayPreferences = _jsonSerializer.DeserializeFromString<DisplayPreferences>(_jsonSerializer.SerializeToString(request));
|
||||
|
||||
var task = _libraryManager.SaveDisplayPreferencesForFolder(user, folder, displayPreferences);
|
||||
var task = _userManager.SaveDisplayPreferences(user.Id, folder.DisplayPreferencesId, displayPreferences, CancellationToken.None);
|
||||
|
||||
Task.WaitAll(task);
|
||||
}
|
||||
|
|
|
@ -142,6 +142,8 @@ namespace MediaBrowser.Api
|
|||
/// Initializes a new instance of the <see cref="UserService" /> class.
|
||||
/// </summary>
|
||||
/// <param name="xmlSerializer">The XML serializer.</param>
|
||||
/// <param name="userManager">The user manager.</param>
|
||||
/// <param name="libraryManager">The library manager.</param>
|
||||
/// <exception cref="System.ArgumentNullException">xmlSerializer</exception>
|
||||
public UserService(IXmlSerializer xmlSerializer, IUserManager userManager, ILibraryManager libraryManager)
|
||||
: base()
|
||||
|
@ -163,7 +165,7 @@ namespace MediaBrowser.Api
|
|||
/// <returns>System.Object.</returns>
|
||||
public object Get(GetUsers request)
|
||||
{
|
||||
var dtoBuilder = new DtoBuilder(Logger, _libraryManager);
|
||||
var dtoBuilder = new DtoBuilder(Logger, _libraryManager, _userManager);
|
||||
|
||||
var tasks = _userManager.Users.OrderBy(u => u.Name).Select(dtoBuilder.GetUserDto).ToArray();
|
||||
|
||||
|
@ -186,7 +188,7 @@ namespace MediaBrowser.Api
|
|||
throw new ResourceNotFoundException("User not found");
|
||||
}
|
||||
|
||||
var result = new DtoBuilder(Logger, _libraryManager).GetUserDto(user).Result;
|
||||
var result = new DtoBuilder(Logger, _libraryManager, _userManager).GetUserDto(user).Result;
|
||||
|
||||
return ToOptimizedResult(result);
|
||||
}
|
||||
|
@ -300,7 +302,7 @@ namespace MediaBrowser.Api
|
|||
|
||||
newUser.UpdateConfiguration(dtoUser.Configuration, _xmlSerializer);
|
||||
|
||||
var result = new DtoBuilder(Logger, _libraryManager).GetUserDto(newUser).Result;
|
||||
var result = new DtoBuilder(Logger, _libraryManager, _userManager).GetUserDto(newUser).Result;
|
||||
|
||||
return ToOptimizedResult(result);
|
||||
}
|
||||
|
|
|
@ -74,109 +74,6 @@ namespace MediaBrowser.Controller.Entities
|
|||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The _display prefs
|
||||
/// </summary>
|
||||
private IEnumerable<DisplayPreferences> _displayPreferences;
|
||||
/// <summary>
|
||||
/// The _display prefs initialized
|
||||
/// </summary>
|
||||
private bool _displayPreferencesInitialized;
|
||||
/// <summary>
|
||||
/// The _display prefs sync lock
|
||||
/// </summary>
|
||||
private object _displayPreferencesSyncLock = new object();
|
||||
/// <summary>
|
||||
/// Gets the display prefs.
|
||||
/// </summary>
|
||||
/// <value>The display prefs.</value>
|
||||
[IgnoreDataMember]
|
||||
public IEnumerable<DisplayPreferences> DisplayPreferences
|
||||
{
|
||||
get
|
||||
{
|
||||
// Call ToList to exhaust the stream because we'll be iterating over this multiple times
|
||||
LazyInitializer.EnsureInitialized(ref _displayPreferences, ref _displayPreferencesInitialized, ref _displayPreferencesSyncLock, () => Kernel.Instance.DisplayPreferencesRepository.RetrieveDisplayPreferences(this).ToList());
|
||||
return _displayPreferences;
|
||||
}
|
||||
private set
|
||||
{
|
||||
_displayPreferences = value;
|
||||
|
||||
if (value == null)
|
||||
{
|
||||
_displayPreferencesInitialized = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the display prefs.
|
||||
/// </summary>
|
||||
/// <param name="user">The user.</param>
|
||||
/// <param name="createIfNull">if set to <c>true</c> [create if null].</param>
|
||||
/// <returns>DisplayPreferences.</returns>
|
||||
/// <exception cref="System.ArgumentNullException"></exception>
|
||||
public DisplayPreferences GetDisplayPreferences(User user, bool createIfNull)
|
||||
{
|
||||
if (user == null)
|
||||
{
|
||||
throw new ArgumentNullException();
|
||||
}
|
||||
|
||||
if (DisplayPreferences == null)
|
||||
{
|
||||
if (!createIfNull)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
AddOrUpdateDisplayPreferences(user, new DisplayPreferences { UserId = user.Id });
|
||||
}
|
||||
|
||||
var data = DisplayPreferences.FirstOrDefault(u => u.UserId == user.Id);
|
||||
|
||||
if (data == null && createIfNull)
|
||||
{
|
||||
data = new DisplayPreferences { UserId = user.Id };
|
||||
AddOrUpdateDisplayPreferences(user, data);
|
||||
}
|
||||
|
||||
return data;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adds the or update display prefs.
|
||||
/// </summary>
|
||||
/// <param name="user">The user.</param>
|
||||
/// <param name="data">The data.</param>
|
||||
/// <exception cref="System.ArgumentNullException"></exception>
|
||||
public void AddOrUpdateDisplayPreferences(User user, DisplayPreferences data)
|
||||
{
|
||||
if (user == null)
|
||||
{
|
||||
throw new ArgumentNullException();
|
||||
}
|
||||
|
||||
if (data == null)
|
||||
{
|
||||
throw new ArgumentNullException();
|
||||
}
|
||||
|
||||
data.UserId = user.Id;
|
||||
|
||||
if (DisplayPreferences == null)
|
||||
{
|
||||
DisplayPreferences = new[] { data };
|
||||
}
|
||||
else
|
||||
{
|
||||
var list = DisplayPreferences.Where(u => u.UserId != user.Id).ToList();
|
||||
list.Add(data);
|
||||
DisplayPreferences = list;
|
||||
}
|
||||
}
|
||||
|
||||
#region Indexing
|
||||
|
||||
/// <summary>
|
||||
|
|
|
@ -28,11 +28,13 @@ namespace MediaBrowser.Controller.Library
|
|||
|
||||
private readonly ILogger _logger;
|
||||
private readonly ILibraryManager _libraryManager;
|
||||
private readonly IUserManager _userManager;
|
||||
|
||||
public DtoBuilder(ILogger logger, ILibraryManager libraryManager)
|
||||
public DtoBuilder(ILogger logger, ILibraryManager libraryManager, IUserManager userManager)
|
||||
{
|
||||
_logger = logger;
|
||||
_libraryManager = libraryManager;
|
||||
_userManager = userManager;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
@ -141,9 +143,9 @@ namespace MediaBrowser.Controller.Library
|
|||
tasks.Add(AttachPeople(dto, item));
|
||||
}
|
||||
|
||||
AttachBasicFields(dto, item, fields);
|
||||
tasks.Add(AttachUserSpecificInfo(dto, item, user, fields));
|
||||
|
||||
AttachUserSpecificInfo(dto, item, user, fields);
|
||||
AttachBasicFields(dto, item, fields);
|
||||
|
||||
// Make sure all the tasks we kicked off have completed.
|
||||
if (tasks.Count > 0)
|
||||
|
@ -161,7 +163,7 @@ namespace MediaBrowser.Controller.Library
|
|||
/// <param name="item">The item.</param>
|
||||
/// <param name="user">The user.</param>
|
||||
/// <param name="fields">The fields.</param>
|
||||
private void AttachUserSpecificInfo(BaseItemDto dto, BaseItem item, User user, List<ItemFields> fields)
|
||||
private async Task AttachUserSpecificInfo(BaseItemDto dto, BaseItem item, User user, List<ItemFields> fields)
|
||||
{
|
||||
if (fields.Contains(ItemFields.UserData))
|
||||
{
|
||||
|
@ -175,7 +177,9 @@ namespace MediaBrowser.Controller.Library
|
|||
|
||||
if (item.IsFolder && fields.Contains(ItemFields.DisplayPreferences))
|
||||
{
|
||||
dto.DisplayPreferences = ((Folder)item).GetDisplayPreferences(user, false) ?? new DisplayPreferences { UserId = user.Id };
|
||||
var displayPreferencesId = ((Folder) item).DisplayPreferencesId;
|
||||
|
||||
dto.DisplayPreferences = await _userManager.GetDisplayPreferences(user.Id, displayPreferencesId).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
if (item.IsFolder)
|
||||
|
@ -191,7 +195,7 @@ namespace MediaBrowser.Controller.Library
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Attaches the primary image aspect ratio.
|
||||
/// </summary>
|
||||
|
|
|
@ -107,15 +107,6 @@ namespace MediaBrowser.Controller.Library
|
|||
/// <returns>Task.</returns>
|
||||
Task ValidateMediaLibrary(IProgress<double> progress, CancellationToken cancellationToken);
|
||||
|
||||
/// <summary>
|
||||
/// Saves display preferences for a Folder
|
||||
/// </summary>
|
||||
/// <param name="user">The user.</param>
|
||||
/// <param name="folder">The folder.</param>
|
||||
/// <param name="data">The data.</param>
|
||||
/// <returns>Task.</returns>
|
||||
Task SaveDisplayPreferencesForFolder(User user, Folder folder, DisplayPreferences data);
|
||||
|
||||
/// <summary>
|
||||
/// Gets the default view.
|
||||
/// </summary>
|
||||
|
|
|
@ -5,6 +5,7 @@ using System;
|
|||
using System.Collections.Generic;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using MediaBrowser.Model.Entities;
|
||||
|
||||
namespace MediaBrowser.Controller.Library
|
||||
{
|
||||
|
@ -178,5 +179,24 @@ namespace MediaBrowser.Controller.Library
|
|||
/// <param name="newPassword">The new password.</param>
|
||||
/// <returns>Task.</returns>
|
||||
Task ChangePassword(User user, string newPassword);
|
||||
|
||||
/// <summary>
|
||||
/// Gets the display preferences.
|
||||
/// </summary>
|
||||
/// <param name="userId">The user id.</param>
|
||||
/// <param name="displayPreferencesId">The display preferences id.</param>
|
||||
/// <returns>DisplayPreferences.</returns>
|
||||
Task<DisplayPreferences> GetDisplayPreferences(Guid userId, Guid displayPreferencesId);
|
||||
|
||||
/// <summary>
|
||||
/// Saves display preferences for an item
|
||||
/// </summary>
|
||||
/// <param name="userId">The user id.</param>
|
||||
/// <param name="displayPreferencesId">The display preferences id.</param>
|
||||
/// <param name="displayPreferences">The display preferences.</param>
|
||||
/// <param name="cancellationToken">The cancellation token.</param>
|
||||
/// <returns>Task.</returns>
|
||||
Task SaveDisplayPreferences(Guid userId, Guid displayPreferencesId, DisplayPreferences displayPreferences,
|
||||
CancellationToken cancellationToken);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,6 +1,5 @@
|
|||
using MediaBrowser.Controller.Entities;
|
||||
using MediaBrowser.Model.Entities;
|
||||
using System.Collections.Generic;
|
||||
using MediaBrowser.Model.Entities;
|
||||
using System;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
|
@ -14,16 +13,20 @@ namespace MediaBrowser.Controller.Persistence
|
|||
/// <summary>
|
||||
/// Saves display preferences for an item
|
||||
/// </summary>
|
||||
/// <param name="item">The item.</param>
|
||||
/// <param name="userId">The user id.</param>
|
||||
/// <param name="displayPreferencesId">The display preferences id.</param>
|
||||
/// <param name="displayPreferences">The display preferences.</param>
|
||||
/// <param name="cancellationToken">The cancellation token.</param>
|
||||
/// <returns>Task.</returns>
|
||||
Task SaveDisplayPreferences(Folder item, CancellationToken cancellationToken);
|
||||
Task SaveDisplayPreferences(Guid userId, Guid displayPreferencesId, DisplayPreferences displayPreferences,
|
||||
CancellationToken cancellationToken);
|
||||
|
||||
/// <summary>
|
||||
/// Gets display preferences for an item
|
||||
/// Gets the display preferences.
|
||||
/// </summary>
|
||||
/// <param name="item">The item.</param>
|
||||
/// <returns>IEnumerable{DisplayPreferences}.</returns>
|
||||
IEnumerable<DisplayPreferences> RetrieveDisplayPreferences(Folder item);
|
||||
/// <param name="userId">The user id.</param>
|
||||
/// <param name="displayPreferencesId">The display preferences id.</param>
|
||||
/// <returns>Task{DisplayPreferences}.</returns>
|
||||
Task<DisplayPreferences> GetDisplayPreferences(Guid userId, Guid displayPreferencesId);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -160,7 +160,7 @@ namespace MediaBrowser.Server.Implementations.HttpServer
|
|||
|
||||
if (exception != null)
|
||||
{
|
||||
_logger.ErrorException("Error processing request", exception);
|
||||
_logger.ErrorException("Error processing request for {0}", exception, req.RawUrl);
|
||||
|
||||
if (!string.IsNullOrEmpty(exception.Message))
|
||||
{
|
||||
|
|
|
@ -124,15 +124,6 @@ namespace MediaBrowser.Server.Implementations.Library
|
|||
LazyInitializer.EnsureInitialized(ref _libraryItemsCache, ref _libraryItemsCacheInitialized, ref _libraryItemsCacheSyncLock, CreateLibraryItemsCache);
|
||||
return _libraryItemsCache;
|
||||
}
|
||||
set
|
||||
{
|
||||
_libraryItemsCache = value;
|
||||
|
||||
if (value == null)
|
||||
{
|
||||
_libraryItemsCacheInitialized = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
@ -756,26 +747,6 @@ namespace MediaBrowser.Server.Implementations.Library
|
|||
await RootFolder.ValidateChildren(progress, cancellationToken, recursive: true).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Saves display preferences for a Folder
|
||||
/// </summary>
|
||||
/// <param name="user">The user.</param>
|
||||
/// <param name="folder">The folder.</param>
|
||||
/// <param name="data">The data.</param>
|
||||
/// <returns>Task.</returns>
|
||||
public Task SaveDisplayPreferencesForFolder(User user, Folder folder, DisplayPreferences data)
|
||||
{
|
||||
// Need to update all items with the same DisplayPreferencesId
|
||||
foreach (var child in RootFolder.GetRecursiveChildren(user)
|
||||
.OfType<Folder>()
|
||||
.Where(i => i.DisplayPreferencesId == folder.DisplayPreferencesId))
|
||||
{
|
||||
child.AddOrUpdateDisplayPreferences(user, data);
|
||||
}
|
||||
|
||||
return Kernel.DisplayPreferencesRepository.SaveDisplayPreferences(folder, CancellationToken.None);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the default view.
|
||||
/// </summary>
|
||||
|
|
|
@ -1,10 +1,12 @@
|
|||
using MediaBrowser.Common.Events;
|
||||
using System.Collections.Concurrent;
|
||||
using MediaBrowser.Common.Events;
|
||||
using MediaBrowser.Common.Extensions;
|
||||
using MediaBrowser.Controller;
|
||||
using MediaBrowser.Controller.Configuration;
|
||||
using MediaBrowser.Controller.Entities;
|
||||
using MediaBrowser.Controller.Library;
|
||||
using MediaBrowser.Model.Connectivity;
|
||||
using MediaBrowser.Model.Entities;
|
||||
using MediaBrowser.Model.Logging;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
@ -97,6 +99,11 @@ namespace MediaBrowser.Server.Implementations.Library
|
|||
/// <value>The configuration manager.</value>
|
||||
private IServerConfigurationManager ConfigurationManager { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// The _user data
|
||||
/// </summary>
|
||||
private readonly ConcurrentDictionary<string, Task<DisplayPreferences>> _displayPreferences = new ConcurrentDictionary<string, Task<DisplayPreferences>>();
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="UserManager" /> class.
|
||||
/// </summary>
|
||||
|
@ -156,6 +163,62 @@ namespace MediaBrowser.Server.Implementations.Library
|
|||
}
|
||||
#endregion
|
||||
|
||||
/// <summary>
|
||||
/// Gets the display preferences.
|
||||
/// </summary>
|
||||
/// <param name="userId">The user id.</param>
|
||||
/// <param name="displayPreferencesId">The display preferences id.</param>
|
||||
/// <returns>DisplayPreferences.</returns>
|
||||
public Task<DisplayPreferences> GetDisplayPreferences(Guid userId, Guid displayPreferencesId)
|
||||
{
|
||||
var key = userId + displayPreferencesId.ToString();
|
||||
|
||||
return _displayPreferences.GetOrAdd(key, keyName => RetrieveDisplayPreferences(userId, displayPreferencesId));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Retrieves the display preferences.
|
||||
/// </summary>
|
||||
/// <param name="userId">The user id.</param>
|
||||
/// <param name="displayPreferencesId">The display preferences id.</param>
|
||||
/// <returns>DisplayPreferences.</returns>
|
||||
private async Task<DisplayPreferences> RetrieveDisplayPreferences(Guid userId, Guid displayPreferencesId)
|
||||
{
|
||||
var displayPreferences = await Kernel.Instance.DisplayPreferencesRepository.GetDisplayPreferences(userId, displayPreferencesId).ConfigureAwait(false);
|
||||
|
||||
return displayPreferences ?? new DisplayPreferences();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Saves display preferences for an item
|
||||
/// </summary>
|
||||
/// <param name="userId">The user id.</param>
|
||||
/// <param name="displayPreferencesId">The display preferences id.</param>
|
||||
/// <param name="displayPreferences">The display preferences.</param>
|
||||
/// <param name="cancellationToken">The cancellation token.</param>
|
||||
/// <returns>Task.</returns>
|
||||
public async Task SaveDisplayPreferences(Guid userId, Guid displayPreferencesId, DisplayPreferences displayPreferences, CancellationToken cancellationToken)
|
||||
{
|
||||
var key = userId + displayPreferencesId.ToString();
|
||||
var newValue = Task.FromResult(displayPreferences);
|
||||
|
||||
try
|
||||
{
|
||||
await Kernel.Instance.DisplayPreferencesRepository.SaveDisplayPreferences(userId, displayPreferencesId,
|
||||
displayPreferences,
|
||||
cancellationToken).ConfigureAwait(false);
|
||||
|
||||
// Once it succeeds, put it into the dictionary to make it available to everyone else
|
||||
_displayPreferences.AddOrUpdate(key, newValue, delegate { return newValue; });
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.ErrorException("Error saving display preferences", ex);
|
||||
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets a User by Id
|
||||
/// </summary>
|
||||
|
@ -711,6 +774,5 @@ namespace MediaBrowser.Server.Implementations.Library
|
|||
|
||||
return Kernel.UserDataRepository.SaveUserData(item, CancellationToken.None);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,11 +1,9 @@
|
|||
using MediaBrowser.Common.Configuration;
|
||||
using MediaBrowser.Controller.Entities;
|
||||
using MediaBrowser.Controller.Persistence;
|
||||
using MediaBrowser.Model.Entities;
|
||||
using MediaBrowser.Model.Logging;
|
||||
using MediaBrowser.Model.Serialization;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data;
|
||||
using System.IO;
|
||||
using System.Threading;
|
||||
|
@ -80,8 +78,8 @@ namespace MediaBrowser.Server.Implementations.Sqlite
|
|||
|
||||
string[] queries = {
|
||||
|
||||
"create table if not exists display_prefs (item_id GUID, user_id GUID, data BLOB)",
|
||||
"create unique index if not exists idx_display_prefs on display_prefs (item_id, user_id)",
|
||||
"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 schema_version (table_name primary key, version)",
|
||||
//pragmas
|
||||
"pragma temp_store = memory"
|
||||
|
@ -93,82 +91,87 @@ namespace MediaBrowser.Server.Implementations.Sqlite
|
|||
/// <summary>
|
||||
/// Save the display preferences associated with an item in the repo
|
||||
/// </summary>
|
||||
/// <param name="item">The item.</param>
|
||||
/// <param name="userId">The user id.</param>
|
||||
/// <param name="displayPreferencesId">The display preferences id.</param>
|
||||
/// <param name="displayPreferences">The display preferences.</param>
|
||||
/// <param name="cancellationToken">The cancellation token.</param>
|
||||
/// <returns>Task.</returns>
|
||||
/// <exception cref="System.ArgumentNullException">item</exception>
|
||||
public Task SaveDisplayPreferences(Folder item, CancellationToken cancellationToken)
|
||||
public Task SaveDisplayPreferences(Guid userId, Guid displayPreferencesId, DisplayPreferences displayPreferences, CancellationToken cancellationToken)
|
||||
{
|
||||
if (item == null)
|
||||
if (displayPreferences == null)
|
||||
{
|
||||
throw new ArgumentNullException("item");
|
||||
throw new ArgumentNullException("displayPreferences");
|
||||
}
|
||||
|
||||
if (cancellationToken == null)
|
||||
{
|
||||
throw new ArgumentNullException("cancellationToken");
|
||||
}
|
||||
if (userId == Guid.Empty)
|
||||
{
|
||||
throw new ArgumentNullException("userId");
|
||||
}
|
||||
if (displayPreferencesId == Guid.Empty)
|
||||
{
|
||||
throw new ArgumentNullException("displayPreferencesId");
|
||||
}
|
||||
|
||||
cancellationToken.ThrowIfCancellationRequested();
|
||||
|
||||
|
||||
return Task.Run(() =>
|
||||
{
|
||||
var serialized = _protobufSerializer.SerializeToBytes(displayPreferences);
|
||||
|
||||
cancellationToken.ThrowIfCancellationRequested();
|
||||
|
||||
var cmd = connection.CreateCommand();
|
||||
|
||||
cmd.CommandText = "delete from display_prefs where item_id = @guid";
|
||||
cmd.AddParam("@guid", item.DisplayPreferencesId);
|
||||
|
||||
cmd.CommandText = "replace into displaypreferences (id, userId, data) values (@1, @2, @3)";
|
||||
cmd.AddParam("@1", displayPreferencesId);
|
||||
cmd.AddParam("@2", userId);
|
||||
cmd.AddParam("@3", serialized);
|
||||
QueueCommand(cmd);
|
||||
|
||||
if (item.DisplayPreferences != null)
|
||||
{
|
||||
foreach (var data in item.DisplayPreferences)
|
||||
{
|
||||
cmd = connection.CreateCommand();
|
||||
cmd.CommandText = "insert into display_prefs (item_id, user_id, data) values (@1, @2, @3)";
|
||||
cmd.AddParam("@1", item.DisplayPreferencesId);
|
||||
cmd.AddParam("@2", data.UserId);
|
||||
|
||||
cmd.AddParam("@3", _protobufSerializer.SerializeToBytes(data));
|
||||
|
||||
QueueCommand(cmd);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets display preferences for an item
|
||||
/// Gets the display preferences.
|
||||
/// </summary>
|
||||
/// <param name="item">The item.</param>
|
||||
/// <returns>IEnumerable{DisplayPreferences}.</returns>
|
||||
/// <exception cref="System.ArgumentNullException"></exception>
|
||||
public IEnumerable<DisplayPreferences> RetrieveDisplayPreferences(Folder item)
|
||||
/// <param name="userId">The user id.</param>
|
||||
/// <param name="displayPreferencesId">The display preferences id.</param>
|
||||
/// <returns>Task{DisplayPreferences}.</returns>
|
||||
/// <exception cref="System.ArgumentNullException">item</exception>
|
||||
public async Task<DisplayPreferences> GetDisplayPreferences(Guid userId, Guid displayPreferencesId)
|
||||
{
|
||||
if (item == null)
|
||||
if (userId == Guid.Empty)
|
||||
{
|
||||
throw new ArgumentNullException("item");
|
||||
throw new ArgumentNullException("userId");
|
||||
}
|
||||
if (displayPreferencesId == Guid.Empty)
|
||||
{
|
||||
throw new ArgumentNullException("displayPreferencesId");
|
||||
}
|
||||
|
||||
var cmd = connection.CreateCommand();
|
||||
cmd.CommandText = "select data from display_prefs where item_id = @guid";
|
||||
var guidParam = cmd.Parameters.Add("@guid", DbType.Guid);
|
||||
guidParam.Value = item.DisplayPreferencesId;
|
||||
cmd.CommandText = "select data from displaypreferences where id = @id and userId=@userId";
|
||||
|
||||
var idParam = cmd.Parameters.Add("@id", DbType.Guid);
|
||||
idParam.Value = displayPreferencesId;
|
||||
|
||||
using (var reader = cmd.ExecuteReader(CommandBehavior.SequentialAccess | CommandBehavior.SingleResult))
|
||||
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))
|
||||
{
|
||||
while (reader.Read())
|
||||
if (reader.Read())
|
||||
{
|
||||
using (var stream = GetStream(reader, 0))
|
||||
{
|
||||
var data = _protobufSerializer.DeserializeFromStream<DisplayPreferences>(stream);
|
||||
if (data != null)
|
||||
{
|
||||
yield return data;
|
||||
}
|
||||
return _protobufSerializer.DeserializeFromStream<DisplayPreferences>(stream);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -174,7 +174,7 @@ namespace MediaBrowser.ServerApplication.EntryPoints
|
|||
/// <param name="e">The e.</param>
|
||||
async void userManager_UserUpdated(object sender, GenericEventArgs<User> e)
|
||||
{
|
||||
var dto = await new DtoBuilder(_logger, _libraryManager).GetUserDto(e.Argument).ConfigureAwait(false);
|
||||
var dto = await new DtoBuilder(_logger, _libraryManager, _userManager).GetUserDto(e.Argument).ConfigureAwait(false);
|
||||
|
||||
_serverManager.SendWebSocketMessage("UserUpdated", dto);
|
||||
}
|
||||
|
|
|
@ -33,7 +33,8 @@ namespace MediaBrowser.ServerApplication
|
|||
|
||||
private readonly IJsonSerializer _jsonSerializer;
|
||||
private readonly ILibraryManager _libraryManager;
|
||||
|
||||
private readonly IUserManager _userManager;
|
||||
|
||||
/// <summary>
|
||||
/// The current user
|
||||
/// </summary>
|
||||
|
@ -46,6 +47,7 @@ namespace MediaBrowser.ServerApplication
|
|||
_logger = logger;
|
||||
_jsonSerializer = jsonSerializer;
|
||||
_libraryManager = libraryManager;
|
||||
_userManager = userManager;
|
||||
|
||||
InitializeComponent();
|
||||
lblVersion.Content = "Version: " + appHost.ApplicationVersion;
|
||||
|
@ -89,7 +91,7 @@ namespace MediaBrowser.ServerApplication
|
|||
var currentFolder = folder;
|
||||
Task.Factory.StartNew(() =>
|
||||
{
|
||||
var prefs = ddlProfile.SelectedItem != null ? currentFolder.GetDisplayPreferences(ddlProfile.SelectedItem as User, false) ?? new DisplayPreferences {SortBy = ItemSortBy.SortName} : new DisplayPreferences {SortBy = ItemSortBy.SortName};
|
||||
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 node = new TreeViewItem { Tag = currentFolder };
|
||||
|
||||
var subChildren = currentFolder.GetChildren(CurrentUser, prefs.IndexBy);
|
||||
|
@ -142,7 +144,8 @@ namespace MediaBrowser.ServerApplication
|
|||
var subFolder = item as Folder;
|
||||
if (subFolder != null)
|
||||
{
|
||||
var prefs = subFolder.GetDisplayPreferences(user, false) ?? new DisplayPreferences {SortBy = LocalizedStrings.Instance.GetString("NameDispPref")};
|
||||
var prefs = _userManager.GetDisplayPreferences(user.Id, subFolder.DisplayPreferencesId).Result;
|
||||
|
||||
AddChildren(node, OrderBy(subFolder.GetChildren(user), user, prefs.SortBy), user);
|
||||
node.Header = item.Name + " (" + node.Items.Count + ")";
|
||||
}
|
||||
|
@ -196,7 +199,10 @@ namespace MediaBrowser.ServerApplication
|
|||
ItemSortBy.Runtime
|
||||
};
|
||||
|
||||
var prefs = folder.GetDisplayPreferences(ddlProfile.SelectedItem as User, false);
|
||||
var prefs =
|
||||
await
|
||||
_userManager.GetDisplayPreferences((ddlProfile.SelectedItem as User).Id,
|
||||
folder.DisplayPreferencesId);
|
||||
ddlIndexBy.SelectedItem = prefs != null
|
||||
? prefs.IndexBy ?? LocalizedStrings.Instance.GetString("NoneDispPref")
|
||||
: LocalizedStrings.Instance.GetString("NoneDispPref");
|
||||
|
@ -354,7 +360,7 @@ namespace MediaBrowser.ServerApplication
|
|||
var folder = treeItem != null
|
||||
? treeItem.Tag as Folder
|
||||
: null;
|
||||
var prefs = folder != null ? folder.GetDisplayPreferences(CurrentUser, true) : new DisplayPreferences {SortBy = ItemSortBy.SortName};
|
||||
var prefs = folder != null ? _userManager.GetDisplayPreferences(CurrentUser.Id, folder.DisplayPreferencesId).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
|
||||
|
@ -395,7 +401,7 @@ namespace MediaBrowser.ServerApplication
|
|||
var folder = treeItem != null
|
||||
? treeItem.Tag as Folder
|
||||
: null;
|
||||
var prefs = folder != null ? folder.GetDisplayPreferences(CurrentUser, true) : new DisplayPreferences();
|
||||
var prefs = folder != null ? _userManager.GetDisplayPreferences(CurrentUser.Id, folder.DisplayPreferencesId).Result : new DisplayPreferences();
|
||||
if (folder != null && prefs.SortBy != ddlSortBy.SelectedItem as string)
|
||||
{
|
||||
//grab UI context so we can update within the below task
|
||||
|
|
|
@ -163,7 +163,7 @@ namespace MediaBrowser.WebDashboard.Api
|
|||
{
|
||||
var connections = userManager.RecentConnections.ToArray();
|
||||
|
||||
var dtoBuilder = new DtoBuilder(logger, libraryManager);
|
||||
var dtoBuilder = new DtoBuilder(logger, libraryManager, userManager);
|
||||
|
||||
var tasks = userManager.Users.Where(u => connections.Any(c => c.UserId == u.Id)).Select(dtoBuilder.GetUserDto);
|
||||
var users = await Task.WhenAll(tasks).ConfigureAwait(false);
|
||||
|
|
Loading…
Reference in New Issue
Block a user