| | | 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.Dto; |
| | | 11 | | using MediaBrowser.Controller.Entities; |
| | | 12 | | using MediaBrowser.Controller.Library; |
| | | 13 | | using MediaBrowser.Controller.Persistence; |
| | | 14 | | using MediaBrowser.Model.Querying; |
| | | 15 | | using MediaBrowser.Model.Search; |
| | | 16 | | using Microsoft.EntityFrameworkCore; |
| | | 17 | | using Microsoft.Extensions.Logging; |
| | | 18 | | |
| | | 19 | | namespace Emby.Server.Implementations.Library.Search; |
| | | 20 | | |
| | | 21 | | /// <summary> |
| | | 22 | | /// Manages search providers and orchestrates search operations. |
| | | 23 | | /// </summary> |
| | | 24 | | public class SearchManager : ISearchManager |
| | | 25 | | { |
| | | 26 | | private readonly ILibraryManager _libraryManager; |
| | | 27 | | private readonly IUserManager _userManager; |
| | | 28 | | private readonly IDbContextFactory<JellyfinDbContext> _dbProvider; |
| | | 29 | | private readonly IItemQueryHelpers _queryHelpers; |
| | | 30 | | private readonly ILogger<SearchManager> _logger; |
| | 21 | 31 | | private IExternalSearchProvider[] _externalProviders = []; |
| | 21 | 32 | | private IInternalSearchProvider[] _internalProviders = []; |
| | | 33 | | |
| | | 34 | | /// <summary> |
| | | 35 | | /// Initializes a new instance of the <see cref="SearchManager"/> class. |
| | | 36 | | /// </summary> |
| | | 37 | | /// <param name="libraryManager">The library manager.</param> |
| | | 38 | | /// <param name="userManager">The user manager.</param> |
| | | 39 | | /// <param name="dbProvider">The database context factory.</param> |
| | | 40 | | /// <param name="queryHelpers">The shared item query helpers.</param> |
| | | 41 | | /// <param name="logger">The logger.</param> |
| | | 42 | | public SearchManager( |
| | | 43 | | ILibraryManager libraryManager, |
| | | 44 | | IUserManager userManager, |
| | | 45 | | IDbContextFactory<JellyfinDbContext> dbProvider, |
| | | 46 | | IItemQueryHelpers queryHelpers, |
| | | 47 | | ILogger<SearchManager> logger) |
| | | 48 | | { |
| | 21 | 49 | | _libraryManager = libraryManager; |
| | 21 | 50 | | _userManager = userManager; |
| | 21 | 51 | | _dbProvider = dbProvider; |
| | 21 | 52 | | _queryHelpers = queryHelpers; |
| | 21 | 53 | | _logger = logger; |
| | 21 | 54 | | } |
| | | 55 | | |
| | | 56 | | /// <inheritdoc/> |
| | | 57 | | public void AddParts(IEnumerable<ISearchProvider> providers) |
| | | 58 | | { |
| | 21 | 59 | | var allProviders = providers.OrderBy(p => p.Priority).ToArray(); |
| | | 60 | | |
| | 21 | 61 | | _externalProviders = allProviders.OfType<IExternalSearchProvider>().ToArray(); |
| | 21 | 62 | | _internalProviders = allProviders.OfType<IInternalSearchProvider>().ToArray(); |
| | | 63 | | |
| | 21 | 64 | | _logger.LogInformation( |
| | 21 | 65 | | "Registered {ExternalCount} external search providers: {ExternalProviders}. Fallback providers: {FallbackPro |
| | 21 | 66 | | _externalProviders.Length, |
| | 21 | 67 | | string.Join(", ", _externalProviders.Select(p => $"{p.Name} (priority {p.Priority})")), |
| | 21 | 68 | | string.Join(", ", _internalProviders.Select(p => $"{p.Name} (priority {p.Priority})"))); |
| | 21 | 69 | | } |
| | | 70 | | |
| | | 71 | | /// <inheritdoc/> |
| | | 72 | | public IReadOnlyList<ISearchProvider> GetProviders() |
| | | 73 | | { |
| | 0 | 74 | | return [.. _externalProviders, .. _internalProviders]; |
| | | 75 | | } |
| | | 76 | | |
| | | 77 | | /// <inheritdoc/> |
| | | 78 | | public async Task<IReadOnlyList<SearchResult>> GetSearchResultsAsync( |
| | | 79 | | SearchProviderQuery query, |
| | | 80 | | CancellationToken cancellationToken = default) |
| | | 81 | | { |
| | 0 | 82 | | ArgumentNullException.ThrowIfNull(query); |
| | 0 | 83 | | ArgumentException.ThrowIfNullOrWhiteSpace(query.SearchTerm); |
| | | 84 | | |
| | 0 | 85 | | var searchTerm = query.SearchTerm.Trim().RemoveDiacritics(); |
| | | 86 | | |
| | 0 | 87 | | var externalTask = CollectFromProvidersAsync(_externalProviders, query, searchTerm, cancellationToken); |
| | 0 | 88 | | var internalTask = _internalProviders.Length > 0 |
| | 0 | 89 | | ? CollectFromProvidersAsync(_internalProviders, query, searchTerm, cancellationToken) |
| | 0 | 90 | | : Task.FromResult<IReadOnlyList<SearchResult>>([]); |
| | | 91 | | |
| | 0 | 92 | | await Task.WhenAll(externalTask, internalTask).ConfigureAwait(false); |
| | | 93 | | |
| | 0 | 94 | | var externalResults = await externalTask.ConfigureAwait(false); |
| | 0 | 95 | | var fromExternal = externalResults.Count > 0; |
| | | 96 | | IReadOnlyList<SearchResult> results; |
| | 0 | 97 | | if (fromExternal) |
| | | 98 | | { |
| | 0 | 99 | | results = externalResults; |
| | | 100 | | } |
| | | 101 | | else |
| | | 102 | | { |
| | 0 | 103 | | results = await internalTask.ConfigureAwait(false); |
| | 0 | 104 | | if (_internalProviders.Length > 0) |
| | | 105 | | { |
| | 0 | 106 | | _logger.LogDebug("No results from external providers, using internal provider results"); |
| | | 107 | | } |
| | | 108 | | } |
| | | 109 | | |
| | | 110 | | // Internal providers apply user-access filtering inline in their queries. External |
| | | 111 | | // providers don't know about user permissions, so they may return IDs from hidden |
| | | 112 | | // libraries or items the user is otherwise blocked from. Run the post-filter only |
| | | 113 | | // when results came from externals to close that gap. The Items controller's second |
| | | 114 | | // roundtrip via folder.GetItems applies most of these again, but it does not restrict |
| | | 115 | | // by TopParentIds when ItemIds is set. |
| | 0 | 116 | | if (fromExternal && results.Count > 0 && query.UserId.HasValue && !query.UserId.Value.IsEmpty()) |
| | | 117 | | { |
| | 0 | 118 | | var user = _userManager.GetUserById(query.UserId.Value); |
| | 0 | 119 | | if (user is not null) |
| | | 120 | | { |
| | 0 | 121 | | results = await FilterByUserAccessAsync(results, user, cancellationToken).ConfigureAwait(false); |
| | | 122 | | } |
| | | 123 | | } |
| | | 124 | | |
| | 0 | 125 | | return results; |
| | 0 | 126 | | } |
| | | 127 | | |
| | | 128 | | private async Task<IReadOnlyList<SearchResult>> FilterByUserAccessAsync( |
| | | 129 | | IReadOnlyList<SearchResult> candidates, |
| | | 130 | | User user, |
| | | 131 | | CancellationToken cancellationToken) |
| | | 132 | | { |
| | | 133 | | // SetUser populates parental rating + blocked/allowed tags. ConfigureUserAccess populates |
| | | 134 | | // TopParentIds for the user's accessible libraries — we call it before assigning ItemIds |
| | | 135 | | // because LibraryManager.AddUserToQuery skips TopParentIds when ItemIds is non-empty. |
| | 0 | 136 | | var accessFilter = new InternalItemsQuery(user); |
| | 0 | 137 | | _libraryManager.ConfigureUserAccess(accessFilter, user); |
| | | 138 | | |
| | 0 | 139 | | Guid[] candidateIds = [.. candidates.Select(c => c.ItemId)]; |
| | | 140 | | |
| | 0 | 141 | | var dbContext = await _dbProvider.CreateDbContextAsync(cancellationToken).ConfigureAwait(false); |
| | 0 | 142 | | await using (dbContext.ConfigureAwait(false)) |
| | | 143 | | { |
| | 0 | 144 | | var baseQuery = dbContext.BaseItems |
| | 0 | 145 | | .AsNoTracking() |
| | 0 | 146 | | .WhereOneOrMany(candidateIds, e => e.Id); |
| | | 147 | | |
| | 0 | 148 | | baseQuery = _queryHelpers.ApplyAccessFiltering(dbContext, baseQuery, accessFilter); |
| | | 149 | | |
| | 0 | 150 | | var allowedCount = await baseQuery.CountAsync(cancellationToken).ConfigureAwait(false); |
| | 0 | 151 | | if (allowedCount == candidates.Count) |
| | | 152 | | { |
| | 0 | 153 | | return candidates; |
| | | 154 | | } |
| | | 155 | | |
| | 0 | 156 | | var allowedIds = await baseQuery |
| | 0 | 157 | | .Select(e => e.Id) |
| | 0 | 158 | | .ToHashSetAsync(cancellationToken) |
| | 0 | 159 | | .ConfigureAwait(false); |
| | | 160 | | |
| | 0 | 161 | | var filtered = candidates.Where(c => allowedIds.Contains(c.ItemId)).ToList(); |
| | 0 | 162 | | if (filtered.Count < candidates.Count) |
| | | 163 | | { |
| | 0 | 164 | | _logger.LogDebug( |
| | 0 | 165 | | "Dropped {Dropped} of {Total} search candidates due to user access filtering", |
| | 0 | 166 | | candidates.Count - filtered.Count, |
| | 0 | 167 | | candidates.Count); |
| | | 168 | | } |
| | | 169 | | |
| | 0 | 170 | | return filtered; |
| | | 171 | | } |
| | 0 | 172 | | } |
| | | 173 | | |
| | | 174 | | /// <inheritdoc/> |
| | | 175 | | public async Task<QueryResult<SearchHintInfo>> GetSearchHintsAsync(SearchQuery query, CancellationToken cancellation |
| | | 176 | | { |
| | 0 | 177 | | ArgumentNullException.ThrowIfNull(query); |
| | 0 | 178 | | ArgumentException.ThrowIfNullOrWhiteSpace(query.SearchTerm); |
| | | 179 | | |
| | 0 | 180 | | var providerQuery = BuildProviderQuery(query); |
| | 0 | 181 | | var candidates = await GetSearchResultsAsync(providerQuery, cancellationToken).ConfigureAwait(false); |
| | 0 | 182 | | if (candidates.Count == 0) |
| | | 183 | | { |
| | 0 | 184 | | return new QueryResult<SearchHintInfo>(); |
| | | 185 | | } |
| | | 186 | | |
| | 0 | 187 | | var candidateScores = BuildScoreLookup(candidates); |
| | 0 | 188 | | var user = query.UserId.IsEmpty() ? null : _userManager.GetUserById(query.UserId); |
| | | 189 | | |
| | 0 | 190 | | var excludeItemTypes = BuildExcludeItemTypes(query); |
| | 0 | 191 | | var includeItemTypes = BuildIncludeItemTypes(query); |
| | | 192 | | |
| | 0 | 193 | | var internalQuery = new InternalItemsQuery(user) |
| | 0 | 194 | | { |
| | 0 | 195 | | ItemIds = candidateScores.Keys.ToArray(), |
| | 0 | 196 | | ExcludeItemTypes = excludeItemTypes.ToArray(), |
| | 0 | 197 | | IncludeItemTypes = includeItemTypes.Count > 0 ? includeItemTypes.ToArray() : [], |
| | 0 | 198 | | MediaTypes = query.MediaTypes.ToArray(), |
| | 0 | 199 | | IncludeItemsByName = !query.ParentId.HasValue, |
| | 0 | 200 | | ParentId = query.ParentId ?? Guid.Empty, |
| | 0 | 201 | | Recursive = true, |
| | 0 | 202 | | IsKids = query.IsKids, |
| | 0 | 203 | | IsMovie = query.IsMovie, |
| | 0 | 204 | | IsNews = query.IsNews, |
| | 0 | 205 | | IsSeries = query.IsSeries, |
| | 0 | 206 | | IsSports = query.IsSports, |
| | 0 | 207 | | DtoOptions = new DtoOptions |
| | 0 | 208 | | { |
| | 0 | 209 | | Fields = |
| | 0 | 210 | | [ |
| | 0 | 211 | | ItemFields.AirTime, |
| | 0 | 212 | | ItemFields.DateCreated, |
| | 0 | 213 | | ItemFields.ChannelInfo, |
| | 0 | 214 | | ItemFields.ParentId |
| | 0 | 215 | | ] |
| | 0 | 216 | | } |
| | 0 | 217 | | }; |
| | | 218 | | |
| | | 219 | | // MusicArtist items are "ItemsByName" entities - virtual items that aggregate content by artist name |
| | | 220 | | // rather than being stored as regular library items. They require special handling: |
| | | 221 | | // 1. Convert ParentId to AncestorIds (to filter by library folder) |
| | | 222 | | // 2. Set IncludeItemsByName = true (to include these virtual items in results) |
| | | 223 | | // 3. Clear IncludeItemTypes (GetAllArtists handles type filtering internally) |
| | | 224 | | // 4. Use GetAllArtists() instead of GetItemList() to query the artist index |
| | | 225 | | IReadOnlyList<BaseItem> items; |
| | 0 | 226 | | if (internalQuery.IncludeItemTypes.Length == 1 && internalQuery.IncludeItemTypes[0] == BaseItemKind.MusicArtist) |
| | | 227 | | { |
| | 0 | 228 | | if (!internalQuery.ParentId.IsEmpty()) |
| | | 229 | | { |
| | 0 | 230 | | internalQuery.AncestorIds = [internalQuery.ParentId]; |
| | 0 | 231 | | internalQuery.ParentId = Guid.Empty; |
| | | 232 | | } |
| | | 233 | | |
| | 0 | 234 | | internalQuery.IncludeItemsByName = true; |
| | 0 | 235 | | internalQuery.IncludeItemTypes = []; |
| | 0 | 236 | | items = _libraryManager.GetAllArtists(internalQuery).Items.Select(i => i.Item).ToList(); |
| | | 237 | | } |
| | | 238 | | else |
| | | 239 | | { |
| | 0 | 240 | | items = _libraryManager.GetItemList(internalQuery); |
| | | 241 | | } |
| | | 242 | | |
| | 0 | 243 | | var orderedResults = items |
| | 0 | 244 | | .Select(item => new SearchHintInfo { Item = item }) |
| | 0 | 245 | | .OrderByDescending(hint => candidateScores.GetValueOrDefault(hint.Item.Id, 0f)) |
| | 0 | 246 | | .ToList(); |
| | | 247 | | |
| | 0 | 248 | | var totalCount = orderedResults.Count; |
| | | 249 | | |
| | 0 | 250 | | if (query.StartIndex.HasValue) |
| | | 251 | | { |
| | 0 | 252 | | orderedResults = orderedResults.Skip(query.StartIndex.Value).ToList(); |
| | | 253 | | } |
| | | 254 | | |
| | 0 | 255 | | if (query.Limit.HasValue) |
| | | 256 | | { |
| | 0 | 257 | | orderedResults = orderedResults.Take(query.Limit.Value).ToList(); |
| | | 258 | | } |
| | | 259 | | |
| | 0 | 260 | | return new QueryResult<SearchHintInfo>(query.StartIndex, totalCount, orderedResults); |
| | 0 | 261 | | } |
| | | 262 | | |
| | | 263 | | private async Task<IReadOnlyList<SearchResult>> CollectFromProvidersAsync( |
| | | 264 | | IEnumerable<ISearchProvider> providers, |
| | | 265 | | SearchProviderQuery providerQuery, |
| | | 266 | | string searchTerm, |
| | | 267 | | CancellationToken cancellationToken) |
| | | 268 | | { |
| | 0 | 269 | | var requestedLimit = providerQuery.Limit ?? 100; |
| | 0 | 270 | | var applicable = providers.Where(p => p.CanSearch(providerQuery)).ToArray(); |
| | 0 | 271 | | if (applicable.Length == 0) |
| | | 272 | | { |
| | 0 | 273 | | return []; |
| | | 274 | | } |
| | | 275 | | |
| | 0 | 276 | | var perProvider = await Task.WhenAll( |
| | 0 | 277 | | applicable.Select(p => CollectFromProviderAsync(p, providerQuery, searchTerm, requestedLimit, cancellationTo |
| | 0 | 278 | | .ConfigureAwait(false); |
| | | 279 | | |
| | 0 | 280 | | var bestScores = new Dictionary<Guid, float>(); |
| | 0 | 281 | | foreach (var providerResults in perProvider) |
| | | 282 | | { |
| | 0 | 283 | | foreach (var result in providerResults) |
| | | 284 | | { |
| | 0 | 285 | | UpdateBestScore(bestScores, result); |
| | | 286 | | } |
| | | 287 | | } |
| | | 288 | | |
| | 0 | 289 | | return bestScores |
| | 0 | 290 | | .Select(kvp => new SearchResult(kvp.Key, kvp.Value)) |
| | 0 | 291 | | .OrderByDescending(r => r.Score) |
| | 0 | 292 | | .Take(requestedLimit) |
| | 0 | 293 | | .ToList(); |
| | 0 | 294 | | } |
| | | 295 | | |
| | | 296 | | private async Task<IReadOnlyList<SearchResult>> CollectFromProviderAsync( |
| | | 297 | | ISearchProvider provider, |
| | | 298 | | SearchProviderQuery providerQuery, |
| | | 299 | | string searchTerm, |
| | | 300 | | int requestedLimit, |
| | | 301 | | CancellationToken cancellationToken) |
| | | 302 | | { |
| | | 303 | | try |
| | | 304 | | { |
| | 0 | 305 | | var results = provider is IExternalSearchProvider externalProvider |
| | 0 | 306 | | ? await CollectFromExternalProviderAsync(externalProvider, providerQuery, requestedLimit, cancellationTo |
| | 0 | 307 | | : await provider.SearchAsync(providerQuery, cancellationToken).ConfigureAwait(false); |
| | | 308 | | |
| | 0 | 309 | | _logger.LogDebug( |
| | 0 | 310 | | "Provider {Provider} returned {Count} candidates for search term '{SearchTerm}'", |
| | 0 | 311 | | provider.Name, |
| | 0 | 312 | | results.Count, |
| | 0 | 313 | | searchTerm); |
| | 0 | 314 | | return results; |
| | | 315 | | } |
| | 0 | 316 | | catch (Exception ex) |
| | | 317 | | { |
| | 0 | 318 | | _logger.LogWarning(ex, "Search provider {Provider} failed for term '{SearchTerm}'", provider.Name, searchTer |
| | 0 | 319 | | return []; |
| | | 320 | | } |
| | 0 | 321 | | } |
| | | 322 | | |
| | | 323 | | private static async Task<IReadOnlyList<SearchResult>> CollectFromExternalProviderAsync( |
| | | 324 | | IExternalSearchProvider provider, |
| | | 325 | | SearchProviderQuery providerQuery, |
| | | 326 | | int requestedLimit, |
| | | 327 | | CancellationToken cancellationToken) |
| | | 328 | | { |
| | 0 | 329 | | var results = new List<SearchResult>(); |
| | 0 | 330 | | await foreach (var result in provider.SearchAsync(providerQuery, cancellationToken).ConfigureAwait(false)) |
| | | 331 | | { |
| | 0 | 332 | | results.Add(result); |
| | 0 | 333 | | if (results.Count >= requestedLimit) |
| | | 334 | | { |
| | | 335 | | break; |
| | | 336 | | } |
| | | 337 | | } |
| | | 338 | | |
| | 0 | 339 | | return results; |
| | 0 | 340 | | } |
| | | 341 | | |
| | | 342 | | private static void UpdateBestScore(Dictionary<Guid, float> bestScores, SearchResult result) |
| | | 343 | | { |
| | 0 | 344 | | if (!bestScores.TryGetValue(result.ItemId, out var existingScore) || result.Score > existingScore) |
| | | 345 | | { |
| | 0 | 346 | | bestScores[result.ItemId] = result.Score; |
| | | 347 | | } |
| | 0 | 348 | | } |
| | | 349 | | |
| | | 350 | | private static Dictionary<Guid, float> BuildScoreLookup(IReadOnlyList<SearchResult> results) |
| | | 351 | | { |
| | 0 | 352 | | var lookup = new Dictionary<Guid, float>(results.Count); |
| | 0 | 353 | | foreach (var result in results) |
| | | 354 | | { |
| | 0 | 355 | | lookup[result.ItemId] = result.Score; |
| | | 356 | | } |
| | | 357 | | |
| | 0 | 358 | | return lookup; |
| | | 359 | | } |
| | | 360 | | |
| | | 361 | | private static SearchProviderQuery BuildProviderQuery(SearchQuery query) |
| | | 362 | | { |
| | 0 | 363 | | var excludeItemTypes = BuildExcludeItemTypes(query); |
| | 0 | 364 | | var includeItemTypes = BuildIncludeItemTypes(query); |
| | | 365 | | |
| | | 366 | | // Remove any excluded types from includes |
| | 0 | 367 | | if (includeItemTypes.Count > 0 && excludeItemTypes.Count > 0) |
| | | 368 | | { |
| | 0 | 369 | | includeItemTypes.RemoveAll(excludeItemTypes.Contains); |
| | | 370 | | } |
| | | 371 | | |
| | 0 | 372 | | return new SearchProviderQuery |
| | 0 | 373 | | { |
| | 0 | 374 | | SearchTerm = query.SearchTerm, |
| | 0 | 375 | | UserId = query.UserId.IsEmpty() ? null : query.UserId, |
| | 0 | 376 | | IncludeItemTypes = includeItemTypes.ToArray(), |
| | 0 | 377 | | ExcludeItemTypes = excludeItemTypes.ToArray(), |
| | 0 | 378 | | MediaTypes = query.MediaTypes.ToArray(), |
| | 0 | 379 | | Limit = query.Limit, |
| | 0 | 380 | | ParentId = query.ParentId |
| | 0 | 381 | | }; |
| | | 382 | | } |
| | | 383 | | |
| | | 384 | | private static List<BaseItemKind> BuildExcludeItemTypes(SearchQuery query) |
| | | 385 | | { |
| | 0 | 386 | | var excludeItemTypes = query.ExcludeItemTypes.ToList(); |
| | | 387 | | |
| | 0 | 388 | | excludeItemTypes.Add(BaseItemKind.Year); |
| | 0 | 389 | | excludeItemTypes.Add(BaseItemKind.Folder); |
| | 0 | 390 | | excludeItemTypes.Add(BaseItemKind.CollectionFolder); |
| | | 391 | | |
| | 0 | 392 | | if (!query.IncludeGenres) |
| | | 393 | | { |
| | 0 | 394 | | AddIfMissing(excludeItemTypes, BaseItemKind.Genre); |
| | 0 | 395 | | AddIfMissing(excludeItemTypes, BaseItemKind.MusicGenre); |
| | | 396 | | } |
| | | 397 | | |
| | 0 | 398 | | if (!query.IncludePeople) |
| | | 399 | | { |
| | 0 | 400 | | AddIfMissing(excludeItemTypes, BaseItemKind.Person); |
| | | 401 | | } |
| | | 402 | | |
| | 0 | 403 | | if (!query.IncludeStudios) |
| | | 404 | | { |
| | 0 | 405 | | AddIfMissing(excludeItemTypes, BaseItemKind.Studio); |
| | | 406 | | } |
| | | 407 | | |
| | 0 | 408 | | if (!query.IncludeArtists) |
| | | 409 | | { |
| | 0 | 410 | | AddIfMissing(excludeItemTypes, BaseItemKind.MusicArtist); |
| | | 411 | | } |
| | | 412 | | |
| | 0 | 413 | | return excludeItemTypes; |
| | | 414 | | } |
| | | 415 | | |
| | | 416 | | private static List<BaseItemKind> BuildIncludeItemTypes(SearchQuery query) |
| | | 417 | | { |
| | 0 | 418 | | var includeItemTypes = query.IncludeItemTypes.ToList(); |
| | 0 | 419 | | if (query.IncludeMedia) |
| | | 420 | | { |
| | 0 | 421 | | return includeItemTypes; |
| | | 422 | | } |
| | | 423 | | |
| | 0 | 424 | | if (query.IncludeGenres && IsEmptyOrContains(includeItemTypes, BaseItemKind.Genre)) |
| | | 425 | | { |
| | 0 | 426 | | AddIfMissing(includeItemTypes, BaseItemKind.Genre); |
| | 0 | 427 | | AddIfMissing(includeItemTypes, BaseItemKind.MusicGenre); |
| | | 428 | | } |
| | | 429 | | |
| | 0 | 430 | | if (query.IncludePeople && IsEmptyOrContains(includeItemTypes, BaseItemKind.Person)) |
| | | 431 | | { |
| | 0 | 432 | | AddIfMissing(includeItemTypes, BaseItemKind.Person); |
| | | 433 | | } |
| | | 434 | | |
| | 0 | 435 | | if (query.IncludeStudios && IsEmptyOrContains(includeItemTypes, BaseItemKind.Studio)) |
| | | 436 | | { |
| | 0 | 437 | | AddIfMissing(includeItemTypes, BaseItemKind.Studio); |
| | | 438 | | } |
| | | 439 | | |
| | 0 | 440 | | if (query.IncludeArtists && IsEmptyOrContains(includeItemTypes, BaseItemKind.MusicArtist)) |
| | | 441 | | { |
| | 0 | 442 | | AddIfMissing(includeItemTypes, BaseItemKind.MusicArtist); |
| | | 443 | | } |
| | | 444 | | |
| | 0 | 445 | | return includeItemTypes; |
| | | 446 | | } |
| | | 447 | | |
| | | 448 | | private static bool IsEmptyOrContains(List<BaseItemKind> list, BaseItemKind value) |
| | 0 | 449 | | => list.Count == 0 || list.Contains(value); |
| | | 450 | | |
| | | 451 | | private static void AddIfMissing(List<BaseItemKind> list, BaseItemKind value) |
| | | 452 | | { |
| | 0 | 453 | | if (!list.Contains(value)) |
| | | 454 | | { |
| | 0 | 455 | | list.Add(value); |
| | | 456 | | } |
| | 0 | 457 | | } |
| | | 458 | | } |