| | 1 | | #pragma warning disable RS0030 // Do not use banned APIs |
| | 2 | | // Do not enforce that because EFCore cannot deal with cultures well. |
| | 3 | | #pragma warning disable CA1304 // Specify CultureInfo |
| | 4 | | #pragma warning disable CA1311 // Specify a culture or use an invariant version |
| | 5 | | #pragma warning disable CA1862 // Use the 'StringComparison' method overloads to perform case-insensitive string compari |
| | 6 | |
|
| | 7 | | using System; |
| | 8 | | using System.Collections.Concurrent; |
| | 9 | | using System.Collections.Generic; |
| | 10 | | using System.Globalization; |
| | 11 | | using System.Linq; |
| | 12 | | using System.Linq.Expressions; |
| | 13 | | using System.Reflection; |
| | 14 | | using System.Text; |
| | 15 | | using System.Text.Json; |
| | 16 | | using System.Threading; |
| | 17 | | using System.Threading.Tasks; |
| | 18 | | using Jellyfin.Data.Enums; |
| | 19 | | using Jellyfin.Database.Implementations; |
| | 20 | | using Jellyfin.Database.Implementations.Entities; |
| | 21 | | using Jellyfin.Database.Implementations.Enums; |
| | 22 | | using Jellyfin.Extensions; |
| | 23 | | using Jellyfin.Extensions.Json; |
| | 24 | | using Jellyfin.Server.Implementations.Extensions; |
| | 25 | | using MediaBrowser.Common; |
| | 26 | | using MediaBrowser.Controller; |
| | 27 | | using MediaBrowser.Controller.Channels; |
| | 28 | | using MediaBrowser.Controller.Configuration; |
| | 29 | | using MediaBrowser.Controller.Entities; |
| | 30 | | using MediaBrowser.Controller.Entities.Audio; |
| | 31 | | using MediaBrowser.Controller.Entities.TV; |
| | 32 | | using MediaBrowser.Controller.LiveTv; |
| | 33 | | using MediaBrowser.Controller.Persistence; |
| | 34 | | using MediaBrowser.Model.Dto; |
| | 35 | | using MediaBrowser.Model.Entities; |
| | 36 | | using MediaBrowser.Model.LiveTv; |
| | 37 | | using MediaBrowser.Model.Querying; |
| | 38 | | using Microsoft.EntityFrameworkCore; |
| | 39 | | using Microsoft.Extensions.Logging; |
| | 40 | | using BaseItemDto = MediaBrowser.Controller.Entities.BaseItem; |
| | 41 | | using BaseItemEntity = Jellyfin.Database.Implementations.Entities.BaseItemEntity; |
| | 42 | |
|
| | 43 | | namespace Jellyfin.Server.Implementations.Item; |
| | 44 | |
|
| | 45 | | /* |
| | 46 | | All queries in this class and all other nullable enabled EFCore repository classes will make liberal use of the null |
| | 47 | | This is done as the code isn't actually executed client side, but only the expressions are interpret and the compile |
| | 48 | | This is your only warning/message regarding this topic. |
| | 49 | | */ |
| | 50 | |
|
| | 51 | | /// <summary> |
| | 52 | | /// Handles all storage logic for BaseItems. |
| | 53 | | /// </summary> |
| | 54 | | public sealed class BaseItemRepository |
| | 55 | | : IItemRepository |
| | 56 | | { |
| | 57 | | /// <summary> |
| | 58 | | /// Gets the placeholder id for UserData detached items. |
| | 59 | | /// </summary> |
| 1 | 60 | | public static readonly Guid PlaceholderId = Guid.Parse("00000000-0000-0000-0000-000000000001"); |
| | 61 | |
|
| | 62 | | /// <summary> |
| | 63 | | /// This holds all the types in the running assemblies |
| | 64 | | /// so that we can de-serialize properly when we don't have strong types. |
| | 65 | | /// </summary> |
| 1 | 66 | | private static readonly ConcurrentDictionary<string, Type?> _typeMap = new ConcurrentDictionary<string, Type?>(); |
| | 67 | | private readonly IDbContextFactory<JellyfinDbContext> _dbProvider; |
| | 68 | | private readonly IServerApplicationHost _appHost; |
| | 69 | | private readonly IItemTypeLookup _itemTypeLookup; |
| | 70 | | private readonly IServerConfigurationManager _serverConfigurationManager; |
| | 71 | | private readonly ILogger<BaseItemRepository> _logger; |
| | 72 | |
|
| 1 | 73 | | private static readonly IReadOnlyList<ItemValueType> _getAllArtistsValueTypes = [ItemValueType.Artist, ItemValueType |
| 1 | 74 | | private static readonly IReadOnlyList<ItemValueType> _getArtistValueTypes = [ItemValueType.Artist]; |
| 1 | 75 | | private static readonly IReadOnlyList<ItemValueType> _getAlbumArtistValueTypes = [ItemValueType.AlbumArtist]; |
| 1 | 76 | | private static readonly IReadOnlyList<ItemValueType> _getStudiosValueTypes = [ItemValueType.Studios]; |
| 1 | 77 | | private static readonly IReadOnlyList<ItemValueType> _getGenreValueTypes = [ItemValueType.Genre]; |
| 1 | 78 | | private static readonly IReadOnlyList<char> SearchWildcardTerms = ['%', '_', '[', ']', '^']; |
| | 79 | |
|
| | 80 | | /// <summary> |
| | 81 | | /// Initializes a new instance of the <see cref="BaseItemRepository"/> class. |
| | 82 | | /// </summary> |
| | 83 | | /// <param name="dbProvider">The db factory.</param> |
| | 84 | | /// <param name="appHost">The Application host.</param> |
| | 85 | | /// <param name="itemTypeLookup">The static type lookup.</param> |
| | 86 | | /// <param name="serverConfigurationManager">The server Configuration manager.</param> |
| | 87 | | /// <param name="logger">System logger.</param> |
| | 88 | | public BaseItemRepository( |
| | 89 | | IDbContextFactory<JellyfinDbContext> dbProvider, |
| | 90 | | IServerApplicationHost appHost, |
| | 91 | | IItemTypeLookup itemTypeLookup, |
| | 92 | | IServerConfigurationManager serverConfigurationManager, |
| | 93 | | ILogger<BaseItemRepository> logger) |
| | 94 | | { |
| 21 | 95 | | _dbProvider = dbProvider; |
| 21 | 96 | | _appHost = appHost; |
| 21 | 97 | | _itemTypeLookup = itemTypeLookup; |
| 21 | 98 | | _serverConfigurationManager = serverConfigurationManager; |
| 21 | 99 | | _logger = logger; |
| 21 | 100 | | } |
| | 101 | |
|
| | 102 | | /// <inheritdoc /> |
| | 103 | | public void DeleteItem(params IReadOnlyList<Guid> ids) |
| | 104 | | { |
| 2 | 105 | | if (ids is null || ids.Count == 0 || ids.Any(f => f.Equals(PlaceholderId))) |
| | 106 | | { |
| 0 | 107 | | throw new ArgumentException("Guid can't be empty or the placeholder id.", nameof(ids)); |
| | 108 | | } |
| | 109 | |
|
| 2 | 110 | | using var context = _dbProvider.CreateDbContext(); |
| 2 | 111 | | using var transaction = context.Database.BeginTransaction(); |
| | 112 | |
|
| 2 | 113 | | var date = (DateTime?)DateTime.UtcNow; |
| | 114 | |
|
| 2 | 115 | | var relatedItems = ids.SelectMany(f => TraverseHirachyDown(f, context)).ToArray(); |
| | 116 | |
|
| | 117 | | // Remove any UserData entries for the placeholder item that would conflict with the UserData |
| | 118 | | // being detached from the item being deleted. This is necessary because, during an update, |
| | 119 | | // UserData may be reattached to a new entry, but some entries can be left behind. |
| | 120 | | // Ensures there are no duplicate UserId/CustomDataKey combinations for the placeholder. |
| 2 | 121 | | context.UserData |
| 2 | 122 | | .Join( |
| 2 | 123 | | context.UserData.WhereOneOrMany(relatedItems, e => e.ItemId), |
| 2 | 124 | | placeholder => new { placeholder.UserId, placeholder.CustomDataKey }, |
| 2 | 125 | | userData => new { userData.UserId, userData.CustomDataKey }, |
| 2 | 126 | | (placeholder, userData) => placeholder) |
| 2 | 127 | | .Where(e => e.ItemId == PlaceholderId) |
| 2 | 128 | | .ExecuteDelete(); |
| | 129 | |
|
| | 130 | | // Detach all user watch data |
| 2 | 131 | | context.UserData.WhereOneOrMany(relatedItems, e => e.ItemId) |
| 2 | 132 | | .ExecuteUpdate(e => e |
| 2 | 133 | | .SetProperty(f => f.RetentionDate, date) |
| 2 | 134 | | .SetProperty(f => f.ItemId, PlaceholderId)); |
| | 135 | |
|
| 2 | 136 | | context.AncestorIds.WhereOneOrMany(relatedItems, e => e.ItemId).ExecuteDelete(); |
| 2 | 137 | | context.AncestorIds.WhereOneOrMany(relatedItems, e => e.ParentItemId).ExecuteDelete(); |
| 2 | 138 | | context.AttachmentStreamInfos.WhereOneOrMany(relatedItems, e => e.ItemId).ExecuteDelete(); |
| 2 | 139 | | context.BaseItemImageInfos.WhereOneOrMany(relatedItems, e => e.ItemId).ExecuteDelete(); |
| 2 | 140 | | context.BaseItemMetadataFields.WhereOneOrMany(relatedItems, e => e.ItemId).ExecuteDelete(); |
| 2 | 141 | | context.BaseItemProviders.WhereOneOrMany(relatedItems, e => e.ItemId).ExecuteDelete(); |
| 2 | 142 | | context.BaseItemTrailerTypes.WhereOneOrMany(relatedItems, e => e.ItemId).ExecuteDelete(); |
| 2 | 143 | | context.BaseItems.WhereOneOrMany(relatedItems, e => e.Id).ExecuteDelete(); |
| 2 | 144 | | context.Chapters.WhereOneOrMany(relatedItems, e => e.ItemId).ExecuteDelete(); |
| 2 | 145 | | context.CustomItemDisplayPreferences.WhereOneOrMany(relatedItems, e => e.ItemId).ExecuteDelete(); |
| 2 | 146 | | context.ItemDisplayPreferences.WhereOneOrMany(relatedItems, e => e.ItemId).ExecuteDelete(); |
| 2 | 147 | | context.ItemValues.Where(e => e.BaseItemsMap!.Count == 0).ExecuteDelete(); |
| 2 | 148 | | context.ItemValuesMap.WhereOneOrMany(relatedItems, e => e.ItemId).ExecuteDelete(); |
| 2 | 149 | | context.KeyframeData.WhereOneOrMany(relatedItems, e => e.ItemId).ExecuteDelete(); |
| 2 | 150 | | context.MediaSegments.WhereOneOrMany(relatedItems, e => e.ItemId).ExecuteDelete(); |
| 2 | 151 | | context.MediaStreamInfos.WhereOneOrMany(relatedItems, e => e.ItemId).ExecuteDelete(); |
| 2 | 152 | | var query = context.PeopleBaseItemMap.WhereOneOrMany(relatedItems, e => e.ItemId).Select(f => f.PeopleId).Distin |
| 2 | 153 | | context.PeopleBaseItemMap.WhereOneOrMany(relatedItems, e => e.ItemId).ExecuteDelete(); |
| 2 | 154 | | context.Peoples.WhereOneOrMany(query, e => e.Id).Where(e => e.BaseItems!.Count == 0).ExecuteDelete(); |
| 2 | 155 | | context.TrickplayInfos.WhereOneOrMany(relatedItems, e => e.ItemId).ExecuteDelete(); |
| 2 | 156 | | context.SaveChanges(); |
| 2 | 157 | | transaction.Commit(); |
| 4 | 158 | | } |
| | 159 | |
|
| | 160 | | /// <inheritdoc /> |
| | 161 | | public void UpdateInheritedValues() |
| | 162 | | { |
| 16 | 163 | | using var context = _dbProvider.CreateDbContext(); |
| 16 | 164 | | using var transaction = context.Database.BeginTransaction(); |
| | 165 | |
|
| 16 | 166 | | context.ItemValuesMap.Where(e => e.ItemValue.Type == ItemValueType.InheritedTags).ExecuteDelete(); |
| | 167 | | // ItemValue Inheritance is now correctly mapped via AncestorId on demand |
| 16 | 168 | | context.SaveChanges(); |
| | 169 | |
|
| 16 | 170 | | transaction.Commit(); |
| 32 | 171 | | } |
| | 172 | |
|
| | 173 | | /// <inheritdoc /> |
| | 174 | | public IReadOnlyList<Guid> GetItemIdsList(InternalItemsQuery filter) |
| | 175 | | { |
| 17 | 176 | | ArgumentNullException.ThrowIfNull(filter); |
| 17 | 177 | | PrepareFilterQuery(filter); |
| | 178 | |
|
| 17 | 179 | | using var context = _dbProvider.CreateDbContext(); |
| 17 | 180 | | return ApplyQueryFilter(context.BaseItems.AsNoTracking().Where(e => e.Id != EF.Constant(PlaceholderId)), context |
| 17 | 181 | | } |
| | 182 | |
|
| | 183 | | /// <inheritdoc /> |
| | 184 | | public QueryResult<(BaseItemDto Item, ItemCounts? ItemCounts)> GetAllArtists(InternalItemsQuery filter) |
| | 185 | | { |
| 0 | 186 | | return GetItemValues(filter, _getAllArtistsValueTypes, _itemTypeLookup.BaseItemKindNames[BaseItemKind.MusicArtis |
| | 187 | | } |
| | 188 | |
|
| | 189 | | /// <inheritdoc /> |
| | 190 | | public QueryResult<(BaseItemDto Item, ItemCounts? ItemCounts)> GetArtists(InternalItemsQuery filter) |
| | 191 | | { |
| 0 | 192 | | return GetItemValues(filter, _getArtistValueTypes, _itemTypeLookup.BaseItemKindNames[BaseItemKind.MusicArtist]); |
| | 193 | | } |
| | 194 | |
|
| | 195 | | /// <inheritdoc /> |
| | 196 | | public QueryResult<(BaseItemDto Item, ItemCounts? ItemCounts)> GetAlbumArtists(InternalItemsQuery filter) |
| | 197 | | { |
| 0 | 198 | | return GetItemValues(filter, _getAlbumArtistValueTypes, _itemTypeLookup.BaseItemKindNames[BaseItemKind.MusicArti |
| | 199 | | } |
| | 200 | |
|
| | 201 | | /// <inheritdoc /> |
| | 202 | | public QueryResult<(BaseItemDto Item, ItemCounts? ItemCounts)> GetStudios(InternalItemsQuery filter) |
| | 203 | | { |
| 0 | 204 | | return GetItemValues(filter, _getStudiosValueTypes, _itemTypeLookup.BaseItemKindNames[BaseItemKind.Studio]); |
| | 205 | | } |
| | 206 | |
|
| | 207 | | /// <inheritdoc /> |
| | 208 | | public QueryResult<(BaseItemDto Item, ItemCounts? ItemCounts)> GetGenres(InternalItemsQuery filter) |
| | 209 | | { |
| 0 | 210 | | return GetItemValues(filter, _getGenreValueTypes, _itemTypeLookup.BaseItemKindNames[BaseItemKind.Genre]); |
| | 211 | | } |
| | 212 | |
|
| | 213 | | /// <inheritdoc /> |
| | 214 | | public QueryResult<(BaseItemDto Item, ItemCounts? ItemCounts)> GetMusicGenres(InternalItemsQuery filter) |
| | 215 | | { |
| 0 | 216 | | return GetItemValues(filter, _getGenreValueTypes, _itemTypeLookup.BaseItemKindNames[BaseItemKind.MusicGenre]); |
| | 217 | | } |
| | 218 | |
|
| | 219 | | /// <inheritdoc /> |
| | 220 | | public IReadOnlyList<string> GetStudioNames() |
| | 221 | | { |
| 17 | 222 | | return GetItemValueNames(_getStudiosValueTypes, [], []); |
| | 223 | | } |
| | 224 | |
|
| | 225 | | /// <inheritdoc /> |
| | 226 | | public IReadOnlyList<string> GetAllArtistNames() |
| | 227 | | { |
| 17 | 228 | | return GetItemValueNames(_getAllArtistsValueTypes, [], []); |
| | 229 | | } |
| | 230 | |
|
| | 231 | | /// <inheritdoc /> |
| | 232 | | public IReadOnlyList<string> GetMusicGenreNames() |
| | 233 | | { |
| 17 | 234 | | return GetItemValueNames( |
| 17 | 235 | | _getGenreValueTypes, |
| 17 | 236 | | _itemTypeLookup.MusicGenreTypes, |
| 17 | 237 | | []); |
| | 238 | | } |
| | 239 | |
|
| | 240 | | /// <inheritdoc /> |
| | 241 | | public IReadOnlyList<string> GetGenreNames() |
| | 242 | | { |
| 17 | 243 | | return GetItemValueNames( |
| 17 | 244 | | _getGenreValueTypes, |
| 17 | 245 | | [], |
| 17 | 246 | | _itemTypeLookup.MusicGenreTypes); |
| | 247 | | } |
| | 248 | |
|
| | 249 | | /// <inheritdoc /> |
| | 250 | | public QueryResult<BaseItemDto> GetItems(InternalItemsQuery filter) |
| | 251 | | { |
| 1 | 252 | | ArgumentNullException.ThrowIfNull(filter); |
| 1 | 253 | | if (!filter.EnableTotalRecordCount || (!filter.Limit.HasValue && (filter.StartIndex ?? 0) == 0)) |
| | 254 | | { |
| 1 | 255 | | var returnList = GetItemList(filter); |
| 1 | 256 | | return new QueryResult<BaseItemDto>( |
| 1 | 257 | | filter.StartIndex, |
| 1 | 258 | | returnList.Count, |
| 1 | 259 | | returnList); |
| | 260 | | } |
| | 261 | |
|
| 0 | 262 | | PrepareFilterQuery(filter); |
| 0 | 263 | | var result = new QueryResult<BaseItemDto>(); |
| | 264 | |
|
| 0 | 265 | | using var context = _dbProvider.CreateDbContext(); |
| | 266 | |
|
| 0 | 267 | | IQueryable<BaseItemEntity> dbQuery = PrepareItemQuery(context, filter); |
| | 268 | |
|
| 0 | 269 | | dbQuery = TranslateQuery(dbQuery, context, filter); |
| 0 | 270 | | dbQuery = ApplyGroupingFilter(context, dbQuery, filter); |
| | 271 | |
|
| 0 | 272 | | if (filter.EnableTotalRecordCount) |
| | 273 | | { |
| 0 | 274 | | result.TotalRecordCount = dbQuery.Count(); |
| | 275 | | } |
| | 276 | |
|
| 0 | 277 | | dbQuery = ApplyQueryPaging(dbQuery, filter); |
| | 278 | |
|
| 0 | 279 | | result.Items = dbQuery.AsEnumerable().Where(e => e is not null).Select(w => DeserializeBaseItem(w, filter.SkipDe |
| 0 | 280 | | result.StartIndex = filter.StartIndex ?? 0; |
| 0 | 281 | | return result; |
| 0 | 282 | | } |
| | 283 | |
|
| | 284 | | /// <inheritdoc /> |
| | 285 | | public IReadOnlyList<BaseItemDto> GetItemList(InternalItemsQuery filter) |
| | 286 | | { |
| 307 | 287 | | ArgumentNullException.ThrowIfNull(filter); |
| 307 | 288 | | PrepareFilterQuery(filter); |
| | 289 | |
|
| 307 | 290 | | using var context = _dbProvider.CreateDbContext(); |
| 307 | 291 | | IQueryable<BaseItemEntity> dbQuery = PrepareItemQuery(context, filter); |
| | 292 | |
|
| 307 | 293 | | dbQuery = TranslateQuery(dbQuery, context, filter); |
| | 294 | |
|
| 307 | 295 | | dbQuery = ApplyGroupingFilter(context, dbQuery, filter); |
| 307 | 296 | | dbQuery = ApplyQueryPaging(dbQuery, filter); |
| | 297 | |
|
| 307 | 298 | | return dbQuery.AsEnumerable().Where(e => e is not null).Select(w => DeserializeBaseItem(w, filter.SkipDeserializ |
| 307 | 299 | | } |
| | 300 | |
|
| | 301 | | /// <inheritdoc/> |
| | 302 | | public IReadOnlyList<BaseItem> GetLatestItemList(InternalItemsQuery filter, CollectionType collectionType) |
| | 303 | | { |
| 0 | 304 | | ArgumentNullException.ThrowIfNull(filter); |
| 0 | 305 | | PrepareFilterQuery(filter); |
| | 306 | |
|
| | 307 | | // Early exit if collection type is not tvshows or music |
| 0 | 308 | | if (collectionType != CollectionType.tvshows && collectionType != CollectionType.music) |
| | 309 | | { |
| 0 | 310 | | return Array.Empty<BaseItem>(); |
| | 311 | | } |
| | 312 | |
|
| 0 | 313 | | using var context = _dbProvider.CreateDbContext(); |
| | 314 | |
|
| | 315 | | // Subquery to group by SeriesNames/Album and get the max Date Created for each group. |
| 0 | 316 | | var subquery = PrepareItemQuery(context, filter); |
| 0 | 317 | | subquery = TranslateQuery(subquery, context, filter); |
| 0 | 318 | | var subqueryGrouped = subquery.GroupBy(g => collectionType == CollectionType.tvshows ? g.SeriesName : g.Album) |
| 0 | 319 | | .Select(g => new |
| 0 | 320 | | { |
| 0 | 321 | | Key = g.Key, |
| 0 | 322 | | MaxDateCreated = g.Max(a => a.DateCreated) |
| 0 | 323 | | }) |
| 0 | 324 | | .OrderByDescending(g => g.MaxDateCreated) |
| 0 | 325 | | .Select(g => g); |
| | 326 | |
|
| 0 | 327 | | if (filter.Limit.HasValue) |
| | 328 | | { |
| 0 | 329 | | subqueryGrouped = subqueryGrouped.Take(filter.Limit.Value); |
| | 330 | | } |
| | 331 | |
|
| 0 | 332 | | filter.Limit = null; |
| | 333 | |
|
| 0 | 334 | | var mainquery = PrepareItemQuery(context, filter); |
| 0 | 335 | | mainquery = TranslateQuery(mainquery, context, filter); |
| 0 | 336 | | mainquery = mainquery.Where(g => g.DateCreated >= subqueryGrouped.Min(s => s.MaxDateCreated)); |
| 0 | 337 | | mainquery = ApplyGroupingFilter(context, mainquery, filter); |
| 0 | 338 | | mainquery = ApplyQueryPaging(mainquery, filter); |
| | 339 | |
|
| 0 | 340 | | return mainquery.AsEnumerable().Where(e => e is not null).Select(w => DeserializeBaseItem(w, filter.SkipDeserial |
| 0 | 341 | | } |
| | 342 | |
|
| | 343 | | /// <inheritdoc /> |
| | 344 | | public IReadOnlyList<string> GetNextUpSeriesKeys(InternalItemsQuery filter, DateTime dateCutoff) |
| | 345 | | { |
| 0 | 346 | | ArgumentNullException.ThrowIfNull(filter); |
| 0 | 347 | | ArgumentNullException.ThrowIfNull(filter.User); |
| | 348 | |
|
| 0 | 349 | | using var context = _dbProvider.CreateDbContext(); |
| | 350 | |
|
| 0 | 351 | | var query = context.BaseItems |
| 0 | 352 | | .AsNoTracking() |
| 0 | 353 | | .Where(i => filter.TopParentIds.Contains(i.TopParentId!.Value)) |
| 0 | 354 | | .Where(i => i.Type == _itemTypeLookup.BaseItemKindNames[BaseItemKind.Episode]) |
| 0 | 355 | | .Join( |
| 0 | 356 | | context.UserData.AsNoTracking().Where(e => e.ItemId != EF.Constant(PlaceholderId)), |
| 0 | 357 | | i => new { UserId = filter.User.Id, ItemId = i.Id }, |
| 0 | 358 | | u => new { UserId = u.UserId, ItemId = u.ItemId }, |
| 0 | 359 | | (entity, data) => new { Item = entity, UserData = data }) |
| 0 | 360 | | .GroupBy(g => g.Item.SeriesPresentationUniqueKey) |
| 0 | 361 | | .Select(g => new { g.Key, LastPlayedDate = g.Max(u => u.UserData.LastPlayedDate) }) |
| 0 | 362 | | .Where(g => g.Key != null && g.LastPlayedDate != null && g.LastPlayedDate >= dateCutoff) |
| 0 | 363 | | .OrderByDescending(g => g.LastPlayedDate) |
| 0 | 364 | | .Select(g => g.Key!); |
| | 365 | |
|
| 0 | 366 | | if (filter.Limit.HasValue) |
| | 367 | | { |
| 0 | 368 | | query = query.Take(filter.Limit.Value); |
| | 369 | | } |
| | 370 | |
|
| 0 | 371 | | return query.ToArray(); |
| 0 | 372 | | } |
| | 373 | |
|
| | 374 | | private IQueryable<BaseItemEntity> ApplyGroupingFilter(JellyfinDbContext context, IQueryable<BaseItemEntity> dbQuery |
| | 375 | | { |
| | 376 | | // This whole block is needed to filter duplicate entries on request |
| | 377 | | // for the time being it cannot be used because it would destroy the ordering |
| | 378 | | // this results in "duplicate" responses for queries that try to lookup individual series or multiple versions b |
| | 379 | | // for that case the invoker has to run a DistinctBy(e => e.PresentationUniqueKey) on their own |
| | 380 | |
|
| 324 | 381 | | var enableGroupByPresentationUniqueKey = EnableGroupByPresentationUniqueKey(filter); |
| 324 | 382 | | if (enableGroupByPresentationUniqueKey && filter.GroupBySeriesPresentationUniqueKey) |
| | 383 | | { |
| 0 | 384 | | var tempQuery = dbQuery.GroupBy(e => new { e.PresentationUniqueKey, e.SeriesPresentationUniqueKey }).Select( |
| 0 | 385 | | dbQuery = context.BaseItems.Where(e => tempQuery.Contains(e.Id)); |
| | 386 | | } |
| 324 | 387 | | else if (enableGroupByPresentationUniqueKey) |
| | 388 | | { |
| 1 | 389 | | var tempQuery = dbQuery.GroupBy(e => e.PresentationUniqueKey).Select(e => e.FirstOrDefault()).Select(e => e! |
| 1 | 390 | | dbQuery = context.BaseItems.Where(e => tempQuery.Contains(e.Id)); |
| | 391 | | } |
| 323 | 392 | | else if (filter.GroupBySeriesPresentationUniqueKey) |
| | 393 | | { |
| 0 | 394 | | var tempQuery = dbQuery.GroupBy(e => e.SeriesPresentationUniqueKey).Select(e => e.FirstOrDefault()).Select(e |
| 0 | 395 | | dbQuery = context.BaseItems.Where(e => tempQuery.Contains(e.Id)); |
| | 396 | | } |
| | 397 | | else |
| | 398 | | { |
| 323 | 399 | | dbQuery = dbQuery.Distinct(); |
| | 400 | | } |
| | 401 | |
|
| 324 | 402 | | dbQuery = ApplyOrder(dbQuery, filter); |
| | 403 | |
|
| 324 | 404 | | dbQuery = ApplyNavigations(dbQuery, filter); |
| | 405 | |
|
| 324 | 406 | | return dbQuery; |
| | 407 | | } |
| | 408 | |
|
| | 409 | | private static IQueryable<BaseItemEntity> ApplyNavigations(IQueryable<BaseItemEntity> dbQuery, InternalItemsQuery fi |
| | 410 | | { |
| 324 | 411 | | dbQuery = dbQuery.Include(e => e.TrailerTypes) |
| 324 | 412 | | .Include(e => e.Provider) |
| 324 | 413 | | .Include(e => e.LockedFields) |
| 324 | 414 | | .Include(e => e.UserData); |
| | 415 | |
|
| 324 | 416 | | if (filter.DtoOptions.EnableImages) |
| | 417 | | { |
| 324 | 418 | | dbQuery = dbQuery.Include(e => e.Images); |
| | 419 | | } |
| | 420 | |
|
| 324 | 421 | | return dbQuery; |
| | 422 | | } |
| | 423 | |
|
| | 424 | | private IQueryable<BaseItemEntity> ApplyQueryPaging(IQueryable<BaseItemEntity> dbQuery, InternalItemsQuery filter) |
| | 425 | | { |
| 324 | 426 | | if (filter.Limit.HasValue || filter.StartIndex.HasValue) |
| | 427 | | { |
| 106 | 428 | | var offset = filter.StartIndex ?? 0; |
| | 429 | |
|
| 106 | 430 | | if (offset > 0) |
| | 431 | | { |
| 0 | 432 | | dbQuery = dbQuery.Skip(offset); |
| | 433 | | } |
| | 434 | |
|
| 106 | 435 | | if (filter.Limit.HasValue) |
| | 436 | | { |
| 106 | 437 | | dbQuery = dbQuery.Take(filter.Limit.Value); |
| | 438 | | } |
| | 439 | | } |
| | 440 | |
|
| 324 | 441 | | return dbQuery; |
| | 442 | | } |
| | 443 | |
|
| | 444 | | private IQueryable<BaseItemEntity> ApplyQueryFilter(IQueryable<BaseItemEntity> dbQuery, JellyfinDbContext context, I |
| | 445 | | { |
| 17 | 446 | | dbQuery = TranslateQuery(dbQuery, context, filter); |
| 17 | 447 | | dbQuery = ApplyGroupingFilter(context, dbQuery, filter); |
| 17 | 448 | | dbQuery = ApplyQueryPaging(dbQuery, filter); |
| 17 | 449 | | return dbQuery; |
| | 450 | | } |
| | 451 | |
|
| | 452 | | private IQueryable<BaseItemEntity> PrepareItemQuery(JellyfinDbContext context, InternalItemsQuery filter) |
| | 453 | | { |
| 393 | 454 | | IQueryable<BaseItemEntity> dbQuery = context.BaseItems.AsNoTracking(); |
| 393 | 455 | | dbQuery = dbQuery.AsSingleQuery(); |
| | 456 | |
|
| 393 | 457 | | return dbQuery; |
| | 458 | | } |
| | 459 | |
|
| | 460 | | /// <inheritdoc/> |
| | 461 | | public int GetCount(InternalItemsQuery filter) |
| | 462 | | { |
| 0 | 463 | | ArgumentNullException.ThrowIfNull(filter); |
| | 464 | | // Hack for right now since we currently don't support filtering out these duplicates within a query |
| 0 | 465 | | PrepareFilterQuery(filter); |
| | 466 | |
|
| 0 | 467 | | using var context = _dbProvider.CreateDbContext(); |
| 0 | 468 | | var dbQuery = TranslateQuery(context.BaseItems.AsNoTracking(), context, filter); |
| | 469 | |
|
| 0 | 470 | | return dbQuery.Count(); |
| 0 | 471 | | } |
| | 472 | |
|
| | 473 | | /// <inheritdoc /> |
| | 474 | | public ItemCounts GetItemCounts(InternalItemsQuery filter) |
| | 475 | | { |
| 0 | 476 | | ArgumentNullException.ThrowIfNull(filter); |
| | 477 | | // Hack for right now since we currently don't support filtering out these duplicates within a query |
| 0 | 478 | | PrepareFilterQuery(filter); |
| | 479 | |
|
| 0 | 480 | | using var context = _dbProvider.CreateDbContext(); |
| 0 | 481 | | var dbQuery = TranslateQuery(context.BaseItems.AsNoTracking(), context, filter); |
| | 482 | |
|
| 0 | 483 | | var counts = dbQuery |
| 0 | 484 | | .GroupBy(x => x.Type) |
| 0 | 485 | | .Select(x => new { x.Key, Count = x.Count() }) |
| 0 | 486 | | .ToArray(); |
| | 487 | |
|
| 0 | 488 | | var lookup = _itemTypeLookup.BaseItemKindNames; |
| 0 | 489 | | var result = new ItemCounts(); |
| 0 | 490 | | foreach (var count in counts) |
| | 491 | | { |
| 0 | 492 | | if (string.Equals(count.Key, lookup[BaseItemKind.MusicAlbum], StringComparison.Ordinal)) |
| | 493 | | { |
| 0 | 494 | | result.AlbumCount = count.Count; |
| | 495 | | } |
| 0 | 496 | | else if (string.Equals(count.Key, lookup[BaseItemKind.MusicArtist], StringComparison.Ordinal)) |
| | 497 | | { |
| 0 | 498 | | result.ArtistCount = count.Count; |
| | 499 | | } |
| 0 | 500 | | else if (string.Equals(count.Key, lookup[BaseItemKind.Episode], StringComparison.Ordinal)) |
| | 501 | | { |
| 0 | 502 | | result.EpisodeCount = count.Count; |
| | 503 | | } |
| 0 | 504 | | else if (string.Equals(count.Key, lookup[BaseItemKind.Movie], StringComparison.Ordinal)) |
| | 505 | | { |
| 0 | 506 | | result.MovieCount = count.Count; |
| | 507 | | } |
| 0 | 508 | | else if (string.Equals(count.Key, lookup[BaseItemKind.MusicVideo], StringComparison.Ordinal)) |
| | 509 | | { |
| 0 | 510 | | result.MusicVideoCount = count.Count; |
| | 511 | | } |
| 0 | 512 | | else if (string.Equals(count.Key, lookup[BaseItemKind.LiveTvProgram], StringComparison.Ordinal)) |
| | 513 | | { |
| 0 | 514 | | result.ProgramCount = count.Count; |
| | 515 | | } |
| 0 | 516 | | else if (string.Equals(count.Key, lookup[BaseItemKind.Series], StringComparison.Ordinal)) |
| | 517 | | { |
| 0 | 518 | | result.SeriesCount = count.Count; |
| | 519 | | } |
| 0 | 520 | | else if (string.Equals(count.Key, lookup[BaseItemKind.Audio], StringComparison.Ordinal)) |
| | 521 | | { |
| 0 | 522 | | result.SongCount = count.Count; |
| | 523 | | } |
| 0 | 524 | | else if (string.Equals(count.Key, lookup[BaseItemKind.Trailer], StringComparison.Ordinal)) |
| | 525 | | { |
| 0 | 526 | | result.TrailerCount = count.Count; |
| | 527 | | } |
| | 528 | | } |
| | 529 | |
|
| 0 | 530 | | return result; |
| 0 | 531 | | } |
| | 532 | |
|
| | 533 | | #pragma warning disable CA1307 // Specify StringComparison for clarity |
| | 534 | | /// <summary> |
| | 535 | | /// Gets the type. |
| | 536 | | /// </summary> |
| | 537 | | /// <param name="typeName">Name of the type.</param> |
| | 538 | | /// <returns>Type.</returns> |
| | 539 | | /// <exception cref="ArgumentNullException"><c>typeName</c> is null.</exception> |
| | 540 | | private static Type? GetType(string typeName) |
| | 541 | | { |
| 104 | 542 | | ArgumentException.ThrowIfNullOrEmpty(typeName); |
| | 543 | |
|
| | 544 | | // TODO: this isn't great. Refactor later to be both globally handled by a dedicated service not just an static |
| | 545 | | // currently this is done so that plugins may introduce their own type of baseitems as we dont know when we are |
| 104 | 546 | | return _typeMap.GetOrAdd(typeName, k => AppDomain.CurrentDomain.GetAssemblies() |
| 104 | 547 | | .Select(a => a.GetType(k)) |
| 104 | 548 | | .FirstOrDefault(t => t is not null)); |
| | 549 | | } |
| | 550 | |
|
| | 551 | | /// <inheritdoc /> |
| | 552 | | public void SaveImages(BaseItemDto item) |
| | 553 | | { |
| 0 | 554 | | ArgumentNullException.ThrowIfNull(item); |
| | 555 | |
|
| 0 | 556 | | var images = item.ImageInfos.Select(e => Map(item.Id, e)); |
| 0 | 557 | | using var context = _dbProvider.CreateDbContext(); |
| | 558 | |
|
| 0 | 559 | | if (!context.BaseItems.Any(bi => bi.Id == item.Id)) |
| | 560 | | { |
| 0 | 561 | | _logger.LogWarning("Unable to save ImageInfo for non existing BaseItem"); |
| 0 | 562 | | return; |
| | 563 | | } |
| | 564 | |
|
| 0 | 565 | | context.BaseItemImageInfos.Where(e => e.ItemId == item.Id).ExecuteDelete(); |
| 0 | 566 | | context.BaseItemImageInfos.AddRange(images); |
| 0 | 567 | | context.SaveChanges(); |
| 0 | 568 | | } |
| | 569 | |
|
| | 570 | | /// <inheritdoc /> |
| | 571 | | public void SaveItems(IReadOnlyList<BaseItemDto> items, CancellationToken cancellationToken) |
| | 572 | | { |
| 114 | 573 | | UpdateOrInsertItems(items, cancellationToken); |
| 114 | 574 | | } |
| | 575 | |
|
| | 576 | | /// <inheritdoc cref="IItemRepository"/> |
| | 577 | | public void UpdateOrInsertItems(IReadOnlyList<BaseItemDto> items, CancellationToken cancellationToken) |
| | 578 | | { |
| 114 | 579 | | ArgumentNullException.ThrowIfNull(items); |
| 114 | 580 | | cancellationToken.ThrowIfCancellationRequested(); |
| | 581 | |
|
| 114 | 582 | | var tuples = new List<(BaseItemDto Item, List<Guid>? AncestorIds, BaseItemDto TopParent, IEnumerable<string> Use |
| 456 | 583 | | foreach (var item in items.GroupBy(e => e.Id).Select(e => e.Last()).Where(e => e.Id != PlaceholderId)) |
| | 584 | | { |
| 114 | 585 | | var ancestorIds = item.SupportsAncestors ? |
| 114 | 586 | | item.GetAncestorIds().Distinct().ToList() : |
| 114 | 587 | | null; |
| | 588 | |
|
| 114 | 589 | | var topParent = item.GetTopParent(); |
| | 590 | |
|
| 114 | 591 | | var userdataKey = item.GetUserDataKeys(); |
| 114 | 592 | | var inheritedTags = item.GetInheritedTags(); |
| | 593 | |
|
| 114 | 594 | | tuples.Add((item, ancestorIds, topParent, userdataKey, inheritedTags)); |
| | 595 | | } |
| | 596 | |
|
| 114 | 597 | | using var context = _dbProvider.CreateDbContext(); |
| 114 | 598 | | using var transaction = context.Database.BeginTransaction(); |
| | 599 | |
|
| 114 | 600 | | var ids = tuples.Select(f => f.Item.Id).ToArray(); |
| 114 | 601 | | var existingItems = context.BaseItems.Where(e => ids.Contains(e.Id)).Select(f => f.Id).ToArray(); |
| 114 | 602 | | var newItems = tuples.Where(e => !existingItems.Contains(e.Item.Id)).ToArray(); |
| | 603 | |
|
| 456 | 604 | | foreach (var item in tuples) |
| | 605 | | { |
| 114 | 606 | | var entity = Map(item.Item); |
| | 607 | | // TODO: refactor this "inconsistency" |
| 114 | 608 | | entity.TopParentId = item.TopParent?.Id; |
| | 609 | |
|
| 114 | 610 | | if (!existingItems.Any(e => e == entity.Id)) |
| | 611 | | { |
| 59 | 612 | | context.BaseItems.Add(entity); |
| | 613 | | } |
| | 614 | | else |
| | 615 | | { |
| 55 | 616 | | context.BaseItemProviders.Where(e => e.ItemId == entity.Id).ExecuteDelete(); |
| 55 | 617 | | context.BaseItems.Attach(entity).State = EntityState.Modified; |
| | 618 | | } |
| | 619 | | } |
| | 620 | |
|
| 114 | 621 | | context.SaveChanges(); |
| | 622 | |
|
| 346 | 623 | | foreach (var item in newItems) |
| | 624 | | { |
| | 625 | | // reattach old userData entries |
| 59 | 626 | | var userKeys = item.UserDataKey.ToArray(); |
| 59 | 627 | | var retentionDate = (DateTime?)null; |
| 59 | 628 | | context.UserData |
| 59 | 629 | | .Where(e => e.ItemId == PlaceholderId) |
| 59 | 630 | | .Where(e => userKeys.Contains(e.CustomDataKey)) |
| 59 | 631 | | .ExecuteUpdate(e => e |
| 59 | 632 | | .SetProperty(f => f.ItemId, item.Item.Id) |
| 59 | 633 | | .SetProperty(f => f.RetentionDate, retentionDate)); |
| | 634 | | } |
| | 635 | |
|
| 114 | 636 | | var itemValueMaps = tuples |
| 114 | 637 | | .Select(e => (e.Item, Values: GetItemValuesToSave(e.Item, e.InheritedTags))) |
| 114 | 638 | | .ToArray(); |
| 114 | 639 | | var allListedItemValues = itemValueMaps |
| 114 | 640 | | .SelectMany(f => f.Values) |
| 114 | 641 | | .Distinct() |
| 114 | 642 | | .ToArray(); |
| 114 | 643 | | var existingValues = context.ItemValues |
| 114 | 644 | | .Select(e => new |
| 114 | 645 | | { |
| 114 | 646 | | item = e, |
| 114 | 647 | | Key = e.Type + "+" + e.Value |
| 114 | 648 | | }) |
| 114 | 649 | | .Where(f => allListedItemValues.Select(e => $"{(int)e.MagicNumber}+{e.Value}").Contains(f.Key)) |
| 114 | 650 | | .Select(e => e.item) |
| 114 | 651 | | .ToArray(); |
| 114 | 652 | | var missingItemValues = allListedItemValues.Except(existingValues.Select(f => (MagicNumber: f.Type, f.Value))).S |
| 114 | 653 | | { |
| 114 | 654 | | CleanValue = GetCleanValue(f.Value), |
| 114 | 655 | | ItemValueId = Guid.NewGuid(), |
| 114 | 656 | | Type = f.MagicNumber, |
| 114 | 657 | | Value = f.Value |
| 114 | 658 | | }).ToArray(); |
| 114 | 659 | | context.ItemValues.AddRange(missingItemValues); |
| 114 | 660 | | context.SaveChanges(); |
| | 661 | |
|
| 114 | 662 | | var itemValuesStore = existingValues.Concat(missingItemValues).ToArray(); |
| 114 | 663 | | var valueMap = itemValueMaps |
| 114 | 664 | | .Select(f => (f.Item, Values: f.Values.Select(e => itemValuesStore.First(g => g.Value == e.Value && g.Type = |
| 114 | 665 | | .ToArray(); |
| | 666 | |
|
| 114 | 667 | | var mappedValues = context.ItemValuesMap.Where(e => ids.Contains(e.ItemId)).ToList(); |
| | 668 | |
|
| 456 | 669 | | foreach (var item in valueMap) |
| | 670 | | { |
| 114 | 671 | | var itemMappedValues = mappedValues.Where(e => e.ItemId == item.Item.Id).ToList(); |
| 228 | 672 | | foreach (var itemValue in item.Values) |
| | 673 | | { |
| 0 | 674 | | var existingItem = itemMappedValues.FirstOrDefault(f => f.ItemValueId == itemValue.ItemValueId); |
| 0 | 675 | | if (existingItem is null) |
| | 676 | | { |
| 0 | 677 | | context.ItemValuesMap.Add(new ItemValueMap() |
| 0 | 678 | | { |
| 0 | 679 | | Item = null!, |
| 0 | 680 | | ItemId = item.Item.Id, |
| 0 | 681 | | ItemValue = null!, |
| 0 | 682 | | ItemValueId = itemValue.ItemValueId |
| 0 | 683 | | }); |
| | 684 | | } |
| | 685 | | else |
| | 686 | | { |
| | 687 | | // map exists, remove from list so its been handled. |
| 0 | 688 | | itemMappedValues.Remove(existingItem); |
| | 689 | | } |
| | 690 | | } |
| | 691 | |
|
| | 692 | | // all still listed values are not in the new list so remove them. |
| 114 | 693 | | context.ItemValuesMap.RemoveRange(itemMappedValues); |
| | 694 | | } |
| | 695 | |
|
| 114 | 696 | | context.SaveChanges(); |
| | 697 | |
|
| 456 | 698 | | foreach (var item in tuples) |
| | 699 | | { |
| 114 | 700 | | if (item.Item.SupportsAncestors && item.AncestorIds != null) |
| | 701 | | { |
| 114 | 702 | | var existingAncestorIds = context.AncestorIds.Where(e => e.ItemId == item.Item.Id).ToList(); |
| 114 | 703 | | var validAncestorIds = context.BaseItems.Where(e => item.AncestorIds.Contains(e.Id)).Select(f => f.Id).T |
| 282 | 704 | | foreach (var ancestorId in validAncestorIds) |
| | 705 | | { |
| 27 | 706 | | var existingAncestorId = existingAncestorIds.FirstOrDefault(e => e.ParentItemId == ancestorId); |
| 27 | 707 | | if (existingAncestorId is null) |
| | 708 | | { |
| 23 | 709 | | context.AncestorIds.Add(new AncestorId() |
| 23 | 710 | | { |
| 23 | 711 | | ParentItemId = ancestorId, |
| 23 | 712 | | ItemId = item.Item.Id, |
| 23 | 713 | | Item = null!, |
| 23 | 714 | | ParentItem = null! |
| 23 | 715 | | }); |
| | 716 | | } |
| | 717 | | else |
| | 718 | | { |
| 4 | 719 | | existingAncestorIds.Remove(existingAncestorId); |
| | 720 | | } |
| | 721 | | } |
| | 722 | |
|
| 114 | 723 | | context.AncestorIds.RemoveRange(existingAncestorIds); |
| | 724 | | } |
| | 725 | | } |
| | 726 | |
|
| 114 | 727 | | context.SaveChanges(); |
| 114 | 728 | | transaction.Commit(); |
| 228 | 729 | | } |
| | 730 | |
|
| | 731 | | /// <inheritdoc /> |
| | 732 | | public BaseItemDto? RetrieveItem(Guid id) |
| | 733 | | { |
| 86 | 734 | | if (id.IsEmpty()) |
| | 735 | | { |
| 0 | 736 | | throw new ArgumentException("Guid can't be empty", nameof(id)); |
| | 737 | | } |
| | 738 | |
|
| 86 | 739 | | using var context = _dbProvider.CreateDbContext(); |
| 86 | 740 | | var dbQuery = PrepareItemQuery(context, new() |
| 86 | 741 | | { |
| 86 | 742 | | DtoOptions = new() |
| 86 | 743 | | { |
| 86 | 744 | | EnableImages = true |
| 86 | 745 | | } |
| 86 | 746 | | }); |
| 86 | 747 | | dbQuery = dbQuery.Include(e => e.TrailerTypes) |
| 86 | 748 | | .Include(e => e.Provider) |
| 86 | 749 | | .Include(e => e.LockedFields) |
| 86 | 750 | | .Include(e => e.UserData) |
| 86 | 751 | | .Include(e => e.Images); |
| | 752 | |
|
| 86 | 753 | | var item = dbQuery.FirstOrDefault(e => e.Id == id); |
| 86 | 754 | | if (item is null) |
| | 755 | | { |
| 86 | 756 | | return null; |
| | 757 | | } |
| | 758 | |
|
| 0 | 759 | | return DeserializeBaseItem(item); |
| 86 | 760 | | } |
| | 761 | |
|
| | 762 | | /// <summary> |
| | 763 | | /// Maps a Entity to the DTO. |
| | 764 | | /// </summary> |
| | 765 | | /// <param name="entity">The entity.</param> |
| | 766 | | /// <param name="dto">The dto base instance.</param> |
| | 767 | | /// <param name="appHost">The Application server Host.</param> |
| | 768 | | /// <param name="logger">The applogger.</param> |
| | 769 | | /// <returns>The dto to map.</returns> |
| | 770 | | public static BaseItemDto Map(BaseItemEntity entity, BaseItemDto dto, IServerApplicationHost? appHost, ILogger logge |
| | 771 | | { |
| 52 | 772 | | dto.Id = entity.Id; |
| 52 | 773 | | dto.ParentId = entity.ParentId.GetValueOrDefault(); |
| 52 | 774 | | dto.Path = appHost?.ExpandVirtualPath(entity.Path) ?? entity.Path; |
| 52 | 775 | | dto.EndDate = entity.EndDate; |
| 52 | 776 | | dto.CommunityRating = entity.CommunityRating; |
| 52 | 777 | | dto.CustomRating = entity.CustomRating; |
| 52 | 778 | | dto.IndexNumber = entity.IndexNumber; |
| 52 | 779 | | dto.IsLocked = entity.IsLocked; |
| 52 | 780 | | dto.Name = entity.Name; |
| 52 | 781 | | dto.OfficialRating = entity.OfficialRating; |
| 52 | 782 | | dto.Overview = entity.Overview; |
| 52 | 783 | | dto.ParentIndexNumber = entity.ParentIndexNumber; |
| 52 | 784 | | dto.PremiereDate = entity.PremiereDate; |
| 52 | 785 | | dto.ProductionYear = entity.ProductionYear; |
| 52 | 786 | | dto.SortName = entity.SortName; |
| 52 | 787 | | dto.ForcedSortName = entity.ForcedSortName; |
| 52 | 788 | | dto.RunTimeTicks = entity.RunTimeTicks; |
| 52 | 789 | | dto.PreferredMetadataLanguage = entity.PreferredMetadataLanguage; |
| 52 | 790 | | dto.PreferredMetadataCountryCode = entity.PreferredMetadataCountryCode; |
| 52 | 791 | | dto.IsInMixedFolder = entity.IsInMixedFolder; |
| 52 | 792 | | dto.InheritedParentalRatingValue = entity.InheritedParentalRatingValue; |
| 52 | 793 | | dto.InheritedParentalRatingSubValue = entity.InheritedParentalRatingSubValue; |
| 52 | 794 | | dto.CriticRating = entity.CriticRating; |
| 52 | 795 | | dto.PresentationUniqueKey = entity.PresentationUniqueKey; |
| 52 | 796 | | dto.OriginalTitle = entity.OriginalTitle; |
| 52 | 797 | | dto.Album = entity.Album; |
| 52 | 798 | | dto.LUFS = entity.LUFS; |
| 52 | 799 | | dto.NormalizationGain = entity.NormalizationGain; |
| 52 | 800 | | dto.IsVirtualItem = entity.IsVirtualItem; |
| 52 | 801 | | dto.ExternalSeriesId = entity.ExternalSeriesId; |
| 52 | 802 | | dto.Tagline = entity.Tagline; |
| 52 | 803 | | dto.TotalBitrate = entity.TotalBitrate; |
| 52 | 804 | | dto.ExternalId = entity.ExternalId; |
| 52 | 805 | | dto.Size = entity.Size; |
| 52 | 806 | | dto.Genres = string.IsNullOrWhiteSpace(entity.Genres) ? [] : entity.Genres.Split('|'); |
| 52 | 807 | | dto.DateCreated = entity.DateCreated ?? DateTime.SpecifyKind(DateTime.MinValue, DateTimeKind.Utc); |
| 52 | 808 | | dto.DateModified = entity.DateModified ?? DateTime.SpecifyKind(DateTime.MinValue, DateTimeKind.Utc); |
| 52 | 809 | | dto.ChannelId = entity.ChannelId ?? Guid.Empty; |
| 52 | 810 | | dto.DateLastRefreshed = entity.DateLastRefreshed ?? DateTime.SpecifyKind(DateTime.MinValue, DateTimeKind.Utc); |
| 52 | 811 | | dto.DateLastSaved = entity.DateLastSaved ?? DateTime.SpecifyKind(DateTime.MinValue, DateTimeKind.Utc); |
| 52 | 812 | | dto.OwnerId = string.IsNullOrWhiteSpace(entity.OwnerId) ? Guid.Empty : (Guid.TryParse(entity.OwnerId, out var ow |
| 52 | 813 | | dto.Width = entity.Width.GetValueOrDefault(); |
| 52 | 814 | | dto.Height = entity.Height.GetValueOrDefault(); |
| 52 | 815 | | dto.UserData = entity.UserData; |
| | 816 | |
|
| 52 | 817 | | if (entity.Provider is not null) |
| | 818 | | { |
| 52 | 819 | | dto.ProviderIds = entity.Provider.ToDictionary(e => e.ProviderId, e => e.ProviderValue); |
| | 820 | | } |
| | 821 | |
|
| 52 | 822 | | if (entity.ExtraType is not null) |
| | 823 | | { |
| 0 | 824 | | dto.ExtraType = (ExtraType)entity.ExtraType; |
| | 825 | | } |
| | 826 | |
|
| 52 | 827 | | if (entity.LockedFields is not null) |
| | 828 | | { |
| 52 | 829 | | dto.LockedFields = entity.LockedFields?.Select(e => (MetadataField)e.Id).ToArray() ?? []; |
| | 830 | | } |
| | 831 | |
|
| 52 | 832 | | if (entity.Audio is not null) |
| | 833 | | { |
| 0 | 834 | | dto.Audio = (ProgramAudio)entity.Audio; |
| | 835 | | } |
| | 836 | |
|
| 52 | 837 | | dto.ExtraIds = string.IsNullOrWhiteSpace(entity.ExtraIds) ? [] : entity.ExtraIds.Split('|').Select(e => Guid.Par |
| 52 | 838 | | dto.ProductionLocations = entity.ProductionLocations?.Split('|') ?? []; |
| 52 | 839 | | dto.Studios = entity.Studios?.Split('|') ?? []; |
| 52 | 840 | | dto.Tags = string.IsNullOrWhiteSpace(entity.Tags) ? [] : entity.Tags.Split('|'); |
| | 841 | |
|
| 52 | 842 | | if (dto is IHasProgramAttributes hasProgramAttributes) |
| | 843 | | { |
| 0 | 844 | | hasProgramAttributes.IsMovie = entity.IsMovie; |
| 0 | 845 | | hasProgramAttributes.IsSeries = entity.IsSeries; |
| 0 | 846 | | hasProgramAttributes.EpisodeTitle = entity.EpisodeTitle; |
| 0 | 847 | | hasProgramAttributes.IsRepeat = entity.IsRepeat; |
| | 848 | | } |
| | 849 | |
|
| 52 | 850 | | if (dto is LiveTvChannel liveTvChannel) |
| | 851 | | { |
| 0 | 852 | | liveTvChannel.ServiceName = entity.ExternalServiceId; |
| | 853 | | } |
| | 854 | |
|
| 52 | 855 | | if (dto is Trailer trailer) |
| | 856 | | { |
| 0 | 857 | | trailer.TrailerTypes = entity.TrailerTypes?.Select(e => (TrailerType)e.Id).ToArray() ?? []; |
| | 858 | | } |
| | 859 | |
|
| 52 | 860 | | if (dto is Video video) |
| | 861 | | { |
| 0 | 862 | | video.PrimaryVersionId = entity.PrimaryVersionId; |
| | 863 | | } |
| | 864 | |
|
| 52 | 865 | | if (dto is IHasSeries hasSeriesName) |
| | 866 | | { |
| 0 | 867 | | hasSeriesName.SeriesName = entity.SeriesName; |
| 0 | 868 | | hasSeriesName.SeriesId = entity.SeriesId.GetValueOrDefault(); |
| 0 | 869 | | hasSeriesName.SeriesPresentationUniqueKey = entity.SeriesPresentationUniqueKey; |
| | 870 | | } |
| | 871 | |
|
| 52 | 872 | | if (dto is Episode episode) |
| | 873 | | { |
| 0 | 874 | | episode.SeasonName = entity.SeasonName; |
| 0 | 875 | | episode.SeasonId = entity.SeasonId.GetValueOrDefault(); |
| | 876 | | } |
| | 877 | |
|
| 52 | 878 | | if (dto is IHasArtist hasArtists) |
| | 879 | | { |
| 0 | 880 | | hasArtists.Artists = entity.Artists?.Split('|', StringSplitOptions.RemoveEmptyEntries) ?? []; |
| | 881 | | } |
| | 882 | |
|
| 52 | 883 | | if (dto is IHasAlbumArtist hasAlbumArtists) |
| | 884 | | { |
| 0 | 885 | | hasAlbumArtists.AlbumArtists = entity.AlbumArtists?.Split('|', StringSplitOptions.RemoveEmptyEntries) ?? []; |
| | 886 | | } |
| | 887 | |
|
| 52 | 888 | | if (dto is LiveTvProgram program) |
| | 889 | | { |
| 0 | 890 | | program.ShowId = entity.ShowId; |
| | 891 | | } |
| | 892 | |
|
| 52 | 893 | | if (entity.Images is not null) |
| | 894 | | { |
| 52 | 895 | | dto.ImageInfos = entity.Images.Select(e => Map(e, appHost)).ToArray(); |
| | 896 | | } |
| | 897 | |
|
| | 898 | | // dto.Type = entity.Type; |
| | 899 | | // dto.Data = entity.Data; |
| | 900 | | // dto.MediaType = Enum.TryParse<MediaType>(entity.MediaType); |
| 52 | 901 | | if (dto is IHasStartDate hasStartDate) |
| | 902 | | { |
| 0 | 903 | | hasStartDate.StartDate = entity.StartDate.GetValueOrDefault(); |
| | 904 | | } |
| | 905 | |
|
| | 906 | | // Fields that are present in the DB but are never actually used |
| | 907 | | // dto.UnratedType = entity.UnratedType; |
| | 908 | | // dto.TopParentId = entity.TopParentId; |
| | 909 | | // dto.CleanName = entity.CleanName; |
| | 910 | | // dto.UserDataKey = entity.UserDataKey; |
| | 911 | |
|
| 52 | 912 | | if (dto is Folder folder) |
| | 913 | | { |
| 52 | 914 | | folder.DateLastMediaAdded = entity.DateLastMediaAdded ?? DateTime.SpecifyKind(DateTime.MinValue, DateTimeKin |
| | 915 | | } |
| | 916 | |
|
| 52 | 917 | | return dto; |
| | 918 | | } |
| | 919 | |
|
| | 920 | | /// <summary> |
| | 921 | | /// Maps a Entity to the DTO. |
| | 922 | | /// </summary> |
| | 923 | | /// <param name="dto">The entity.</param> |
| | 924 | | /// <returns>The dto to map.</returns> |
| | 925 | | public BaseItemEntity Map(BaseItemDto dto) |
| | 926 | | { |
| 114 | 927 | | var dtoType = dto.GetType(); |
| 114 | 928 | | var entity = new BaseItemEntity() |
| 114 | 929 | | { |
| 114 | 930 | | Type = dtoType.ToString(), |
| 114 | 931 | | Id = dto.Id |
| 114 | 932 | | }; |
| | 933 | |
|
| 114 | 934 | | if (TypeRequiresDeserialization(dtoType)) |
| | 935 | | { |
| 93 | 936 | | entity.Data = JsonSerializer.Serialize(dto, dtoType, JsonDefaults.Options); |
| | 937 | | } |
| | 938 | |
|
| 114 | 939 | | entity.ParentId = !dto.ParentId.IsEmpty() ? dto.ParentId : null; |
| 114 | 940 | | entity.Path = GetPathToSave(dto.Path); |
| 114 | 941 | | entity.EndDate = dto.EndDate; |
| 114 | 942 | | entity.CommunityRating = dto.CommunityRating; |
| 114 | 943 | | entity.CustomRating = dto.CustomRating; |
| 114 | 944 | | entity.IndexNumber = dto.IndexNumber; |
| 114 | 945 | | entity.IsLocked = dto.IsLocked; |
| 114 | 946 | | entity.Name = dto.Name; |
| 114 | 947 | | entity.CleanName = GetCleanValue(dto.Name); |
| 114 | 948 | | entity.OfficialRating = dto.OfficialRating; |
| 114 | 949 | | entity.Overview = dto.Overview; |
| 114 | 950 | | entity.ParentIndexNumber = dto.ParentIndexNumber; |
| 114 | 951 | | entity.PremiereDate = dto.PremiereDate; |
| 114 | 952 | | entity.ProductionYear = dto.ProductionYear; |
| 114 | 953 | | entity.SortName = dto.SortName; |
| 114 | 954 | | entity.ForcedSortName = dto.ForcedSortName; |
| 114 | 955 | | entity.RunTimeTicks = dto.RunTimeTicks; |
| 114 | 956 | | entity.PreferredMetadataLanguage = dto.PreferredMetadataLanguage; |
| 114 | 957 | | entity.PreferredMetadataCountryCode = dto.PreferredMetadataCountryCode; |
| 114 | 958 | | entity.IsInMixedFolder = dto.IsInMixedFolder; |
| 114 | 959 | | entity.InheritedParentalRatingValue = dto.InheritedParentalRatingValue; |
| 114 | 960 | | entity.InheritedParentalRatingSubValue = dto.InheritedParentalRatingSubValue; |
| 114 | 961 | | entity.CriticRating = dto.CriticRating; |
| 114 | 962 | | entity.PresentationUniqueKey = dto.PresentationUniqueKey; |
| 114 | 963 | | entity.OriginalTitle = dto.OriginalTitle; |
| 114 | 964 | | entity.Album = dto.Album; |
| 114 | 965 | | entity.LUFS = dto.LUFS; |
| 114 | 966 | | entity.NormalizationGain = dto.NormalizationGain; |
| 114 | 967 | | entity.IsVirtualItem = dto.IsVirtualItem; |
| 114 | 968 | | entity.ExternalSeriesId = dto.ExternalSeriesId; |
| 114 | 969 | | entity.Tagline = dto.Tagline; |
| 114 | 970 | | entity.TotalBitrate = dto.TotalBitrate; |
| 114 | 971 | | entity.ExternalId = dto.ExternalId; |
| 114 | 972 | | entity.Size = dto.Size; |
| 114 | 973 | | entity.Genres = string.Join('|', dto.Genres); |
| 114 | 974 | | entity.DateCreated = dto.DateCreated == DateTime.MinValue ? null : dto.DateCreated; |
| 114 | 975 | | entity.DateModified = dto.DateModified == DateTime.MinValue ? null : dto.DateModified; |
| 114 | 976 | | entity.ChannelId = dto.ChannelId; |
| 114 | 977 | | entity.DateLastRefreshed = dto.DateLastRefreshed == DateTime.MinValue ? null : dto.DateLastRefreshed; |
| 114 | 978 | | entity.DateLastSaved = dto.DateLastSaved == DateTime.MinValue ? null : dto.DateLastSaved; |
| 114 | 979 | | entity.OwnerId = dto.OwnerId.ToString(); |
| 114 | 980 | | entity.Width = dto.Width; |
| 114 | 981 | | entity.Height = dto.Height; |
| 114 | 982 | | entity.Provider = dto.ProviderIds.Select(e => new BaseItemProvider() |
| 114 | 983 | | { |
| 114 | 984 | | Item = entity, |
| 114 | 985 | | ProviderId = e.Key, |
| 114 | 986 | | ProviderValue = e.Value |
| 114 | 987 | | }).ToList(); |
| | 988 | |
|
| 114 | 989 | | if (dto.Audio.HasValue) |
| | 990 | | { |
| 0 | 991 | | entity.Audio = (ProgramAudioEntity)dto.Audio; |
| | 992 | | } |
| | 993 | |
|
| 114 | 994 | | if (dto.ExtraType.HasValue) |
| | 995 | | { |
| 0 | 996 | | entity.ExtraType = (BaseItemExtraType)dto.ExtraType; |
| | 997 | | } |
| | 998 | |
|
| 114 | 999 | | entity.ExtraIds = dto.ExtraIds is not null ? string.Join('|', dto.ExtraIds) : null; |
| 114 | 1000 | | entity.ProductionLocations = dto.ProductionLocations is not null ? string.Join('|', dto.ProductionLocations) : n |
| 114 | 1001 | | entity.Studios = dto.Studios is not null ? string.Join('|', dto.Studios) : null; |
| 114 | 1002 | | entity.Tags = dto.Tags is not null ? string.Join('|', dto.Tags) : null; |
| 114 | 1003 | | entity.LockedFields = dto.LockedFields is not null ? dto.LockedFields |
| 114 | 1004 | | .Select(e => new BaseItemMetadataField() |
| 114 | 1005 | | { |
| 114 | 1006 | | Id = (int)e, |
| 114 | 1007 | | Item = entity, |
| 114 | 1008 | | ItemId = entity.Id |
| 114 | 1009 | | }) |
| 114 | 1010 | | .ToArray() : null; |
| | 1011 | |
|
| 114 | 1012 | | if (dto is IHasProgramAttributes hasProgramAttributes) |
| | 1013 | | { |
| 0 | 1014 | | entity.IsMovie = hasProgramAttributes.IsMovie; |
| 0 | 1015 | | entity.IsSeries = hasProgramAttributes.IsSeries; |
| 0 | 1016 | | entity.EpisodeTitle = hasProgramAttributes.EpisodeTitle; |
| 0 | 1017 | | entity.IsRepeat = hasProgramAttributes.IsRepeat; |
| | 1018 | | } |
| | 1019 | |
|
| 114 | 1020 | | if (dto is LiveTvChannel liveTvChannel) |
| | 1021 | | { |
| 0 | 1022 | | entity.ExternalServiceId = liveTvChannel.ServiceName; |
| | 1023 | | } |
| | 1024 | |
|
| 114 | 1025 | | if (dto is Video video) |
| | 1026 | | { |
| 0 | 1027 | | entity.PrimaryVersionId = video.PrimaryVersionId; |
| | 1028 | | } |
| | 1029 | |
|
| 114 | 1030 | | if (dto is IHasSeries hasSeriesName) |
| | 1031 | | { |
| 0 | 1032 | | entity.SeriesName = hasSeriesName.SeriesName; |
| 0 | 1033 | | entity.SeriesId = hasSeriesName.SeriesId; |
| 0 | 1034 | | entity.SeriesPresentationUniqueKey = hasSeriesName.SeriesPresentationUniqueKey; |
| | 1035 | | } |
| | 1036 | |
|
| 114 | 1037 | | if (dto is Episode episode) |
| | 1038 | | { |
| 0 | 1039 | | entity.SeasonName = episode.SeasonName; |
| 0 | 1040 | | entity.SeasonId = episode.SeasonId; |
| | 1041 | | } |
| | 1042 | |
|
| 114 | 1043 | | if (dto is IHasArtist hasArtists) |
| | 1044 | | { |
| 0 | 1045 | | entity.Artists = hasArtists.Artists is not null ? string.Join('|', hasArtists.Artists) : null; |
| | 1046 | | } |
| | 1047 | |
|
| 114 | 1048 | | if (dto is IHasAlbumArtist hasAlbumArtists) |
| | 1049 | | { |
| 0 | 1050 | | entity.AlbumArtists = hasAlbumArtists.AlbumArtists is not null ? string.Join('|', hasAlbumArtists.AlbumArtis |
| | 1051 | | } |
| | 1052 | |
|
| 114 | 1053 | | if (dto is LiveTvProgram program) |
| | 1054 | | { |
| 0 | 1055 | | entity.ShowId = program.ShowId; |
| | 1056 | | } |
| | 1057 | |
|
| 114 | 1058 | | if (dto.ImageInfos is not null) |
| | 1059 | | { |
| 114 | 1060 | | entity.Images = dto.ImageInfos.Select(f => Map(dto.Id, f)).ToArray(); |
| | 1061 | | } |
| | 1062 | |
|
| 114 | 1063 | | if (dto is Trailer trailer) |
| | 1064 | | { |
| 0 | 1065 | | entity.TrailerTypes = trailer.TrailerTypes?.Select(e => new BaseItemTrailerType() |
| 0 | 1066 | | { |
| 0 | 1067 | | Id = (int)e, |
| 0 | 1068 | | Item = entity, |
| 0 | 1069 | | ItemId = entity.Id |
| 0 | 1070 | | }).ToArray() ?? []; |
| | 1071 | | } |
| | 1072 | |
|
| | 1073 | | // dto.Type = entity.Type; |
| | 1074 | | // dto.Data = entity.Data; |
| 114 | 1075 | | entity.MediaType = dto.MediaType.ToString(); |
| 114 | 1076 | | if (dto is IHasStartDate hasStartDate) |
| | 1077 | | { |
| 0 | 1078 | | entity.StartDate = hasStartDate.StartDate; |
| | 1079 | | } |
| | 1080 | |
|
| 114 | 1081 | | entity.UnratedType = dto.GetBlockUnratedType().ToString(); |
| | 1082 | |
|
| | 1083 | | // Fields that are present in the DB but are never actually used |
| | 1084 | | // dto.UserDataKey = entity.UserDataKey; |
| | 1085 | |
|
| 114 | 1086 | | if (dto is Folder folder) |
| | 1087 | | { |
| 114 | 1088 | | entity.DateLastMediaAdded = folder.DateLastMediaAdded == DateTime.MinValue ? null : folder.DateLastMediaAdde |
| 114 | 1089 | | entity.IsFolder = folder.IsFolder; |
| | 1090 | | } |
| | 1091 | |
|
| 114 | 1092 | | return entity; |
| | 1093 | | } |
| | 1094 | |
|
| | 1095 | | private string[] GetItemValueNames(IReadOnlyList<ItemValueType> itemValueTypes, IReadOnlyList<string> withItemTypes, |
| | 1096 | | { |
| 68 | 1097 | | using var context = _dbProvider.CreateDbContext(); |
| | 1098 | |
|
| 68 | 1099 | | var query = context.ItemValuesMap |
| 68 | 1100 | | .AsNoTracking() |
| 68 | 1101 | | .Where(e => itemValueTypes.Any(w => (ItemValueType)w == e.ItemValue.Type)); |
| 68 | 1102 | | if (withItemTypes.Count > 0) |
| | 1103 | | { |
| 17 | 1104 | | query = query.Where(e => withItemTypes.Contains(e.Item.Type)); |
| | 1105 | | } |
| | 1106 | |
|
| 68 | 1107 | | if (excludeItemTypes.Count > 0) |
| | 1108 | | { |
| 17 | 1109 | | query = query.Where(e => !excludeItemTypes.Contains(e.Item.Type)); |
| | 1110 | | } |
| | 1111 | |
|
| | 1112 | | // query = query.DistinctBy(e => e.CleanValue); |
| 68 | 1113 | | return query.Select(e => e.ItemValue) |
| 68 | 1114 | | .GroupBy(e => e.CleanValue) |
| 68 | 1115 | | .Select(e => e.First().Value) |
| 68 | 1116 | | .ToArray(); |
| 68 | 1117 | | } |
| | 1118 | |
|
| | 1119 | | private static bool TypeRequiresDeserialization(Type type) |
| | 1120 | | { |
| 166 | 1121 | | return type.GetCustomAttribute<RequiresSourceSerialisationAttribute>() == null; |
| | 1122 | | } |
| | 1123 | |
|
| | 1124 | | private BaseItemDto DeserializeBaseItem(BaseItemEntity baseItemEntity, bool skipDeserialization = false) |
| | 1125 | | { |
| 52 | 1126 | | ArgumentNullException.ThrowIfNull(baseItemEntity, nameof(baseItemEntity)); |
| 52 | 1127 | | if (_serverConfigurationManager?.Configuration is null) |
| | 1128 | | { |
| 0 | 1129 | | throw new InvalidOperationException("Server Configuration manager or configuration is null"); |
| | 1130 | | } |
| | 1131 | |
|
| 52 | 1132 | | var typeToSerialise = GetType(baseItemEntity.Type); |
| 52 | 1133 | | return BaseItemRepository.DeserializeBaseItem( |
| 52 | 1134 | | baseItemEntity, |
| 52 | 1135 | | _logger, |
| 52 | 1136 | | _appHost, |
| 52 | 1137 | | skipDeserialization || (_serverConfigurationManager.Configuration.SkipDeserializationForBasicTypes && (typeT |
| | 1138 | | } |
| | 1139 | |
|
| | 1140 | | /// <summary> |
| | 1141 | | /// Deserializes a BaseItemEntity and sets all properties. |
| | 1142 | | /// </summary> |
| | 1143 | | /// <param name="baseItemEntity">The DB entity.</param> |
| | 1144 | | /// <param name="logger">Logger.</param> |
| | 1145 | | /// <param name="appHost">The application server Host.</param> |
| | 1146 | | /// <param name="skipDeserialization">If only mapping should be processed.</param> |
| | 1147 | | /// <returns>A mapped BaseItem.</returns> |
| | 1148 | | /// <exception cref="InvalidOperationException">Will be thrown if an invalid serialisation is requested.</exception> |
| | 1149 | | public static BaseItemDto DeserializeBaseItem(BaseItemEntity baseItemEntity, ILogger logger, IServerApplicationHost? |
| | 1150 | | { |
| 52 | 1151 | | var type = GetType(baseItemEntity.Type) ?? throw new InvalidOperationException("Cannot deserialize unknown type. |
| 52 | 1152 | | BaseItemDto? dto = null; |
| 52 | 1153 | | if (TypeRequiresDeserialization(type) && baseItemEntity.Data is not null && !skipDeserialization) |
| | 1154 | | { |
| | 1155 | | try |
| | 1156 | | { |
| 12 | 1157 | | dto = JsonSerializer.Deserialize(baseItemEntity.Data, type, JsonDefaults.Options) as BaseItemDto; |
| 12 | 1158 | | } |
| 0 | 1159 | | catch (JsonException ex) |
| | 1160 | | { |
| 0 | 1161 | | logger.LogError(ex, "Error deserializing item with JSON: {Data}", baseItemEntity.Data); |
| 0 | 1162 | | } |
| | 1163 | | } |
| | 1164 | |
|
| 52 | 1165 | | if (dto is null) |
| | 1166 | | { |
| 40 | 1167 | | dto = Activator.CreateInstance(type) as BaseItemDto ?? throw new InvalidOperationException("Cannot deseriali |
| | 1168 | | } |
| | 1169 | |
|
| 52 | 1170 | | return Map(baseItemEntity, dto, appHost, logger); |
| | 1171 | | } |
| | 1172 | |
|
| | 1173 | | private QueryResult<(BaseItemDto Item, ItemCounts? ItemCounts)> GetItemValues(InternalItemsQuery filter, IReadOnlyLi |
| | 1174 | | { |
| 0 | 1175 | | ArgumentNullException.ThrowIfNull(filter); |
| | 1176 | |
|
| 0 | 1177 | | if (!filter.Limit.HasValue) |
| | 1178 | | { |
| 0 | 1179 | | filter.EnableTotalRecordCount = false; |
| | 1180 | | } |
| | 1181 | |
|
| 0 | 1182 | | using var context = _dbProvider.CreateDbContext(); |
| | 1183 | |
|
| 0 | 1184 | | var innerQueryFilter = TranslateQuery(context.BaseItems.Where(e => e.Id != EF.Constant(PlaceholderId)), context, |
| 0 | 1185 | | { |
| 0 | 1186 | | ExcludeItemTypes = filter.ExcludeItemTypes, |
| 0 | 1187 | | IncludeItemTypes = filter.IncludeItemTypes, |
| 0 | 1188 | | MediaTypes = filter.MediaTypes, |
| 0 | 1189 | | AncestorIds = filter.AncestorIds, |
| 0 | 1190 | | ItemIds = filter.ItemIds, |
| 0 | 1191 | | TopParentIds = filter.TopParentIds, |
| 0 | 1192 | | ParentId = filter.ParentId, |
| 0 | 1193 | | IsAiring = filter.IsAiring, |
| 0 | 1194 | | IsMovie = filter.IsMovie, |
| 0 | 1195 | | IsSports = filter.IsSports, |
| 0 | 1196 | | IsKids = filter.IsKids, |
| 0 | 1197 | | IsNews = filter.IsNews, |
| 0 | 1198 | | IsSeries = filter.IsSeries |
| 0 | 1199 | | }); |
| | 1200 | |
|
| 0 | 1201 | | var itemValuesQuery = context.ItemValues |
| 0 | 1202 | | .Where(f => itemValueTypes.Contains(f.Type)) |
| 0 | 1203 | | .SelectMany(f => f.BaseItemsMap!, (f, w) => new { f, w }) |
| 0 | 1204 | | .Join( |
| 0 | 1205 | | innerQueryFilter, |
| 0 | 1206 | | fw => fw.w.ItemId, |
| 0 | 1207 | | g => g.Id, |
| 0 | 1208 | | (fw, g) => fw.f.CleanValue); |
| | 1209 | |
|
| 0 | 1210 | | var innerQuery = PrepareItemQuery(context, filter) |
| 0 | 1211 | | .Where(e => e.Type == returnType) |
| 0 | 1212 | | .Where(e => itemValuesQuery.Contains(e.CleanName)); |
| | 1213 | |
|
| 0 | 1214 | | var outerQueryFilter = new InternalItemsQuery(filter.User) |
| 0 | 1215 | | { |
| 0 | 1216 | | IsPlayed = filter.IsPlayed, |
| 0 | 1217 | | IsFavorite = filter.IsFavorite, |
| 0 | 1218 | | IsFavoriteOrLiked = filter.IsFavoriteOrLiked, |
| 0 | 1219 | | IsLiked = filter.IsLiked, |
| 0 | 1220 | | IsLocked = filter.IsLocked, |
| 0 | 1221 | | NameLessThan = filter.NameLessThan, |
| 0 | 1222 | | NameStartsWith = filter.NameStartsWith, |
| 0 | 1223 | | NameStartsWithOrGreater = filter.NameStartsWithOrGreater, |
| 0 | 1224 | | Tags = filter.Tags, |
| 0 | 1225 | | OfficialRatings = filter.OfficialRatings, |
| 0 | 1226 | | StudioIds = filter.StudioIds, |
| 0 | 1227 | | GenreIds = filter.GenreIds, |
| 0 | 1228 | | Genres = filter.Genres, |
| 0 | 1229 | | Years = filter.Years, |
| 0 | 1230 | | NameContains = filter.NameContains, |
| 0 | 1231 | | SearchTerm = filter.SearchTerm, |
| 0 | 1232 | | ExcludeItemIds = filter.ExcludeItemIds |
| 0 | 1233 | | }; |
| | 1234 | |
|
| 0 | 1235 | | var masterQuery = TranslateQuery(innerQuery, context, outerQueryFilter) |
| 0 | 1236 | | .GroupBy(e => e.PresentationUniqueKey) |
| 0 | 1237 | | .Select(e => e.FirstOrDefault()) |
| 0 | 1238 | | .Select(e => e!.Id); |
| | 1239 | |
|
| 0 | 1240 | | var query = context.BaseItems |
| 0 | 1241 | | .Include(e => e.TrailerTypes) |
| 0 | 1242 | | .Include(e => e.Provider) |
| 0 | 1243 | | .Include(e => e.LockedFields) |
| 0 | 1244 | | .Include(e => e.Images) |
| 0 | 1245 | | .AsSingleQuery() |
| 0 | 1246 | | .Where(e => masterQuery.Contains(e.Id)); |
| | 1247 | |
|
| 0 | 1248 | | query = ApplyOrder(query, filter); |
| | 1249 | |
|
| 0 | 1250 | | var result = new QueryResult<(BaseItemDto, ItemCounts?)>(); |
| 0 | 1251 | | if (filter.EnableTotalRecordCount) |
| | 1252 | | { |
| 0 | 1253 | | result.TotalRecordCount = query.Count(); |
| | 1254 | | } |
| | 1255 | |
|
| 0 | 1256 | | if (filter.Limit.HasValue || filter.StartIndex.HasValue) |
| | 1257 | | { |
| 0 | 1258 | | var offset = filter.StartIndex ?? 0; |
| | 1259 | |
|
| 0 | 1260 | | if (offset > 0) |
| | 1261 | | { |
| 0 | 1262 | | query = query.Skip(offset); |
| | 1263 | | } |
| | 1264 | |
|
| 0 | 1265 | | if (filter.Limit.HasValue) |
| | 1266 | | { |
| 0 | 1267 | | query = query.Take(filter.Limit.Value); |
| | 1268 | | } |
| | 1269 | | } |
| | 1270 | |
|
| 0 | 1271 | | IQueryable<BaseItemEntity>? itemCountQuery = null; |
| | 1272 | |
|
| 0 | 1273 | | if (filter.IncludeItemTypes.Length > 0) |
| | 1274 | | { |
| | 1275 | | // if we are to include more then one type, sub query those items beforehand. |
| | 1276 | |
|
| 0 | 1277 | | var typeSubQuery = new InternalItemsQuery(filter.User) |
| 0 | 1278 | | { |
| 0 | 1279 | | ExcludeItemTypes = filter.ExcludeItemTypes, |
| 0 | 1280 | | IncludeItemTypes = filter.IncludeItemTypes, |
| 0 | 1281 | | MediaTypes = filter.MediaTypes, |
| 0 | 1282 | | AncestorIds = filter.AncestorIds, |
| 0 | 1283 | | ExcludeItemIds = filter.ExcludeItemIds, |
| 0 | 1284 | | ItemIds = filter.ItemIds, |
| 0 | 1285 | | TopParentIds = filter.TopParentIds, |
| 0 | 1286 | | ParentId = filter.ParentId, |
| 0 | 1287 | | IsPlayed = filter.IsPlayed |
| 0 | 1288 | | }; |
| | 1289 | |
|
| 0 | 1290 | | itemCountQuery = TranslateQuery(context.BaseItems.AsNoTracking().Where(e => e.Id != EF.Constant(PlaceholderI |
| 0 | 1291 | | .Where(e => e.ItemValues!.Any(f => itemValueTypes!.Contains(f.ItemValue.Type))); |
| | 1292 | |
|
| 0 | 1293 | | var seriesTypeName = _itemTypeLookup.BaseItemKindNames[BaseItemKind.Series]; |
| 0 | 1294 | | var movieTypeName = _itemTypeLookup.BaseItemKindNames[BaseItemKind.Movie]; |
| 0 | 1295 | | var episodeTypeName = _itemTypeLookup.BaseItemKindNames[BaseItemKind.Episode]; |
| 0 | 1296 | | var musicAlbumTypeName = _itemTypeLookup.BaseItemKindNames[BaseItemKind.MusicAlbum]; |
| 0 | 1297 | | var musicArtistTypeName = _itemTypeLookup.BaseItemKindNames[BaseItemKind.MusicArtist]; |
| 0 | 1298 | | var audioTypeName = _itemTypeLookup.BaseItemKindNames[BaseItemKind.Audio]; |
| 0 | 1299 | | var trailerTypeName = _itemTypeLookup.BaseItemKindNames[BaseItemKind.Trailer]; |
| | 1300 | |
|
| 0 | 1301 | | var resultQuery = query.Select(e => new |
| 0 | 1302 | | { |
| 0 | 1303 | | item = e, |
| 0 | 1304 | | // TODO: This is bad refactor! |
| 0 | 1305 | | itemCount = new ItemCounts() |
| 0 | 1306 | | { |
| 0 | 1307 | | SeriesCount = itemCountQuery!.Count(f => f.Type == seriesTypeName), |
| 0 | 1308 | | EpisodeCount = itemCountQuery!.Count(f => f.Type == episodeTypeName), |
| 0 | 1309 | | MovieCount = itemCountQuery!.Count(f => f.Type == movieTypeName), |
| 0 | 1310 | | AlbumCount = itemCountQuery!.Count(f => f.Type == musicAlbumTypeName), |
| 0 | 1311 | | ArtistCount = itemCountQuery!.Count(f => f.Type == musicArtistTypeName), |
| 0 | 1312 | | SongCount = itemCountQuery!.Count(f => f.Type == audioTypeName), |
| 0 | 1313 | | TrailerCount = itemCountQuery!.Count(f => f.Type == trailerTypeName), |
| 0 | 1314 | | } |
| 0 | 1315 | | }); |
| | 1316 | |
|
| 0 | 1317 | | result.StartIndex = filter.StartIndex ?? 0; |
| 0 | 1318 | | result.Items = |
| 0 | 1319 | | [ |
| 0 | 1320 | | .. resultQuery |
| 0 | 1321 | | .AsEnumerable() |
| 0 | 1322 | | .Where(e => e is not null) |
| 0 | 1323 | | .Select(e => |
| 0 | 1324 | | { |
| 0 | 1325 | | return (DeserializeBaseItem(e.item, filter.SkipDeserialization), e.itemCount); |
| 0 | 1326 | | }) |
| 0 | 1327 | | ]; |
| | 1328 | | } |
| | 1329 | | else |
| | 1330 | | { |
| 0 | 1331 | | result.StartIndex = filter.StartIndex ?? 0; |
| 0 | 1332 | | result.Items = |
| 0 | 1333 | | [ |
| 0 | 1334 | | .. query |
| 0 | 1335 | | .AsEnumerable() |
| 0 | 1336 | | .Where(e => e is not null) |
| 0 | 1337 | | .Select<BaseItemEntity, (BaseItemDto, ItemCounts?)>(e => |
| 0 | 1338 | | { |
| 0 | 1339 | | return (DeserializeBaseItem(e, filter.SkipDeserialization), null); |
| 0 | 1340 | | }) |
| 0 | 1341 | | ]; |
| | 1342 | | } |
| | 1343 | |
|
| 0 | 1344 | | return result; |
| 0 | 1345 | | } |
| | 1346 | |
|
| | 1347 | | private static void PrepareFilterQuery(InternalItemsQuery query) |
| | 1348 | | { |
| 324 | 1349 | | if (query.Limit.HasValue && query.EnableGroupByMetadataKey) |
| | 1350 | | { |
| 0 | 1351 | | query.Limit = query.Limit.Value + 4; |
| | 1352 | | } |
| | 1353 | |
|
| 324 | 1354 | | if (query.IsResumable ?? false) |
| | 1355 | | { |
| 1 | 1356 | | query.IsVirtualItem = false; |
| | 1357 | | } |
| 324 | 1358 | | } |
| | 1359 | |
|
| | 1360 | | private string GetCleanValue(string value) |
| | 1361 | | { |
| 117 | 1362 | | if (string.IsNullOrWhiteSpace(value)) |
| | 1363 | | { |
| 0 | 1364 | | return value; |
| | 1365 | | } |
| | 1366 | |
|
| 117 | 1367 | | return value.RemoveDiacritics().ToLowerInvariant(); |
| | 1368 | | } |
| | 1369 | |
|
| | 1370 | | private List<(ItemValueType MagicNumber, string Value)> GetItemValuesToSave(BaseItemDto item, List<string> inherited |
| | 1371 | | { |
| 114 | 1372 | | var list = new List<(ItemValueType, string)>(); |
| | 1373 | |
|
| 114 | 1374 | | if (item is IHasArtist hasArtist) |
| | 1375 | | { |
| 0 | 1376 | | list.AddRange(hasArtist.Artists.Select(i => ((ItemValueType)0, i))); |
| | 1377 | | } |
| | 1378 | |
|
| 114 | 1379 | | if (item is IHasAlbumArtist hasAlbumArtist) |
| | 1380 | | { |
| 0 | 1381 | | list.AddRange(hasAlbumArtist.AlbumArtists.Select(i => (ItemValueType.AlbumArtist, i))); |
| | 1382 | | } |
| | 1383 | |
|
| 114 | 1384 | | list.AddRange(item.Genres.Select(i => (ItemValueType.Genre, i))); |
| 114 | 1385 | | list.AddRange(item.Studios.Select(i => (ItemValueType.Studios, i))); |
| 114 | 1386 | | list.AddRange(item.Tags.Select(i => (ItemValueType.Tags, i))); |
| | 1387 | |
|
| | 1388 | | // keywords was 5 |
| | 1389 | |
|
| 114 | 1390 | | list.AddRange(inheritedTags.Select(i => (ItemValueType.InheritedTags, i))); |
| | 1391 | |
|
| | 1392 | | // Remove all invalid values. |
| 114 | 1393 | | list.RemoveAll(i => string.IsNullOrWhiteSpace(i.Item2)); |
| | 1394 | |
|
| 114 | 1395 | | return list; |
| | 1396 | | } |
| | 1397 | |
|
| | 1398 | | private static BaseItemImageInfo Map(Guid baseItemId, ItemImageInfo e) |
| | 1399 | | { |
| 0 | 1400 | | return new BaseItemImageInfo() |
| 0 | 1401 | | { |
| 0 | 1402 | | ItemId = baseItemId, |
| 0 | 1403 | | Id = Guid.NewGuid(), |
| 0 | 1404 | | Path = e.Path, |
| 0 | 1405 | | Blurhash = e.BlurHash is null ? null : Encoding.UTF8.GetBytes(e.BlurHash), |
| 0 | 1406 | | DateModified = e.DateModified, |
| 0 | 1407 | | Height = e.Height, |
| 0 | 1408 | | Width = e.Width, |
| 0 | 1409 | | ImageType = (ImageInfoImageType)e.Type, |
| 0 | 1410 | | Item = null! |
| 0 | 1411 | | }; |
| | 1412 | | } |
| | 1413 | |
|
| | 1414 | | private static ItemImageInfo Map(BaseItemImageInfo e, IServerApplicationHost? appHost) |
| | 1415 | | { |
| 0 | 1416 | | return new ItemImageInfo() |
| 0 | 1417 | | { |
| 0 | 1418 | | Path = appHost?.ExpandVirtualPath(e.Path) ?? e.Path, |
| 0 | 1419 | | BlurHash = e.Blurhash is null ? null : Encoding.UTF8.GetString(e.Blurhash), |
| 0 | 1420 | | DateModified = e.DateModified ?? DateTime.SpecifyKind(DateTime.MinValue, DateTimeKind.Utc), |
| 0 | 1421 | | Height = e.Height, |
| 0 | 1422 | | Width = e.Width, |
| 0 | 1423 | | Type = (ImageType)e.ImageType |
| 0 | 1424 | | }; |
| | 1425 | | } |
| | 1426 | |
|
| | 1427 | | private string? GetPathToSave(string path) |
| | 1428 | | { |
| 114 | 1429 | | if (path is null) |
| | 1430 | | { |
| 0 | 1431 | | return null; |
| | 1432 | | } |
| | 1433 | |
|
| 114 | 1434 | | return _appHost.ReverseVirtualPath(path); |
| | 1435 | | } |
| | 1436 | |
|
| | 1437 | | private List<string> GetItemByNameTypesInQuery(InternalItemsQuery query) |
| | 1438 | | { |
| 13 | 1439 | | var list = new List<string>(); |
| | 1440 | |
|
| 13 | 1441 | | if (IsTypeInQuery(BaseItemKind.Person, query)) |
| | 1442 | | { |
| 1 | 1443 | | list.Add(_itemTypeLookup.BaseItemKindNames[BaseItemKind.Person]!); |
| | 1444 | | } |
| | 1445 | |
|
| 13 | 1446 | | if (IsTypeInQuery(BaseItemKind.Genre, query)) |
| | 1447 | | { |
| 1 | 1448 | | list.Add(_itemTypeLookup.BaseItemKindNames[BaseItemKind.Genre]!); |
| | 1449 | | } |
| | 1450 | |
|
| 13 | 1451 | | if (IsTypeInQuery(BaseItemKind.MusicGenre, query)) |
| | 1452 | | { |
| 1 | 1453 | | list.Add(_itemTypeLookup.BaseItemKindNames[BaseItemKind.MusicGenre]!); |
| | 1454 | | } |
| | 1455 | |
|
| 13 | 1456 | | if (IsTypeInQuery(BaseItemKind.MusicArtist, query)) |
| | 1457 | | { |
| 1 | 1458 | | list.Add(_itemTypeLookup.BaseItemKindNames[BaseItemKind.MusicArtist]!); |
| | 1459 | | } |
| | 1460 | |
|
| 13 | 1461 | | if (IsTypeInQuery(BaseItemKind.Studio, query)) |
| | 1462 | | { |
| 1 | 1463 | | list.Add(_itemTypeLookup.BaseItemKindNames[BaseItemKind.Studio]!); |
| | 1464 | | } |
| | 1465 | |
|
| 13 | 1466 | | return list; |
| | 1467 | | } |
| | 1468 | |
|
| | 1469 | | private bool IsTypeInQuery(BaseItemKind type, InternalItemsQuery query) |
| | 1470 | | { |
| 65 | 1471 | | if (query.ExcludeItemTypes.Contains(type)) |
| | 1472 | | { |
| 0 | 1473 | | return false; |
| | 1474 | | } |
| | 1475 | |
|
| 65 | 1476 | | return query.IncludeItemTypes.Length == 0 || query.IncludeItemTypes.Contains(type); |
| | 1477 | | } |
| | 1478 | |
|
| | 1479 | | private bool EnableGroupByPresentationUniqueKey(InternalItemsQuery query) |
| | 1480 | | { |
| 324 | 1481 | | if (!query.GroupByPresentationUniqueKey) |
| | 1482 | | { |
| 124 | 1483 | | return false; |
| | 1484 | | } |
| | 1485 | |
|
| 200 | 1486 | | if (query.GroupBySeriesPresentationUniqueKey) |
| | 1487 | | { |
| 0 | 1488 | | return false; |
| | 1489 | | } |
| | 1490 | |
|
| 200 | 1491 | | if (!string.IsNullOrWhiteSpace(query.PresentationUniqueKey)) |
| | 1492 | | { |
| 0 | 1493 | | return false; |
| | 1494 | | } |
| | 1495 | |
|
| 200 | 1496 | | if (query.User is null) |
| | 1497 | | { |
| 198 | 1498 | | return false; |
| | 1499 | | } |
| | 1500 | |
|
| 2 | 1501 | | if (query.IncludeItemTypes.Length == 0) |
| | 1502 | | { |
| 1 | 1503 | | return true; |
| | 1504 | | } |
| | 1505 | |
|
| 1 | 1506 | | return query.IncludeItemTypes.Contains(BaseItemKind.Episode) |
| 1 | 1507 | | || query.IncludeItemTypes.Contains(BaseItemKind.Video) |
| 1 | 1508 | | || query.IncludeItemTypes.Contains(BaseItemKind.Movie) |
| 1 | 1509 | | || query.IncludeItemTypes.Contains(BaseItemKind.MusicVideo) |
| 1 | 1510 | | || query.IncludeItemTypes.Contains(BaseItemKind.Series) |
| 1 | 1511 | | || query.IncludeItemTypes.Contains(BaseItemKind.Season); |
| | 1512 | | } |
| | 1513 | |
|
| | 1514 | | private IQueryable<BaseItemEntity> ApplyOrder(IQueryable<BaseItemEntity> query, InternalItemsQuery filter) |
| | 1515 | | { |
| 324 | 1516 | | var orderBy = filter.OrderBy; |
| 324 | 1517 | | var hasSearch = !string.IsNullOrEmpty(filter.SearchTerm); |
| | 1518 | |
|
| 324 | 1519 | | if (hasSearch) |
| | 1520 | | { |
| 0 | 1521 | | orderBy = filter.OrderBy = [(ItemSortBy.SortName, SortOrder.Ascending), .. orderBy]; |
| | 1522 | | } |
| 324 | 1523 | | else if (orderBy.Count == 0) |
| | 1524 | | { |
| 217 | 1525 | | return query.OrderBy(e => e.SortName); |
| | 1526 | | } |
| | 1527 | |
|
| 107 | 1528 | | IOrderedQueryable<BaseItemEntity>? orderedQuery = null; |
| | 1529 | |
|
| 107 | 1530 | | var firstOrdering = orderBy.FirstOrDefault(); |
| 107 | 1531 | | if (firstOrdering != default) |
| | 1532 | | { |
| 107 | 1533 | | var expression = OrderMapper.MapOrderByField(firstOrdering.OrderBy, filter); |
| 107 | 1534 | | if (firstOrdering.SortOrder == SortOrder.Ascending) |
| | 1535 | | { |
| 106 | 1536 | | orderedQuery = query.OrderBy(expression); |
| | 1537 | | } |
| | 1538 | | else |
| | 1539 | | { |
| 1 | 1540 | | orderedQuery = query.OrderByDescending(expression); |
| | 1541 | | } |
| | 1542 | |
|
| 107 | 1543 | | if (firstOrdering.OrderBy is ItemSortBy.Default or ItemSortBy.SortName) |
| | 1544 | | { |
| 0 | 1545 | | if (firstOrdering.SortOrder is SortOrder.Ascending) |
| | 1546 | | { |
| 0 | 1547 | | orderedQuery = orderedQuery.ThenBy(e => e.Name); |
| | 1548 | | } |
| | 1549 | | else |
| | 1550 | | { |
| 0 | 1551 | | orderedQuery = orderedQuery.ThenByDescending(e => e.Name); |
| | 1552 | | } |
| | 1553 | | } |
| | 1554 | | } |
| | 1555 | |
|
| 300 | 1556 | | foreach (var item in orderBy.Skip(1)) |
| | 1557 | | { |
| 43 | 1558 | | var expression = OrderMapper.MapOrderByField(item.OrderBy, filter); |
| 43 | 1559 | | if (item.SortOrder == SortOrder.Ascending) |
| | 1560 | | { |
| 43 | 1561 | | orderedQuery = orderedQuery!.ThenBy(expression); |
| | 1562 | | } |
| | 1563 | | else |
| | 1564 | | { |
| 0 | 1565 | | orderedQuery = orderedQuery!.ThenByDescending(expression); |
| | 1566 | | } |
| | 1567 | | } |
| | 1568 | |
|
| 107 | 1569 | | return orderedQuery ?? query; |
| | 1570 | | } |
| | 1571 | |
|
| | 1572 | | private IQueryable<BaseItemEntity> TranslateQuery( |
| | 1573 | | IQueryable<BaseItemEntity> baseQuery, |
| | 1574 | | JellyfinDbContext context, |
| | 1575 | | InternalItemsQuery filter) |
| | 1576 | | { |
| | 1577 | | const int HDWidth = 1200; |
| | 1578 | | const int UHDWidth = 3800; |
| | 1579 | | const int UHDHeight = 2100; |
| | 1580 | |
|
| 324 | 1581 | | var minWidth = filter.MinWidth; |
| 324 | 1582 | | var maxWidth = filter.MaxWidth; |
| 324 | 1583 | | var now = DateTime.UtcNow; |
| | 1584 | |
|
| 324 | 1585 | | if (filter.IsHD.HasValue || filter.Is4K.HasValue) |
| | 1586 | | { |
| 0 | 1587 | | bool includeSD = false; |
| 0 | 1588 | | bool includeHD = false; |
| 0 | 1589 | | bool include4K = false; |
| | 1590 | |
|
| 0 | 1591 | | if (filter.IsHD.HasValue && !filter.IsHD.Value) |
| | 1592 | | { |
| 0 | 1593 | | includeSD = true; |
| | 1594 | | } |
| | 1595 | |
|
| 0 | 1596 | | if (filter.IsHD.HasValue && filter.IsHD.Value) |
| | 1597 | | { |
| 0 | 1598 | | includeHD = true; |
| | 1599 | | } |
| | 1600 | |
|
| 0 | 1601 | | if (filter.Is4K.HasValue && filter.Is4K.Value) |
| | 1602 | | { |
| 0 | 1603 | | include4K = true; |
| | 1604 | | } |
| | 1605 | |
|
| 0 | 1606 | | baseQuery = baseQuery.Where(e => |
| 0 | 1607 | | (includeSD && e.Width < HDWidth) || |
| 0 | 1608 | | (includeHD && e.Width >= HDWidth && !(e.Width >= UHDWidth || e.Height >= UHDHeight)) || |
| 0 | 1609 | | (include4K && (e.Width >= UHDWidth || e.Height >= UHDHeight))); |
| | 1610 | | } |
| | 1611 | |
|
| 324 | 1612 | | if (minWidth.HasValue) |
| | 1613 | | { |
| 0 | 1614 | | baseQuery = baseQuery.Where(e => e.Width >= minWidth); |
| | 1615 | | } |
| | 1616 | |
|
| 324 | 1617 | | if (filter.MinHeight.HasValue) |
| | 1618 | | { |
| 0 | 1619 | | baseQuery = baseQuery.Where(e => e.Height >= filter.MinHeight); |
| | 1620 | | } |
| | 1621 | |
|
| 324 | 1622 | | if (maxWidth.HasValue) |
| | 1623 | | { |
| 0 | 1624 | | baseQuery = baseQuery.Where(e => e.Width <= maxWidth); |
| | 1625 | | } |
| | 1626 | |
|
| 324 | 1627 | | if (filter.MaxHeight.HasValue) |
| | 1628 | | { |
| 0 | 1629 | | baseQuery = baseQuery.Where(e => e.Height <= filter.MaxHeight); |
| | 1630 | | } |
| | 1631 | |
|
| 324 | 1632 | | if (filter.IsLocked.HasValue) |
| | 1633 | | { |
| 51 | 1634 | | baseQuery = baseQuery.Where(e => e.IsLocked == filter.IsLocked); |
| | 1635 | | } |
| | 1636 | |
|
| 324 | 1637 | | var tags = filter.Tags.ToList(); |
| 324 | 1638 | | var excludeTags = filter.ExcludeTags.ToList(); |
| | 1639 | |
|
| 324 | 1640 | | if (filter.IsMovie == true) |
| | 1641 | | { |
| 0 | 1642 | | if (filter.IncludeItemTypes.Length == 0 |
| 0 | 1643 | | || filter.IncludeItemTypes.Contains(BaseItemKind.Movie) |
| 0 | 1644 | | || filter.IncludeItemTypes.Contains(BaseItemKind.Trailer)) |
| | 1645 | | { |
| 0 | 1646 | | baseQuery = baseQuery.Where(e => e.IsMovie); |
| | 1647 | | } |
| | 1648 | | } |
| 324 | 1649 | | else if (filter.IsMovie.HasValue) |
| | 1650 | | { |
| 0 | 1651 | | baseQuery = baseQuery.Where(e => e.IsMovie == filter.IsMovie); |
| | 1652 | | } |
| | 1653 | |
|
| 324 | 1654 | | if (filter.IsSeries.HasValue) |
| | 1655 | | { |
| 0 | 1656 | | baseQuery = baseQuery.Where(e => e.IsSeries == filter.IsSeries); |
| | 1657 | | } |
| | 1658 | |
|
| 324 | 1659 | | if (filter.IsSports.HasValue) |
| | 1660 | | { |
| 0 | 1661 | | if (filter.IsSports.Value) |
| | 1662 | | { |
| 0 | 1663 | | tags.Add("Sports"); |
| | 1664 | | } |
| | 1665 | | else |
| | 1666 | | { |
| 0 | 1667 | | excludeTags.Add("Sports"); |
| | 1668 | | } |
| | 1669 | | } |
| | 1670 | |
|
| 324 | 1671 | | if (filter.IsNews.HasValue) |
| | 1672 | | { |
| 0 | 1673 | | if (filter.IsNews.Value) |
| | 1674 | | { |
| 0 | 1675 | | tags.Add("News"); |
| | 1676 | | } |
| | 1677 | | else |
| | 1678 | | { |
| 0 | 1679 | | excludeTags.Add("News"); |
| | 1680 | | } |
| | 1681 | | } |
| | 1682 | |
|
| 324 | 1683 | | if (filter.IsKids.HasValue) |
| | 1684 | | { |
| 0 | 1685 | | if (filter.IsKids.Value) |
| | 1686 | | { |
| 0 | 1687 | | tags.Add("Kids"); |
| | 1688 | | } |
| | 1689 | | else |
| | 1690 | | { |
| 0 | 1691 | | excludeTags.Add("Kids"); |
| | 1692 | | } |
| | 1693 | | } |
| | 1694 | |
|
| 324 | 1695 | | if (!string.IsNullOrEmpty(filter.SearchTerm)) |
| | 1696 | | { |
| 0 | 1697 | | var searchTerm = filter.SearchTerm.ToLower(); |
| 0 | 1698 | | if (SearchWildcardTerms.Any(f => searchTerm.Contains(f))) |
| | 1699 | | { |
| 0 | 1700 | | searchTerm = $"%{searchTerm.Trim('%')}%"; |
| 0 | 1701 | | baseQuery = baseQuery.Where(e => EF.Functions.Like(e.CleanName!.ToLower(), searchTerm) || (e.OriginalTit |
| | 1702 | | } |
| | 1703 | | else |
| | 1704 | | { |
| 0 | 1705 | | baseQuery = baseQuery.Where(e => e.CleanName!.ToLower().Contains(searchTerm) || (e.OriginalTitle != null |
| | 1706 | | } |
| | 1707 | | } |
| | 1708 | |
|
| 324 | 1709 | | if (filter.IsFolder.HasValue) |
| | 1710 | | { |
| 21 | 1711 | | baseQuery = baseQuery.Where(e => e.IsFolder == filter.IsFolder); |
| | 1712 | | } |
| | 1713 | |
|
| 324 | 1714 | | var includeTypes = filter.IncludeItemTypes; |
| | 1715 | |
|
| | 1716 | | // Only specify excluded types if no included types are specified |
| 324 | 1717 | | if (filter.IncludeItemTypes.Length == 0) |
| | 1718 | | { |
| 206 | 1719 | | var excludeTypes = filter.ExcludeItemTypes; |
| 206 | 1720 | | if (excludeTypes.Length == 1) |
| | 1721 | | { |
| 0 | 1722 | | if (_itemTypeLookup.BaseItemKindNames.TryGetValue(excludeTypes[0], out var excludeTypeName)) |
| | 1723 | | { |
| 0 | 1724 | | baseQuery = baseQuery.Where(e => e.Type != excludeTypeName); |
| | 1725 | | } |
| | 1726 | | } |
| 206 | 1727 | | else if (excludeTypes.Length > 1) |
| | 1728 | | { |
| 0 | 1729 | | var excludeTypeName = new List<string>(); |
| 0 | 1730 | | foreach (var excludeType in excludeTypes) |
| | 1731 | | { |
| 0 | 1732 | | if (_itemTypeLookup.BaseItemKindNames.TryGetValue(excludeType, out var baseItemKindName)) |
| | 1733 | | { |
| 0 | 1734 | | excludeTypeName.Add(baseItemKindName!); |
| | 1735 | | } |
| | 1736 | | } |
| | 1737 | |
|
| 0 | 1738 | | baseQuery = baseQuery.Where(e => !excludeTypeName.Contains(e.Type)); |
| | 1739 | | } |
| | 1740 | | } |
| | 1741 | | else |
| | 1742 | | { |
| 118 | 1743 | | string[] types = includeTypes.Select(f => _itemTypeLookup.BaseItemKindNames.GetValueOrDefault(f)).Where(e => |
| 118 | 1744 | | baseQuery = baseQuery.WhereOneOrMany(types, f => f.Type); |
| | 1745 | | } |
| | 1746 | |
|
| 324 | 1747 | | if (filter.ChannelIds.Count > 0) |
| | 1748 | | { |
| 0 | 1749 | | baseQuery = baseQuery.Where(e => e.ChannelId != null && filter.ChannelIds.Contains(e.ChannelId.Value)); |
| | 1750 | | } |
| | 1751 | |
|
| 324 | 1752 | | if (!filter.ParentId.IsEmpty()) |
| | 1753 | | { |
| 124 | 1754 | | baseQuery = baseQuery.Where(e => e.ParentId!.Value == filter.ParentId); |
| | 1755 | | } |
| | 1756 | |
|
| 324 | 1757 | | if (!string.IsNullOrWhiteSpace(filter.Path)) |
| | 1758 | | { |
| 0 | 1759 | | baseQuery = baseQuery.Where(e => e.Path == filter.Path); |
| | 1760 | | } |
| | 1761 | |
|
| 324 | 1762 | | if (!string.IsNullOrWhiteSpace(filter.PresentationUniqueKey)) |
| | 1763 | | { |
| 0 | 1764 | | baseQuery = baseQuery.Where(e => e.PresentationUniqueKey == filter.PresentationUniqueKey); |
| | 1765 | | } |
| | 1766 | |
|
| 324 | 1767 | | if (filter.MinCommunityRating.HasValue) |
| | 1768 | | { |
| 0 | 1769 | | baseQuery = baseQuery.Where(e => e.CommunityRating >= filter.MinCommunityRating); |
| | 1770 | | } |
| | 1771 | |
|
| 324 | 1772 | | if (filter.MinIndexNumber.HasValue) |
| | 1773 | | { |
| 0 | 1774 | | baseQuery = baseQuery.Where(e => e.IndexNumber >= filter.MinIndexNumber); |
| | 1775 | | } |
| | 1776 | |
|
| 324 | 1777 | | if (filter.MinParentAndIndexNumber.HasValue) |
| | 1778 | | { |
| 0 | 1779 | | baseQuery = baseQuery |
| 0 | 1780 | | .Where(e => (e.ParentIndexNumber == filter.MinParentAndIndexNumber.Value.ParentIndexNumber && e.IndexNum |
| | 1781 | | } |
| | 1782 | |
|
| 324 | 1783 | | if (filter.MinDateCreated.HasValue) |
| | 1784 | | { |
| 0 | 1785 | | baseQuery = baseQuery.Where(e => e.DateCreated >= filter.MinDateCreated); |
| | 1786 | | } |
| | 1787 | |
|
| 324 | 1788 | | if (filter.MinDateLastSaved.HasValue) |
| | 1789 | | { |
| 0 | 1790 | | baseQuery = baseQuery.Where(e => e.DateLastSaved != null && e.DateLastSaved >= filter.MinDateLastSaved.Value |
| | 1791 | | } |
| | 1792 | |
|
| 324 | 1793 | | if (filter.MinDateLastSavedForUser.HasValue) |
| | 1794 | | { |
| 0 | 1795 | | baseQuery = baseQuery.Where(e => e.DateLastSaved != null && e.DateLastSaved >= filter.MinDateLastSavedForUse |
| | 1796 | | } |
| | 1797 | |
|
| 324 | 1798 | | if (filter.IndexNumber.HasValue) |
| | 1799 | | { |
| 0 | 1800 | | baseQuery = baseQuery.Where(e => e.IndexNumber == filter.IndexNumber.Value); |
| | 1801 | | } |
| | 1802 | |
|
| 324 | 1803 | | if (filter.ParentIndexNumber.HasValue) |
| | 1804 | | { |
| 0 | 1805 | | baseQuery = baseQuery.Where(e => e.ParentIndexNumber == filter.ParentIndexNumber.Value); |
| | 1806 | | } |
| | 1807 | |
|
| 324 | 1808 | | if (filter.ParentIndexNumberNotEquals.HasValue) |
| | 1809 | | { |
| 0 | 1810 | | baseQuery = baseQuery.Where(e => e.ParentIndexNumber != filter.ParentIndexNumberNotEquals.Value || e.ParentI |
| | 1811 | | } |
| | 1812 | |
|
| 324 | 1813 | | var minEndDate = filter.MinEndDate; |
| 324 | 1814 | | var maxEndDate = filter.MaxEndDate; |
| | 1815 | |
|
| 324 | 1816 | | if (filter.HasAired.HasValue) |
| | 1817 | | { |
| 0 | 1818 | | if (filter.HasAired.Value) |
| | 1819 | | { |
| 0 | 1820 | | maxEndDate = DateTime.UtcNow; |
| | 1821 | | } |
| | 1822 | | else |
| | 1823 | | { |
| 0 | 1824 | | minEndDate = DateTime.UtcNow; |
| | 1825 | | } |
| | 1826 | | } |
| | 1827 | |
|
| 324 | 1828 | | if (minEndDate.HasValue) |
| | 1829 | | { |
| 0 | 1830 | | baseQuery = baseQuery.Where(e => e.EndDate >= minEndDate); |
| | 1831 | | } |
| | 1832 | |
|
| 324 | 1833 | | if (maxEndDate.HasValue) |
| | 1834 | | { |
| 0 | 1835 | | baseQuery = baseQuery.Where(e => e.EndDate <= maxEndDate); |
| | 1836 | | } |
| | 1837 | |
|
| 324 | 1838 | | if (filter.MinStartDate.HasValue) |
| | 1839 | | { |
| 0 | 1840 | | baseQuery = baseQuery.Where(e => e.StartDate >= filter.MinStartDate.Value); |
| | 1841 | | } |
| | 1842 | |
|
| 324 | 1843 | | if (filter.MaxStartDate.HasValue) |
| | 1844 | | { |
| 0 | 1845 | | baseQuery = baseQuery.Where(e => e.StartDate <= filter.MaxStartDate.Value); |
| | 1846 | | } |
| | 1847 | |
|
| 324 | 1848 | | if (filter.MinPremiereDate.HasValue) |
| | 1849 | | { |
| 0 | 1850 | | baseQuery = baseQuery.Where(e => e.PremiereDate >= filter.MinPremiereDate.Value); |
| | 1851 | | } |
| | 1852 | |
|
| 324 | 1853 | | if (filter.MaxPremiereDate.HasValue) |
| | 1854 | | { |
| 0 | 1855 | | baseQuery = baseQuery.Where(e => e.PremiereDate <= filter.MaxPremiereDate.Value); |
| | 1856 | | } |
| | 1857 | |
|
| 324 | 1858 | | if (filter.TrailerTypes.Length > 0) |
| | 1859 | | { |
| 0 | 1860 | | var trailerTypes = filter.TrailerTypes.Select(e => (int)e).ToArray(); |
| 0 | 1861 | | baseQuery = baseQuery.Where(e => trailerTypes.Any(f => e.TrailerTypes!.Any(w => w.Id == f))); |
| | 1862 | | } |
| | 1863 | |
|
| 324 | 1864 | | if (filter.IsAiring.HasValue) |
| | 1865 | | { |
| 0 | 1866 | | if (filter.IsAiring.Value) |
| | 1867 | | { |
| 0 | 1868 | | baseQuery = baseQuery.Where(e => e.StartDate <= now && e.EndDate >= now); |
| | 1869 | | } |
| | 1870 | | else |
| | 1871 | | { |
| 0 | 1872 | | baseQuery = baseQuery.Where(e => e.StartDate > now && e.EndDate < now); |
| | 1873 | | } |
| | 1874 | | } |
| | 1875 | |
|
| 324 | 1876 | | if (filter.PersonIds.Length > 0) |
| | 1877 | | { |
| 0 | 1878 | | var peopleEntityIds = context.BaseItems |
| 0 | 1879 | | .WhereOneOrMany(filter.PersonIds, b => b.Id) |
| 0 | 1880 | | .Join( |
| 0 | 1881 | | context.Peoples, |
| 0 | 1882 | | b => b.Name, |
| 0 | 1883 | | p => p.Name, |
| 0 | 1884 | | (b, p) => p.Id); |
| | 1885 | |
|
| 0 | 1886 | | baseQuery = baseQuery |
| 0 | 1887 | | .Where(e => context.PeopleBaseItemMap |
| 0 | 1888 | | .Any(m => m.ItemId == e.Id && peopleEntityIds.Contains(m.PeopleId))); |
| | 1889 | | } |
| | 1890 | |
|
| 324 | 1891 | | if (!string.IsNullOrWhiteSpace(filter.Person)) |
| | 1892 | | { |
| 0 | 1893 | | baseQuery = baseQuery.Where(e => e.Peoples!.Any(f => f.People.Name == filter.Person)); |
| | 1894 | | } |
| | 1895 | |
|
| 324 | 1896 | | if (!string.IsNullOrWhiteSpace(filter.MinSortName)) |
| | 1897 | | { |
| | 1898 | | // this does not makes sense. |
| | 1899 | | // baseQuery = baseQuery.Where(e => e.SortName >= query.MinSortName); |
| | 1900 | | // whereClauses.Add("SortName>=@MinSortName"); |
| | 1901 | | // statement?.TryBind("@MinSortName", query.MinSortName); |
| | 1902 | | } |
| | 1903 | |
|
| 324 | 1904 | | if (!string.IsNullOrWhiteSpace(filter.ExternalSeriesId)) |
| | 1905 | | { |
| 0 | 1906 | | baseQuery = baseQuery.Where(e => e.ExternalSeriesId == filter.ExternalSeriesId); |
| | 1907 | | } |
| | 1908 | |
|
| 324 | 1909 | | if (!string.IsNullOrWhiteSpace(filter.ExternalId)) |
| | 1910 | | { |
| 0 | 1911 | | baseQuery = baseQuery.Where(e => e.ExternalId == filter.ExternalId); |
| | 1912 | | } |
| | 1913 | |
|
| 324 | 1914 | | if (!string.IsNullOrWhiteSpace(filter.Name)) |
| | 1915 | | { |
| 3 | 1916 | | var cleanName = GetCleanValue(filter.Name); |
| 3 | 1917 | | baseQuery = baseQuery.Where(e => e.CleanName == cleanName); |
| | 1918 | | } |
| | 1919 | |
|
| | 1920 | | // These are the same, for now |
| 324 | 1921 | | var nameContains = filter.NameContains; |
| 324 | 1922 | | if (!string.IsNullOrWhiteSpace(nameContains)) |
| | 1923 | | { |
| 0 | 1924 | | if (SearchWildcardTerms.Any(f => nameContains.Contains(f))) |
| | 1925 | | { |
| 0 | 1926 | | nameContains = $"%{nameContains.Trim('%')}%"; |
| 0 | 1927 | | baseQuery = baseQuery.Where(e => EF.Functions.Like(e.CleanName, nameContains) || EF.Functions.Like(e.Ori |
| | 1928 | | } |
| | 1929 | | else |
| | 1930 | | { |
| 0 | 1931 | | baseQuery = baseQuery.Where(e => |
| 0 | 1932 | | e.CleanName!.Contains(nameContains) |
| 0 | 1933 | | || e.OriginalTitle!.ToLower().Contains(nameContains!)); |
| | 1934 | | } |
| | 1935 | | } |
| | 1936 | |
|
| 324 | 1937 | | if (!string.IsNullOrWhiteSpace(filter.NameStartsWith)) |
| | 1938 | | { |
| 0 | 1939 | | baseQuery = baseQuery.Where(e => e.SortName!.StartsWith(filter.NameStartsWith)); |
| | 1940 | | } |
| | 1941 | |
|
| 324 | 1942 | | if (!string.IsNullOrWhiteSpace(filter.NameStartsWithOrGreater)) |
| | 1943 | | { |
| | 1944 | | // i hate this |
| 0 | 1945 | | baseQuery = baseQuery.Where(e => e.SortName!.FirstOrDefault() > filter.NameStartsWithOrGreater[0] || e.Name! |
| | 1946 | | } |
| | 1947 | |
|
| 324 | 1948 | | if (!string.IsNullOrWhiteSpace(filter.NameLessThan)) |
| | 1949 | | { |
| | 1950 | | // i hate this |
| 0 | 1951 | | baseQuery = baseQuery.Where(e => e.SortName!.FirstOrDefault() < filter.NameLessThan[0] || e.Name!.FirstOrDef |
| | 1952 | | } |
| | 1953 | |
|
| 324 | 1954 | | if (filter.ImageTypes.Length > 0) |
| | 1955 | | { |
| 106 | 1956 | | var imgTypes = filter.ImageTypes.Select(e => (ImageInfoImageType)e).ToArray(); |
| 106 | 1957 | | baseQuery = baseQuery.Where(e => imgTypes.Any(f => e.Images!.Any(w => w.ImageType == f))); |
| | 1958 | | } |
| | 1959 | |
|
| 324 | 1960 | | if (filter.IsLiked.HasValue) |
| | 1961 | | { |
| 0 | 1962 | | baseQuery = baseQuery |
| 0 | 1963 | | .Where(e => e.UserData!.FirstOrDefault(f => f.UserId == filter.User!.Id)!.Rating >= UserItemData.MinLike |
| | 1964 | | } |
| | 1965 | |
|
| 324 | 1966 | | if (filter.IsFavoriteOrLiked.HasValue) |
| | 1967 | | { |
| 0 | 1968 | | baseQuery = baseQuery |
| 0 | 1969 | | .Where(e => e.UserData!.FirstOrDefault(f => f.UserId == filter.User!.Id)!.IsFavorite == filter.IsFavorit |
| | 1970 | | } |
| | 1971 | |
|
| 324 | 1972 | | if (filter.IsFavorite.HasValue) |
| | 1973 | | { |
| 0 | 1974 | | baseQuery = baseQuery |
| 0 | 1975 | | .Where(e => e.UserData!.FirstOrDefault(f => f.UserId == filter.User!.Id)!.IsFavorite == filter.IsFavorit |
| | 1976 | | } |
| | 1977 | |
|
| 324 | 1978 | | if (filter.IsPlayed.HasValue) |
| | 1979 | | { |
| | 1980 | | // We should probably figure this out for all folders, but for right now, this is the only place where we ne |
| 0 | 1981 | | if (filter.IncludeItemTypes.Length == 1 && filter.IncludeItemTypes[0] == BaseItemKind.Series) |
| | 1982 | | { |
| 0 | 1983 | | baseQuery = baseQuery.Where(e => context.BaseItems.Where(e => e.Id != EF.Constant(PlaceholderId)) |
| 0 | 1984 | | .Where(e => e.IsFolder == false && e.IsVirtualItem == false) |
| 0 | 1985 | | .Where(f => f.UserData!.FirstOrDefault(e => e.UserId == filter.User!.Id && e.Played)!.Played) |
| 0 | 1986 | | .Any(f => f.SeriesPresentationUniqueKey == e.PresentationUniqueKey) == filter.IsPlayed); |
| | 1987 | | } |
| | 1988 | | else |
| | 1989 | | { |
| 0 | 1990 | | baseQuery = baseQuery |
| 0 | 1991 | | .Select(e => new |
| 0 | 1992 | | { |
| 0 | 1993 | | IsPlayed = e.UserData!.Where(f => f.UserId == filter.User!.Id).Select(f => (bool?)f.Played).Firs |
| 0 | 1994 | | Item = e |
| 0 | 1995 | | }) |
| 0 | 1996 | | .Where(e => e.IsPlayed == filter.IsPlayed) |
| 0 | 1997 | | .Select(f => f.Item); |
| | 1998 | | } |
| | 1999 | | } |
| | 2000 | |
|
| 324 | 2001 | | if (filter.IsResumable.HasValue) |
| | 2002 | | { |
| 1 | 2003 | | if (filter.IsResumable.Value) |
| | 2004 | | { |
| 1 | 2005 | | baseQuery = baseQuery |
| 1 | 2006 | | .Where(e => e.UserData!.FirstOrDefault(f => f.UserId == filter.User!.Id)!.PlaybackPositionTicks > |
| | 2007 | | } |
| | 2008 | | else |
| | 2009 | | { |
| 0 | 2010 | | baseQuery = baseQuery |
| 0 | 2011 | | .Where(e => e.UserData!.FirstOrDefault(f => f.UserId == filter.User!.Id)!.PlaybackPositionTicks = |
| | 2012 | | } |
| | 2013 | | } |
| | 2014 | |
|
| 324 | 2015 | | if (filter.ArtistIds.Length > 0) |
| | 2016 | | { |
| 0 | 2017 | | baseQuery = baseQuery.WhereReferencedItem(context, ItemValueType.Artist, filter.ArtistIds); |
| | 2018 | | } |
| | 2019 | |
|
| 324 | 2020 | | if (filter.AlbumArtistIds.Length > 0) |
| | 2021 | | { |
| 0 | 2022 | | baseQuery = baseQuery.WhereReferencedItem(context, ItemValueType.AlbumArtist, filter.AlbumArtistIds); |
| | 2023 | | } |
| | 2024 | |
|
| 324 | 2025 | | if (filter.ContributingArtistIds.Length > 0) |
| | 2026 | | { |
| 0 | 2027 | | baseQuery = baseQuery.WhereReferencedItem(context, ItemValueType.Artist, filter.ContributingArtistIds); |
| | 2028 | | } |
| | 2029 | |
|
| 324 | 2030 | | if (filter.AlbumIds.Length > 0) |
| | 2031 | | { |
| 0 | 2032 | | var subQuery = context.BaseItems.WhereOneOrMany(filter.AlbumIds, f => f.Id); |
| 0 | 2033 | | baseQuery = baseQuery.Where(e => subQuery.Any(f => f.Name == e.Album)); |
| | 2034 | | } |
| | 2035 | |
|
| 324 | 2036 | | if (filter.ExcludeArtistIds.Length > 0) |
| | 2037 | | { |
| 0 | 2038 | | baseQuery = baseQuery.WhereReferencedItem(context, ItemValueType.Artist, filter.ExcludeArtistIds, true); |
| | 2039 | | } |
| | 2040 | |
|
| 324 | 2041 | | if (filter.GenreIds.Count > 0) |
| | 2042 | | { |
| 0 | 2043 | | baseQuery = baseQuery.WhereReferencedItem(context, ItemValueType.Genre, filter.GenreIds.ToArray()); |
| | 2044 | | } |
| | 2045 | |
|
| 324 | 2046 | | if (filter.Genres.Count > 0) |
| | 2047 | | { |
| 0 | 2048 | | var cleanGenres = filter.Genres.Select(e => GetCleanValue(e)).ToArray().OneOrManyExpressionBuilder<ItemValue |
| 0 | 2049 | | baseQuery = baseQuery |
| 0 | 2050 | | .Where(e => e.ItemValues!.AsQueryable().Where(f => f.ItemValue.Type == ItemValueType.Genre).Any(clea |
| | 2051 | | } |
| | 2052 | |
|
| 324 | 2053 | | if (tags.Count > 0) |
| | 2054 | | { |
| 0 | 2055 | | var cleanValues = tags.Select(e => GetCleanValue(e)).ToArray().OneOrManyExpressionBuilder<ItemValueMap, stri |
| 0 | 2056 | | baseQuery = baseQuery |
| 0 | 2057 | | .Where(e => e.ItemValues!.AsQueryable().Where(f => f.ItemValue.Type == ItemValueType.Tags).Any(clean |
| | 2058 | | } |
| | 2059 | |
|
| 324 | 2060 | | if (excludeTags.Count > 0) |
| | 2061 | | { |
| 0 | 2062 | | var cleanValues = excludeTags.Select(e => GetCleanValue(e)).ToArray().OneOrManyExpressionBuilder<ItemValueMa |
| 0 | 2063 | | baseQuery = baseQuery |
| 0 | 2064 | | .Where(e => !e.ItemValues!.AsQueryable().Where(f => f.ItemValue.Type == ItemValueType.Tags).Any(clea |
| | 2065 | | } |
| | 2066 | |
|
| 324 | 2067 | | if (filter.StudioIds.Length > 0) |
| | 2068 | | { |
| 0 | 2069 | | baseQuery = baseQuery.WhereReferencedItem(context, ItemValueType.Studios, filter.StudioIds.ToArray()); |
| | 2070 | | } |
| | 2071 | |
|
| 324 | 2072 | | if (filter.OfficialRatings.Length > 0) |
| | 2073 | | { |
| 0 | 2074 | | baseQuery = baseQuery |
| 0 | 2075 | | .Where(e => filter.OfficialRatings.Contains(e.OfficialRating)); |
| | 2076 | | } |
| | 2077 | |
|
| 324 | 2078 | | Expression<Func<BaseItemEntity, bool>>? minParentalRatingFilter = null; |
| 324 | 2079 | | if (filter.MinParentalRating != null) |
| | 2080 | | { |
| 0 | 2081 | | var min = filter.MinParentalRating; |
| 0 | 2082 | | var minScore = min.Score; |
| 0 | 2083 | | var minSubScore = min.SubScore ?? 0; |
| | 2084 | |
|
| 0 | 2085 | | minParentalRatingFilter = e => |
| 0 | 2086 | | e.InheritedParentalRatingValue == null || |
| 0 | 2087 | | e.InheritedParentalRatingValue > minScore || |
| 0 | 2088 | | (e.InheritedParentalRatingValue == minScore && (e.InheritedParentalRatingSubValue ?? 0) >= minSubScore); |
| | 2089 | | } |
| | 2090 | |
|
| 324 | 2091 | | Expression<Func<BaseItemEntity, bool>>? maxParentalRatingFilter = null; |
| 324 | 2092 | | if (filter.MaxParentalRating != null) |
| | 2093 | | { |
| 51 | 2094 | | var max = filter.MaxParentalRating; |
| 51 | 2095 | | var maxScore = max.Score; |
| 51 | 2096 | | var maxSubScore = max.SubScore ?? 0; |
| | 2097 | |
|
| 51 | 2098 | | maxParentalRatingFilter = e => |
| 51 | 2099 | | e.InheritedParentalRatingValue == null || |
| 51 | 2100 | | e.InheritedParentalRatingValue < maxScore || |
| 51 | 2101 | | (e.InheritedParentalRatingValue == maxScore && (e.InheritedParentalRatingSubValue ?? 0) <= maxSubScore); |
| | 2102 | | } |
| | 2103 | |
|
| 324 | 2104 | | if (filter.HasParentalRating ?? false) |
| | 2105 | | { |
| 0 | 2106 | | if (minParentalRatingFilter != null) |
| | 2107 | | { |
| 0 | 2108 | | baseQuery = baseQuery.Where(minParentalRatingFilter); |
| | 2109 | | } |
| | 2110 | |
|
| 0 | 2111 | | if (maxParentalRatingFilter != null) |
| | 2112 | | { |
| 0 | 2113 | | baseQuery = baseQuery.Where(maxParentalRatingFilter); |
| | 2114 | | } |
| | 2115 | | } |
| 324 | 2116 | | else if (filter.BlockUnratedItems.Length > 0) |
| | 2117 | | { |
| 0 | 2118 | | var unratedItemTypes = filter.BlockUnratedItems.Select(f => f.ToString()).ToArray(); |
| 0 | 2119 | | Expression<Func<BaseItemEntity, bool>> unratedItemFilter = e => e.InheritedParentalRatingValue != null || !u |
| | 2120 | |
|
| 0 | 2121 | | if (minParentalRatingFilter != null && maxParentalRatingFilter != null) |
| | 2122 | | { |
| 0 | 2123 | | baseQuery = baseQuery.Where(unratedItemFilter.And(minParentalRatingFilter.And(maxParentalRatingFilter))) |
| | 2124 | | } |
| 0 | 2125 | | else if (minParentalRatingFilter != null) |
| | 2126 | | { |
| 0 | 2127 | | baseQuery = baseQuery.Where(unratedItemFilter.And(minParentalRatingFilter)); |
| | 2128 | | } |
| 0 | 2129 | | else if (maxParentalRatingFilter != null) |
| | 2130 | | { |
| 0 | 2131 | | baseQuery = baseQuery.Where(unratedItemFilter.And(maxParentalRatingFilter)); |
| | 2132 | | } |
| | 2133 | | else |
| | 2134 | | { |
| 0 | 2135 | | baseQuery = baseQuery.Where(unratedItemFilter); |
| | 2136 | | } |
| | 2137 | | } |
| 324 | 2138 | | else if (minParentalRatingFilter != null || maxParentalRatingFilter != null) |
| | 2139 | | { |
| 51 | 2140 | | if (minParentalRatingFilter != null) |
| | 2141 | | { |
| 0 | 2142 | | baseQuery = baseQuery.Where(minParentalRatingFilter); |
| | 2143 | | } |
| | 2144 | |
|
| 51 | 2145 | | if (maxParentalRatingFilter != null) |
| | 2146 | | { |
| 51 | 2147 | | baseQuery = baseQuery.Where(maxParentalRatingFilter); |
| | 2148 | | } |
| | 2149 | | } |
| 273 | 2150 | | else if (!filter.HasParentalRating ?? false) |
| | 2151 | | { |
| 0 | 2152 | | baseQuery = baseQuery |
| 0 | 2153 | | .Where(e => e.InheritedParentalRatingValue == null); |
| | 2154 | | } |
| | 2155 | |
|
| 324 | 2156 | | if (filter.HasOfficialRating.HasValue) |
| | 2157 | | { |
| 0 | 2158 | | if (filter.HasOfficialRating.Value) |
| | 2159 | | { |
| 0 | 2160 | | baseQuery = baseQuery |
| 0 | 2161 | | .Where(e => e.OfficialRating != null && e.OfficialRating != string.Empty); |
| | 2162 | | } |
| | 2163 | | else |
| | 2164 | | { |
| 0 | 2165 | | baseQuery = baseQuery |
| 0 | 2166 | | .Where(e => e.OfficialRating == null || e.OfficialRating == string.Empty); |
| | 2167 | | } |
| | 2168 | | } |
| | 2169 | |
|
| 324 | 2170 | | if (filter.HasOverview.HasValue) |
| | 2171 | | { |
| 0 | 2172 | | if (filter.HasOverview.Value) |
| | 2173 | | { |
| 0 | 2174 | | baseQuery = baseQuery |
| 0 | 2175 | | .Where(e => e.Overview != null && e.Overview != string.Empty); |
| | 2176 | | } |
| | 2177 | | else |
| | 2178 | | { |
| 0 | 2179 | | baseQuery = baseQuery |
| 0 | 2180 | | .Where(e => e.Overview == null || e.Overview == string.Empty); |
| | 2181 | | } |
| | 2182 | | } |
| | 2183 | |
|
| 324 | 2184 | | if (filter.HasOwnerId.HasValue) |
| | 2185 | | { |
| 0 | 2186 | | if (filter.HasOwnerId.Value) |
| | 2187 | | { |
| 0 | 2188 | | baseQuery = baseQuery |
| 0 | 2189 | | .Where(e => e.OwnerId != null); |
| | 2190 | | } |
| | 2191 | | else |
| | 2192 | | { |
| 0 | 2193 | | baseQuery = baseQuery |
| 0 | 2194 | | .Where(e => e.OwnerId == null); |
| | 2195 | | } |
| | 2196 | | } |
| | 2197 | |
|
| 324 | 2198 | | if (!string.IsNullOrWhiteSpace(filter.HasNoAudioTrackWithLanguage)) |
| | 2199 | | { |
| 0 | 2200 | | baseQuery = baseQuery |
| 0 | 2201 | | .Where(e => !e.MediaStreams!.Any(f => f.StreamType == MediaStreamTypeEntity.Audio && f.Language == filte |
| | 2202 | | } |
| | 2203 | |
|
| 324 | 2204 | | if (!string.IsNullOrWhiteSpace(filter.HasNoInternalSubtitleTrackWithLanguage)) |
| | 2205 | | { |
| 0 | 2206 | | baseQuery = baseQuery |
| 0 | 2207 | | .Where(e => !e.MediaStreams!.Any(f => f.StreamType == MediaStreamTypeEntity.Subtitle && !f.IsExternal && |
| | 2208 | | } |
| | 2209 | |
|
| 324 | 2210 | | if (!string.IsNullOrWhiteSpace(filter.HasNoExternalSubtitleTrackWithLanguage)) |
| | 2211 | | { |
| 0 | 2212 | | baseQuery = baseQuery |
| 0 | 2213 | | .Where(e => !e.MediaStreams!.Any(f => f.StreamType == MediaStreamTypeEntity.Subtitle && f.IsExternal && |
| | 2214 | | } |
| | 2215 | |
|
| 324 | 2216 | | if (!string.IsNullOrWhiteSpace(filter.HasNoSubtitleTrackWithLanguage)) |
| | 2217 | | { |
| 0 | 2218 | | baseQuery = baseQuery |
| 0 | 2219 | | .Where(e => !e.MediaStreams!.Any(f => f.StreamType == MediaStreamTypeEntity.Subtitle && f.Language == fi |
| | 2220 | | } |
| | 2221 | |
|
| 324 | 2222 | | if (filter.HasSubtitles.HasValue) |
| | 2223 | | { |
| 0 | 2224 | | baseQuery = baseQuery |
| 0 | 2225 | | .Where(e => e.MediaStreams!.Any(f => f.StreamType == MediaStreamTypeEntity.Subtitle) == filter.HasSubtit |
| | 2226 | | } |
| | 2227 | |
|
| 324 | 2228 | | if (filter.HasChapterImages.HasValue) |
| | 2229 | | { |
| 0 | 2230 | | baseQuery = baseQuery |
| 0 | 2231 | | .Where(e => e.Chapters!.Any(f => f.ImagePath != null) == filter.HasChapterImages.Value); |
| | 2232 | | } |
| | 2233 | |
|
| 324 | 2234 | | if (filter.HasDeadParentId.HasValue && filter.HasDeadParentId.Value) |
| | 2235 | | { |
| 17 | 2236 | | baseQuery = baseQuery |
| 17 | 2237 | | .Where(e => e.ParentId.HasValue && !context.BaseItems.Where(e => e.Id != EF.Constant(PlaceholderId)).Any |
| | 2238 | | } |
| | 2239 | |
|
| 324 | 2240 | | if (filter.IsDeadArtist.HasValue && filter.IsDeadArtist.Value) |
| | 2241 | | { |
| 17 | 2242 | | baseQuery = baseQuery |
| 17 | 2243 | | .Where(e => !context.ItemValues.Where(f => _getAllArtistsValueTypes.Contains(f.Type)).Any(f => f.Val |
| | 2244 | | } |
| | 2245 | |
|
| 324 | 2246 | | if (filter.IsDeadStudio.HasValue && filter.IsDeadStudio.Value) |
| | 2247 | | { |
| 17 | 2248 | | baseQuery = baseQuery |
| 17 | 2249 | | .Where(e => !context.ItemValues.Where(f => _getStudiosValueTypes.Contains(f.Type)).Any(f => f.Value |
| | 2250 | | } |
| | 2251 | |
|
| 324 | 2252 | | if (filter.IsDeadGenre.HasValue && filter.IsDeadGenre.Value) |
| | 2253 | | { |
| 17 | 2254 | | baseQuery = baseQuery |
| 17 | 2255 | | .Where(e => !context.ItemValues.Where(f => _getGenreValueTypes.Contains(f.Type)).Any(f => f.Value == |
| | 2256 | | } |
| | 2257 | |
|
| 324 | 2258 | | if (filter.IsDeadPerson.HasValue && filter.IsDeadPerson.Value) |
| | 2259 | | { |
| 0 | 2260 | | baseQuery = baseQuery |
| 0 | 2261 | | .Where(e => !context.Peoples.Any(f => f.Name == e.Name)); |
| | 2262 | | } |
| | 2263 | |
|
| 324 | 2264 | | if (filter.Years.Length > 0) |
| | 2265 | | { |
| 0 | 2266 | | baseQuery = baseQuery.WhereOneOrMany(filter.Years, e => e.ProductionYear!.Value); |
| | 2267 | | } |
| | 2268 | |
|
| 324 | 2269 | | var isVirtualItem = filter.IsVirtualItem ?? filter.IsMissing; |
| 324 | 2270 | | if (isVirtualItem.HasValue) |
| | 2271 | | { |
| 22 | 2272 | | baseQuery = baseQuery |
| 22 | 2273 | | .Where(e => e.IsVirtualItem == isVirtualItem.Value); |
| | 2274 | | } |
| | 2275 | |
|
| 324 | 2276 | | if (filter.IsSpecialSeason.HasValue) |
| | 2277 | | { |
| 0 | 2278 | | if (filter.IsSpecialSeason.Value) |
| | 2279 | | { |
| 0 | 2280 | | baseQuery = baseQuery |
| 0 | 2281 | | .Where(e => e.IndexNumber == 0); |
| | 2282 | | } |
| | 2283 | | else |
| | 2284 | | { |
| 0 | 2285 | | baseQuery = baseQuery |
| 0 | 2286 | | .Where(e => e.IndexNumber != 0); |
| | 2287 | | } |
| | 2288 | | } |
| | 2289 | |
|
| 324 | 2290 | | if (filter.IsUnaired.HasValue) |
| | 2291 | | { |
| 0 | 2292 | | if (filter.IsUnaired.Value) |
| | 2293 | | { |
| 0 | 2294 | | baseQuery = baseQuery |
| 0 | 2295 | | .Where(e => e.PremiereDate >= now); |
| | 2296 | | } |
| | 2297 | | else |
| | 2298 | | { |
| 0 | 2299 | | baseQuery = baseQuery |
| 0 | 2300 | | .Where(e => e.PremiereDate < now); |
| | 2301 | | } |
| | 2302 | | } |
| | 2303 | |
|
| 324 | 2304 | | if (filter.MediaTypes.Length > 0) |
| | 2305 | | { |
| 21 | 2306 | | var mediaTypes = filter.MediaTypes.Select(f => f.ToString()).ToArray(); |
| 21 | 2307 | | baseQuery = baseQuery.WhereOneOrMany(mediaTypes, e => e.MediaType); |
| | 2308 | | } |
| | 2309 | |
|
| 324 | 2310 | | if (filter.ItemIds.Length > 0) |
| | 2311 | | { |
| 0 | 2312 | | baseQuery = baseQuery.WhereOneOrMany(filter.ItemIds, e => e.Id); |
| | 2313 | | } |
| | 2314 | |
|
| 324 | 2315 | | if (filter.ExcludeItemIds.Length > 0) |
| | 2316 | | { |
| 0 | 2317 | | baseQuery = baseQuery |
| 0 | 2318 | | .Where(e => !filter.ExcludeItemIds.Contains(e.Id)); |
| | 2319 | | } |
| | 2320 | |
|
| 324 | 2321 | | if (filter.ExcludeProviderIds is not null && filter.ExcludeProviderIds.Count > 0) |
| | 2322 | | { |
| 0 | 2323 | | var exclude = filter.ExcludeProviderIds.Select(e => $"{e.Key}:{e.Value}").ToArray(); |
| 0 | 2324 | | baseQuery = baseQuery.Where(e => e.Provider!.Select(f => f.ProviderId + ":" + f.ProviderValue)!.All(f => !ex |
| | 2325 | | } |
| | 2326 | |
|
| 324 | 2327 | | if (filter.HasAnyProviderId is not null && filter.HasAnyProviderId.Count > 0) |
| | 2328 | | { |
| | 2329 | | // Allow setting a null or empty value to get all items that have the specified provider set. |
| 0 | 2330 | | var includeAny = filter.HasAnyProviderId.Where(e => string.IsNullOrEmpty(e.Value)).Select(e => e.Key).ToArra |
| 0 | 2331 | | if (includeAny.Length > 0) |
| | 2332 | | { |
| 0 | 2333 | | baseQuery = baseQuery.Where(e => e.Provider!.Any(f => includeAny.Contains(f.ProviderId))); |
| | 2334 | | } |
| | 2335 | |
|
| 0 | 2336 | | var includeSelected = filter.HasAnyProviderId.Where(e => !string.IsNullOrEmpty(e.Value)).Select(e => $"{e.Ke |
| 0 | 2337 | | if (includeSelected.Length > 0) |
| | 2338 | | { |
| 0 | 2339 | | baseQuery = baseQuery.Where(e => e.Provider!.Select(f => f.ProviderId + ":" + f.ProviderValue)!.Any(f => |
| | 2340 | | } |
| | 2341 | | } |
| | 2342 | |
|
| 324 | 2343 | | if (filter.HasImdbId.HasValue) |
| | 2344 | | { |
| 0 | 2345 | | baseQuery = baseQuery.Where(e => e.Provider!.Any(f => f.ProviderId == "imdb")); |
| | 2346 | | } |
| | 2347 | |
|
| 324 | 2348 | | if (filter.HasTmdbId.HasValue) |
| | 2349 | | { |
| 0 | 2350 | | baseQuery = baseQuery.Where(e => e.Provider!.Any(f => f.ProviderId == "tmdb")); |
| | 2351 | | } |
| | 2352 | |
|
| 324 | 2353 | | if (filter.HasTvdbId.HasValue) |
| | 2354 | | { |
| 0 | 2355 | | baseQuery = baseQuery.Where(e => e.Provider!.Any(f => f.ProviderId == "tvdb")); |
| | 2356 | | } |
| | 2357 | |
|
| 324 | 2358 | | var queryTopParentIds = filter.TopParentIds; |
| | 2359 | |
|
| 324 | 2360 | | if (queryTopParentIds.Length > 0) |
| | 2361 | | { |
| 13 | 2362 | | var includedItemByNameTypes = GetItemByNameTypesInQuery(filter); |
| 13 | 2363 | | var enableItemsByName = (filter.IncludeItemsByName ?? false) && includedItemByNameTypes.Count > 0; |
| 13 | 2364 | | if (enableItemsByName && includedItemByNameTypes.Count > 0) |
| | 2365 | | { |
| 0 | 2366 | | baseQuery = baseQuery.Where(e => includedItemByNameTypes.Contains(e.Type) || queryTopParentIds.Any(w => |
| | 2367 | | } |
| | 2368 | | else |
| | 2369 | | { |
| 13 | 2370 | | baseQuery = baseQuery.WhereOneOrMany(queryTopParentIds, e => e.TopParentId!.Value); |
| | 2371 | | } |
| | 2372 | | } |
| | 2373 | |
|
| 324 | 2374 | | if (filter.AncestorIds.Length > 0) |
| | 2375 | | { |
| 44 | 2376 | | baseQuery = baseQuery.Where(e => e.Parents!.Any(f => filter.AncestorIds.Contains(f.ParentItemId))); |
| | 2377 | | } |
| | 2378 | |
|
| 324 | 2379 | | if (!string.IsNullOrWhiteSpace(filter.AncestorWithPresentationUniqueKey)) |
| | 2380 | | { |
| 0 | 2381 | | baseQuery = baseQuery |
| 0 | 2382 | | .Where(e => context.BaseItems.Where(e => e.Id != EF.Constant(PlaceholderId)).Where(f => f.PresentationUn |
| | 2383 | | } |
| | 2384 | |
|
| 324 | 2385 | | if (!string.IsNullOrWhiteSpace(filter.SeriesPresentationUniqueKey)) |
| | 2386 | | { |
| 0 | 2387 | | baseQuery = baseQuery |
| 0 | 2388 | | .Where(e => e.SeriesPresentationUniqueKey == filter.SeriesPresentationUniqueKey); |
| | 2389 | | } |
| | 2390 | |
|
| 324 | 2391 | | if (filter.ExcludeInheritedTags.Length > 0) |
| | 2392 | | { |
| 0 | 2393 | | baseQuery = baseQuery |
| 0 | 2394 | | .Where(e => !e.ItemValues!.Where(w => w.ItemValue.Type == ItemValueType.InheritedTags || w.ItemValue.Typ |
| 0 | 2395 | | .Any(f => filter.ExcludeInheritedTags.Contains(f.ItemValue.CleanValue))); |
| | 2396 | | } |
| | 2397 | |
|
| 324 | 2398 | | if (filter.IncludeInheritedTags.Length > 0) |
| | 2399 | | { |
| | 2400 | | // Episodes do not store inherit tags from their parents in the database, and the tag may be still required |
| | 2401 | | // In addition to the tags for the episodes themselves, we need to manually query its parent (the season)'s |
| 0 | 2402 | | if (includeTypes.Length == 1 && includeTypes.FirstOrDefault() is BaseItemKind.Episode) |
| | 2403 | | { |
| 0 | 2404 | | baseQuery = baseQuery |
| 0 | 2405 | | .Where(e => e.ItemValues!.Where(f => f.ItemValue.Type == ItemValueType.InheritedTags || f.ItemValue. |
| 0 | 2406 | | .Any(f => filter.IncludeInheritedTags.Contains(f.ItemValue.CleanValue)) |
| 0 | 2407 | | || |
| 0 | 2408 | | (e.ParentId.HasValue && context.ItemValuesMap.Where(w => w.ItemId == e.ParentId.Value && (w.Item |
| 0 | 2409 | | .Any(f => filter.IncludeInheritedTags.Contains(f.ItemValue.CleanValue)))); |
| | 2410 | | } |
| | 2411 | |
|
| | 2412 | | // A playlist should be accessible to its owner regardless of allowed tags. |
| 0 | 2413 | | else if (includeTypes.Length == 1 && includeTypes.FirstOrDefault() is BaseItemKind.Playlist) |
| | 2414 | | { |
| 0 | 2415 | | baseQuery = baseQuery |
| 0 | 2416 | | .Where(e => e.ItemValues!.Where(f => f.ItemValue.Type == ItemValueType.InheritedTags || f.ItemValue. |
| 0 | 2417 | | .Any(f => filter.IncludeInheritedTags.Contains(f.ItemValue.CleanValue)) |
| 0 | 2418 | | || e.Data!.Contains($"OwnerUserId\":\"{filter.User!.Id:N}\"")); |
| | 2419 | | // d ^^ this is stupid it hate this. |
| | 2420 | | } |
| | 2421 | | else |
| | 2422 | | { |
| 0 | 2423 | | baseQuery = baseQuery |
| 0 | 2424 | | .Where(e => e.ItemValues!.Where(f => f.ItemValue.Type == ItemValueType.InheritedTags || f.ItemValue. |
| 0 | 2425 | | .Any(f => filter.IncludeInheritedTags.Contains(f.ItemValue.CleanValue))); |
| | 2426 | | } |
| | 2427 | | } |
| | 2428 | |
|
| 324 | 2429 | | if (filter.SeriesStatuses.Length > 0) |
| | 2430 | | { |
| 0 | 2431 | | var seriesStatus = filter.SeriesStatuses.Select(e => e.ToString()).ToArray(); |
| 0 | 2432 | | baseQuery = baseQuery |
| 0 | 2433 | | .Where(e => seriesStatus.Any(f => e.Data!.Contains(f))); |
| | 2434 | | } |
| | 2435 | |
|
| 324 | 2436 | | if (filter.BoxSetLibraryFolders.Length > 0) |
| | 2437 | | { |
| 0 | 2438 | | var boxsetFolders = filter.BoxSetLibraryFolders.Select(e => e.ToString("N", CultureInfo.InvariantCulture)).T |
| 0 | 2439 | | baseQuery = baseQuery |
| 0 | 2440 | | .Where(e => boxsetFolders.Any(f => e.Data!.Contains(f))); |
| | 2441 | | } |
| | 2442 | |
|
| 324 | 2443 | | if (filter.VideoTypes.Length > 0) |
| | 2444 | | { |
| 0 | 2445 | | var videoTypeBs = filter.VideoTypes.Select(e => $"\"VideoType\":\"{e}\""); |
| 0 | 2446 | | baseQuery = baseQuery |
| 0 | 2447 | | .Where(e => videoTypeBs.Any(f => e.Data!.Contains(f))); |
| | 2448 | | } |
| | 2449 | |
|
| 324 | 2450 | | if (filter.Is3D.HasValue) |
| | 2451 | | { |
| 0 | 2452 | | if (filter.Is3D.Value) |
| | 2453 | | { |
| 0 | 2454 | | baseQuery = baseQuery |
| 0 | 2455 | | .Where(e => e.Data!.Contains("Video3DFormat")); |
| | 2456 | | } |
| | 2457 | | else |
| | 2458 | | { |
| 0 | 2459 | | baseQuery = baseQuery |
| 0 | 2460 | | .Where(e => !e.Data!.Contains("Video3DFormat")); |
| | 2461 | | } |
| | 2462 | | } |
| | 2463 | |
|
| 324 | 2464 | | if (filter.IsPlaceHolder.HasValue) |
| | 2465 | | { |
| 0 | 2466 | | if (filter.IsPlaceHolder.Value) |
| | 2467 | | { |
| 0 | 2468 | | baseQuery = baseQuery |
| 0 | 2469 | | .Where(e => e.Data!.Contains("IsPlaceHolder\":true")); |
| | 2470 | | } |
| | 2471 | | else |
| | 2472 | | { |
| 0 | 2473 | | baseQuery = baseQuery |
| 0 | 2474 | | .Where(e => !e.Data!.Contains("IsPlaceHolder\":true")); |
| | 2475 | | } |
| | 2476 | | } |
| | 2477 | |
|
| 324 | 2478 | | if (filter.HasSpecialFeature.HasValue) |
| | 2479 | | { |
| 0 | 2480 | | if (filter.HasSpecialFeature.Value) |
| | 2481 | | { |
| 0 | 2482 | | baseQuery = baseQuery |
| 0 | 2483 | | .Where(e => e.ExtraIds != null); |
| | 2484 | | } |
| | 2485 | | else |
| | 2486 | | { |
| 0 | 2487 | | baseQuery = baseQuery |
| 0 | 2488 | | .Where(e => e.ExtraIds == null); |
| | 2489 | | } |
| | 2490 | | } |
| | 2491 | |
|
| 324 | 2492 | | if (filter.HasTrailer.HasValue || filter.HasThemeSong.HasValue || filter.HasThemeVideo.HasValue) |
| | 2493 | | { |
| 0 | 2494 | | if (filter.HasTrailer.GetValueOrDefault() || filter.HasThemeSong.GetValueOrDefault() || filter.HasThemeVideo |
| | 2495 | | { |
| 0 | 2496 | | baseQuery = baseQuery |
| 0 | 2497 | | .Where(e => e.ExtraIds != null); |
| | 2498 | | } |
| | 2499 | | else |
| | 2500 | | { |
| 0 | 2501 | | baseQuery = baseQuery |
| 0 | 2502 | | .Where(e => e.ExtraIds == null); |
| | 2503 | | } |
| | 2504 | | } |
| | 2505 | |
|
| 324 | 2506 | | return baseQuery; |
| | 2507 | | } |
| | 2508 | |
|
| | 2509 | | /// <inheritdoc/> |
| | 2510 | | public async Task<bool> ItemExistsAsync(Guid id) |
| | 2511 | | { |
| | 2512 | | var dbContext = await _dbProvider.CreateDbContextAsync().ConfigureAwait(false); |
| | 2513 | | await using (dbContext.ConfigureAwait(false)) |
| | 2514 | | { |
| | 2515 | | return await dbContext.BaseItems.AnyAsync(f => f.Id == id).ConfigureAwait(false); |
| | 2516 | | } |
| | 2517 | | } |
| | 2518 | |
|
| | 2519 | | /// <inheritdoc/> |
| | 2520 | | public bool GetIsPlayed(User user, Guid id, bool recursive) |
| | 2521 | | { |
| 0 | 2522 | | using var dbContext = _dbProvider.CreateDbContext(); |
| | 2523 | |
|
| 0 | 2524 | | if (recursive) |
| | 2525 | | { |
| 0 | 2526 | | var folderList = TraverseHirachyDown(id, dbContext, item => (item.IsFolder || item.IsVirtualItem)); |
| | 2527 | |
|
| 0 | 2528 | | return dbContext.BaseItems |
| 0 | 2529 | | .Where(e => folderList.Contains(e.ParentId!.Value) && !e.IsFolder && !e.IsVirtualItem) |
| 0 | 2530 | | .All(f => f.UserData!.Any(e => e.UserId == user.Id && e.Played)); |
| | 2531 | | } |
| | 2532 | |
|
| 0 | 2533 | | return dbContext.BaseItems.Where(e => e.ParentId == id).All(f => f.UserData!.Any(e => e.UserId == user.Id && e.P |
| 0 | 2534 | | } |
| | 2535 | |
|
| | 2536 | | private static HashSet<Guid> TraverseHirachyDown(Guid parentId, JellyfinDbContext dbContext, Expression<Func<BaseIte |
| | 2537 | | { |
| 2 | 2538 | | var folderStack = new HashSet<Guid>() |
| 2 | 2539 | | { |
| 2 | 2540 | | parentId |
| 2 | 2541 | | }; |
| 2 | 2542 | | var folderList = new HashSet<Guid>() |
| 2 | 2543 | | { |
| 2 | 2544 | | parentId |
| 2 | 2545 | | }; |
| | 2546 | |
|
| 4 | 2547 | | while (folderStack.Count != 0) |
| | 2548 | | { |
| 2 | 2549 | | var items = folderStack.ToArray(); |
| 2 | 2550 | | folderStack.Clear(); |
| 2 | 2551 | | var query = dbContext.BaseItems |
| 2 | 2552 | | .WhereOneOrMany(items, e => e.ParentId!.Value); |
| | 2553 | |
|
| 2 | 2554 | | if (filter != null) |
| | 2555 | | { |
| 0 | 2556 | | query = query.Where(filter); |
| | 2557 | | } |
| | 2558 | |
|
| 4 | 2559 | | foreach (var item in query.Select(e => e.Id).ToArray()) |
| | 2560 | | { |
| 0 | 2561 | | if (folderList.Add(item)) |
| | 2562 | | { |
| 0 | 2563 | | folderStack.Add(item); |
| | 2564 | | } |
| | 2565 | | } |
| | 2566 | | } |
| | 2567 | |
|
| 2 | 2568 | | return folderList; |
| | 2569 | | } |
| | 2570 | |
|
| | 2571 | | /// <inheritdoc/> |
| | 2572 | | public IReadOnlyDictionary<string, MusicArtist[]> FindArtists(IReadOnlyList<string> artistNames) |
| | 2573 | | { |
| 0 | 2574 | | using var dbContext = _dbProvider.CreateDbContext(); |
| | 2575 | |
|
| 0 | 2576 | | var artists = dbContext.BaseItems.Where(e => e.Type == _itemTypeLookup.BaseItemKindNames[BaseItemKind.MusicArtis |
| 0 | 2577 | | .Where(e => artistNames.Contains(e.Name)) |
| 0 | 2578 | | .ToArray(); |
| | 2579 | |
|
| 0 | 2580 | | return artists.GroupBy(e => e.Name).ToDictionary(e => e.Key!, e => e.Select(f => DeserializeBaseItem(f)).Cast<Mu |
| 0 | 2581 | | } |
| | 2582 | | } |