From 4ebce3907062ade1937440628eebd665440b338d Mon Sep 17 00:00:00 2001 From: Shadowghost Date: Sun, 3 May 2026 23:43:01 +0200 Subject: Implement Similarity providers --- .../Library/ILocalSimilarItemsProvider.cs | 27 ++++++++++++ .../Library/IRemoteSimilarItemsProvider.cs | 26 +++++++++++ .../Library/ISimilarItemsManager.cs | 50 ++++++++++++++++++++++ .../Library/ISimilarItemsProvider.cs | 26 +++++++++++ .../Library/SimilarItemReference.cs | 22 ++++++++++ .../Library/SimilarItemsQuery.cs | 37 ++++++++++++++++ 6 files changed, 188 insertions(+) create mode 100644 MediaBrowser.Controller/Library/ILocalSimilarItemsProvider.cs create mode 100644 MediaBrowser.Controller/Library/IRemoteSimilarItemsProvider.cs create mode 100644 MediaBrowser.Controller/Library/ISimilarItemsManager.cs create mode 100644 MediaBrowser.Controller/Library/ISimilarItemsProvider.cs create mode 100644 MediaBrowser.Controller/Library/SimilarItemReference.cs create mode 100644 MediaBrowser.Controller/Library/SimilarItemsQuery.cs (limited to 'MediaBrowser.Controller/Library') diff --git a/MediaBrowser.Controller/Library/ILocalSimilarItemsProvider.cs b/MediaBrowser.Controller/Library/ILocalSimilarItemsProvider.cs new file mode 100644 index 0000000000..9bf0121f5f --- /dev/null +++ b/MediaBrowser.Controller/Library/ILocalSimilarItemsProvider.cs @@ -0,0 +1,27 @@ +using System.Collections.Generic; +using System.Threading; +using System.Threading.Tasks; +using MediaBrowser.Controller.Entities; + +namespace MediaBrowser.Controller.Library; + +/// +/// Provides similar items from the local library for a specific item type. +/// Returns fully resolved BaseItems directly - no additional resolution needed. +/// +/// The type of item this provider handles. +public interface ILocalSimilarItemsProvider : ISimilarItemsProvider + where TItemType : BaseItem +{ + /// + /// Gets similar items from the local library. + /// + /// The source item to find similar items for. + /// The query options (user, limit, exclusions, etc.). + /// Cancellation token. + /// The list of similar items from the library. + Task> GetSimilarItemsAsync( + TItemType item, + SimilarItemsQuery query, + CancellationToken cancellationToken); +} diff --git a/MediaBrowser.Controller/Library/IRemoteSimilarItemsProvider.cs b/MediaBrowser.Controller/Library/IRemoteSimilarItemsProvider.cs new file mode 100644 index 0000000000..a77b6628d9 --- /dev/null +++ b/MediaBrowser.Controller/Library/IRemoteSimilarItemsProvider.cs @@ -0,0 +1,26 @@ +using System.Collections.Generic; +using System.Threading; +using MediaBrowser.Controller.Entities; + +namespace MediaBrowser.Controller.Library; + +/// +/// Provides similar item references from remote/external sources for a specific item type. +/// Returns lightweight references with ProviderIds that the manager resolves to library items. +/// +/// The type of item this provider handles. +public interface IRemoteSimilarItemsProvider : ISimilarItemsProvider + where TItemType : BaseItem +{ + /// + /// Gets similar item references from an external source as an async stream. + /// + /// The source item to find similar items for. + /// The query options (user, limit, exclusions). + /// Cancellation token. + /// An async enumerable of similar item references. + IAsyncEnumerable GetSimilarItemsAsync( + TItemType item, + SimilarItemsQuery query, + CancellationToken cancellationToken); +} diff --git a/MediaBrowser.Controller/Library/ISimilarItemsManager.cs b/MediaBrowser.Controller/Library/ISimilarItemsManager.cs new file mode 100644 index 0000000000..0ced6f71ee --- /dev/null +++ b/MediaBrowser.Controller/Library/ISimilarItemsManager.cs @@ -0,0 +1,50 @@ +using System; +using System.Collections.Generic; +using System.Threading; +using System.Threading.Tasks; +using Jellyfin.Database.Implementations.Entities; +using MediaBrowser.Controller.Dto; +using MediaBrowser.Controller.Entities; +using MediaBrowser.Model.Configuration; + +namespace MediaBrowser.Controller.Library; + +/// +/// Interface for managing similar items providers and operations. +/// +public interface ISimilarItemsManager +{ + /// + /// Registers similar items providers discovered through dependency injection. + /// + /// The similar items providers to register. + void AddParts(IEnumerable providers); + + /// + /// Gets the similar items providers for a specific item type. + /// + /// The item type. + /// The list of similar items providers for that type. + IReadOnlyList GetSimilarItemsProviders() + where T : BaseItem; + + /// + /// Gets similar items for the specified item. + /// + /// The source item to find similar items for. + /// Artist IDs to exclude from results. + /// The user context. + /// The DTO options. + /// Maximum number of results. + /// The library options for provider configuration. + /// The cancellation token. + /// The list of similar items. + Task> GetSimilarItemsAsync( + BaseItem item, + IReadOnlyList excludeArtistIds, + User? user, + DtoOptions dtoOptions, + int? limit, + LibraryOptions? libraryOptions, + CancellationToken cancellationToken); +} diff --git a/MediaBrowser.Controller/Library/ISimilarItemsProvider.cs b/MediaBrowser.Controller/Library/ISimilarItemsProvider.cs new file mode 100644 index 0000000000..0d089369a8 --- /dev/null +++ b/MediaBrowser.Controller/Library/ISimilarItemsProvider.cs @@ -0,0 +1,26 @@ +using System; +using MediaBrowser.Model.Configuration; + +namespace MediaBrowser.Controller.Library; + +/// +/// Base marker interface for similar items providers. +/// +public interface ISimilarItemsProvider +{ + /// + /// Gets the name of the provider. + /// + string Name { get; } + + /// + /// Gets the type of the provider. + /// + MetadataPluginType Type { get; } + + /// + /// Gets the cache duration for results from this provider. + /// If null, results will not be cached. + /// + TimeSpan? CacheDuration => null; +} diff --git a/MediaBrowser.Controller/Library/SimilarItemReference.cs b/MediaBrowser.Controller/Library/SimilarItemReference.cs new file mode 100644 index 0000000000..2a40c93bdd --- /dev/null +++ b/MediaBrowser.Controller/Library/SimilarItemReference.cs @@ -0,0 +1,22 @@ +namespace MediaBrowser.Controller.Library; + +/// +/// A reference to a similar item by provider ID with a similarity score. +/// +public class SimilarItemReference +{ + /// + /// Gets or sets the provider name (e.g., "Tmdb", "MusicBrainzArtist"). + /// + public required string ProviderName { get; set; } + + /// + /// Gets or sets the provider ID value. + /// + public required string ProviderId { get; set; } + + /// + /// Gets or sets the similarity score (0.0 to 1.0). + /// + public float? Score { get; set; } +} diff --git a/MediaBrowser.Controller/Library/SimilarItemsQuery.cs b/MediaBrowser.Controller/Library/SimilarItemsQuery.cs new file mode 100644 index 0000000000..1ed3ceec16 --- /dev/null +++ b/MediaBrowser.Controller/Library/SimilarItemsQuery.cs @@ -0,0 +1,37 @@ +using System; +using System.Collections.Generic; +using Jellyfin.Database.Implementations.Entities; +using MediaBrowser.Controller.Dto; + +namespace MediaBrowser.Controller.Library; + +/// +/// Query options for similar items requests. +/// +public class SimilarItemsQuery +{ + /// + /// Gets or sets the user context. + /// + public User? User { get; set; } + + /// + /// Gets or sets the maximum number of results. + /// + public int? Limit { get; set; } + + /// + /// Gets or sets the DTO options. + /// + public DtoOptions? DtoOptions { get; set; } + + /// + /// Gets or sets the item IDs to exclude from results. + /// + public IReadOnlyList ExcludeItemIds { get; set; } = []; + + /// + /// Gets or sets the artist IDs to exclude from results. + /// + public IReadOnlyList ExcludeArtistIds { get; set; } = []; +} -- cgit v1.2.3