Clean up UserDataManager

* enable nullable
* remove unused methods
* fix warnings and docs
This commit is contained in:
Bond_009 2024-08-30 15:08:56 +02:00
parent 9644e894f0
commit 277830855d
5 changed files with 56 additions and 100 deletions

View File

@ -133,7 +133,8 @@ namespace Emby.Server.Implementations.EntryPoints
private UserDataChangeInfo GetUserDataChangeInfo(Guid userId, List<BaseItem> changedItems)
{
var user = _userManager.GetUserById(userId);
var user = _userManager.GetUserById(userId)
?? throw new ArgumentException("Invalid user ID", nameof(userId));
return new UserDataChangeInfo
{

View File

@ -1,10 +1,7 @@
#nullable disable
#pragma warning disable CS1591
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.Threading;
using Jellyfin.Data.Entities;
@ -32,6 +29,12 @@ namespace Emby.Server.Implementations.Library
private readonly IUserManager _userManager;
private readonly IUserDataRepository _repository;
/// <summary>
/// Initializes a new instance of the <see cref="UserDataManager"/> class.
/// </summary>
/// <param name="config">Instance of the <see cref="IServerConfigurationManager"/> interface.</param>
/// <param name="userManager">Instance of the <see cref="IUserManager"/> interface.</param>
/// <param name="repository">Instance of the <see cref="IUserDataRepository"/> interface.</param>
public UserDataManager(
IServerConfigurationManager config,
IUserManager userManager,
@ -42,15 +45,10 @@ namespace Emby.Server.Implementations.Library
_repository = repository;
}
public event EventHandler<UserDataSaveEventArgs> UserDataSaved;
public void SaveUserData(Guid userId, BaseItem item, UserItemData userData, UserDataSaveReason reason, CancellationToken cancellationToken)
{
var user = _userManager.GetUserById(userId);
SaveUserData(user, item, userData, reason, cancellationToken);
}
/// <inheritdoc />
public event EventHandler<UserDataSaveEventArgs>? UserDataSaved;
/// <inheritdoc />
public void SaveUserData(User user, BaseItem item, UserItemData userData, UserDataSaveReason reason, CancellationToken cancellationToken)
{
ArgumentNullException.ThrowIfNull(userData);
@ -81,6 +79,7 @@ namespace Emby.Server.Implementations.Library
});
}
/// <inheritdoc />
public void SaveUserData(User user, BaseItem item, UpdateUserItemDataDto userDataDto, UserDataSaveReason reason)
{
ArgumentNullException.ThrowIfNull(user);
@ -128,39 +127,7 @@ namespace Emby.Server.Implementations.Library
SaveUserData(user, item, userData, reason, CancellationToken.None);
}
/// <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">The user id.</param>
/// <param name="userData">The user item data.</param>
/// <param name="cancellationToken">The cancellation token.</param>
public void SaveAllUserData(Guid userId, UserItemData[] userData, CancellationToken cancellationToken)
{
var user = _userManager.GetUserById(userId);
_repository.SaveAllUserData(user.InternalId, userData, cancellationToken);
}
/// <summary>
/// Retrieve all user data for the given user.
/// </summary>
/// <param name="userId">The user id.</param>
/// <returns>A <see cref="List{UserItemData}"/> containing all of the user's item data.</returns>
public List<UserItemData> GetAllUserData(Guid userId)
{
var user = _userManager.GetUserById(userId);
return _repository.GetAllUserData(user.InternalId);
}
public UserItemData GetUserData(Guid userId, Guid itemId, List<string> keys)
{
var user = _userManager.GetUserById(userId);
return GetUserData(user, itemId, keys);
}
public UserItemData GetUserData(User user, Guid itemId, List<string> keys)
private UserItemData GetUserData(User user, Guid itemId, List<string> keys)
{
var userId = user.InternalId;
@ -186,7 +153,7 @@ namespace Emby.Server.Implementations.Library
};
}
return null;
throw new UnreachableException();
}
/// <summary>
@ -198,27 +165,18 @@ namespace Emby.Server.Implementations.Library
return internalUserId.ToString(CultureInfo.InvariantCulture) + "-" + itemId.ToString("N", CultureInfo.InvariantCulture);
}
/// <inheritdoc />
public UserItemData GetUserData(User user, BaseItem item)
{
return GetUserData(user, item.Id, item.GetUserDataKeys());
}
public UserItemData GetUserData(Guid userId, BaseItem item)
{
return GetUserData(userId, item.Id, item.GetUserDataKeys());
}
/// <inheritdoc />
public UserItemDataDto GetUserDataDto(BaseItem item, User user)
{
var userData = GetUserData(user, item);
var dto = GetUserItemDataDto(userData);
item.FillUserDataDtoValues(dto, userData, null, user, new DtoOptions());
return dto;
}
=> GetUserDataDto(item, null, user, new DtoOptions());
/// <inheritdoc />
public UserItemDataDto GetUserDataDto(BaseItem item, BaseItemDto itemDto, User user, DtoOptions options)
public UserItemDataDto GetUserDataDto(BaseItem item, BaseItemDto? itemDto, User user, DtoOptions options)
{
var userData = GetUserData(user, item);
var dto = GetUserItemDataDto(userData);

View File

@ -1732,12 +1732,9 @@ namespace MediaBrowser.Controller.Entities
return;
}
if (itemDto is not null)
if (itemDto is not null && fields.ContainsField(ItemFields.RecursiveItemCount))
{
if (fields.ContainsField(ItemFields.RecursiveItemCount))
{
itemDto.RecursiveItemCount = GetRecursiveChildCount(user);
}
itemDto.RecursiveItemCount = GetRecursiveChildCount(user);
}
if (SupportsPlayedStatus)

