2021-05-20 19:28:18 +00:00
|
|
|
#nullable disable
|
|
|
|
|
2019-11-01 17:38:54 +00:00
|
|
|
#pragma warning disable CS1591
|
|
|
|
|
2019-01-13 19:54:44 +00:00
|
|
|
using System;
|
2019-01-13 19:21:32 +00:00
|
|
|
using System.Collections.Concurrent;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Globalization;
|
|
|
|
using System.Threading;
|
2020-05-20 17:07:53 +00:00
|
|
|
using Jellyfin.Data.Entities;
|
2019-01-13 19:21:32 +00:00
|
|
|
using MediaBrowser.Controller.Configuration;
|
|
|
|
using MediaBrowser.Controller.Dto;
|
2013-10-02 19:08:58 +00:00
|
|
|
using MediaBrowser.Controller.Entities;
|
2013-10-02 16:08:58 +00:00
|
|
|
using MediaBrowser.Controller.Library;
|
|
|
|
using MediaBrowser.Controller.Persistence;
|
2014-07-04 02:22:57 +00:00
|
|
|
using MediaBrowser.Model.Dto;
|
2013-10-02 17:23:10 +00:00
|
|
|
using MediaBrowser.Model.Entities;
|
2020-12-30 13:48:33 +00:00
|
|
|
using AudioBook = MediaBrowser.Controller.Entities.AudioBook;
|
2021-04-17 10:37:55 +00:00
|
|
|
using Book = MediaBrowser.Controller.Entities.Book;
|
2013-10-02 16:08:58 +00:00
|
|
|
|
2016-11-03 07:14:14 +00:00
|
|
|
namespace Emby.Server.Implementations.Library
|
2013-10-02 16:08:58 +00:00
|
|
|
{
|
|
|
|
/// <summary>
|
2019-11-01 17:38:54 +00:00
|
|
|
/// Class UserDataManager.
|
2013-10-02 16:08:58 +00:00
|
|
|
/// </summary>
|
|
|
|
public class UserDataManager : IUserDataManager
|
|
|
|
{
|
2016-06-04 00:15:14 +00:00
|
|
|
private readonly ConcurrentDictionary<string, UserItemData> _userData =
|
|
|
|
new ConcurrentDictionary<string, UserItemData>(StringComparer.OrdinalIgnoreCase);
|
2013-10-02 16:58:30 +00:00
|
|
|
|
2014-12-26 17:45:06 +00:00
|
|
|
private readonly IServerConfigurationManager _config;
|
2020-04-04 23:57:26 +00:00
|
|
|
private readonly IUserManager _userManager;
|
|
|
|
private readonly IUserDataRepository _repository;
|
|
|
|
|
|
|
|
public UserDataManager(
|
|
|
|
IServerConfigurationManager config,
|
|
|
|
IUserManager userManager,
|
|
|
|
IUserDataRepository repository)
|
2013-10-02 16:58:30 +00:00
|
|
|
{
|
2014-12-26 17:45:06 +00:00
|
|
|
_config = config;
|
2018-09-12 17:26:21 +00:00
|
|
|
_userManager = userManager;
|
2020-04-04 23:57:26 +00:00
|
|
|
_repository = repository;
|
2013-10-02 16:58:30 +00:00
|
|
|
}
|
|
|
|
|
2021-10-02 17:59:58 +00:00
|
|
|
public event EventHandler<UserDataSaveEventArgs> UserDataSaved;
|
|
|
|
|
2018-09-12 17:26:21 +00:00
|
|
|
public void SaveUserData(Guid userId, BaseItem item, UserItemData userData, UserDataSaveReason reason, CancellationToken cancellationToken)
|
|
|
|
{
|
2020-04-04 23:57:26 +00:00
|
|
|
var user = _userManager.GetUserById(userId);
|
2018-09-12 17:26:21 +00:00
|
|
|
|
|
|
|
SaveUserData(user, item, userData, reason, cancellationToken);
|
|
|
|
}
|
|
|
|
|
2020-05-20 17:07:53 +00:00
|
|
|
public void SaveUserData(User user, BaseItem item, UserItemData userData, UserDataSaveReason reason, CancellationToken cancellationToken)
|
2013-10-02 16:08:58 +00:00
|
|
|
{
|
2022-10-06 18:21:23 +00:00
|
|
|
ArgumentNullException.ThrowIfNull(userData);
|
2019-11-17 22:05:39 +00:00
|
|
|
|
2022-10-06 18:21:23 +00:00
|
|
|
ArgumentNullException.ThrowIfNull(item);
|
2013-10-02 16:58:30 +00:00
|
|
|
|
|
|
|
cancellationToken.ThrowIfCancellationRequested();
|
|
|
|
|
2016-04-30 23:05:21 +00:00
|
|
|
var keys = item.GetUserDataKeys();
|
2013-10-23 16:03:12 +00:00
|
|
|
|
2018-09-12 17:26:21 +00:00
|
|
|
var userId = user.InternalId;
|
|
|
|
|
2016-04-30 23:05:21 +00:00
|
|
|
foreach (var key in keys)
|
2013-10-02 16:58:30 +00:00
|
|
|
{
|
2020-04-04 23:57:26 +00:00
|
|
|
_repository.SaveUserData(userId, key, userData, cancellationToken);
|
2013-10-02 16:58:30 +00:00
|
|
|
}
|
2013-10-02 19:08:58 +00:00
|
|
|
|
2016-06-04 00:15:14 +00:00
|
|
|
var cacheKey = GetCacheKey(userId, item.Id);
|
2021-12-15 17:25:36 +00:00
|
|
|
_userData.AddOrUpdate(cacheKey, userData, (_, _) => userData);
|
2016-06-04 00:15:14 +00:00
|
|
|
|
2018-12-28 14:21:02 +00:00
|
|
|
UserDataSaved?.Invoke(this, new UserDataSaveEventArgs
|
2013-10-02 19:08:58 +00:00
|
|
|
{
|
2016-04-30 23:05:21 +00:00
|
|
|
Keys = keys,
|
2013-10-02 19:08:58 +00:00
|
|
|
UserData = userData,
|
|
|
|
SaveReason = reason,
|
2018-09-12 17:26:21 +00:00
|
|
|
UserId = user.Id,
|
2013-10-23 16:03:12 +00:00
|
|
|
Item = item
|
2018-12-28 14:21:02 +00:00
|
|
|
});
|
2013-10-02 16:08:58 +00:00
|
|
|
}
|
|
|
|
|
2023-11-15 10:55:14 +00:00
|
|
|
public void SaveUserData(User user, BaseItem item, UpdateUserItemDataDto userDataDto, UserDataSaveReason reason)
|
2023-11-13 12:51:06 +00:00
|
|
|
{
|
|
|
|
ArgumentNullException.ThrowIfNull(user);
|
|
|
|
ArgumentNullException.ThrowIfNull(item);
|
|
|
|
ArgumentNullException.ThrowIfNull(reason);
|
|
|
|
ArgumentNullException.ThrowIfNull(userDataDto);
|
|
|
|
|
|
|
|
var userData = GetUserData(user, item);
|
|
|
|
|
2023-11-13 14:32:24 +00:00
|
|
|
if (userDataDto.PlaybackPositionTicks.HasValue)
|
|
|
|
{
|
|
|
|
userData.PlaybackPositionTicks = userDataDto.PlaybackPositionTicks.Value;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (userDataDto.PlayCount.HasValue)
|
|
|
|
{
|
|
|
|
userData.PlayCount = userDataDto.PlayCount.Value;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (userDataDto.IsFavorite.HasValue)
|
|
|
|
{
|
|
|
|
userData.IsFavorite = userDataDto.IsFavorite.Value;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (userDataDto.Likes.HasValue)
|
|
|
|
{
|
|
|
|
userData.Likes = userDataDto.Likes.Value;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (userDataDto.Played.HasValue)
|
|
|
|
{
|
|
|
|
userData.Played = userDataDto.Played.Value;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (userDataDto.LastPlayedDate.HasValue)
|
|
|
|
{
|
|
|
|
userData.LastPlayedDate = userDataDto.LastPlayedDate.Value;
|
|
|
|
}
|
2023-11-13 12:51:06 +00:00
|
|
|
|
2023-11-13 14:32:24 +00:00
|
|
|
if (userDataDto.Rating.HasValue)
|
2023-11-13 12:51:06 +00:00
|
|
|
{
|
2023-11-13 14:32:24 +00:00
|
|
|
userData.Rating = userDataDto.Rating.Value;
|
2023-11-13 12:51:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
SaveUserData(user, item, userData, reason, CancellationToken.None);
|
|
|
|
}
|
|
|
|
|
2014-09-20 22:14:16 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Save the provided user data for the given user. Batch operation. Does not fire any events or update the cache.
|
|
|
|
/// </summary>
|
2021-10-02 17:59:58 +00:00
|
|
|
/// <param name="userId">The user id.</param>
|
|
|
|
/// <param name="userData">The user item data.</param>
|
|
|
|
/// <param name="cancellationToken">The cancellation token.</param>
|
2017-08-27 00:32:33 +00:00
|
|
|
public void SaveAllUserData(Guid userId, UserItemData[] userData, CancellationToken cancellationToken)
|
2014-09-20 22:14:16 +00:00
|
|
|
{
|
2020-04-04 23:57:26 +00:00
|
|
|
var user = _userManager.GetUserById(userId);
|
2014-09-20 22:14:16 +00:00
|
|
|
|
2020-04-04 23:57:26 +00:00
|
|
|
_repository.SaveAllUserData(user.InternalId, userData, cancellationToken);
|
2014-09-20 22:14:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
2020-06-15 22:37:52 +00:00
|
|
|
/// Retrieve all user data for the given user.
|
2014-09-20 22:14:16 +00:00
|
|
|
/// </summary>
|
2021-10-02 17:59:58 +00:00
|
|
|
/// <param name="userId">The user id.</param>
|
|
|
|
/// <returns>A <see cref="List{UserItemData}"/> containing all of the user's item data.</returns>
|
2017-08-27 00:32:33 +00:00
|
|
|
public List<UserItemData> GetAllUserData(Guid userId)
|
2014-09-20 22:14:16 +00:00
|
|
|
{
|
2020-04-04 23:57:26 +00:00
|
|
|
var user = _userManager.GetUserById(userId);
|
2014-09-20 22:14:16 +00:00
|
|
|
|
2020-04-04 23:57:26 +00:00
|
|
|
return _repository.GetAllUserData(user.InternalId);
|
2014-09-20 22:14:16 +00:00
|
|
|
}
|
|
|
|
|
2016-06-04 00:15:14 +00:00
|
|
|
public UserItemData GetUserData(Guid userId, Guid itemId, List<string> keys)
|
2016-05-11 14:36:28 +00:00
|
|
|
{
|
2020-04-04 23:57:26 +00:00
|
|
|
var user = _userManager.GetUserById(userId);
|
2018-09-12 17:26:21 +00:00
|
|
|
|
|
|
|
return GetUserData(user, itemId, keys);
|
|
|
|
}
|
|
|
|
|
2020-05-20 17:07:53 +00:00
|
|
|
public UserItemData GetUserData(User user, Guid itemId, List<string> keys)
|
2018-09-12 17:26:21 +00:00
|
|
|
{
|
|
|
|
var userId = user.InternalId;
|
2016-06-04 00:15:14 +00:00
|
|
|
|
|
|
|
var cacheKey = GetCacheKey(userId, itemId);
|
|
|
|
|
2021-12-15 17:25:36 +00:00
|
|
|
return _userData.GetOrAdd(cacheKey, _ => GetUserDataInternal(userId, keys));
|
2016-06-04 00:15:14 +00:00
|
|
|
}
|
2016-05-11 14:36:28 +00:00
|
|
|
|
2018-09-12 17:26:21 +00:00
|
|
|
private UserItemData GetUserDataInternal(long internalUserId, List<string> keys)
|
2016-06-04 00:15:14 +00:00
|
|
|
{
|
2020-04-04 23:57:26 +00:00
|
|
|
var userData = _repository.GetUserData(internalUserId, keys);
|
2016-06-04 00:15:14 +00:00
|
|
|
|
2022-12-05 14:01:13 +00:00
|
|
|
if (userData is not null)
|
2016-05-11 14:36:28 +00:00
|
|
|
{
|
2016-06-04 00:15:14 +00:00
|
|
|
return userData;
|
2016-05-24 16:58:36 +00:00
|
|
|
}
|
2016-05-11 14:36:28 +00:00
|
|
|
|
2016-05-24 16:58:36 +00:00
|
|
|
if (keys.Count > 0)
|
|
|
|
{
|
|
|
|
return new UserItemData
|
2016-05-11 14:36:28 +00:00
|
|
|
{
|
2016-05-24 16:58:36 +00:00
|
|
|
Key = keys[0]
|
|
|
|
};
|
2016-05-11 14:36:28 +00:00
|
|
|
}
|
2016-05-24 16:58:36 +00:00
|
|
|
|
|
|
|
return null;
|
2016-05-11 14:36:28 +00:00
|
|
|
}
|
|
|
|
|
2013-10-02 16:58:30 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Gets the internal key.
|
|
|
|
/// </summary>
|
|
|
|
/// <returns>System.String.</returns>
|
2019-01-06 20:50:43 +00:00
|
|
|
private static string GetCacheKey(long internalUserId, Guid itemId)
|
2013-10-02 16:58:30 +00:00
|
|
|
{
|
2019-02-28 22:22:57 +00:00
|
|
|
return internalUserId.ToString(CultureInfo.InvariantCulture) + "-" + itemId.ToString("N", CultureInfo.InvariantCulture);
|
2013-10-02 16:08:58 +00:00
|
|
|
}
|
2014-07-04 02:22:57 +00:00
|
|
|
|
2020-05-20 17:07:53 +00:00
|
|
|
public UserItemData GetUserData(User user, BaseItem item)
|
2016-04-30 22:05:13 +00:00
|
|
|
{
|
2018-09-12 17:26:21 +00:00
|
|
|
return GetUserData(user, item.Id, item.GetUserDataKeys());
|
2016-04-30 22:05:13 +00:00
|
|
|
}
|
|
|
|
|
2018-09-12 17:26:21 +00:00
|
|
|
public UserItemData GetUserData(Guid userId, BaseItem item)
|
2016-04-30 22:05:13 +00:00
|
|
|
{
|
2016-06-04 00:15:14 +00:00
|
|
|
return GetUserData(userId, item.Id, item.GetUserDataKeys());
|
2016-04-30 22:05:13 +00:00
|
|
|
}
|
|
|
|
|
2020-05-20 17:07:53 +00:00
|
|
|
public UserItemDataDto GetUserDataDto(BaseItem item, User user)
|
2014-07-04 02:22:57 +00:00
|
|
|
{
|
2018-09-12 17:26:21 +00:00
|
|
|
var userData = GetUserData(user, item);
|
2014-07-04 02:22:57 +00:00
|
|
|
var dto = GetUserItemDataDto(userData);
|
|
|
|
|
2018-09-12 17:26:21 +00:00
|
|
|
item.FillUserDataDtoValues(dto, userData, null, user, new DtoOptions());
|
2016-06-19 06:18:29 +00:00
|
|
|
return dto;
|
|
|
|
}
|
|
|
|
|
2021-09-03 16:46:34 +00:00
|
|
|
/// <inheritdoc />
|
2020-05-20 17:07:53 +00:00
|
|
|
public UserItemDataDto GetUserDataDto(BaseItem item, BaseItemDto itemDto, User user, DtoOptions options)
|
2016-06-19 06:18:29 +00:00
|
|
|
{
|
2018-09-12 17:26:21 +00:00
|
|
|
var userData = GetUserData(user, item);
|
2016-06-19 06:18:29 +00:00
|
|
|
var dto = GetUserItemDataDto(userData);
|
|
|
|
|
2018-09-12 17:26:21 +00:00
|
|
|
item.FillUserDataDtoValues(dto, userData, itemDto, user, options);
|
2014-07-04 02:22:57 +00:00
|
|
|
return dto;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
2020-06-15 22:37:52 +00:00
|
|
|
/// Converts a UserItemData to a DTOUserItemData.
|
2014-07-04 02:22:57 +00:00
|
|
|
/// </summary>
|
|
|
|
/// <param name="data">The data.</param>
|
|
|
|
/// <returns>DtoUserItemData.</returns>
|
2021-09-03 16:46:34 +00:00
|
|
|
/// <exception cref="ArgumentNullException"><paramref name="data"/> is <c>null</c>.</exception>
|
2014-07-04 02:22:57 +00:00
|
|
|
private UserItemDataDto GetUserItemDataDto(UserItemData data)
|
|
|
|
{
|
2022-10-06 18:21:23 +00:00
|
|
|
ArgumentNullException.ThrowIfNull(data);
|
2014-07-04 02:22:57 +00:00
|
|
|
|
|
|
|
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 17:45:06 +00:00
|
|
|
|
2021-09-03 16:46:34 +00:00
|
|
|
/// <inheritdoc />
|
2015-02-17 18:42:46 +00:00
|
|
|
public bool UpdatePlayState(BaseItem item, UserItemData data, long? reportedPositionTicks)
|
2014-12-26 17:45:06 +00:00
|
|
|
{
|
|
|
|
var playedToCompletion = false;
|
|
|
|
|
2018-09-12 17:26:21 +00:00
|
|
|
var runtimeTicks = item.GetRunTimeTicksForPlayState();
|
|
|
|
|
|
|
|
var positionTicks = reportedPositionTicks ?? runtimeTicks;
|
|
|
|
var hasRuntime = runtimeTicks > 0;
|
2014-12-26 17:45:06 +00:00
|
|
|
|
|
|
|
// If a position has been reported, and if we know the duration
|
2021-05-07 09:35:03 +00:00
|
|
|
if (positionTicks > 0 && hasRuntime && item is not AudioBook && item is not Book)
|
2014-12-26 17:45:06 +00:00
|
|
|
{
|
2019-01-06 20:50:43 +00:00
|
|
|
var pctIn = decimal.Divide(positionTicks, runtimeTicks) * 100;
|
2014-12-26 17:45:06 +00:00
|
|
|
|
|
|
|
if (pctIn < _config.Configuration.MinResumePct)
|
|
|
|
{
|
2019-12-14 02:36:06 +00:00
|
|
|
// ignore progress during the beginning
|
2014-12-26 17:45:06 +00:00
|
|
|
positionTicks = 0;
|
|
|
|
}
|
2018-09-12 17:26:21 +00:00
|
|
|
else if (pctIn > _config.Configuration.MaxResumePct || positionTicks >= runtimeTicks)
|
2014-12-26 17:45:06 +00:00
|
|
|
{
|
2019-12-14 02:36:06 +00:00
|
|
|
// mark as completed close to the end
|
2014-12-26 17:45:06 +00:00
|
|
|
positionTicks = 0;
|
|
|
|
data.Played = playedToCompletion = true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Enforce MinResumeDuration
|
2018-09-12 17:26:21 +00:00
|
|
|
var durationSeconds = TimeSpan.FromTicks(runtimeTicks).TotalSeconds;
|
2021-05-06 18:50:00 +00:00
|
|
|
if (durationSeconds < _config.Configuration.MinResumeDurationSeconds)
|
2014-12-26 17:45:06 +00:00
|
|
|
{
|
|
|
|
positionTicks = 0;
|
|
|
|
data.Played = playedToCompletion = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-12-30 14:30:02 +00:00
|
|
|
else if (positionTicks > 0 && hasRuntime && item is AudioBook)
|
2020-12-30 13:48:33 +00:00
|
|
|
{
|
2021-04-21 11:11:14 +00:00
|
|
|
var playbackPositionInMinutes = TimeSpan.FromTicks(positionTicks).TotalMinutes;
|
|
|
|
var remainingTimeInMinutes = TimeSpan.FromTicks(runtimeTicks - positionTicks).TotalMinutes;
|
2020-12-30 13:48:33 +00:00
|
|
|
|
2021-04-21 11:11:14 +00:00
|
|
|
if (playbackPositionInMinutes < _config.Configuration.MinAudiobookResume)
|
2020-12-30 13:48:33 +00:00
|
|
|
{
|
|
|
|
// ignore progress during the beginning
|
|
|
|
positionTicks = 0;
|
|
|
|
}
|
2021-04-21 11:11:14 +00:00
|
|
|
else if (remainingTimeInMinutes < _config.Configuration.MaxAudiobookResume || positionTicks >= runtimeTicks)
|
2020-12-30 13:48:33 +00:00
|
|
|
{
|
|
|
|
// mark as completed close to the end
|
|
|
|
positionTicks = 0;
|
|
|
|
data.Played = playedToCompletion = true;
|
|
|
|
}
|
|
|
|
}
|
2014-12-26 17:45:06 +00:00
|
|
|
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 06:46:59 +00:00
|
|
|
if (!item.SupportsPlayedStatus)
|
2014-12-26 17:45:06 +00:00
|
|
|
{
|
|
|
|
positionTicks = 0;
|
2016-10-11 06:46:59 +00:00
|
|
|
data.Played = false;
|
2014-12-26 17:45:06 +00:00
|
|
|
}
|
2019-12-14 02:36:06 +00:00
|
|
|
|
2016-12-12 05:49:19 +00:00
|
|
|
if (!item.SupportsPositionTicksResume)
|
2016-10-11 21:33:38 +00:00
|
|
|
{
|
|
|
|
positionTicks = 0;
|
|
|
|
}
|
2014-12-26 17:45:06 +00:00
|
|
|
|
|
|
|
data.PlaybackPositionTicks = positionTicks;
|
|
|
|
|
|
|
|
return playedToCompletion;
|
|
|
|
}
|
2013-10-02 16:08:58 +00:00
|
|
|
}
|
|
|
|
}
|