From 277830855ddd9c7fa5a5db21f02d2d2f199f6cf8 Mon Sep 17 00:00:00 2001 From: Bond_009 Date: Fri, 30 Aug 2024 15:08:56 +0200 Subject: [PATCH] Clean up UserDataManager * enable nullable * remove unused methods * fix warnings and docs --- .../EntryPoints/UserDataChangeNotifier.cs | 3 +- .../Library/UserDataManager.cs | 76 +++++-------------- MediaBrowser.Controller/Entities/Folder.cs | 7 +- .../Library/IUserDataManager.cs | 39 ++++------ .../Parsers/BaseNfoParser.cs | 31 +++++--- 5 files changed, 56 insertions(+), 100 deletions(-) diff --git a/Emby.Server.Implementations/EntryPoints/UserDataChangeNotifier.cs b/Emby.Server.Implementations/EntryPoints/UserDataChangeNotifier.cs index 47f9dfbc8..aef02ce6b 100644 --- a/Emby.Server.Implementations/EntryPoints/UserDataChangeNotifier.cs +++ b/Emby.Server.Implementations/EntryPoints/UserDataChangeNotifier.cs @@ -133,7 +133,8 @@ namespace Emby.Server.Implementations.EntryPoints private UserDataChangeInfo GetUserDataChangeInfo(Guid userId, List changedItems) { - var user = _userManager.GetUserById(userId); + var user = _userManager.GetUserById(userId) + ?? throw new ArgumentException("Invalid user ID", nameof(userId)); return new UserDataChangeInfo { diff --git a/Emby.Server.Implementations/Library/UserDataManager.cs b/Emby.Server.Implementations/Library/UserDataManager.cs index 8beeb8041..62d22b23f 100644 --- a/Emby.Server.Implementations/Library/UserDataManager.cs +++ b/Emby.Server.Implementations/Library/UserDataManager.cs @@ -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; + /// + /// Initializes a new instance of the class. + /// + /// Instance of the interface. + /// Instance of the interface. + /// Instance of the interface. public UserDataManager( IServerConfigurationManager config, IUserManager userManager, @@ -42,15 +45,10 @@ namespace Emby.Server.Implementations.Library _repository = repository; } - public event EventHandler 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); - } + /// + public event EventHandler? UserDataSaved; + /// 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 }); } + /// 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); } - /// - /// Save the provided user data for the given user. Batch operation. Does not fire any events or update the cache. - /// - /// The user id. - /// The user item data. - /// The cancellation token. - public void SaveAllUserData(Guid userId, UserItemData[] userData, CancellationToken cancellationToken) - { - var user = _userManager.GetUserById(userId); - - _repository.SaveAllUserData(user.InternalId, userData, cancellationToken); - } - - /// - /// Retrieve all user data for the given user. - /// - /// The user id. - /// A containing all of the user's item data. - public List GetAllUserData(Guid userId) - { - var user = _userManager.GetUserById(userId); - - return _repository.GetAllUserData(user.InternalId); - } - - public UserItemData GetUserData(Guid userId, Guid itemId, List keys) - { - var user = _userManager.GetUserById(userId); - - return GetUserData(user, itemId, keys); - } - - public UserItemData GetUserData(User user, Guid itemId, List keys) + private UserItemData GetUserData(User user, Guid itemId, List keys) { var userId = user.InternalId; @@ -186,7 +153,7 @@ namespace Emby.Server.Implementations.Library }; } - return null; + throw new UnreachableException(); } /// @@ -198,27 +165,18 @@ namespace Emby.Server.Implementations.Library return internalUserId.ToString(CultureInfo.InvariantCulture) + "-" + itemId.ToString("N", CultureInfo.InvariantCulture); } + /// 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()); - } - + /// 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()); /// - 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); diff --git a/MediaBrowser.Controller/Entities/Folder.cs b/MediaBrowser.Controller/Entities/Folder.cs index b2e5d7263..83c19a54e 100644 --- a/MediaBrowser.Controller/Entities/Folder.cs +++ b/MediaBrowser.Controller/Entities/Folder.cs @@ -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) diff --git a/MediaBrowser.Controller/Library/IUserDataManager.cs b/MediaBrowser.Controller/Library/IUserDataManager.cs index 43cccfc65..f36fd393f 100644 --- a/MediaBrowser.Controller/Library/IUserDataManager.cs +++ b/MediaBrowser.Controller/Library/IUserDataManager.cs @@ -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 /// /// Occurs when [user data saved]. /// - event EventHandler UserDataSaved; + event EventHandler? UserDataSaved; /// /// Saves the user data. /// - /// The user id. + /// The user. /// The item. /// The user data. /// The reason. /// The cancellation token. - void SaveUserData(Guid userId, BaseItem item, UserItemData userData, UserDataSaveReason reason, CancellationToken cancellationToken); - void SaveUserData(User user, BaseItem item, UserItemData userData, UserDataSaveReason reason, CancellationToken cancellationToken); /// @@ -44,10 +38,14 @@ namespace MediaBrowser.Controller.Library /// The reason. void SaveUserData(User user, BaseItem item, UpdateUserItemDataDto userDataDto, UserDataSaveReason reason); + /// + /// Gets the user data. + /// + /// User to use. + /// Item to use. + /// User data. UserItemData GetUserData(User user, BaseItem item); - UserItemData GetUserData(Guid userId, BaseItem item); - /// /// Gets the user data dto. /// @@ -56,22 +54,15 @@ namespace MediaBrowser.Controller.Library /// User data dto. UserItemDataDto GetUserDataDto(BaseItem item, User user); - UserItemDataDto GetUserDataDto(BaseItem item, BaseItemDto itemDto, User user, DtoOptions options); - /// - /// Get all user data for the given user. + /// Gets the user data dto. /// - /// The user id. - /// The user item data. - List GetAllUserData(Guid userId); - - /// - /// Save the all provided user data for the given user. - /// - /// The user id. - /// The array of user data. - /// The cancellation token. - void SaveAllUserData(Guid userId, UserItemData[] userData, CancellationToken cancellationToken); + /// Item to use. + /// Item dto to use. + /// User to use. + /// Dto options to use. + /// User data dto. + UserItemDataDto GetUserDataDto(BaseItem item, BaseItemDto? itemDto, User user, DtoOptions options); /// /// Updates playstate for an item and returns true or false indicating if it was played to completion. diff --git a/MediaBrowser.XbmcMetadata/Parsers/BaseNfoParser.cs b/MediaBrowser.XbmcMetadata/Parsers/BaseNfoParser.cs index f2681500b..a8800431e 100644 --- a/MediaBrowser.XbmcMetadata/Parsers/BaseNfoParser.cs +++ b/MediaBrowser.XbmcMetadata/Parsers/BaseNfoParser.cs @@ -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;