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