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