| | | 1 | | using System; |
| | | 2 | | using System.Collections.Generic; |
| | | 3 | | using System.Threading; |
| | | 4 | | using System.Threading.Tasks; |
| | | 5 | | using Jellyfin.Data.Enums; |
| | | 6 | | using Jellyfin.Database.Implementations.Enums; |
| | | 7 | | using MediaBrowser.Controller.Configuration; |
| | | 8 | | using MediaBrowser.Controller.Dto; |
| | | 9 | | using MediaBrowser.Controller.Entities; |
| | | 10 | | using MediaBrowser.Controller.Entities.Movies; |
| | | 11 | | using MediaBrowser.Controller.Library; |
| | | 12 | | using MediaBrowser.Model.Configuration; |
| | | 13 | | |
| | | 14 | | namespace Emby.Server.Implementations.Library.SimilarItems; |
| | | 15 | | |
| | | 16 | | /// <summary> |
| | | 17 | | /// Provides similar items for movies and trailers. |
| | | 18 | | /// </summary> |
| | | 19 | | public sealed class MovieSimilarItemsProvider : ILocalSimilarItemsProvider<Movie>, ILocalSimilarItemsProvider<Trailer> |
| | | 20 | | { |
| | | 21 | | private readonly ILibraryManager _libraryManager; |
| | | 22 | | private readonly IServerConfigurationManager _serverConfigurationManager; |
| | | 23 | | |
| | | 24 | | /// <summary> |
| | | 25 | | /// Initializes a new instance of the <see cref="MovieSimilarItemsProvider"/> class. |
| | | 26 | | /// </summary> |
| | | 27 | | /// <param name="libraryManager">The library manager.</param> |
| | | 28 | | /// <param name="serverConfigurationManager">The server configuration manager.</param> |
| | | 29 | | public MovieSimilarItemsProvider( |
| | | 30 | | ILibraryManager libraryManager, |
| | | 31 | | IServerConfigurationManager serverConfigurationManager) |
| | | 32 | | { |
| | 21 | 33 | | _libraryManager = libraryManager; |
| | 21 | 34 | | _serverConfigurationManager = serverConfigurationManager; |
| | 21 | 35 | | } |
| | | 36 | | |
| | | 37 | | /// <inheritdoc/> |
| | 0 | 38 | | public string Name => "Local Genre/Tag"; |
| | | 39 | | |
| | | 40 | | /// <inheritdoc/> |
| | 0 | 41 | | public MetadataPluginType Type => MetadataPluginType.LocalSimilarityProvider; |
| | | 42 | | |
| | | 43 | | /// <inheritdoc/> |
| | | 44 | | public Task<IReadOnlyList<BaseItem>> GetSimilarItemsAsync(Movie item, SimilarItemsQuery query, CancellationToken can |
| | | 45 | | { |
| | 0 | 46 | | return Task.FromResult(GetSimilarMovieItems(item, query)); |
| | | 47 | | } |
| | | 48 | | |
| | | 49 | | /// <inheritdoc/> |
| | | 50 | | public Task<IReadOnlyList<BaseItem>> GetSimilarItemsAsync(Trailer item, SimilarItemsQuery query, CancellationToken c |
| | | 51 | | { |
| | 0 | 52 | | return Task.FromResult(GetSimilarMovieItems(item, query)); |
| | | 53 | | } |
| | | 54 | | |
| | | 55 | | bool ILocalSimilarItemsProvider.Supports(Type itemType) |
| | 0 | 56 | | => typeof(Movie).IsAssignableFrom(itemType) || typeof(Trailer).IsAssignableFrom(itemType); |
| | | 57 | | |
| | | 58 | | Task<IReadOnlyList<BaseItem>> ILocalSimilarItemsProvider.GetSimilarItemsAsync(BaseItem item, SimilarItemsQuery query |
| | 0 | 59 | | => item switch |
| | 0 | 60 | | { |
| | 0 | 61 | | Movie movie => GetSimilarItemsAsync(movie, query, cancellationToken), |
| | 0 | 62 | | Trailer trailer => GetSimilarItemsAsync(trailer, query, cancellationToken), |
| | 0 | 63 | | _ => throw new ArgumentException($"Unsupported item type {item.GetType()}", nameof(item)) |
| | 0 | 64 | | }; |
| | | 65 | | |
| | | 66 | | private IReadOnlyList<BaseItem> GetSimilarMovieItems(BaseItem item, SimilarItemsQuery query) |
| | | 67 | | { |
| | 0 | 68 | | var includeItemTypes = new List<BaseItemKind> { BaseItemKind.Movie }; |
| | | 69 | | |
| | 0 | 70 | | if (_serverConfigurationManager.Configuration.EnableExternalContentInSuggestions) |
| | | 71 | | { |
| | 0 | 72 | | includeItemTypes.Add(BaseItemKind.Trailer); |
| | 0 | 73 | | includeItemTypes.Add(BaseItemKind.LiveTvProgram); |
| | | 74 | | } |
| | | 75 | | |
| | 0 | 76 | | var internalQuery = new InternalItemsQuery(query.User) |
| | 0 | 77 | | { |
| | 0 | 78 | | Genres = item.Genres, |
| | 0 | 79 | | Tags = item.Tags, |
| | 0 | 80 | | Limit = query.Limit, |
| | 0 | 81 | | DtoOptions = query.DtoOptions ?? new DtoOptions(), |
| | 0 | 82 | | ExcludeItemIds = [.. query.ExcludeItemIds], |
| | 0 | 83 | | IncludeItemTypes = [.. includeItemTypes], |
| | 0 | 84 | | EnableGroupByMetadataKey = true, |
| | 0 | 85 | | EnableTotalRecordCount = false, |
| | 0 | 86 | | OrderBy = [(ItemSortBy.Random, SortOrder.Ascending)] |
| | 0 | 87 | | }; |
| | | 88 | | |
| | 0 | 89 | | return _libraryManager.GetItemList(internalQuery); |
| | | 90 | | } |
| | | 91 | | } |