| | | 1 | | #pragma warning disable RS0030 // Do not use banned APIs: Guid == is required inside EF expression trees. |
| | | 2 | | |
| | | 3 | | using System; |
| | | 4 | | using System.Collections.Generic; |
| | | 5 | | using System.Linq; |
| | | 6 | | using System.Threading; |
| | | 7 | | using System.Threading.Tasks; |
| | | 8 | | using Jellyfin.Data.Enums; |
| | | 9 | | using Jellyfin.Database.Implementations; |
| | | 10 | | using Jellyfin.Database.Implementations.Entities; |
| | | 11 | | using Jellyfin.Extensions; |
| | | 12 | | using MediaBrowser.Controller.Configuration; |
| | | 13 | | using MediaBrowser.Controller.Dto; |
| | | 14 | | using MediaBrowser.Controller.Entities; |
| | | 15 | | using MediaBrowser.Controller.Entities.Movies; |
| | | 16 | | using MediaBrowser.Controller.Library; |
| | | 17 | | using MediaBrowser.Controller.Persistence; |
| | | 18 | | using MediaBrowser.Model.Configuration; |
| | | 19 | | using Microsoft.EntityFrameworkCore; |
| | | 20 | | using BaseItemDto = MediaBrowser.Controller.Entities.BaseItem; |
| | | 21 | | |
| | | 22 | | namespace Emby.Server.Implementations.Library.SimilarItems; |
| | | 23 | | |
| | | 24 | | /// <summary> |
| | | 25 | | /// Provides similar items for movies and trailers using weighted scoring. |
| | | 26 | | /// </summary> |
| | | 27 | | public sealed class MovieSimilarItemsProvider : ILocalSimilarItemsProvider<Movie>, ILocalSimilarItemsProvider<Trailer>, |
| | | 28 | | { |
| | | 29 | | private const int GenreWeight = 10; |
| | | 30 | | private const int TagWeight = 5; |
| | | 31 | | private const int StudioWeight = 5; |
| | | 32 | | private const int DirectorWeight = 50; |
| | | 33 | | private const int ActorWeight = 15; |
| | | 34 | | |
| | | 35 | | // Caps the batch fan-out so downstream IN-list sizes (per-source scores, accessible-id |
| | | 36 | | // load, navigation includes) stay bounded regardless of caller input. |
| | | 37 | | private const int MaxBatchSourceItems = 64; |
| | | 38 | | |
| | 1 | 39 | | private static readonly (ItemValueType Type, int Weight)[] _itemValueDimensions = |
| | 1 | 40 | | [ |
| | 1 | 41 | | (ItemValueType.Genre, GenreWeight), |
| | 1 | 42 | | (ItemValueType.Tags, TagWeight), |
| | 1 | 43 | | (ItemValueType.Studios, StudioWeight) |
| | 1 | 44 | | ]; |
| | | 45 | | |
| | 1 | 46 | | private static readonly Dictionary<string, int> _personTypeWeights = new(StringComparer.Ordinal) |
| | 1 | 47 | | { |
| | 1 | 48 | | [nameof(PersonKind.Director)] = DirectorWeight, |
| | 1 | 49 | | [nameof(PersonKind.Actor)] = ActorWeight, |
| | 1 | 50 | | [nameof(PersonKind.GuestStar)] = ActorWeight, |
| | 1 | 51 | | }; |
| | | 52 | | |
| | 1 | 53 | | private static readonly string[] _scoredPersonTypes = [.. _personTypeWeights.Keys]; |
| | | 54 | | |
| | | 55 | | private readonly IDbContextFactory<JellyfinDbContext> _dbProvider; |
| | | 56 | | private readonly IItemQueryHelpers _queryHelpers; |
| | | 57 | | private readonly IServerConfigurationManager _serverConfigurationManager; |
| | | 58 | | private readonly ILibraryManager _libraryManager; |
| | | 59 | | |
| | | 60 | | /// <summary> |
| | | 61 | | /// Initializes a new instance of the <see cref="MovieSimilarItemsProvider"/> class. |
| | | 62 | | /// </summary> |
| | | 63 | | /// <param name="dbProvider">The database context factory.</param> |
| | | 64 | | /// <param name="queryHelpers">The shared query helpers.</param> |
| | | 65 | | /// <param name="serverConfigurationManager">The server configuration manager.</param> |
| | | 66 | | /// <param name="libraryManager">The library manager.</param> |
| | | 67 | | public MovieSimilarItemsProvider( |
| | | 68 | | IDbContextFactory<JellyfinDbContext> dbProvider, |
| | | 69 | | IItemQueryHelpers queryHelpers, |
| | | 70 | | IServerConfigurationManager serverConfigurationManager, |
| | | 71 | | ILibraryManager libraryManager) |
| | | 72 | | { |
| | 33 | 73 | | _dbProvider = dbProvider; |
| | 33 | 74 | | _queryHelpers = queryHelpers; |
| | 33 | 75 | | _serverConfigurationManager = serverConfigurationManager; |
| | 33 | 76 | | _libraryManager = libraryManager; |
| | 33 | 77 | | } |
| | | 78 | | |
| | | 79 | | /// <inheritdoc/> |
| | 0 | 80 | | public string Name => "Local Genre/Tag"; |
| | | 81 | | |
| | | 82 | | /// <inheritdoc/> |
| | 0 | 83 | | public MetadataPluginType Type => MetadataPluginType.LocalSimilarityProvider; |
| | | 84 | | |
| | | 85 | | /// <inheritdoc/> |
| | | 86 | | public async Task<IReadOnlyList<BaseItemDto>> GetSimilarItemsAsync(Movie item, SimilarItemsQuery query, Cancellation |
| | | 87 | | { |
| | 6 | 88 | | var results = await GetBatchSimilarItemsAsync([item], query, cancellationToken).ConfigureAwait(false); |
| | 6 | 89 | | return results.TryGetValue(item.Id, out var items) ? items : []; |
| | 6 | 90 | | } |
| | | 91 | | |
| | | 92 | | /// <inheritdoc/> |
| | | 93 | | public async Task<IReadOnlyList<BaseItemDto>> GetSimilarItemsAsync(Trailer item, SimilarItemsQuery query, Cancellati |
| | | 94 | | { |
| | 0 | 95 | | var results = await GetBatchSimilarItemsAsync([item], query, cancellationToken).ConfigureAwait(false); |
| | 0 | 96 | | return results.TryGetValue(item.Id, out var items) ? items : []; |
| | 0 | 97 | | } |
| | | 98 | | |
| | | 99 | | bool ILocalSimilarItemsProvider.Supports(Type itemType) |
| | 0 | 100 | | => typeof(Movie).IsAssignableFrom(itemType) || typeof(Trailer).IsAssignableFrom(itemType); |
| | | 101 | | |
| | | 102 | | Task<IReadOnlyList<BaseItem>> ILocalSimilarItemsProvider.GetSimilarItemsAsync(BaseItem item, SimilarItemsQuery query |
| | 0 | 103 | | => item switch |
| | 0 | 104 | | { |
| | 0 | 105 | | Movie movie => GetSimilarItemsAsync(movie, query, cancellationToken), |
| | 0 | 106 | | Trailer trailer => GetSimilarItemsAsync(trailer, query, cancellationToken), |
| | 0 | 107 | | _ => throw new ArgumentException($"Unsupported item type {item.GetType()}", nameof(item)) |
| | 0 | 108 | | }; |
| | | 109 | | |
| | | 110 | | /// <inheritdoc/> |
| | | 111 | | public async Task<Dictionary<Guid, IReadOnlyList<BaseItemDto>>> GetBatchSimilarItemsAsync( |
| | | 112 | | IReadOnlyList<BaseItemDto> sourceItems, |
| | | 113 | | SimilarItemsQuery query, |
| | | 114 | | CancellationToken cancellationToken) |
| | | 115 | | { |
| | 6 | 116 | | var includeItemTypes = new List<BaseItemKind> { BaseItemKind.Movie }; |
| | 6 | 117 | | if (_serverConfigurationManager.Configuration.EnableExternalContentInSuggestions) |
| | | 118 | | { |
| | 6 | 119 | | includeItemTypes.Add(BaseItemKind.Trailer); |
| | 6 | 120 | | includeItemTypes.Add(BaseItemKind.LiveTvProgram); |
| | | 121 | | } |
| | | 122 | | |
| | 6 | 123 | | var limit = query.Limit ?? 50; |
| | 6 | 124 | | var dtoOptions = query.DtoOptions ?? new DtoOptions(); |
| | | 125 | | |
| | 6 | 126 | | if (sourceItems.Count > MaxBatchSourceItems) |
| | | 127 | | { |
| | 0 | 128 | | sourceItems = sourceItems.Take(MaxBatchSourceItems).ToList(); |
| | | 129 | | } |
| | | 130 | | |
| | 6 | 131 | | var context = await _dbProvider.CreateDbContextAsync(cancellationToken).ConfigureAwait(false); |
| | 6 | 132 | | await using (context.ConfigureAwait(false)) |
| | | 133 | | { |
| | | 134 | | // Phase 1: Score all candidates per source item |
| | 6 | 135 | | var sourceIds = sourceItems.Select(i => i.Id).ToList(); |
| | 6 | 136 | | var perSourceScores = await ComputeBatchScoresAsync(sourceIds, context, cancellationToken).ConfigureAwait(fa |
| | | 137 | | |
| | 6 | 138 | | var allCandidateIds = new HashSet<Guid>(); |
| | 24 | 139 | | foreach (var (_, scores) in perSourceScores) |
| | | 140 | | { |
| | 6 | 141 | | allCandidateIds.UnionWith( |
| | 6 | 142 | | scores.OrderByDescending(kvp => kvp.Value) |
| | 6 | 143 | | .Take(limit * 3) |
| | 6 | 144 | | .Select(kvp => kvp.Key)); |
| | | 145 | | } |
| | | 146 | | |
| | 6 | 147 | | var result = new Dictionary<Guid, IReadOnlyList<BaseItemDto>>(); |
| | 6 | 148 | | if (allCandidateIds.Count == 0) |
| | | 149 | | { |
| | 0 | 150 | | return result; |
| | | 151 | | } |
| | | 152 | | |
| | | 153 | | // Phase 2: One access filter for all candidates |
| | 6 | 154 | | var filter = new InternalItemsQuery(query.User) |
| | 6 | 155 | | { |
| | 6 | 156 | | IncludeItemTypes = [.. includeItemTypes], |
| | 6 | 157 | | ExcludeItemIds = [.. query.ExcludeItemIds], |
| | 6 | 158 | | DtoOptions = dtoOptions, |
| | 6 | 159 | | EnableGroupByMetadataKey = true, |
| | 6 | 160 | | EnableTotalRecordCount = false, |
| | 6 | 161 | | IsMovie = true, |
| | 6 | 162 | | IsPlayed = false |
| | 6 | 163 | | }; |
| | | 164 | | |
| | 6 | 165 | | if (query.User is not null) |
| | | 166 | | { |
| | 6 | 167 | | _libraryManager.ConfigureUserAccess(filter, query.User); |
| | | 168 | | } |
| | | 169 | | |
| | 6 | 170 | | _queryHelpers.PrepareFilterQuery(filter); |
| | 6 | 171 | | var baseQuery = _queryHelpers.PrepareItemQuery(context, filter); |
| | 6 | 172 | | baseQuery = _queryHelpers.TranslateQuery(baseQuery, context, filter); |
| | | 173 | | |
| | 6 | 174 | | var allCandidateIdsList = allCandidateIds.ToList(); |
| | 6 | 175 | | var accessibleItems = await baseQuery |
| | 6 | 176 | | .WhereOneOrMany(allCandidateIdsList, e => e.Id) |
| | 6 | 177 | | .Select(e => new { e.Id, e.PresentationUniqueKey, e.PrimaryVersionId }) |
| | 6 | 178 | | .ToListAsync(cancellationToken).ConfigureAwait(false); |
| | | 179 | | |
| | | 180 | | // Phase 3: Pick top IDs per source, dedup by PresentationUniqueKey |
| | 6 | 181 | | var allOrderedIds = new HashSet<Guid>(); |
| | 6 | 182 | | var perSourceOrderedIds = new Dictionary<Guid, List<Guid>>(); |
| | | 183 | | |
| | 24 | 184 | | foreach (var item in sourceItems) |
| | | 185 | | { |
| | 6 | 186 | | if (!perSourceScores.TryGetValue(item.Id, out var scores)) |
| | | 187 | | { |
| | | 188 | | continue; |
| | | 189 | | } |
| | | 190 | | |
| | 6 | 191 | | var orderedIds = accessibleItems |
| | 6 | 192 | | .Where(x => scores.ContainsKey(x.Id)) |
| | 6 | 193 | | .OrderByDescending(x => scores.GetValueOrDefault(x.Id)) |
| | 6 | 194 | | // Two versions of one movie score the same, so name the primary as the |
| | 6 | 195 | | // representative of the group rather than whichever came back first. |
| | 6 | 196 | | .ThenBy(x => x.PrimaryVersionId.HasValue) |
| | 6 | 197 | | .DistinctBy(x => x.PresentationUniqueKey) |
| | 6 | 198 | | .Take(limit) |
| | 6 | 199 | | .Select(x => x.Id) |
| | 6 | 200 | | .ToList(); |
| | | 201 | | |
| | 6 | 202 | | if (orderedIds.Count > 0) |
| | | 203 | | { |
| | 6 | 204 | | perSourceOrderedIds[item.Id] = orderedIds; |
| | 6 | 205 | | allOrderedIds.UnionWith(orderedIds); |
| | | 206 | | } |
| | | 207 | | } |
| | | 208 | | |
| | 6 | 209 | | if (allOrderedIds.Count == 0) |
| | | 210 | | { |
| | 0 | 211 | | return result; |
| | | 212 | | } |
| | | 213 | | |
| | | 214 | | // Phase 4: One entity load for all results |
| | 6 | 215 | | var allOrderedIdsList = allOrderedIds.ToList(); |
| | 6 | 216 | | var entities = await _queryHelpers.ApplyNavigations( |
| | 6 | 217 | | context.BaseItems.AsNoTracking().WhereOneOrMany(allOrderedIdsList, e => e.Id), |
| | 6 | 218 | | filter) |
| | 6 | 219 | | .AsSplitQuery() |
| | 6 | 220 | | .ToListAsync(cancellationToken).ConfigureAwait(false); |
| | | 221 | | |
| | 6 | 222 | | var entitiesById = entities |
| | 6 | 223 | | .Select(e => _queryHelpers.DeserializeBaseItem(e, filter.SkipDeserialization)) |
| | 6 | 224 | | .Where(dto => dto is not null) |
| | 6 | 225 | | .ToDictionary(i => i!.Id); |
| | | 226 | | |
| | | 227 | | // Phase 5: Split by source, preserving score order |
| | 24 | 228 | | foreach (var (sourceId, orderedIds) in perSourceOrderedIds) |
| | | 229 | | { |
| | 6 | 230 | | var items = orderedIds |
| | 6 | 231 | | .Where(entitiesById.ContainsKey) |
| | 6 | 232 | | .Select(id => entitiesById[id]!) |
| | 6 | 233 | | .ToList(); |
| | | 234 | | |
| | 6 | 235 | | if (items.Count > 0) |
| | | 236 | | { |
| | 6 | 237 | | result[sourceId] = items; |
| | | 238 | | } |
| | | 239 | | } |
| | | 240 | | |
| | 6 | 241 | | return result; |
| | | 242 | | } |
| | 6 | 243 | | } |
| | | 244 | | |
| | | 245 | | private static async Task<Dictionary<Guid, Dictionary<Guid, int>>> ComputeBatchScoresAsync(List<Guid> sourceIds, Jel |
| | | 246 | | { |
| | 6 | 247 | | var result = new Dictionary<Guid, Dictionary<Guid, int>>(); |
| | 24 | 248 | | foreach (var id in sourceIds) |
| | | 249 | | { |
| | 6 | 250 | | result[id] = []; |
| | | 251 | | } |
| | | 252 | | |
| | 6 | 253 | | var hiddenVersionIds = context.BaseItems.AsNoTracking() |
| | 6 | 254 | | .Where(e => e.PrimaryVersionId != null |
| | 6 | 255 | | && context.BaseItems.Any(p => p.Id == e.PrimaryVersionId && p.TopParentId == e.TopParentId)) |
| | 6 | 256 | | .Select(e => e.Id); |
| | | 257 | | |
| | 48 | 258 | | foreach (var (valueType, weight) in _itemValueDimensions) |
| | | 259 | | { |
| | 18 | 260 | | var sourceRows = await context.ItemValuesMap.AsNoTracking() |
| | 18 | 261 | | .Where(m => sourceIds.Contains(m.ItemId) && m.ItemValue.Type == valueType) |
| | 18 | 262 | | .Select(m => new { m.ItemId, Key = m.ItemValue.CleanValue }) |
| | 18 | 263 | | .ToListAsync(cancellationToken).ConfigureAwait(false); |
| | | 264 | | |
| | 18 | 265 | | var sourceMap = sourceRows.GroupBy(r => r.ItemId).ToDictionary(g => g.Key, g => g.Select(x => x.Key).ToHashS |
| | 18 | 266 | | var allKeys = sourceMap.Values.SelectMany(v => v).Distinct().ToList(); |
| | 18 | 267 | | if (allKeys.Count == 0) |
| | | 268 | | { |
| | | 269 | | continue; |
| | | 270 | | } |
| | | 271 | | |
| | 6 | 272 | | var candidateRows = await context.ItemValuesMap.AsNoTracking() |
| | 6 | 273 | | .Where(m => !hiddenVersionIds.Contains(m.ItemId) && m.ItemValue.Type == valueType && allKeys.Contains(m. |
| | 6 | 274 | | .Select(m => new { m.ItemId, Key = m.ItemValue.CleanValue }) |
| | 6 | 275 | | .ToListAsync(cancellationToken).ConfigureAwait(false); |
| | | 276 | | |
| | 6 | 277 | | var keyToCandidates = candidateRows.GroupBy(r => r.Key).ToDictionary(g => g.Key, g => g.Select(x => x.ItemId |
| | 6 | 278 | | ApplyDimensionScores(sourceIds, sourceMap, keyToCandidates, weight, result); |
| | 6 | 279 | | } |
| | | 280 | | |
| | 6 | 281 | | var personSourceRows = await context.PeopleBaseItemMap.AsNoTracking() |
| | 6 | 282 | | .Where(m => sourceIds.Contains(m.ItemId) && _scoredPersonTypes.Contains(m.People.PersonType)) |
| | 6 | 283 | | .Select(m => new { m.ItemId, m.PeopleId, m.People.PersonType }) |
| | 6 | 284 | | .ToListAsync(cancellationToken).ConfigureAwait(false); |
| | | 285 | | |
| | 6 | 286 | | if (personSourceRows.Count > 0) |
| | | 287 | | { |
| | 0 | 288 | | var personCandidateRows = await context.PeopleBaseItemMap.AsNoTracking() |
| | 0 | 289 | | .Where(m => !hiddenVersionIds.Contains(m.ItemId)) |
| | 0 | 290 | | .Where(m => context.PeopleBaseItemMap |
| | 0 | 291 | | .Where(s => sourceIds.Contains(s.ItemId) && _scoredPersonTypes.Contains(s.People.PersonType)) |
| | 0 | 292 | | .Select(s => s.PeopleId) |
| | 0 | 293 | | .Contains(m.PeopleId)) |
| | 0 | 294 | | .Select(m => new { m.ItemId, m.PeopleId }) |
| | 0 | 295 | | .ToListAsync(cancellationToken).ConfigureAwait(false); |
| | | 296 | | |
| | 0 | 297 | | var personToCandidates = personCandidateRows |
| | 0 | 298 | | .GroupBy(r => r.PeopleId) |
| | 0 | 299 | | .ToDictionary(g => g.Key, g => g.Select(x => x.ItemId).ToList()); |
| | | 300 | | |
| | 0 | 301 | | foreach (var weightGroup in personSourceRows.GroupBy(r => _personTypeWeights[r.PersonType!])) |
| | | 302 | | { |
| | 0 | 303 | | var sourceMap = weightGroup |
| | 0 | 304 | | .GroupBy(r => r.ItemId) |
| | 0 | 305 | | .ToDictionary(g => g.Key, g => g.Select(x => x.PeopleId).ToHashSet()); |
| | 0 | 306 | | ApplyDimensionScores(sourceIds, sourceMap, personToCandidates, weightGroup.Key, result); |
| | | 307 | | } |
| | | 308 | | } |
| | | 309 | | |
| | 24 | 310 | | foreach (var sourceId in sourceIds) |
| | | 311 | | { |
| | 6 | 312 | | var scoreMap = result[sourceId]; |
| | 6 | 313 | | scoreMap.Remove(sourceId); |
| | 6 | 314 | | if (scoreMap.Count == 0) |
| | | 315 | | { |
| | 0 | 316 | | result.Remove(sourceId); |
| | | 317 | | } |
| | | 318 | | } |
| | | 319 | | |
| | 6 | 320 | | return result; |
| | 6 | 321 | | } |
| | | 322 | | |
| | | 323 | | private static void ApplyDimensionScores<TKey>( |
| | | 324 | | List<Guid> sourceIds, |
| | | 325 | | Dictionary<Guid, HashSet<TKey>> sourceMap, |
| | | 326 | | Dictionary<TKey, List<Guid>> keyToCandidates, |
| | | 327 | | int weight, |
| | | 328 | | Dictionary<Guid, Dictionary<Guid, int>> result) |
| | | 329 | | where TKey : notnull |
| | | 330 | | { |
| | 24 | 331 | | foreach (var sourceId in sourceIds) |
| | | 332 | | { |
| | 6 | 333 | | if (!sourceMap.TryGetValue(sourceId, out var sourceKeys)) |
| | | 334 | | { |
| | | 335 | | continue; |
| | | 336 | | } |
| | | 337 | | |
| | 6 | 338 | | var scoreMap = result[sourceId]; |
| | 24 | 339 | | foreach (var key in sourceKeys) |
| | | 340 | | { |
| | 6 | 341 | | if (!keyToCandidates.TryGetValue(key, out var candidates)) |
| | | 342 | | { |
| | | 343 | | continue; |
| | | 344 | | } |
| | | 345 | | |
| | 48 | 346 | | foreach (var candidateId in candidates) |
| | | 347 | | { |
| | 18 | 348 | | scoreMap[candidateId] = scoreMap.GetValueOrDefault(candidateId) + weight; |
| | | 349 | | } |
| | | 350 | | } |
| | | 351 | | } |
| | 6 | 352 | | } |
| | | 353 | | } |