View File

@ -1,7 +1,3 @@
#nullable disable
#pragma warning disable CA1002, CA1707, CS1591
using System;
using System.Collections.Generic;
using System.Threading;
@ -21,18 +17,16 @@ namespace MediaBrowser.Controller.Library
/// <summary>
/// Occurs when [user data saved].
/// </summary>
event EventHandler<UserDataSaveEventArgs> UserDataSaved;
event EventHandler<UserDataSaveEventArgs>? UserDataSaved;
/// <summary>
/// Saves the user data.
/// </summary>
/// <param name="userId">The user id.</param>
/// <param name="user">The user.</param>
/// <param name="item">The item.</param>
/// <param name="userData">The user data.</param>
/// <param name="reason">The reason.</param>
/// <param name="cancellationToken">The cancellation token.</param>
void SaveUserData(Guid userId, BaseItem item, UserItemData userData, UserDataSaveReason reason, CancellationToken cancellationToken);
void SaveUserData(User user, BaseItem item, UserItemData userData, UserDataSaveReason reason, CancellationToken cancellationToken);
/// <summary>
@ -44,10 +38,14 @@ namespace MediaBrowser.Controller.Library
/// <param name="reason">The reason.</param>
void SaveUserData(User user, BaseItem item, UpdateUserItemDataDto userDataDto, UserDataSaveReason reason);
/// <summary>
/// Gets the user data.
/// </summary>
/// <param name="user">User to use.</param>
/// <param name="item">Item to use.</param>
/// <returns>User data.</returns>
UserItemData GetUserData(User user, BaseItem item);
UserItemData GetUserData(Guid userId, BaseItem item);
/// <summary>
/// Gets the user data dto.
/// </summary>
@ -56,22 +54,15 @@ namespace MediaBrowser.Controller.Library
/// <returns>User data dto.</returns>
UserItemDataDto GetUserDataDto(BaseItem item, User user);
UserItemDataDto GetUserDataDto(BaseItem item, BaseItemDto itemDto, User user, DtoOptions options);
/// <summary>
/// Get all user data for the given user.
/// Gets the user data dto.
/// </summary>
/// <param name="userId">The user id.</param>
/// <returns>The user item data.</returns>
List<UserItemData> GetAllUserData(Guid userId);
/// <summary>
/// Save the all provided user data for the given user.
/// </summary>
/// <param name="userId">The user id.</param>
/// <param name="userData">The array of user data.</param>
/// <param name="cancellationToken">The cancellation token.</param>
void SaveAllUserData(Guid userId, UserItemData[] userData, CancellationToken cancellationToken);
/// <param name="item">Item to use.</param>
/// <param name="itemDto">Item dto to use.</param>
/// <param name="user">User to use.</param>
/// <param name="options">Dto options to use.</param>
/// <returns>User data dto.</returns>
UserItemDataDto GetUserDataDto(BaseItem item, BaseItemDto? itemDto, User user, DtoOptions options);
/// <summary>
/// Updates playstate for an item and returns true or false indicating if it was played to completion.

View File

@ -306,12 +306,15 @@ namespace MediaBrowser.XbmcMetadata.Parsers
break;
case "watched":
var played = reader.ReadElementContentAsBoolean();
if (!string.IsNullOrWhiteSpace(nfoConfiguration.UserId))
if (Guid.TryParse(nfoConfiguration.UserId, out var userId))
{
var user = _userManager.GetUserById(Guid.Parse(nfoConfiguration.UserId));
userData = _userDataManager.GetUserData(user, item);
userData.Played = played;
_userDataManager.SaveUserData(user, item, userData, UserDataSaveReason.Import, CancellationToken.None);
var user = _userManager.GetUserById(userId);
if (user is not null)
{
userData = _userDataManager.GetUserData(user, item);
userData.Played = played;
_userDataManager.SaveUserData(user, item, userData, UserDataSaveReason.Import, CancellationToken.None);
}
}
break;
@ -320,9 +323,12 @@ namespace MediaBrowser.XbmcMetadata.Parsers
&& Guid.TryParse(nfoConfiguration.UserId, out var playCountUserId))
{
var user = _userManager.GetUserById(playCountUserId);
userData = _userDataManager.GetUserData(user, item);
userData.PlayCount = count;
_userDataManager.SaveUserData(user, item, userData, UserDataSaveReason.Import, CancellationToken.None);
if (user is not null)
{
userData = _userDataManager.GetUserData(user, item);
userData.PlayCount = count;
_userDataManager.SaveUserData(user, item, userData, UserDataSaveReason.Import, CancellationToken.None);
}
}
break;
@ -331,9 +337,12 @@ namespace MediaBrowser.XbmcMetadata.Parsers
&& Guid.TryParse(nfoConfiguration.UserId, out var lastPlayedUserId))
{
var user = _userManager.GetUserById(lastPlayedUserId);
userData = _userDataManager.GetUserData(user, item);
userData.LastPlayedDate = lastPlayed;
_userDataManager.SaveUserData(user, item, userData, UserDataSaveReason.Import, CancellationToken.None);
if (user is not null)
{
userData = _userDataManager.GetUserData(user, item);
userData.LastPlayedDate = lastPlayed;
_userDataManager.SaveUserData(user, item, userData, UserDataSaveReason.Import, CancellationToken.None);
}
}
break;