jellyfin/Emby.Server.Implementations/Library/UserDataManager.cs

289 lines
9.4 KiB
C#
Raw Normal View History

2015-05-30 01:51:33 +02:00
using MediaBrowser.Common.Events;
2014-12-26 18:45:06 +01:00
using MediaBrowser.Controller.Configuration;
2013-10-02 21:08:58 +02:00
using MediaBrowser.Controller.Entities;
2014-12-26 18:45:06 +01:00
using MediaBrowser.Controller.Entities.Audio;
2013-10-02 18:08:58 +02:00
using MediaBrowser.Controller.Library;
using MediaBrowser.Controller.Persistence;
using MediaBrowser.Model.Dto;
2013-10-02 19:23:10 +02:00
using MediaBrowser.Model.Entities;
2013-10-02 18:58:30 +02:00
using MediaBrowser.Model.Logging;
2013-10-02 18:08:58 +02:00
using System;
2013-10-02 18:58:30 +02:00
using System.Collections.Concurrent;
2015-05-30 01:51:33 +02:00
using System.Collections.Generic;
2013-10-02 18:08:58 +02:00
using System.Threading;
using System.Threading.Tasks;
2016-12-13 08:36:30 +01:00
using MediaBrowser.Model.Querying;
2013-10-02 18:08:58 +02:00
2016-11-03 08:14:14 +01:00
namespace Emby.Server.Implementations.Library
2013-10-02 18:08:58 +02:00
{
/// <summary>
/// Class UserDataManager
/// </summary>
public class UserDataManager : IUserDataManager
{
2013-10-02 21:08:58 +02:00
public event EventHandler<UserDataSaveEventArgs> UserDataSaved;
2016-06-04 02:15:14 +02:00
private readonly ConcurrentDictionary<string, UserItemData> _userData =
new ConcurrentDictionary<string, UserItemData>(StringComparer.OrdinalIgnoreCase);
2013-10-02 18:58:30 +02:00
private readonly ILogger _logger;
2014-12-26 18:45:06 +01:00
private readonly IServerConfigurationManager _config;
2013-10-02 18:58:30 +02:00
2014-12-26 18:45:06 +01:00
public UserDataManager(ILogManager logManager, IServerConfigurationManager config)
2013-10-02 18:58:30 +02:00
{
2014-12-26 18:45:06 +01:00
_config = config;
2013-10-02 18:58:30 +02:00
_logger = logManager.GetLogger(GetType().Name);
}
2013-10-02 18:08:58 +02:00
/// <summary>
/// Gets or sets the repository.
/// </summary>
/// <value>The repository.</value>
public IUserDataRepository Repository { get; set; }
2013-12-19 22:51:32 +01:00
public async Task SaveUserData(Guid userId, IHasUserData item, UserItemData userData, UserDataSaveReason reason, CancellationToken cancellationToken)
2013-10-02 18:08:58 +02:00
{
2013-10-02 18:58:30 +02:00
if (userData == null)
{
throw new ArgumentNullException("userData");
}
2013-10-23 18:03:12 +02:00
if (item == null)
2013-10-02 18:58:30 +02:00
{
2013-10-23 18:03:12 +02:00
throw new ArgumentNullException("item");
2013-10-02 18:58:30 +02:00
}
2013-10-23 18:03:12 +02:00
if (userId == Guid.Empty)
2013-10-02 18:58:30 +02:00
{
2013-10-23 18:03:12 +02:00
throw new ArgumentNullException("userId");
2013-10-02 18:58:30 +02:00
}
cancellationToken.ThrowIfCancellationRequested();
2016-05-01 01:05:21 +02:00
var keys = item.GetUserDataKeys();
2013-10-23 18:03:12 +02:00
2016-05-01 01:05:21 +02:00
foreach (var key in keys)
2013-10-02 18:58:30 +02:00
{
2016-10-07 17:08:13 +02:00
await Repository.SaveUserData(userId, key, userData, cancellationToken).ConfigureAwait(false);
2013-10-02 18:58:30 +02:00
}
2013-10-02 21:08:58 +02:00
2016-06-04 02:15:14 +02:00
var cacheKey = GetCacheKey(userId, item.Id);
_userData.AddOrUpdate(cacheKey, userData, (k, v) => userData);
2013-10-02 21:08:58 +02:00
EventHelper.FireEventIfNotNull(UserDataSaved, this, new UserDataSaveEventArgs
{
2016-05-01 01:05:21 +02:00
Keys = keys,
2013-10-02 21:08:58 +02:00
UserData = userData,
SaveReason = reason,
2013-10-23 18:03:12 +02:00
UserId = userId,
Item = item
2013-10-02 21:08:58 +02:00
}, _logger);
2013-10-02 18:08:58 +02:00
}
2014-09-21 00:14:16 +02:00
/// <summary>
/// Save the provided user data for the given user. Batch operation. Does not fire any events or update the cache.
/// </summary>
/// <param name="userId"></param>
/// <param name="userData"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public async Task SaveAllUserData(Guid userId, IEnumerable<UserItemData> userData, CancellationToken cancellationToken)
{
if (userData == null)
{
throw new ArgumentNullException("userData");
}
if (userId == Guid.Empty)
{
throw new ArgumentNullException("userId");
}
cancellationToken.ThrowIfCancellationRequested();
2016-10-07 17:08:13 +02:00
await Repository.SaveAllUserData(userId, userData, cancellationToken).ConfigureAwait(false);
2014-09-21 00:14:16 +02:00
}
/// <summary>
/// Retrieve all user data for the given user
/// </summary>
/// <param name="userId"></param>
/// <returns></returns>
public IEnumerable<UserItemData> GetAllUserData(Guid userId)
{
if (userId == Guid.Empty)
{
throw new ArgumentNullException("userId");
}
return Repository.GetAllUserData(userId);
}
2016-06-04 02:15:14 +02:00
public UserItemData GetUserData(Guid userId, Guid itemId, List<string> keys)
2016-05-11 16:36:28 +02:00
{
if (userId == Guid.Empty)
{
throw new ArgumentNullException("userId");
}
if (keys == null)
{
throw new ArgumentNullException("keys");
}
2016-06-04 02:15:14 +02:00
if (keys.Count == 0)
{
throw new ArgumentException("UserData keys cannot be empty.");
}
var cacheKey = GetCacheKey(userId, itemId);
return _userData.GetOrAdd(cacheKey, k => GetUserDataInternal(userId, keys));
}
2016-05-11 16:36:28 +02:00
2016-06-04 02:15:14 +02:00
private UserItemData GetUserDataInternal(Guid userId, List<string> keys)
{
var userData = Repository.GetUserData(userId, keys);
if (userData != null)
2016-05-11 16:36:28 +02:00
{
2016-06-04 02:15:14 +02:00
return userData;
2016-05-24 18:58:36 +02:00
}
2016-05-11 16:36:28 +02:00
2016-05-24 18:58:36 +02:00
if (keys.Count > 0)
{
return new UserItemData
2016-05-11 16:36:28 +02:00
{
2016-05-24 18:58:36 +02:00
UserId = userId,
Key = keys[0]
};
2016-05-11 16:36:28 +02:00
}
2016-05-24 18:58:36 +02:00
return null;
2016-05-11 16:36:28 +02:00
}
2013-10-02 18:58:30 +02:00
/// <summary>
/// Gets the internal key.
/// </summary>
/// <returns>System.String.</returns>
2016-06-04 02:15:14 +02:00
private string GetCacheKey(Guid userId, Guid itemId)
2013-10-02 18:58:30 +02:00
{
2016-06-04 02:15:14 +02:00
return userId.ToString("N") + itemId.ToString("N");
2013-10-02 18:08:58 +02:00
}
2016-05-01 00:05:13 +02:00
public UserItemData GetUserData(IHasUserData user, IHasUserData item)
{
2016-05-11 16:36:28 +02:00
return GetUserData(user.Id, item);
2016-05-01 00:05:13 +02:00
}
public UserItemData GetUserData(string userId, IHasUserData item)
{
2016-05-11 16:36:28 +02:00
return GetUserData(new Guid(userId), item);
2016-05-01 00:05:13 +02:00
}
public UserItemData GetUserData(Guid userId, IHasUserData item)
{
2016-06-04 02:15:14 +02:00
return GetUserData(userId, item.Id, item.GetUserDataKeys());
2016-05-01 00:05:13 +02:00
}
2017-05-26 08:48:54 +02:00
public UserItemDataDto GetUserDataDto(IHasUserData item, User user)
{
2016-05-11 16:36:28 +02:00
var userData = GetUserData(user.Id, item);
var dto = GetUserItemDataDto(userData);
2017-08-19 21:43:35 +02:00
item.FillUserDataDtoValues(dto, userData, null, user, new ItemFields[]{});
2016-06-19 08:18:29 +02:00
return dto;
}
2017-08-19 21:43:35 +02:00
public UserItemDataDto GetUserDataDto(IHasUserData item, BaseItemDto itemDto, User user, ItemFields[] fields)
2016-06-19 08:18:29 +02:00
{
var userData = GetUserData(user.Id, item);
var dto = GetUserItemDataDto(userData);
2017-05-26 08:48:54 +02:00
item.FillUserDataDtoValues(dto, userData, itemDto, user, fields);
return dto;
}
/// <summary>
/// Converts a UserItemData to a DTOUserItemData
/// </summary>
/// <param name="data">The data.</param>
/// <returns>DtoUserItemData.</returns>
/// <exception cref="System.ArgumentNullException"></exception>
private UserItemDataDto GetUserItemDataDto(UserItemData data)
{
if (data == null)
{
throw new ArgumentNullException("data");
}
return new UserItemDataDto
{
IsFavorite = data.IsFavorite,
Likes = data.Likes,
PlaybackPositionTicks = data.PlaybackPositionTicks,
PlayCount = data.PlayCount,
Rating = data.Rating,
Played = data.Played,
LastPlayedDate = data.LastPlayedDate,
Key = data.Key
};
}
2014-12-26 18:45:06 +01:00
2015-02-17 19:42:46 +01:00
public bool UpdatePlayState(BaseItem item, UserItemData data, long? reportedPositionTicks)
2014-12-26 18:45:06 +01:00
{
var playedToCompletion = false;
2015-02-17 19:42:46 +01:00
var positionTicks = reportedPositionTicks ?? item.RunTimeTicks ?? 0;
2014-12-26 18:45:06 +01:00
var hasRuntime = item.RunTimeTicks.HasValue && item.RunTimeTicks > 0;
// If a position has been reported, and if we know the duration
if (positionTicks > 0 && hasRuntime)
{
var pctIn = Decimal.Divide(positionTicks, item.RunTimeTicks.Value) * 100;
// Don't track in very beginning
if (pctIn < _config.Configuration.MinResumePct)
{
positionTicks = 0;
}
// If we're at the end, assume completed
else if (pctIn > _config.Configuration.MaxResumePct || positionTicks >= item.RunTimeTicks.Value)
{
positionTicks = 0;
data.Played = playedToCompletion = true;
}
else
{
// Enforce MinResumeDuration
var durationSeconds = TimeSpan.FromTicks(item.RunTimeTicks.Value).TotalSeconds;
if (durationSeconds < _config.Configuration.MinResumeDurationSeconds)
{
positionTicks = 0;
data.Played = playedToCompletion = true;
}
}
}
else if (!hasRuntime)
{
// If we don't know the runtime we'll just have to assume it was fully played
data.Played = playedToCompletion = true;
positionTicks = 0;
}
2016-10-11 08:46:59 +02:00
if (!item.SupportsPlayedStatus)
2014-12-26 18:45:06 +01:00
{
positionTicks = 0;
2016-10-11 08:46:59 +02:00
data.Played = false;
2014-12-26 18:45:06 +01:00
}
2016-12-12 06:49:19 +01:00
if (!item.SupportsPositionTicksResume)
2016-10-11 23:33:38 +02:00
{
positionTicks = 0;
}
2014-12-26 18:45:06 +01:00
data.PlaybackPositionTicks = positionTicks;
return playedToCompletion;
}
2013-10-02 18:08:58 +02:00
}
}