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