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