mirror of
https://github.com/jellyfin/jellyfin.git
synced 2024-11-15 18:08:53 -07:00
Moved userdata to user manager to resolve concurrency issues. must delete userdata and displaypreferences db files.
This commit is contained in:
parent
b4dd8a2106
commit
e2562879d8
@ -241,12 +241,15 @@ namespace MediaBrowser.Api.UserLibrary
|
||||
/// <returns>IEnumerable{BaseItem}.</returns>
|
||||
private IEnumerable<BaseItem> ApplyFilter(IEnumerable<BaseItem> items, ItemFilter filter, User user)
|
||||
{
|
||||
// Avoids implicitly captured closure
|
||||
var currentUser = user;
|
||||
|
||||
switch (filter)
|
||||
{
|
||||
case ItemFilter.Likes:
|
||||
return items.Where(item =>
|
||||
{
|
||||
var userdata = item.GetUserData(user, false);
|
||||
var userdata = _userManager.GetUserData(user.Id, item.UserDataId).Result;
|
||||
|
||||
return userdata != null && userdata.Likes.HasValue && userdata.Likes.Value;
|
||||
});
|
||||
@ -254,7 +257,7 @@ namespace MediaBrowser.Api.UserLibrary
|
||||
case ItemFilter.Dislikes:
|
||||
return items.Where(item =>
|
||||
{
|
||||
var userdata = item.GetUserData(user, false);
|
||||
var userdata = _userManager.GetUserData(user.Id, item.UserDataId).Result;
|
||||
|
||||
return userdata != null && userdata.Likes.HasValue && !userdata.Likes.Value;
|
||||
});
|
||||
@ -262,18 +265,18 @@ namespace MediaBrowser.Api.UserLibrary
|
||||
case ItemFilter.IsFavorite:
|
||||
return items.Where(item =>
|
||||
{
|
||||
var userdata = item.GetUserData(user, false);
|
||||
var userdata = _userManager.GetUserData(user.Id, item.UserDataId).Result;
|
||||
|
||||
return userdata != null && userdata.IsFavorite;
|
||||
});
|
||||
|
||||
case ItemFilter.IsRecentlyAdded:
|
||||
return items.Where(item => item.IsRecentlyAdded(user));
|
||||
return items.Where(item => item.IsRecentlyAdded(currentUser));
|
||||
|
||||
case ItemFilter.IsResumable:
|
||||
return items.Where(item =>
|
||||
{
|
||||
var userdata = item.GetUserData(user, false);
|
||||
var userdata = _userManager.GetUserData(user.Id, item.UserDataId).Result;
|
||||
|
||||
return userdata != null && userdata.PlaybackPositionTicks > 0;
|
||||
});
|
||||
@ -281,7 +284,7 @@ namespace MediaBrowser.Api.UserLibrary
|
||||
case ItemFilter.IsPlayed:
|
||||
return items.Where(item =>
|
||||
{
|
||||
var userdata = item.GetUserData(user, false);
|
||||
var userdata = _userManager.GetUserData(user.Id, item.UserDataId).Result;
|
||||
|
||||
return userdata != null && userdata.PlayCount > 0;
|
||||
});
|
||||
@ -289,7 +292,7 @@ namespace MediaBrowser.Api.UserLibrary
|
||||
case ItemFilter.IsUnplayed:
|
||||
return items.Where(item =>
|
||||
{
|
||||
var userdata = item.GetUserData(user, false);
|
||||
var userdata = _userManager.GetUserData(user.Id, item.UserDataId).Result;
|
||||
|
||||
return userdata == null || userdata.PlayCount == 0;
|
||||
});
|
||||
|
@ -503,12 +503,12 @@ namespace MediaBrowser.Api.UserLibrary
|
||||
var item = string.IsNullOrEmpty(request.Id) ? user.RootFolder : DtoBuilder.GetItemByClientId(request.Id, _userManager, _libraryManager, user.Id);
|
||||
|
||||
// Get the user data for this item
|
||||
var data = item.GetUserData(user, true);
|
||||
var data = _userManager.GetUserData(user.Id, item.UserDataId).Result;
|
||||
|
||||
// Set favorite status
|
||||
data.IsFavorite = true;
|
||||
|
||||
var task = _userManager.SaveUserDataForItem(user, item, data);
|
||||
var task = _userManager.SaveUserData(user.Id, item.UserDataId, data, CancellationToken.None);
|
||||
|
||||
Task.WaitAll(task);
|
||||
}
|
||||
@ -524,12 +524,12 @@ namespace MediaBrowser.Api.UserLibrary
|
||||
var item = string.IsNullOrEmpty(request.Id) ? user.RootFolder : DtoBuilder.GetItemByClientId(request.Id, _userManager, _libraryManager, user.Id);
|
||||
|
||||
// Get the user data for this item
|
||||
var data = item.GetUserData(user, true);
|
||||
var data = _userManager.GetUserData(user.Id, item.UserDataId).Result;
|
||||
|
||||
// Set favorite status
|
||||
data.IsFavorite = false;
|
||||
|
||||
var task = _userManager.SaveUserDataForItem(user, item, data);
|
||||
var task = _userManager.SaveUserData(user.Id, item.UserDataId, data, CancellationToken.None);
|
||||
|
||||
Task.WaitAll(task);
|
||||
}
|
||||
@ -545,11 +545,11 @@ namespace MediaBrowser.Api.UserLibrary
|
||||
var item = string.IsNullOrEmpty(request.Id) ? user.RootFolder : DtoBuilder.GetItemByClientId(request.Id, _userManager, _libraryManager, user.Id);
|
||||
|
||||
// Get the user data for this item
|
||||
var data = item.GetUserData(user, true);
|
||||
var data = _userManager.GetUserData(user.Id, item.UserDataId).Result;
|
||||
|
||||
data.Rating = null;
|
||||
|
||||
var task = _userManager.SaveUserDataForItem(user, item, data);
|
||||
var task = _userManager.SaveUserData(user.Id, item.UserDataId, data, CancellationToken.None);
|
||||
|
||||
Task.WaitAll(task);
|
||||
}
|
||||
@ -565,11 +565,11 @@ namespace MediaBrowser.Api.UserLibrary
|
||||
var item = string.IsNullOrEmpty(request.Id) ? user.RootFolder : DtoBuilder.GetItemByClientId(request.Id, _userManager, _libraryManager, user.Id);
|
||||
|
||||
// Get the user data for this item
|
||||
var data = item.GetUserData(user, true);
|
||||
var data = _userManager.GetUserData(user.Id, item.UserDataId).Result;
|
||||
|
||||
data.Likes = request.Likes;
|
||||
|
||||
var task = _userManager.SaveUserDataForItem(user, item, data);
|
||||
var task = _userManager.SaveUserData(user.Id, item.UserDataId, data, CancellationToken.None);
|
||||
|
||||
Task.WaitAll(task);
|
||||
}
|
||||
|
@ -796,109 +796,6 @@ namespace MediaBrowser.Controller.Entities
|
||||
get { return null; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The _user data
|
||||
/// </summary>
|
||||
private IEnumerable<UserItemData> _userData;
|
||||
/// <summary>
|
||||
/// The _user data initialized
|
||||
/// </summary>
|
||||
private bool _userDataInitialized;
|
||||
/// <summary>
|
||||
/// The _user data sync lock
|
||||
/// </summary>
|
||||
private object _userDataSyncLock = new object();
|
||||
/// <summary>
|
||||
/// Gets the user data.
|
||||
/// </summary>
|
||||
/// <value>The user data.</value>
|
||||
[IgnoreDataMember]
|
||||
public IEnumerable<UserItemData> UserData
|
||||
{
|
||||
get
|
||||
{
|
||||
// Call ToList to exhaust the stream because we'll be iterating over this multiple times
|
||||
LazyInitializer.EnsureInitialized(ref _userData, ref _userDataInitialized, ref _userDataSyncLock, () => Kernel.Instance.UserDataRepository.RetrieveUserData(this).ToList());
|
||||
return _userData;
|
||||
}
|
||||
private set
|
||||
{
|
||||
_userData = value;
|
||||
|
||||
if (value == null)
|
||||
{
|
||||
_userDataInitialized = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the user data.
|
||||
/// </summary>
|
||||
/// <param name="user">The user.</param>
|
||||
/// <param name="createIfNull">if set to <c>true</c> [create if null].</param>
|
||||
/// <returns>UserItemData.</returns>
|
||||
/// <exception cref="System.ArgumentNullException"></exception>
|
||||
public UserItemData GetUserData(User user, bool createIfNull)
|
||||
{
|
||||
if (user == null)
|
||||
{
|
||||
throw new ArgumentNullException();
|
||||
}
|
||||
|
||||
if (UserData == null)
|
||||
{
|
||||
if (!createIfNull)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
AddOrUpdateUserData(user, new UserItemData { UserId = user.Id });
|
||||
}
|
||||
|
||||
var data = UserData.FirstOrDefault(u => u.UserId == user.Id);
|
||||
|
||||
if (data == null && createIfNull)
|
||||
{
|
||||
data = new UserItemData { UserId = user.Id };
|
||||
AddOrUpdateUserData(user, data);
|
||||
}
|
||||
|
||||
return data;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adds the or update user data.
|
||||
/// </summary>
|
||||
/// <param name="user">The user.</param>
|
||||
/// <param name="data">The data.</param>
|
||||
/// <exception cref="System.ArgumentNullException"></exception>
|
||||
public void AddOrUpdateUserData(User user, UserItemData data)
|
||||
{
|
||||
if (user == null)
|
||||
{
|
||||
throw new ArgumentNullException();
|
||||
}
|
||||
|
||||
if (data == null)
|
||||
{
|
||||
throw new ArgumentNullException();
|
||||
}
|
||||
|
||||
data.UserId = user.Id;
|
||||
|
||||
if (UserData == null)
|
||||
{
|
||||
UserData = new[] { data };
|
||||
}
|
||||
else
|
||||
{
|
||||
var list = UserData.Where(u => u.UserId != user.Id).ToList();
|
||||
list.Add(data);
|
||||
UserData = list;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The _user data id
|
||||
/// </summary>
|
||||
@ -1205,16 +1102,17 @@ namespace MediaBrowser.Controller.Entities
|
||||
/// </summary>
|
||||
/// <param name="user">The user.</param>
|
||||
/// <param name="wasPlayed">if set to <c>true</c> [was played].</param>
|
||||
/// <param name="userManager">The user manager.</param>
|
||||
/// <returns>Task.</returns>
|
||||
/// <exception cref="System.ArgumentNullException"></exception>
|
||||
public virtual Task SetPlayedStatus(User user, bool wasPlayed, IUserManager userManager)
|
||||
public virtual async Task SetPlayedStatus(User user, bool wasPlayed, IUserManager userManager)
|
||||
{
|
||||
if (user == null)
|
||||
{
|
||||
throw new ArgumentNullException();
|
||||
}
|
||||
|
||||
var data = GetUserData(user, true);
|
||||
var data = await userManager.GetUserData(user.Id, UserDataId).ConfigureAwait(false);
|
||||
|
||||
if (wasPlayed)
|
||||
{
|
||||
@ -1237,7 +1135,7 @@ namespace MediaBrowser.Controller.Entities
|
||||
|
||||
data.Played = wasPlayed;
|
||||
|
||||
return userManager.SaveUserDataForItem(user, this, data);
|
||||
await userManager.SaveUserData(user.Id, UserDataId, data, CancellationToken.None).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
@ -799,6 +799,7 @@ namespace MediaBrowser.Controller.Entities
|
||||
/// </summary>
|
||||
/// <param name="user">The user.</param>
|
||||
/// <param name="wasPlayed">if set to <c>true</c> [was played].</param>
|
||||
/// <param name="userManager">The user manager.</param>
|
||||
/// <returns>Task.</returns>
|
||||
public override async Task SetPlayedStatus(User user, bool wasPlayed, IUserManager userManager)
|
||||
{
|
||||
|
@ -167,12 +167,9 @@ namespace MediaBrowser.Controller.Library
|
||||
{
|
||||
if (fields.Contains(ItemFields.UserData))
|
||||
{
|
||||
var userData = item.GetUserData(user, false);
|
||||
var userData = await _userManager.GetUserData(user.Id, item.UserDataId).ConfigureAwait(false);
|
||||
|
||||
if (userData != null)
|
||||
{
|
||||
dto.UserData = GetUserItemDataDto(userData);
|
||||
}
|
||||
dto.UserData = GetUserItemDataDto(userData);
|
||||
}
|
||||
|
||||
if (item.IsFolder && fields.Contains(ItemFields.DisplayPreferences))
|
||||
@ -191,7 +188,7 @@ namespace MediaBrowser.Controller.Library
|
||||
// Skip sorting since all we want is a count
|
||||
dto.ChildCount = folder.GetChildren(user).Count();
|
||||
|
||||
SetSpecialCounts(folder, user, dto);
|
||||
await SetSpecialCounts(folder, user, dto, _userManager).ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -488,7 +485,9 @@ namespace MediaBrowser.Controller.Library
|
||||
/// <param name="folder">The folder.</param>
|
||||
/// <param name="user">The user.</param>
|
||||
/// <param name="dto">The dto.</param>
|
||||
private static void SetSpecialCounts(Folder folder, User user, BaseItemDto dto)
|
||||
/// <param name="userManager">The user manager.</param>
|
||||
/// <returns>Task.</returns>
|
||||
private static async Task SetSpecialCounts(Folder folder, User user, BaseItemDto dto, IUserManager userManager)
|
||||
{
|
||||
var rcentlyAddedItemCount = 0;
|
||||
var recursiveItemCount = 0;
|
||||
@ -498,7 +497,7 @@ namespace MediaBrowser.Controller.Library
|
||||
// Loop through each recursive child
|
||||
foreach (var child in folder.GetRecursiveChildren(user).Where(i => !i.IsFolder))
|
||||
{
|
||||
var userdata = child.GetUserData(user, false);
|
||||
var userdata = await userManager.GetUserData(user.Id, child.UserDataId).ConfigureAwait(false);
|
||||
|
||||
recursiveItemCount++;
|
||||
|
||||
|
@ -1,11 +1,11 @@
|
||||
using MediaBrowser.Common.Events;
|
||||
using MediaBrowser.Controller.Entities;
|
||||
using MediaBrowser.Model.Connectivity;
|
||||
using MediaBrowser.Model.Entities;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using MediaBrowser.Model.Entities;
|
||||
|
||||
namespace MediaBrowser.Controller.Library
|
||||
{
|
||||
@ -157,14 +157,6 @@ namespace MediaBrowser.Controller.Library
|
||||
/// <exception cref="System.ArgumentNullException"></exception>
|
||||
Task OnPlaybackStopped(User user, BaseItem item, long? positionTicks, string clientType, string deviceId, string deviceName);
|
||||
|
||||
/// <summary>
|
||||
/// Saves user data for an item
|
||||
/// </summary>
|
||||
/// <param name="user">The user.</param>
|
||||
/// <param name="item">The item.</param>
|
||||
/// <param name="data">The data.</param>
|
||||
Task SaveUserDataForItem(User user, BaseItem item, UserItemData data);
|
||||
|
||||
/// <summary>
|
||||
/// Resets the password.
|
||||
/// </summary>
|
||||
@ -180,6 +172,25 @@ namespace MediaBrowser.Controller.Library
|
||||
/// <returns>Task.</returns>
|
||||
Task ChangePassword(User user, string newPassword);
|
||||
|
||||
/// <summary>
|
||||
/// Saves display preferences for an item
|
||||
/// </summary>
|
||||
/// <param name="userId">The user id.</param>
|
||||
/// <param name="userDataId">The user data id.</param>
|
||||
/// <param name="userData">The user data.</param>
|
||||
/// <param name="cancellationToken">The cancellation token.</param>
|
||||
/// <returns>Task.</returns>
|
||||
Task SaveUserData(Guid userId, Guid userDataId, UserItemData userData,
|
||||
CancellationToken cancellationToken);
|
||||
|
||||
/// <summary>
|
||||
/// Gets the display preferences.
|
||||
/// </summary>
|
||||
/// <param name="userId">The user id.</param>
|
||||
/// <param name="userDataId">The user data id.</param>
|
||||
/// <returns>Task{DisplayPreferences}.</returns>
|
||||
Task<UserItemData> GetUserData(Guid userId, Guid userDataId);
|
||||
|
||||
/// <summary>
|
||||
/// Gets the display preferences.
|
||||
/// </summary>
|
||||
|
@ -1,5 +1,5 @@
|
||||
using MediaBrowser.Controller.Entities;
|
||||
using System.Collections.Generic;
|
||||
using System;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
@ -11,18 +11,22 @@ namespace MediaBrowser.Controller.Persistence
|
||||
public interface IUserDataRepository : IRepository
|
||||
{
|
||||
/// <summary>
|
||||
/// Saves user data for an item
|
||||
/// Saves the user data.
|
||||
/// </summary>
|
||||
/// <param name="item">The item.</param>
|
||||
/// <param name="userId">The user id.</param>
|
||||
/// <param name="userDataId">The user data id.</param>
|
||||
/// <param name="userData">The user data.</param>
|
||||
/// <param name="cancellationToken">The cancellation token.</param>
|
||||
/// <returns>Task.</returns>
|
||||
Task SaveUserData(BaseItem item, CancellationToken cancellationToken);
|
||||
Task SaveUserData(Guid userId, Guid userDataId, UserItemData userData,
|
||||
CancellationToken cancellationToken);
|
||||
|
||||
/// <summary>
|
||||
/// Gets user data for an item
|
||||
/// Gets the user data.
|
||||
/// </summary>
|
||||
/// <param name="item">The item.</param>
|
||||
/// <returns>IEnumerable{UserItemData}.</returns>
|
||||
IEnumerable<UserItemData> RetrieveUserData(BaseItem item);
|
||||
/// <param name="userId">The user id.</param>
|
||||
/// <param name="userDataId">The user data id.</param>
|
||||
/// <returns>Task{UserItemData}.</returns>
|
||||
Task<UserItemData> GetUserData(Guid userId, Guid userDataId);
|
||||
}
|
||||
}
|
||||
|
@ -1,4 +1,5 @@
|
||||
using MediaBrowser.Controller.Entities;
|
||||
using MediaBrowser.Controller.Library;
|
||||
|
||||
namespace MediaBrowser.Controller.Sorting
|
||||
{
|
||||
@ -12,5 +13,11 @@ namespace MediaBrowser.Controller.Sorting
|
||||
/// </summary>
|
||||
/// <value>The user.</value>
|
||||
User User { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the user manager.
|
||||
/// </summary>
|
||||
/// <value>The user manager.</value>
|
||||
IUserManager UserManager { get; set; }
|
||||
}
|
||||
}
|
||||
|
@ -861,6 +861,7 @@ namespace MediaBrowser.Server.Implementations.Library
|
||||
var userComparer = (IUserBaseItemComparer)Activator.CreateInstance(comparer.GetType());
|
||||
|
||||
userComparer.User = user;
|
||||
userComparer.UserManager = _userManager;
|
||||
|
||||
return userComparer;
|
||||
}
|
||||
|
@ -1,5 +1,4 @@
|
||||
using System.Collections.Concurrent;
|
||||
using MediaBrowser.Common.Events;
|
||||
using MediaBrowser.Common.Events;
|
||||
using MediaBrowser.Common.Extensions;
|
||||
using MediaBrowser.Controller;
|
||||
using MediaBrowser.Controller.Configuration;
|
||||
@ -9,6 +8,7 @@ using MediaBrowser.Model.Connectivity;
|
||||
using MediaBrowser.Model.Entities;
|
||||
using MediaBrowser.Model.Logging;
|
||||
using System;
|
||||
using System.Collections.Concurrent;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Security.Cryptography;
|
||||
@ -104,6 +104,8 @@ namespace MediaBrowser.Server.Implementations.Library
|
||||
/// </summary>
|
||||
private readonly ConcurrentDictionary<string, Task<DisplayPreferences>> _displayPreferences = new ConcurrentDictionary<string, Task<DisplayPreferences>>();
|
||||
|
||||
private readonly ConcurrentDictionary<string, Task<UserItemData>> _userData = new ConcurrentDictionary<string, Task<UserItemData>>();
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="UserManager" /> class.
|
||||
/// </summary>
|
||||
@ -200,14 +202,15 @@ namespace MediaBrowser.Server.Implementations.Library
|
||||
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);
|
||||
|
||||
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; });
|
||||
}
|
||||
@ -651,10 +654,10 @@ namespace MediaBrowser.Server.Implementations.Library
|
||||
|
||||
if (positionTicks.HasValue)
|
||||
{
|
||||
var data = item.GetUserData(user, true);
|
||||
var data = await GetUserData(user.Id, item.UserDataId).ConfigureAwait(false);
|
||||
|
||||
UpdatePlayState(item, data, positionTicks.Value, false);
|
||||
await SaveUserDataForItem(user, item, data).ConfigureAwait(false);
|
||||
await SaveUserData(user.Id, item.UserDataId, data, CancellationToken.None).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
EventHelper.QueueEventIfNotNull(PlaybackProgress, this, new PlaybackProgressEventArgs
|
||||
@ -689,7 +692,7 @@ namespace MediaBrowser.Server.Implementations.Library
|
||||
|
||||
RemoveNowPlayingItemId(user, clientType, deviceId, deviceName, item);
|
||||
|
||||
var data = item.GetUserData(user, true);
|
||||
var data = await GetUserData(user.Id, item.UserDataId).ConfigureAwait(false);
|
||||
|
||||
if (positionTicks.HasValue)
|
||||
{
|
||||
@ -702,7 +705,7 @@ namespace MediaBrowser.Server.Implementations.Library
|
||||
data.Played = true;
|
||||
}
|
||||
|
||||
await SaveUserDataForItem(user, item, data).ConfigureAwait(false);
|
||||
await SaveUserData(user.Id, item.UserDataId, data, CancellationToken.None).ConfigureAwait(false);
|
||||
|
||||
EventHelper.QueueEventIfNotNull(PlaybackStopped, this, new PlaybackProgressEventArgs
|
||||
{
|
||||
@ -763,16 +766,57 @@ namespace MediaBrowser.Server.Implementations.Library
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Saves user data for an item
|
||||
/// Saves display preferences for an item
|
||||
/// </summary>
|
||||
/// <param name="user">The user.</param>
|
||||
/// <param name="item">The item.</param>
|
||||
/// <param name="data">The data.</param>
|
||||
public Task SaveUserDataForItem(User user, BaseItem item, UserItemData data)
|
||||
/// <param name="userId">The user id.</param>
|
||||
/// <param name="userDataId">The user data id.</param>
|
||||
/// <param name="userData">The user data.</param>
|
||||
/// <param name="cancellationToken">The cancellation token.</param>
|
||||
/// <returns>Task.</returns>
|
||||
public async Task SaveUserData(Guid userId, Guid userDataId, UserItemData userData, CancellationToken cancellationToken)
|
||||
{
|
||||
item.AddOrUpdateUserData(user, data);
|
||||
var key = userId + userDataId.ToString();
|
||||
try
|
||||
{
|
||||
await Kernel.Instance.UserDataRepository.SaveUserData(userId, userDataId, userData, cancellationToken).ConfigureAwait(false);
|
||||
|
||||
return Kernel.UserDataRepository.SaveUserData(item, CancellationToken.None);
|
||||
var newValue = Task.FromResult(userData);
|
||||
|
||||
// Once it succeeds, put it into the dictionary to make it available to everyone else
|
||||
_userData.AddOrUpdate(key, newValue, delegate { return newValue; });
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.ErrorException("Error saving user data", ex);
|
||||
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the display preferences.
|
||||
/// </summary>
|
||||
/// <param name="userId">The user id.</param>
|
||||
/// <param name="userDataId">The user data id.</param>
|
||||
/// <returns>Task{DisplayPreferences}.</returns>
|
||||
public Task<UserItemData> GetUserData(Guid userId, Guid userDataId)
|
||||
{
|
||||
var key = userId + userDataId.ToString();
|
||||
|
||||
return _userData.GetOrAdd(key, keyName => RetrieveUserData(userId, userDataId));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Retrieves the display preferences.
|
||||
/// </summary>
|
||||
/// <param name="userId">The user id.</param>
|
||||
/// <param name="userDataId">The user data id.</param>
|
||||
/// <returns>DisplayPreferences.</returns>
|
||||
private async Task<UserItemData> RetrieveUserData(Guid userId, Guid userDataId)
|
||||
{
|
||||
var userdata = await Kernel.Instance.UserDataRepository.GetUserData(userId, userDataId).ConfigureAwait(false);
|
||||
|
||||
return userdata ?? new UserItemData();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,4 +1,5 @@
|
||||
using MediaBrowser.Controller.Entities;
|
||||
using MediaBrowser.Controller.Library;
|
||||
using MediaBrowser.Controller.Sorting;
|
||||
using MediaBrowser.Model.Querying;
|
||||
using System;
|
||||
@ -16,6 +17,12 @@ namespace MediaBrowser.Server.Implementations.Sorting
|
||||
/// <value>The user.</value>
|
||||
public User User { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the user manager.
|
||||
/// </summary>
|
||||
/// <value>The user manager.</value>
|
||||
public IUserManager UserManager { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Compares the specified x.
|
||||
/// </summary>
|
||||
@ -34,7 +41,7 @@ namespace MediaBrowser.Server.Implementations.Sorting
|
||||
/// <returns>DateTime.</returns>
|
||||
private DateTime GetDate(BaseItem x)
|
||||
{
|
||||
var userdata = x.GetUserData(User, false);
|
||||
var userdata = UserManager.GetUserData(User.Id, x.UserDataId).Result;
|
||||
|
||||
if (userdata != null && userdata.LastPlayedDate.HasValue)
|
||||
{
|
||||
|
@ -1,4 +1,5 @@
|
||||
using MediaBrowser.Controller.Entities;
|
||||
using MediaBrowser.Controller.Library;
|
||||
using MediaBrowser.Controller.Sorting;
|
||||
using MediaBrowser.Model.Querying;
|
||||
|
||||
@ -33,7 +34,7 @@ namespace MediaBrowser.Server.Implementations.Sorting
|
||||
/// <returns>DateTime.</returns>
|
||||
private int GetValue(BaseItem x)
|
||||
{
|
||||
var userdata = x.GetUserData(User, false);
|
||||
var userdata = UserManager.GetUserData(User.Id, x.UserDataId).Result;
|
||||
|
||||
return userdata == null ? 0 : userdata.PlayCount;
|
||||
}
|
||||
@ -46,5 +47,11 @@ namespace MediaBrowser.Server.Implementations.Sorting
|
||||
{
|
||||
get { return ItemSortBy.PlayCount; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the user manager.
|
||||
/// </summary>
|
||||
/// <value>The user manager.</value>
|
||||
public IUserManager UserManager { get; set; }
|
||||
}
|
||||
}
|
||||
|
@ -79,8 +79,8 @@ namespace MediaBrowser.Server.Implementations.Sqlite
|
||||
|
||||
string[] queries = {
|
||||
|
||||
"create table if not exists user_data (item_id GUID, user_id GUID, data BLOB)",
|
||||
"create unique index if not exists idx_user_data on user_data (item_id, user_id)",
|
||||
"create table if not exists userdata (id GUID, userId GUID, data BLOB)",
|
||||
"create unique index if not exists userdataindex on userdata (id, userId)",
|
||||
"create table if not exists schema_version (table_name primary key, version)",
|
||||
//pragmas
|
||||
"pragma temp_store = memory"
|
||||
@ -90,84 +90,101 @@ namespace MediaBrowser.Server.Implementations.Sqlite
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Save the user specific data associated with an item in the repo
|
||||
/// Saves the user data.
|
||||
/// </summary>
|
||||
/// <param name="item">The item.</param>
|
||||
/// <param name="userId">The user id.</param>
|
||||
/// <param name="userDataId">The user data id.</param>
|
||||
/// <param name="userData">The user data.</param>
|
||||
/// <param name="cancellationToken">The cancellation token.</param>
|
||||
/// <returns>Task.</returns>
|
||||
/// <exception cref="System.ArgumentNullException">item</exception>
|
||||
public Task SaveUserData(BaseItem item, CancellationToken cancellationToken)
|
||||
/// <exception cref="System.ArgumentNullException">
|
||||
/// userData
|
||||
/// or
|
||||
/// cancellationToken
|
||||
/// or
|
||||
/// userId
|
||||
/// or
|
||||
/// userDataId
|
||||
/// </exception>
|
||||
public Task SaveUserData(Guid userId, Guid userDataId, UserItemData userData, CancellationToken cancellationToken)
|
||||
{
|
||||
if (item == null)
|
||||
if (userData == null)
|
||||
{
|
||||
throw new ArgumentNullException("item");
|
||||
throw new ArgumentNullException("userData");
|
||||
}
|
||||
|
||||
if (cancellationToken == null)
|
||||
{
|
||||
throw new ArgumentNullException("cancellationToken");
|
||||
}
|
||||
|
||||
if (userId == Guid.Empty)
|
||||
{
|
||||
throw new ArgumentNullException("userId");
|
||||
}
|
||||
if (userDataId == Guid.Empty)
|
||||
{
|
||||
throw new ArgumentNullException("userDataId");
|
||||
}
|
||||
|
||||
cancellationToken.ThrowIfCancellationRequested();
|
||||
|
||||
return Task.Run(() =>
|
||||
{
|
||||
var serialized = _protobufSerializer.SerializeToBytes(userData);
|
||||
|
||||
cancellationToken.ThrowIfCancellationRequested();
|
||||
|
||||
var cmd = connection.CreateCommand();
|
||||
|
||||
cmd.CommandText = "delete from user_data where item_id = @guid";
|
||||
cmd.AddParam("@guid", item.UserDataId);
|
||||
|
||||
cmd.CommandText = "replace into userdata (id, userId, data) values (@1, @2, @3)";
|
||||
cmd.AddParam("@1", userDataId);
|
||||
cmd.AddParam("@2", userId);
|
||||
cmd.AddParam("@3", serialized);
|
||||
QueueCommand(cmd);
|
||||
|
||||
if (item.UserData != null)
|
||||
{
|
||||
foreach (var data in item.UserData)
|
||||
{
|
||||
cmd = connection.CreateCommand();
|
||||
cmd.CommandText = "insert into user_data (item_id, user_id, data) values (@1, @2, @3)";
|
||||
cmd.AddParam("@1", item.UserDataId);
|
||||
cmd.AddParam("@2", data.UserId);
|
||||
|
||||
cmd.AddParam("@3", _protobufSerializer.SerializeToBytes(data));
|
||||
|
||||
QueueCommand(cmd);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets user data for an item
|
||||
/// Gets the user data.
|
||||
/// </summary>
|
||||
/// <param name="item">The item.</param>
|
||||
/// <returns>IEnumerable{UserItemData}.</returns>
|
||||
/// <exception cref="System.ArgumentNullException">item</exception>
|
||||
public IEnumerable<UserItemData> RetrieveUserData(BaseItem item)
|
||||
/// <param name="userId">The user id.</param>
|
||||
/// <param name="userDataId">The user data id.</param>
|
||||
/// <returns>Task{UserItemData}.</returns>
|
||||
/// <exception cref="System.ArgumentNullException">
|
||||
/// userId
|
||||
/// or
|
||||
/// userDataId
|
||||
/// </exception>
|
||||
public async Task<UserItemData> GetUserData(Guid userId, Guid userDataId)
|
||||
{
|
||||
if (item == null)
|
||||
if (userId == Guid.Empty)
|
||||
{
|
||||
throw new ArgumentNullException("item");
|
||||
throw new ArgumentNullException("userId");
|
||||
}
|
||||
if (userDataId == Guid.Empty)
|
||||
{
|
||||
throw new ArgumentNullException("userDataId");
|
||||
}
|
||||
|
||||
var cmd = connection.CreateCommand();
|
||||
cmd.CommandText = "select data from user_data where item_id = @guid";
|
||||
var guidParam = cmd.Parameters.Add("@guid", DbType.Guid);
|
||||
guidParam.Value = item.UserDataId;
|
||||
cmd.CommandText = "select data from userdata where id = @id and userId=@userId";
|
||||
|
||||
using (var reader = cmd.ExecuteReader(CommandBehavior.SequentialAccess | CommandBehavior.SingleResult))
|
||||
var idParam = cmd.Parameters.Add("@id", DbType.Guid);
|
||||
idParam.Value = userDataId;
|
||||
|
||||
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<UserItemData>(stream);
|
||||
if (data != null)
|
||||
{
|
||||
yield return data;
|
||||
}
|
||||
return _protobufSerializer.DeserializeFromStream<UserItemData>(stream);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user