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