BaseItemRepository.cs 78 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150
  1. using System;
  2. using System.Collections.Concurrent;
  3. using System.Collections.Generic;
  4. using System.Collections.Immutable;
  5. using System.Globalization;
  6. using System.IO;
  7. using System.Linq;
  8. using System.Linq.Expressions;
  9. using System.Reflection;
  10. using System.Text;
  11. using System.Text.Json;
  12. using System.Threading;
  13. using Jellyfin.Data.Entities;
  14. using Jellyfin.Data.Enums;
  15. using Jellyfin.Extensions;
  16. using Jellyfin.Extensions.Json;
  17. using MediaBrowser.Common;
  18. using MediaBrowser.Controller;
  19. using MediaBrowser.Controller.Channels;
  20. using MediaBrowser.Controller.Configuration;
  21. using MediaBrowser.Controller.Entities;
  22. using MediaBrowser.Controller.Entities.Audio;
  23. using MediaBrowser.Controller.Entities.TV;
  24. using MediaBrowser.Controller.LiveTv;
  25. using MediaBrowser.Controller.Persistence;
  26. using MediaBrowser.Model.Dto;
  27. using MediaBrowser.Model.Entities;
  28. using MediaBrowser.Model.LiveTv;
  29. using MediaBrowser.Model.Querying;
  30. using Microsoft.EntityFrameworkCore;
  31. using Microsoft.Extensions.Logging;
  32. using BaseItemDto = MediaBrowser.Controller.Entities.BaseItem;
  33. using BaseItemEntity = Jellyfin.Data.Entities.BaseItemEntity;
  34. #pragma warning disable RS0030 // Do not use banned APIs
  35. namespace Jellyfin.Server.Implementations.Item;
  36. /// <summary>
  37. /// Handles all storage logic for BaseItems.
  38. /// </summary>
  39. /// <remarks>
  40. /// Initializes a new instance of the <see cref="BaseItemRepository"/> class.
  41. /// </remarks>
  42. /// <param name="dbProvider">The db factory.</param>
  43. /// <param name="appHost">The Application host.</param>
  44. /// <param name="itemTypeLookup">The static type lookup.</param>
  45. /// <param name="serverConfigurationManager">The server Configuration manager.</param>
  46. /// <param name="logger">System logger.</param>
  47. public sealed class BaseItemRepository(
  48. IDbContextFactory<JellyfinDbContext> dbProvider,
  49. IServerApplicationHost appHost,
  50. IItemTypeLookup itemTypeLookup,
  51. IServerConfigurationManager serverConfigurationManager,
  52. ILogger<BaseItemRepository> logger)
  53. : IItemRepository, IDisposable
  54. {
  55. /// <summary>
  56. /// This holds all the types in the running assemblies
  57. /// so that we can de-serialize properly when we don't have strong types.
  58. /// </summary>
  59. private static readonly ConcurrentDictionary<string, Type?> _typeMap = new ConcurrentDictionary<string, Type?>();
  60. private bool _disposed;
  61. /// <inheritdoc/>
  62. public void Dispose()
  63. {
  64. if (_disposed)
  65. {
  66. return;
  67. }
  68. _disposed = true;
  69. }
  70. /// <inheritdoc />
  71. public void DeleteItem(Guid id)
  72. {
  73. ArgumentNullException.ThrowIfNull(id.IsEmpty() ? null : id);
  74. using var context = dbProvider.CreateDbContext();
  75. using var transaction = context.Database.BeginTransaction();
  76. context.PeopleBaseItemMap.Where(e => e.ItemId == id).ExecuteDelete();
  77. context.Peoples.Where(e => e.BaseItems!.Count == 0).ExecuteDelete();
  78. context.Chapters.Where(e => e.ItemId == id).ExecuteDelete();
  79. context.MediaStreamInfos.Where(e => e.ItemId == id).ExecuteDelete();
  80. context.AncestorIds.Where(e => e.ItemId == id || e.ParentItemId == id).ExecuteDelete();
  81. context.ItemValuesMap.Where(e => e.ItemId == id).ExecuteDelete();
  82. context.ItemValues.Where(e => e.BaseItemsMap!.Count == 0).ExecuteDelete();
  83. context.BaseItemImageInfos.Where(e => e.ItemId == id).ExecuteDelete();
  84. context.BaseItemProviders.Where(e => e.ItemId == id).ExecuteDelete();
  85. context.BaseItems.Where(e => e.Id == id).ExecuteDelete();
  86. context.SaveChanges();
  87. transaction.Commit();
  88. }
  89. /// <inheritdoc />
  90. public void UpdateInheritedValues()
  91. {
  92. using var context = dbProvider.CreateDbContext();
  93. using var transaction = context.Database.BeginTransaction();
  94. context.ItemValuesMap.Where(e => e.ItemValue.Type == ItemValueType.InheritedTags).ExecuteDelete();
  95. // ItemValue Inheritence is now correctly mapped via AncestorId on demand
  96. context.SaveChanges();
  97. transaction.Commit();
  98. }
  99. /// <inheritdoc cref="IItemRepository"/>
  100. public IReadOnlyList<Guid> GetItemIdsList(InternalItemsQuery filter)
  101. {
  102. ArgumentNullException.ThrowIfNull(filter);
  103. PrepareFilterQuery(filter);
  104. using var context = dbProvider.CreateDbContext();
  105. return ApplyQueryFilter(context.BaseItems.AsNoTracking(), context, filter).Select(e => e.Id).ToImmutableArray();
  106. }
  107. /// <inheritdoc />
  108. public QueryResult<(BaseItemDto Item, ItemCounts ItemCounts)> GetAllArtists(InternalItemsQuery filter)
  109. {
  110. return GetItemValues(filter, [ItemValueType.Artist, ItemValueType.AlbumArtist], itemTypeLookup.BaseItemKindNames[BaseItemKind.MusicArtist]!);
  111. }
  112. /// <inheritdoc />
  113. public QueryResult<(BaseItemDto Item, ItemCounts ItemCounts)> GetArtists(InternalItemsQuery filter)
  114. {
  115. return GetItemValues(filter, [ItemValueType.Artist], itemTypeLookup.BaseItemKindNames[BaseItemKind.MusicArtist]!);
  116. }
  117. /// <inheritdoc />
  118. public QueryResult<(BaseItemDto Item, ItemCounts ItemCounts)> GetAlbumArtists(InternalItemsQuery filter)
  119. {
  120. return GetItemValues(filter, [ItemValueType.AlbumArtist], itemTypeLookup.BaseItemKindNames[BaseItemKind.MusicArtist]!);
  121. }
  122. /// <inheritdoc />
  123. public QueryResult<(BaseItemDto Item, ItemCounts ItemCounts)> GetStudios(InternalItemsQuery filter)
  124. {
  125. return GetItemValues(filter, [ItemValueType.Studios], itemTypeLookup.BaseItemKindNames[BaseItemKind.Studio]!);
  126. }
  127. /// <inheritdoc />
  128. public QueryResult<(BaseItemDto Item, ItemCounts ItemCounts)> GetGenres(InternalItemsQuery filter)
  129. {
  130. return GetItemValues(filter, [ItemValueType.Genre], itemTypeLookup.BaseItemKindNames[BaseItemKind.Genre]!);
  131. }
  132. /// <inheritdoc />
  133. public QueryResult<(BaseItemDto Item, ItemCounts ItemCounts)> GetMusicGenres(InternalItemsQuery filter)
  134. {
  135. return GetItemValues(filter, [ItemValueType.Genre], itemTypeLookup.BaseItemKindNames[BaseItemKind.MusicGenre]!);
  136. }
  137. /// <inheritdoc />
  138. public IReadOnlyList<string> GetStudioNames()
  139. {
  140. return GetItemValueNames([ItemValueType.Studios], [], []);
  141. }
  142. /// <inheritdoc />
  143. public IReadOnlyList<string> GetAllArtistNames()
  144. {
  145. return GetItemValueNames([ItemValueType.Artist, ItemValueType.AlbumArtist], [], []);
  146. }
  147. /// <inheritdoc />
  148. public IReadOnlyList<string> GetMusicGenreNames()
  149. {
  150. return GetItemValueNames(
  151. [ItemValueType.Genre],
  152. itemTypeLookup.MusicGenreTypes,
  153. []);
  154. }
  155. /// <inheritdoc />
  156. public IReadOnlyList<string> GetGenreNames()
  157. {
  158. return GetItemValueNames(
  159. [ItemValueType.Genre],
  160. [],
  161. itemTypeLookup.MusicGenreTypes);
  162. }
  163. /// <inheritdoc cref="IItemRepository"/>
  164. public QueryResult<BaseItemDto> GetItems(InternalItemsQuery filter)
  165. {
  166. ArgumentNullException.ThrowIfNull(filter);
  167. if (!filter.EnableTotalRecordCount || (!filter.Limit.HasValue && (filter.StartIndex ?? 0) == 0))
  168. {
  169. var returnList = GetItemList(filter);
  170. return new QueryResult<BaseItemDto>(
  171. filter.StartIndex,
  172. returnList.Count,
  173. returnList);
  174. }
  175. PrepareFilterQuery(filter);
  176. var result = new QueryResult<BaseItemDto>();
  177. using var context = dbProvider.CreateDbContext();
  178. IQueryable<BaseItemEntity> dbQuery = PrepareItemQuery(context, filter);
  179. dbQuery = TranslateQuery(dbQuery, context, filter);
  180. // dbQuery = dbQuery.Distinct();
  181. if (filter.EnableTotalRecordCount)
  182. {
  183. result.TotalRecordCount = dbQuery.Count();
  184. }
  185. dbQuery = ApplyOrder(dbQuery, filter);
  186. dbQuery = ApplyQueryPageing(dbQuery, filter);
  187. result.Items = dbQuery.AsEnumerable().Where(e => e is not null).Select(w => DeserialiseBaseItem(w, filter.SkipDeserialization)).ToImmutableArray();
  188. result.StartIndex = filter.StartIndex ?? 0;
  189. return result;
  190. }
  191. /// <inheritdoc cref="IItemRepository"/>
  192. public IReadOnlyList<BaseItemDto> GetItemList(InternalItemsQuery filter)
  193. {
  194. ArgumentNullException.ThrowIfNull(filter);
  195. PrepareFilterQuery(filter);
  196. using var context = dbProvider.CreateDbContext();
  197. IQueryable<BaseItemEntity> dbQuery = PrepareItemQuery(context, filter);
  198. dbQuery = TranslateQuery(dbQuery, context, filter);
  199. // dbQuery = dbQuery.Distinct();
  200. dbQuery = ApplyOrder(dbQuery, filter);
  201. dbQuery = ApplyGroupingFilter(dbQuery, filter);
  202. dbQuery = ApplyQueryPageing(dbQuery, filter);
  203. return dbQuery.AsEnumerable().Where(e => e is not null).Select(w => DeserialiseBaseItem(w, filter.SkipDeserialization)).ToImmutableArray();
  204. }
  205. private IQueryable<BaseItemEntity> ApplyGroupingFilter(IQueryable<BaseItemEntity> dbQuery, InternalItemsQuery filter)
  206. {
  207. var enableGroupByPresentationUniqueKey = EnableGroupByPresentationUniqueKey(filter);
  208. if (enableGroupByPresentationUniqueKey && filter.GroupBySeriesPresentationUniqueKey)
  209. {
  210. dbQuery = dbQuery.GroupBy(e => new { e.PresentationUniqueKey, e.SeriesPresentationUniqueKey }).Select(e => e.First());
  211. }
  212. else if (enableGroupByPresentationUniqueKey)
  213. {
  214. dbQuery = dbQuery.GroupBy(e => e.PresentationUniqueKey).Select(e => e.First());
  215. }
  216. else if (filter.GroupBySeriesPresentationUniqueKey)
  217. {
  218. dbQuery = dbQuery.GroupBy(e => e.SeriesPresentationUniqueKey).Select(e => e.First());
  219. }
  220. else
  221. {
  222. dbQuery = dbQuery.Distinct();
  223. }
  224. return dbQuery;
  225. }
  226. private IQueryable<BaseItemEntity> ApplyQueryPageing(IQueryable<BaseItemEntity> dbQuery, InternalItemsQuery filter)
  227. {
  228. if (filter.Limit.HasValue || filter.StartIndex.HasValue)
  229. {
  230. var offset = filter.StartIndex ?? 0;
  231. if (offset > 0)
  232. {
  233. dbQuery = dbQuery.Skip(offset);
  234. }
  235. if (filter.Limit.HasValue)
  236. {
  237. dbQuery = dbQuery.Take(filter.Limit.Value);
  238. }
  239. }
  240. return dbQuery;
  241. }
  242. private IQueryable<BaseItemEntity> ApplyQueryFilter(IQueryable<BaseItemEntity> dbQuery, JellyfinDbContext context, InternalItemsQuery filter)
  243. {
  244. dbQuery = TranslateQuery(dbQuery, context, filter);
  245. dbQuery = ApplyOrder(dbQuery, filter);
  246. dbQuery = ApplyGroupingFilter(dbQuery, filter);
  247. dbQuery = ApplyQueryPageing(dbQuery, filter);
  248. return dbQuery;
  249. }
  250. private IQueryable<BaseItemEntity> PrepareItemQuery(JellyfinDbContext context, InternalItemsQuery filter)
  251. {
  252. IQueryable<BaseItemEntity> dbQuery = context.BaseItems.AsNoTracking().AsSingleQuery()
  253. .Include(e => e.TrailerTypes)
  254. .Include(e => e.Provider)
  255. .Include(e => e.LockedFields);
  256. if (filter.DtoOptions.EnableImages)
  257. {
  258. dbQuery = dbQuery.Include(e => e.Images);
  259. }
  260. return dbQuery;
  261. }
  262. /// <inheritdoc/>
  263. public int GetCount(InternalItemsQuery filter)
  264. {
  265. ArgumentNullException.ThrowIfNull(filter);
  266. // Hack for right now since we currently don't support filtering out these duplicates within a query
  267. PrepareFilterQuery(filter);
  268. using var context = dbProvider.CreateDbContext();
  269. var dbQuery = TranslateQuery(context.BaseItems.AsNoTracking(), context, filter);
  270. return dbQuery.Count();
  271. }
  272. #pragma warning disable CA1307 // Specify StringComparison for clarity
  273. /// <summary>
  274. /// Gets the type.
  275. /// </summary>
  276. /// <param name="typeName">Name of the type.</param>
  277. /// <returns>Type.</returns>
  278. /// <exception cref="ArgumentNullException"><c>typeName</c> is null.</exception>
  279. private static Type? GetType(string typeName)
  280. {
  281. ArgumentException.ThrowIfNullOrEmpty(typeName);
  282. return _typeMap.GetOrAdd(typeName, k => AppDomain.CurrentDomain.GetAssemblies()
  283. .Select(a => a.GetType(k))
  284. .FirstOrDefault(t => t is not null));
  285. }
  286. /// <inheritdoc cref="IItemRepository" />
  287. public void SaveImages(BaseItemDto item)
  288. {
  289. ArgumentNullException.ThrowIfNull(item);
  290. var images = item.ImageInfos.Select(e => Map(item.Id, e));
  291. using var context = dbProvider.CreateDbContext();
  292. using var transaction = context.Database.BeginTransaction();
  293. context.BaseItemImageInfos.Where(e => e.ItemId == item.Id).ExecuteDelete();
  294. context.BaseItemImageInfos.AddRange(images);
  295. context.SaveChanges();
  296. transaction.Commit();
  297. }
  298. /// <inheritdoc cref="IItemRepository" />
  299. public void SaveItems(IReadOnlyList<BaseItemDto> items, CancellationToken cancellationToken)
  300. {
  301. UpdateOrInsertItems(items, cancellationToken);
  302. }
  303. /// <inheritdoc cref="IItemRepository" />
  304. public void UpdateOrInsertItems(IReadOnlyList<BaseItemDto> items, CancellationToken cancellationToken)
  305. {
  306. ArgumentNullException.ThrowIfNull(items);
  307. cancellationToken.ThrowIfCancellationRequested();
  308. var itemsLen = items.Count;
  309. var tuples = new List<(BaseItemDto Item, List<Guid>? AncestorIds, BaseItemDto TopParent, IEnumerable<string> UserDataKey, List<string> InheritedTags)>();
  310. foreach (var item in items.GroupBy(e => e.Id).Select(e => e.Last()))
  311. {
  312. var ancestorIds = item.SupportsAncestors ?
  313. item.GetAncestorIds().Distinct().ToList() :
  314. null;
  315. var topParent = item.GetTopParent();
  316. var userdataKey = item.GetUserDataKeys();
  317. var inheritedTags = item.GetInheritedTags();
  318. tuples.Add((item, ancestorIds, topParent, userdataKey, inheritedTags));
  319. }
  320. var localFuckingItemValueCache = new Dictionary<(int MagicNumber, string Value), Guid>();
  321. using var context = dbProvider.CreateDbContext();
  322. using var transaction = context.Database.BeginTransaction();
  323. foreach (var item in tuples)
  324. {
  325. var entity = Map(item.Item);
  326. // TODO: refactor this "inconsistency"
  327. entity.TopParentId = item.TopParent?.Id;
  328. if (!context.BaseItems.Any(e => e.Id == entity.Id))
  329. {
  330. context.BaseItems.Add(entity);
  331. }
  332. else
  333. {
  334. context.BaseItemProviders.Where(e => e.ItemId == entity.Id).ExecuteDelete();
  335. context.BaseItems.Attach(entity).State = EntityState.Modified;
  336. }
  337. context.AncestorIds.Where(e => e.ItemId == entity.Id).ExecuteDelete();
  338. if (item.Item.SupportsAncestors && item.AncestorIds != null)
  339. {
  340. foreach (var ancestorId in item.AncestorIds)
  341. {
  342. if (!context.BaseItems.Any(f => f.Id == ancestorId))
  343. {
  344. continue;
  345. }
  346. context.AncestorIds.Add(new AncestorId()
  347. {
  348. ParentItemId = ancestorId,
  349. ItemId = entity.Id,
  350. Item = null!,
  351. ParentItem = null!
  352. });
  353. }
  354. }
  355. // Never save duplicate itemValues as they are now mapped anyway.
  356. var itemValuesToSave = GetItemValuesToSave(item.Item, item.InheritedTags).DistinctBy(e => (GetCleanValue(e.Value), e.MagicNumber));
  357. context.ItemValuesMap.Where(e => e.ItemId == entity.Id).ExecuteDelete();
  358. foreach (var itemValue in itemValuesToSave)
  359. {
  360. if (!localFuckingItemValueCache.TryGetValue(itemValue, out var refValue))
  361. {
  362. refValue = context.ItemValues
  363. .Where(f => f.CleanValue == GetCleanValue(itemValue.Value) && (int)f.Type == itemValue.MagicNumber)
  364. .Select(e => e.ItemValueId)
  365. .FirstOrDefault();
  366. }
  367. if (refValue.IsEmpty())
  368. {
  369. context.ItemValues.Add(new ItemValue()
  370. {
  371. CleanValue = GetCleanValue(itemValue.Value),
  372. Type = (ItemValueType)itemValue.MagicNumber,
  373. ItemValueId = refValue = Guid.NewGuid(),
  374. Value = itemValue.Value
  375. });
  376. localFuckingItemValueCache[itemValue] = refValue;
  377. }
  378. context.ItemValuesMap.Add(new ItemValueMap()
  379. {
  380. Item = null!,
  381. ItemId = entity.Id,
  382. ItemValue = null!,
  383. ItemValueId = refValue
  384. });
  385. }
  386. }
  387. context.SaveChanges();
  388. transaction.Commit();
  389. }
  390. /// <inheritdoc cref="IItemRepository" />
  391. public BaseItemDto? RetrieveItem(Guid id)
  392. {
  393. if (id.IsEmpty())
  394. {
  395. throw new ArgumentException("Guid can't be empty", nameof(id));
  396. }
  397. using var context = dbProvider.CreateDbContext();
  398. var item = PrepareItemQuery(context, new()
  399. {
  400. DtoOptions = new()
  401. {
  402. EnableImages = true
  403. }
  404. }).FirstOrDefault(e => e.Id == id);
  405. if (item is null)
  406. {
  407. return null;
  408. }
  409. return DeserialiseBaseItem(item);
  410. }
  411. /// <summary>
  412. /// Maps a Entity to the DTO.
  413. /// </summary>
  414. /// <param name="entity">The entity.</param>
  415. /// <param name="dto">The dto base instance.</param>
  416. /// <param name="appHost">The Application server Host.</param>
  417. /// <returns>The dto to map.</returns>
  418. public static BaseItemDto Map(BaseItemEntity entity, BaseItemDto dto, IServerApplicationHost? appHost)
  419. {
  420. dto.Id = entity.Id;
  421. dto.ParentId = entity.ParentId.GetValueOrDefault();
  422. dto.Path = appHost?.ExpandVirtualPath(entity.Path) ?? entity.Path;
  423. dto.EndDate = entity.EndDate;
  424. dto.CommunityRating = entity.CommunityRating;
  425. dto.CustomRating = entity.CustomRating;
  426. dto.IndexNumber = entity.IndexNumber;
  427. dto.IsLocked = entity.IsLocked;
  428. dto.Name = entity.Name;
  429. dto.OfficialRating = entity.OfficialRating;
  430. dto.Overview = entity.Overview;
  431. dto.ParentIndexNumber = entity.ParentIndexNumber;
  432. dto.PremiereDate = entity.PremiereDate;
  433. dto.ProductionYear = entity.ProductionYear;
  434. dto.SortName = entity.SortName;
  435. dto.ForcedSortName = entity.ForcedSortName;
  436. dto.RunTimeTicks = entity.RunTimeTicks;
  437. dto.PreferredMetadataLanguage = entity.PreferredMetadataLanguage;
  438. dto.PreferredMetadataCountryCode = entity.PreferredMetadataCountryCode;
  439. dto.IsInMixedFolder = entity.IsInMixedFolder;
  440. dto.InheritedParentalRatingValue = entity.InheritedParentalRatingValue;
  441. dto.CriticRating = entity.CriticRating;
  442. dto.PresentationUniqueKey = entity.PresentationUniqueKey;
  443. dto.OriginalTitle = entity.OriginalTitle;
  444. dto.Album = entity.Album;
  445. dto.LUFS = entity.LUFS;
  446. dto.NormalizationGain = entity.NormalizationGain;
  447. dto.IsVirtualItem = entity.IsVirtualItem;
  448. dto.ExternalSeriesId = entity.ExternalSeriesId;
  449. dto.Tagline = entity.Tagline;
  450. dto.TotalBitrate = entity.TotalBitrate;
  451. dto.ExternalId = entity.ExternalId;
  452. dto.Size = entity.Size;
  453. dto.Genres = entity.Genres?.Split('|') ?? [];
  454. dto.DateCreated = entity.DateCreated.GetValueOrDefault();
  455. dto.DateModified = entity.DateModified.GetValueOrDefault();
  456. dto.ChannelId = string.IsNullOrWhiteSpace(entity.ChannelId) ? Guid.Empty : (Guid.TryParse(entity.ChannelId, out var channelId) ? channelId : Guid.Empty);
  457. dto.DateLastRefreshed = entity.DateLastRefreshed.GetValueOrDefault();
  458. dto.DateLastSaved = entity.DateLastSaved.GetValueOrDefault();
  459. dto.OwnerId = string.IsNullOrWhiteSpace(entity.OwnerId) ? Guid.Empty : (Guid.TryParse(entity.OwnerId, out var ownerId) ? ownerId : Guid.Empty);
  460. dto.Width = entity.Width.GetValueOrDefault();
  461. dto.Height = entity.Height.GetValueOrDefault();
  462. if (entity.Provider is not null)
  463. {
  464. dto.ProviderIds = entity.Provider.ToDictionary(e => e.ProviderId, e => e.ProviderValue);
  465. }
  466. if (entity.ExtraType is not null)
  467. {
  468. dto.ExtraType = (ExtraType)entity.ExtraType;
  469. }
  470. if (entity.LockedFields is not null)
  471. {
  472. dto.LockedFields = entity.LockedFields?.Select(e => (MetadataField)e.Id).ToArray() ?? [];
  473. }
  474. if (entity.Audio is not null)
  475. {
  476. dto.Audio = (ProgramAudio)entity.Audio;
  477. }
  478. dto.ExtraIds = string.IsNullOrWhiteSpace(entity.ExtraIds) ? [] : entity.ExtraIds.Split('|').Select(e => Guid.Parse(e)).ToArray();
  479. dto.ProductionLocations = entity.ProductionLocations?.Split('|') ?? [];
  480. dto.Studios = entity.Studios?.Split('|') ?? [];
  481. dto.Tags = entity.Tags?.Split('|') ?? [];
  482. if (dto is IHasProgramAttributes hasProgramAttributes)
  483. {
  484. hasProgramAttributes.IsMovie = entity.IsMovie;
  485. hasProgramAttributes.IsSeries = entity.IsSeries;
  486. hasProgramAttributes.EpisodeTitle = entity.EpisodeTitle;
  487. hasProgramAttributes.IsRepeat = entity.IsRepeat;
  488. }
  489. if (dto is LiveTvChannel liveTvChannel)
  490. {
  491. liveTvChannel.ServiceName = entity.ExternalServiceId;
  492. }
  493. if (dto is Trailer trailer)
  494. {
  495. trailer.TrailerTypes = entity.TrailerTypes?.Select(e => (TrailerType)e.Id).ToArray() ?? [];
  496. }
  497. if (dto is Video video)
  498. {
  499. video.PrimaryVersionId = entity.PrimaryVersionId;
  500. }
  501. if (dto is IHasSeries hasSeriesName)
  502. {
  503. hasSeriesName.SeriesName = entity.SeriesName;
  504. hasSeriesName.SeriesId = entity.SeriesId.GetValueOrDefault();
  505. hasSeriesName.SeriesPresentationUniqueKey = entity.SeriesPresentationUniqueKey;
  506. }
  507. if (dto is Episode episode)
  508. {
  509. episode.SeasonName = entity.SeasonName;
  510. episode.SeasonId = entity.SeasonId.GetValueOrDefault();
  511. }
  512. if (dto is IHasArtist hasArtists)
  513. {
  514. hasArtists.Artists = entity.Artists?.Split('|', StringSplitOptions.RemoveEmptyEntries) ?? [];
  515. }
  516. if (dto is IHasAlbumArtist hasAlbumArtists)
  517. {
  518. hasAlbumArtists.AlbumArtists = entity.AlbumArtists?.Split('|', StringSplitOptions.RemoveEmptyEntries) ?? [];
  519. }
  520. if (dto is LiveTvProgram program)
  521. {
  522. program.ShowId = entity.ShowId;
  523. }
  524. if (entity.Images is not null)
  525. {
  526. dto.ImageInfos = entity.Images.Select(e => Map(e, appHost)).ToArray();
  527. }
  528. // dto.Type = entity.Type;
  529. // dto.Data = entity.Data;
  530. // dto.MediaType = Enum.TryParse<MediaType>(entity.MediaType);
  531. if (dto is IHasStartDate hasStartDate)
  532. {
  533. hasStartDate.StartDate = entity.StartDate;
  534. }
  535. // Fields that are present in the DB but are never actually used
  536. // dto.UnratedType = entity.UnratedType;
  537. // dto.TopParentId = entity.TopParentId;
  538. // dto.CleanName = entity.CleanName;
  539. // dto.UserDataKey = entity.UserDataKey;
  540. if (dto is Folder folder)
  541. {
  542. folder.DateLastMediaAdded = entity.DateLastMediaAdded;
  543. }
  544. return dto;
  545. }
  546. /// <summary>
  547. /// Maps a Entity to the DTO.
  548. /// </summary>
  549. /// <param name="dto">The entity.</param>
  550. /// <returns>The dto to map.</returns>
  551. public BaseItemEntity Map(BaseItemDto dto)
  552. {
  553. var dtoType = dto.GetType();
  554. var entity = new BaseItemEntity()
  555. {
  556. Type = dtoType.ToString(),
  557. Id = dto.Id
  558. };
  559. if (TypeRequiresDeserialization(dtoType))
  560. {
  561. entity.Data = JsonSerializer.Serialize(dto, dtoType, JsonDefaults.Options);
  562. }
  563. entity.ParentId = !dto.ParentId.IsEmpty() ? dto.ParentId : null;
  564. entity.Path = GetPathToSave(dto.Path);
  565. entity.EndDate = dto.EndDate.GetValueOrDefault();
  566. entity.CommunityRating = dto.CommunityRating;
  567. entity.CustomRating = dto.CustomRating;
  568. entity.IndexNumber = dto.IndexNumber;
  569. entity.IsLocked = dto.IsLocked;
  570. entity.Name = dto.Name;
  571. entity.OfficialRating = dto.OfficialRating;
  572. entity.Overview = dto.Overview;
  573. entity.ParentIndexNumber = dto.ParentIndexNumber;
  574. entity.PremiereDate = dto.PremiereDate;
  575. entity.ProductionYear = dto.ProductionYear;
  576. entity.SortName = dto.SortName;
  577. entity.ForcedSortName = dto.ForcedSortName;
  578. entity.RunTimeTicks = dto.RunTimeTicks;
  579. entity.PreferredMetadataLanguage = dto.PreferredMetadataLanguage;
  580. entity.PreferredMetadataCountryCode = dto.PreferredMetadataCountryCode;
  581. entity.IsInMixedFolder = dto.IsInMixedFolder;
  582. entity.InheritedParentalRatingValue = dto.InheritedParentalRatingValue;
  583. entity.CriticRating = dto.CriticRating;
  584. entity.PresentationUniqueKey = dto.PresentationUniqueKey;
  585. entity.OriginalTitle = dto.OriginalTitle;
  586. entity.Album = dto.Album;
  587. entity.LUFS = dto.LUFS;
  588. entity.NormalizationGain = dto.NormalizationGain;
  589. entity.IsVirtualItem = dto.IsVirtualItem;
  590. entity.ExternalSeriesId = dto.ExternalSeriesId;
  591. entity.Tagline = dto.Tagline;
  592. entity.TotalBitrate = dto.TotalBitrate;
  593. entity.ExternalId = dto.ExternalId;
  594. entity.Size = dto.Size;
  595. entity.Genres = string.Join('|', dto.Genres);
  596. entity.DateCreated = dto.DateCreated;
  597. entity.DateModified = dto.DateModified;
  598. entity.ChannelId = dto.ChannelId.ToString();
  599. entity.DateLastRefreshed = dto.DateLastRefreshed;
  600. entity.DateLastSaved = dto.DateLastSaved;
  601. entity.OwnerId = dto.OwnerId.ToString();
  602. entity.Width = dto.Width;
  603. entity.Height = dto.Height;
  604. entity.Provider = dto.ProviderIds.Select(e => new BaseItemProvider()
  605. {
  606. Item = entity,
  607. ProviderId = e.Key,
  608. ProviderValue = e.Value
  609. }).ToList();
  610. if (dto.Audio.HasValue)
  611. {
  612. entity.Audio = (ProgramAudioEntity)dto.Audio;
  613. }
  614. if (dto.ExtraType.HasValue)
  615. {
  616. entity.ExtraType = (BaseItemExtraType)dto.ExtraType;
  617. }
  618. entity.ExtraIds = dto.ExtraIds is not null ? string.Join('|', dto.ExtraIds) : null;
  619. entity.ProductionLocations = dto.ProductionLocations is not null ? string.Join('|', dto.ProductionLocations) : null;
  620. entity.Studios = dto.Studios is not null ? string.Join('|', dto.Studios) : null;
  621. entity.Tags = dto.Tags is not null ? string.Join('|', dto.Tags) : null;
  622. entity.LockedFields = dto.LockedFields is not null ? dto.LockedFields
  623. .Select(e => new BaseItemMetadataField()
  624. {
  625. Id = (int)e,
  626. Item = entity,
  627. ItemId = entity.Id
  628. })
  629. .ToArray() : null;
  630. if (dto is IHasProgramAttributes hasProgramAttributes)
  631. {
  632. entity.IsMovie = hasProgramAttributes.IsMovie;
  633. entity.IsSeries = hasProgramAttributes.IsSeries;
  634. entity.EpisodeTitle = hasProgramAttributes.EpisodeTitle;
  635. entity.IsRepeat = hasProgramAttributes.IsRepeat;
  636. }
  637. if (dto is LiveTvChannel liveTvChannel)
  638. {
  639. entity.ExternalServiceId = liveTvChannel.ServiceName;
  640. }
  641. if (dto is Video video)
  642. {
  643. entity.PrimaryVersionId = video.PrimaryVersionId;
  644. }
  645. if (dto is IHasSeries hasSeriesName)
  646. {
  647. entity.SeriesName = hasSeriesName.SeriesName;
  648. entity.SeriesId = hasSeriesName.SeriesId;
  649. entity.SeriesPresentationUniqueKey = hasSeriesName.SeriesPresentationUniqueKey;
  650. }
  651. if (dto is Episode episode)
  652. {
  653. entity.SeasonName = episode.SeasonName;
  654. entity.SeasonId = episode.SeasonId;
  655. }
  656. if (dto is IHasArtist hasArtists)
  657. {
  658. entity.Artists = hasArtists.Artists is not null ? string.Join('|', hasArtists.Artists) : null;
  659. }
  660. if (dto is IHasAlbumArtist hasAlbumArtists)
  661. {
  662. entity.AlbumArtists = hasAlbumArtists.AlbumArtists is not null ? string.Join('|', hasAlbumArtists.AlbumArtists) : null;
  663. }
  664. if (dto is LiveTvProgram program)
  665. {
  666. entity.ShowId = program.ShowId;
  667. }
  668. if (dto.ImageInfos is not null)
  669. {
  670. entity.Images = dto.ImageInfos.Select(f => Map(dto.Id, f)).ToArray();
  671. }
  672. if (dto is Trailer trailer)
  673. {
  674. entity.TrailerTypes = trailer.TrailerTypes?.Select(e => new BaseItemTrailerType()
  675. {
  676. Id = (int)e,
  677. Item = entity,
  678. ItemId = entity.Id
  679. }).ToArray() ?? [];
  680. }
  681. // dto.Type = entity.Type;
  682. // dto.Data = entity.Data;
  683. entity.MediaType = dto.MediaType.ToString();
  684. if (dto is IHasStartDate hasStartDate)
  685. {
  686. entity.StartDate = hasStartDate.StartDate;
  687. }
  688. // Fields that are present in the DB but are never actually used
  689. // dto.UnratedType = entity.UnratedType;
  690. // dto.TopParentId = entity.TopParentId;
  691. // dto.CleanName = entity.CleanName;
  692. // dto.UserDataKey = entity.UserDataKey;
  693. if (dto is Folder folder)
  694. {
  695. entity.DateLastMediaAdded = folder.DateLastMediaAdded;
  696. entity.IsFolder = folder.IsFolder;
  697. }
  698. return entity;
  699. }
  700. private IReadOnlyList<string> GetItemValueNames(ItemValueType[] itemValueTypes, IReadOnlyList<string> withItemTypes, IReadOnlyList<string> excludeItemTypes)
  701. {
  702. using var context = dbProvider.CreateDbContext();
  703. var query = context.ItemValuesMap
  704. .AsNoTracking()
  705. .Where(e => itemValueTypes.Any(w => (ItemValueType)w == e.ItemValue.Type));
  706. if (withItemTypes.Count > 0)
  707. {
  708. query = query.Where(e => withItemTypes.Contains(e.Item.Type));
  709. }
  710. if (excludeItemTypes.Count > 0)
  711. {
  712. query = query.Where(e => !excludeItemTypes.Contains(e.Item.Type));
  713. }
  714. // query = query.DistinctBy(e => e.CleanValue);
  715. return query.Select(e => e.ItemValue.CleanValue).ToImmutableArray();
  716. }
  717. private static bool TypeRequiresDeserialization(Type type)
  718. {
  719. return type.GetCustomAttribute<RequiresSourceSerialisationAttribute>() == null;
  720. }
  721. private BaseItemDto DeserialiseBaseItem(BaseItemEntity baseItemEntity, bool skipDeserialization = false)
  722. {
  723. ArgumentNullException.ThrowIfNull(baseItemEntity, nameof(baseItemEntity));
  724. if (serverConfigurationManager?.Configuration is null)
  725. {
  726. throw new InvalidOperationException("Server Configuration manager or configuration is null");
  727. }
  728. var typeToSerialise = GetType(baseItemEntity.Type);
  729. return BaseItemRepository.DeserialiseBaseItem(
  730. baseItemEntity,
  731. logger,
  732. appHost,
  733. skipDeserialization || (serverConfigurationManager.Configuration.SkipDeserializationForBasicTypes && (typeToSerialise == typeof(Channel) || typeToSerialise == typeof(UserRootFolder))));
  734. }
  735. /// <summary>
  736. /// Deserialises a BaseItemEntity and sets all properties.
  737. /// </summary>
  738. /// <param name="baseItemEntity">The DB entity.</param>
  739. /// <param name="logger">Logger.</param>
  740. /// <param name="appHost">The application server Host.</param>
  741. /// <param name="skipDeserialization">If only mapping should be processed.</param>
  742. /// <returns>A mapped BaseItem.</returns>
  743. /// <exception cref="InvalidOperationException">Will be thrown if an invalid serialisation is requested.</exception>
  744. public static BaseItemDto DeserialiseBaseItem(BaseItemEntity baseItemEntity, ILogger logger, IServerApplicationHost? appHost, bool skipDeserialization = false)
  745. {
  746. var type = GetType(baseItemEntity.Type) ?? throw new InvalidOperationException("Cannot deserialise unkown type.");
  747. BaseItemDto? dto = null;
  748. if (TypeRequiresDeserialization(type) && baseItemEntity.Data is not null && !skipDeserialization)
  749. {
  750. try
  751. {
  752. using var dataAsStream = new MemoryStream(Encoding.UTF8.GetBytes(baseItemEntity.Data!));
  753. dto = JsonSerializer.Deserialize(dataAsStream, type, JsonDefaults.Options) as BaseItemDto;
  754. }
  755. catch (JsonException ex)
  756. {
  757. logger.LogError(ex, "Error deserializing item with JSON: {Data}", baseItemEntity.Data);
  758. }
  759. }
  760. if (dto is null)
  761. {
  762. dto = Activator.CreateInstance(type) as BaseItemDto ?? throw new InvalidOperationException("Cannot deserialise unkown type.");
  763. }
  764. return Map(baseItemEntity, dto, appHost);
  765. }
  766. private QueryResult<(BaseItemDto Item, ItemCounts ItemCounts)> GetItemValues(InternalItemsQuery filter, ItemValueType[] itemValueTypes, string returnType)
  767. {
  768. ArgumentNullException.ThrowIfNull(filter);
  769. if (!filter.Limit.HasValue)
  770. {
  771. filter.EnableTotalRecordCount = false;
  772. }
  773. using var context = dbProvider.CreateDbContext();
  774. var innerQuery = new InternalItemsQuery(filter.User)
  775. {
  776. ExcludeItemTypes = filter.ExcludeItemTypes,
  777. IncludeItemTypes = filter.IncludeItemTypes,
  778. MediaTypes = filter.MediaTypes,
  779. AncestorIds = filter.AncestorIds,
  780. ItemIds = filter.ItemIds,
  781. TopParentIds = filter.TopParentIds,
  782. ParentId = filter.ParentId,
  783. IsAiring = filter.IsAiring,
  784. IsMovie = filter.IsMovie,
  785. IsSports = filter.IsSports,
  786. IsKids = filter.IsKids,
  787. IsNews = filter.IsNews,
  788. IsSeries = filter.IsSeries
  789. };
  790. var query = TranslateQuery(context.BaseItems.AsNoTracking(), context, innerQuery);
  791. query = query.Where(e => e.Type == returnType && e.ItemValues!.Any(f => e.CleanName == f.ItemValue.CleanValue && itemValueTypes.Any(w => (ItemValueType)w == f.ItemValue.Type)));
  792. if (filter.OrderBy.Count != 0
  793. || !string.IsNullOrEmpty(filter.SearchTerm))
  794. {
  795. query = ApplyOrder(query, filter);
  796. }
  797. else
  798. {
  799. query = query.OrderBy(e => e.SortName);
  800. }
  801. if (filter.Limit.HasValue || filter.StartIndex.HasValue)
  802. {
  803. var offset = filter.StartIndex ?? 0;
  804. if (offset > 0)
  805. {
  806. query = query.Skip(offset);
  807. }
  808. if (filter.Limit.HasValue)
  809. {
  810. query = query.Take(filter.Limit.Value);
  811. }
  812. }
  813. var result = new QueryResult<(BaseItemDto, ItemCounts)>();
  814. if (filter.EnableTotalRecordCount)
  815. {
  816. result.TotalRecordCount = query.GroupBy(e => e.PresentationUniqueKey).Select(e => e.First()).Count();
  817. }
  818. var seriesTypeName = itemTypeLookup.BaseItemKindNames[BaseItemKind.Series];
  819. var movieTypeName = itemTypeLookup.BaseItemKindNames[BaseItemKind.Movie];
  820. var episodeTypeName = itemTypeLookup.BaseItemKindNames[BaseItemKind.Episode];
  821. var musicAlbumTypeName = itemTypeLookup.BaseItemKindNames[BaseItemKind.MusicAlbum];
  822. var musicArtistTypeName = itemTypeLookup.BaseItemKindNames[BaseItemKind.MusicArtist];
  823. var audioTypeName = itemTypeLookup.BaseItemKindNames[BaseItemKind.Audio];
  824. var trailerTypeName = itemTypeLookup.BaseItemKindNames[BaseItemKind.Trailer];
  825. var resultQuery = query.Select(e => new
  826. {
  827. item = e,
  828. // TODO: This is bad refactor!
  829. itemCount = new ItemCounts()
  830. {
  831. SeriesCount = e.ItemValues!.Count(f => f.Item.Type == seriesTypeName),
  832. EpisodeCount = e.ItemValues!.Count(f => f.Item.Type == episodeTypeName),
  833. MovieCount = e.ItemValues!.Count(f => f.Item.Type == movieTypeName),
  834. AlbumCount = e.ItemValues!.Count(f => f.Item.Type == musicAlbumTypeName),
  835. ArtistCount = e.ItemValues!.Count(f => f.Item.Type == musicArtistTypeName),
  836. SongCount = e.ItemValues!.Count(f => f.Item.Type == audioTypeName),
  837. TrailerCount = e.ItemValues!.Count(f => f.Item.Type == trailerTypeName),
  838. }
  839. });
  840. result.StartIndex = filter.StartIndex ?? 0;
  841. result.Items = resultQuery.ToImmutableArray().Where(e => e is not null).Select(e =>
  842. {
  843. return (DeserialiseBaseItem(e.item, filter.SkipDeserialization), e.itemCount);
  844. }).ToImmutableArray();
  845. return result;
  846. }
  847. private static void PrepareFilterQuery(InternalItemsQuery query)
  848. {
  849. if (query.Limit.HasValue && query.EnableGroupByMetadataKey)
  850. {
  851. query.Limit = query.Limit.Value + 4;
  852. }
  853. if (query.IsResumable ?? false)
  854. {
  855. query.IsVirtualItem = false;
  856. }
  857. }
  858. private string GetCleanValue(string value)
  859. {
  860. if (string.IsNullOrWhiteSpace(value))
  861. {
  862. return value;
  863. }
  864. return value.RemoveDiacritics().ToLowerInvariant();
  865. }
  866. private List<(int MagicNumber, string Value)> GetItemValuesToSave(BaseItemDto item, List<string> inheritedTags)
  867. {
  868. var list = new List<(int, string)>();
  869. if (item is IHasArtist hasArtist)
  870. {
  871. list.AddRange(hasArtist.Artists.Select(i => (0, i)));
  872. }
  873. if (item is IHasAlbumArtist hasAlbumArtist)
  874. {
  875. list.AddRange(hasAlbumArtist.AlbumArtists.Select(i => (1, i)));
  876. }
  877. list.AddRange(item.Genres.Select(i => (2, i)));
  878. list.AddRange(item.Studios.Select(i => (3, i)));
  879. list.AddRange(item.Tags.Select(i => (4, i)));
  880. // keywords was 5
  881. list.AddRange(inheritedTags.Select(i => (6, i)));
  882. // Remove all invalid values.
  883. list.RemoveAll(i => string.IsNullOrWhiteSpace(i.Item2));
  884. return list;
  885. }
  886. private static BaseItemImageInfo Map(Guid baseItemId, ItemImageInfo e)
  887. {
  888. return new BaseItemImageInfo()
  889. {
  890. ItemId = baseItemId,
  891. Id = Guid.NewGuid(),
  892. Path = e.Path,
  893. Blurhash = e.BlurHash != null ? Encoding.UTF8.GetBytes(e.BlurHash) : null,
  894. DateModified = e.DateModified,
  895. Height = e.Height,
  896. Width = e.Width,
  897. ImageType = (ImageInfoImageType)e.Type,
  898. Item = null!
  899. };
  900. }
  901. private static ItemImageInfo Map(BaseItemImageInfo e, IServerApplicationHost? appHost)
  902. {
  903. return new ItemImageInfo()
  904. {
  905. Path = appHost?.ExpandVirtualPath(e.Path) ?? e.Path,
  906. BlurHash = e.Blurhash != null ? Encoding.UTF8.GetString(e.Blurhash) : null,
  907. DateModified = e.DateModified,
  908. Height = e.Height,
  909. Width = e.Width,
  910. Type = (ImageType)e.ImageType
  911. };
  912. }
  913. private string? GetPathToSave(string path)
  914. {
  915. if (path is null)
  916. {
  917. return null;
  918. }
  919. return appHost.ReverseVirtualPath(path);
  920. }
  921. private List<string> GetItemByNameTypesInQuery(InternalItemsQuery query)
  922. {
  923. var list = new List<string>();
  924. if (IsTypeInQuery(BaseItemKind.Person, query))
  925. {
  926. list.Add(itemTypeLookup.BaseItemKindNames[BaseItemKind.Person]!);
  927. }
  928. if (IsTypeInQuery(BaseItemKind.Genre, query))
  929. {
  930. list.Add(itemTypeLookup.BaseItemKindNames[BaseItemKind.Genre]!);
  931. }
  932. if (IsTypeInQuery(BaseItemKind.MusicGenre, query))
  933. {
  934. list.Add(itemTypeLookup.BaseItemKindNames[BaseItemKind.MusicGenre]!);
  935. }
  936. if (IsTypeInQuery(BaseItemKind.MusicArtist, query))
  937. {
  938. list.Add(itemTypeLookup.BaseItemKindNames[BaseItemKind.MusicArtist]!);
  939. }
  940. if (IsTypeInQuery(BaseItemKind.Studio, query))
  941. {
  942. list.Add(itemTypeLookup.BaseItemKindNames[BaseItemKind.Studio]!);
  943. }
  944. return list;
  945. }
  946. private bool IsTypeInQuery(BaseItemKind type, InternalItemsQuery query)
  947. {
  948. if (query.ExcludeItemTypes.Contains(type))
  949. {
  950. return false;
  951. }
  952. return query.IncludeItemTypes.Length == 0 || query.IncludeItemTypes.Contains(type);
  953. }
  954. private Expression<Func<BaseItemEntity, object>> MapOrderByField(ItemSortBy sortBy, InternalItemsQuery query)
  955. {
  956. #pragma warning disable CS8603 // Possible null reference return.
  957. return sortBy switch
  958. {
  959. ItemSortBy.AirTime => e => e.SortName, // TODO
  960. ItemSortBy.Runtime => e => e.RunTimeTicks,
  961. ItemSortBy.Random => e => EF.Functions.Random(),
  962. ItemSortBy.DatePlayed => e => e.UserData!.FirstOrDefault(f => f.UserId == query.User!.Id)!.LastPlayedDate,
  963. ItemSortBy.PlayCount => e => e.UserData!.FirstOrDefault(f => f.UserId == query.User!.Id)!.PlayCount,
  964. ItemSortBy.IsFavoriteOrLiked => e => e.UserData!.FirstOrDefault(f => f.UserId == query.User!.Id)!.IsFavorite,
  965. ItemSortBy.IsFolder => e => e.IsFolder,
  966. ItemSortBy.IsPlayed => e => e.UserData!.FirstOrDefault(f => f.UserId == query.User!.Id)!.Played,
  967. ItemSortBy.IsUnplayed => e => !e.UserData!.FirstOrDefault(f => f.UserId == query.User!.Id)!.Played,
  968. ItemSortBy.DateLastContentAdded => e => e.DateLastMediaAdded,
  969. ItemSortBy.Artist => e => e.ItemValues!.Where(f => f.ItemValue.Type == ItemValueType.Artist).Select(f => f.ItemValue.CleanValue),
  970. ItemSortBy.AlbumArtist => e => e.ItemValues!.Where(f => f.ItemValue.Type == ItemValueType.AlbumArtist).Select(f => f.ItemValue.CleanValue),
  971. ItemSortBy.Studio => e => e.ItemValues!.Where(f => f.ItemValue.Type == ItemValueType.Studios).Select(f => f.ItemValue.CleanValue),
  972. ItemSortBy.OfficialRating => e => e.InheritedParentalRatingValue,
  973. // ItemSortBy.SeriesDatePlayed => "(Select MAX(LastPlayedDate) from TypedBaseItems B" + GetJoinUserDataText(query) + " where Played=1 and B.SeriesPresentationUniqueKey=A.PresentationUniqueKey)",
  974. ItemSortBy.SeriesSortName => e => e.SeriesName,
  975. // ItemSortBy.AiredEpisodeOrder => "AiredEpisodeOrder",
  976. ItemSortBy.Album => e => e.Album,
  977. ItemSortBy.DateCreated => e => e.DateCreated,
  978. ItemSortBy.PremiereDate => e => e.PremiereDate,
  979. ItemSortBy.StartDate => e => e.StartDate,
  980. ItemSortBy.Name => e => e.Name,
  981. ItemSortBy.CommunityRating => e => e.CommunityRating,
  982. ItemSortBy.ProductionYear => e => e.ProductionYear,
  983. ItemSortBy.CriticRating => e => e.CriticRating,
  984. ItemSortBy.VideoBitRate => e => e.TotalBitrate,
  985. ItemSortBy.ParentIndexNumber => e => e.ParentIndexNumber,
  986. ItemSortBy.IndexNumber => e => e.IndexNumber,
  987. _ => e => e.SortName
  988. };
  989. #pragma warning restore CS8603 // Possible null reference return.
  990. }
  991. private bool EnableGroupByPresentationUniqueKey(InternalItemsQuery query)
  992. {
  993. if (!query.GroupByPresentationUniqueKey)
  994. {
  995. return false;
  996. }
  997. if (query.GroupBySeriesPresentationUniqueKey)
  998. {
  999. return false;
  1000. }
  1001. if (!string.IsNullOrWhiteSpace(query.PresentationUniqueKey))
  1002. {
  1003. return false;
  1004. }
  1005. if (query.User is null)
  1006. {
  1007. return false;
  1008. }
  1009. if (query.IncludeItemTypes.Length == 0)
  1010. {
  1011. return true;
  1012. }
  1013. return query.IncludeItemTypes.Contains(BaseItemKind.Episode)
  1014. || query.IncludeItemTypes.Contains(BaseItemKind.Video)
  1015. || query.IncludeItemTypes.Contains(BaseItemKind.Movie)
  1016. || query.IncludeItemTypes.Contains(BaseItemKind.MusicVideo)
  1017. || query.IncludeItemTypes.Contains(BaseItemKind.Series)
  1018. || query.IncludeItemTypes.Contains(BaseItemKind.Season);
  1019. }
  1020. private IQueryable<BaseItemEntity> ApplyOrder(IQueryable<BaseItemEntity> query, InternalItemsQuery filter)
  1021. {
  1022. var orderBy = filter.OrderBy;
  1023. bool hasSearch = !string.IsNullOrEmpty(filter.SearchTerm);
  1024. if (hasSearch)
  1025. {
  1026. List<(ItemSortBy, SortOrder)> prepend = new List<(ItemSortBy, SortOrder)>(4);
  1027. if (hasSearch)
  1028. {
  1029. prepend.Add((ItemSortBy.SortName, SortOrder.Ascending));
  1030. }
  1031. orderBy = filter.OrderBy = [.. prepend, .. orderBy];
  1032. }
  1033. else if (orderBy.Count == 0)
  1034. {
  1035. return query;
  1036. }
  1037. IOrderedQueryable<BaseItemEntity>? orderedQuery = null;
  1038. var firstOrdering = orderBy.FirstOrDefault();
  1039. if (firstOrdering != default)
  1040. {
  1041. var expression = MapOrderByField(firstOrdering.OrderBy, filter);
  1042. if (firstOrdering.SortOrder == SortOrder.Ascending)
  1043. {
  1044. orderedQuery = query.OrderBy(expression);
  1045. }
  1046. else
  1047. {
  1048. orderedQuery = query.OrderByDescending(expression);
  1049. }
  1050. if (firstOrdering.OrderBy is ItemSortBy.Default or ItemSortBy.SortName)
  1051. {
  1052. if (firstOrdering.SortOrder is SortOrder.Ascending)
  1053. {
  1054. orderedQuery = orderedQuery.ThenBy(e => e.Name);
  1055. }
  1056. else
  1057. {
  1058. orderedQuery = orderedQuery.ThenByDescending(e => e.Name);
  1059. }
  1060. }
  1061. }
  1062. foreach (var item in orderBy.Skip(1))
  1063. {
  1064. var expression = MapOrderByField(item.OrderBy, filter);
  1065. if (item.SortOrder == SortOrder.Ascending)
  1066. {
  1067. orderedQuery = orderedQuery!.ThenBy(expression);
  1068. }
  1069. else
  1070. {
  1071. orderedQuery = orderedQuery!.ThenByDescending(expression);
  1072. }
  1073. }
  1074. return orderedQuery ?? query;
  1075. }
  1076. private IQueryable<BaseItemEntity> TranslateQuery(
  1077. IQueryable<BaseItemEntity> baseQuery,
  1078. JellyfinDbContext context,
  1079. InternalItemsQuery filter)
  1080. {
  1081. var minWidth = filter.MinWidth;
  1082. var maxWidth = filter.MaxWidth;
  1083. var now = DateTime.UtcNow;
  1084. if (filter.IsHD.HasValue)
  1085. {
  1086. const int Threshold = 1200;
  1087. if (filter.IsHD.Value)
  1088. {
  1089. minWidth = Threshold;
  1090. }
  1091. else
  1092. {
  1093. maxWidth = Threshold - 1;
  1094. }
  1095. }
  1096. if (filter.Is4K.HasValue)
  1097. {
  1098. const int Threshold = 3800;
  1099. if (filter.Is4K.Value)
  1100. {
  1101. minWidth = Threshold;
  1102. }
  1103. else
  1104. {
  1105. maxWidth = Threshold - 1;
  1106. }
  1107. }
  1108. if (minWidth.HasValue)
  1109. {
  1110. baseQuery = baseQuery.Where(e => e.Width >= minWidth);
  1111. }
  1112. if (filter.MinHeight.HasValue)
  1113. {
  1114. baseQuery = baseQuery.Where(e => e.Height >= filter.MinHeight);
  1115. }
  1116. if (maxWidth.HasValue)
  1117. {
  1118. baseQuery = baseQuery.Where(e => e.Width >= maxWidth);
  1119. }
  1120. if (filter.MaxHeight.HasValue)
  1121. {
  1122. baseQuery = baseQuery.Where(e => e.Height <= filter.MaxHeight);
  1123. }
  1124. if (filter.IsLocked.HasValue)
  1125. {
  1126. baseQuery = baseQuery.Where(e => e.IsLocked == filter.IsLocked);
  1127. }
  1128. var tags = filter.Tags.ToList();
  1129. var excludeTags = filter.ExcludeTags.ToList();
  1130. if (filter.IsMovie == true)
  1131. {
  1132. if (filter.IncludeItemTypes.Length == 0
  1133. || filter.IncludeItemTypes.Contains(BaseItemKind.Movie)
  1134. || filter.IncludeItemTypes.Contains(BaseItemKind.Trailer))
  1135. {
  1136. baseQuery = baseQuery.Where(e => e.IsMovie);
  1137. }
  1138. }
  1139. else if (filter.IsMovie.HasValue)
  1140. {
  1141. baseQuery = baseQuery.Where(e => e.IsMovie == filter.IsMovie);
  1142. }
  1143. if (filter.IsSeries.HasValue)
  1144. {
  1145. baseQuery = baseQuery.Where(e => e.IsSeries == filter.IsSeries);
  1146. }
  1147. if (filter.IsSports.HasValue)
  1148. {
  1149. if (filter.IsSports.Value)
  1150. {
  1151. tags.Add("Sports");
  1152. }
  1153. else
  1154. {
  1155. excludeTags.Add("Sports");
  1156. }
  1157. }
  1158. if (filter.IsNews.HasValue)
  1159. {
  1160. if (filter.IsNews.Value)
  1161. {
  1162. tags.Add("News");
  1163. }
  1164. else
  1165. {
  1166. excludeTags.Add("News");
  1167. }
  1168. }
  1169. if (filter.IsKids.HasValue)
  1170. {
  1171. if (filter.IsKids.Value)
  1172. {
  1173. tags.Add("Kids");
  1174. }
  1175. else
  1176. {
  1177. excludeTags.Add("Kids");
  1178. }
  1179. }
  1180. if (!string.IsNullOrEmpty(filter.SearchTerm))
  1181. {
  1182. baseQuery = baseQuery.Where(e => e.CleanName!.Contains(filter.SearchTerm) || (e.OriginalTitle != null && e.OriginalTitle.Contains(filter.SearchTerm)));
  1183. }
  1184. if (filter.IsFolder.HasValue)
  1185. {
  1186. baseQuery = baseQuery.Where(e => e.IsFolder == filter.IsFolder);
  1187. }
  1188. var includeTypes = filter.IncludeItemTypes;
  1189. // Only specify excluded types if no included types are specified
  1190. if (filter.IncludeItemTypes.Length == 0)
  1191. {
  1192. var excludeTypes = filter.ExcludeItemTypes;
  1193. if (excludeTypes.Length == 1)
  1194. {
  1195. if (itemTypeLookup.BaseItemKindNames.TryGetValue(excludeTypes[0], out var excludeTypeName))
  1196. {
  1197. baseQuery = baseQuery.Where(e => e.Type != excludeTypeName);
  1198. }
  1199. }
  1200. else if (excludeTypes.Length > 1)
  1201. {
  1202. var excludeTypeName = new List<string>();
  1203. foreach (var excludeType in excludeTypes)
  1204. {
  1205. if (itemTypeLookup.BaseItemKindNames.TryGetValue(excludeType, out var baseItemKindName))
  1206. {
  1207. excludeTypeName.Add(baseItemKindName!);
  1208. }
  1209. }
  1210. baseQuery = baseQuery.Where(e => !excludeTypeName.Contains(e.Type));
  1211. }
  1212. }
  1213. else if (includeTypes.Length == 1)
  1214. {
  1215. if (itemTypeLookup.BaseItemKindNames.TryGetValue(includeTypes[0], out var includeTypeName))
  1216. {
  1217. baseQuery = baseQuery.Where(e => e.Type == includeTypeName);
  1218. }
  1219. }
  1220. else if (includeTypes.Length > 1)
  1221. {
  1222. var includeTypeName = new List<string>();
  1223. foreach (var includeType in includeTypes)
  1224. {
  1225. if (itemTypeLookup.BaseItemKindNames.TryGetValue(includeType, out var baseItemKindName))
  1226. {
  1227. includeTypeName.Add(baseItemKindName!);
  1228. }
  1229. }
  1230. baseQuery = baseQuery.Where(e => includeTypeName.Contains(e.Type));
  1231. }
  1232. if (filter.ChannelIds.Count > 0)
  1233. {
  1234. var channelIds = filter.ChannelIds.Select(e => e.ToString("N", CultureInfo.InvariantCulture)).ToArray();
  1235. baseQuery = baseQuery.Where(e => channelIds.Contains(e.ChannelId));
  1236. }
  1237. if (!filter.ParentId.IsEmpty())
  1238. {
  1239. baseQuery = baseQuery.Where(e => e.ParentId!.Value == filter.ParentId);
  1240. }
  1241. if (!string.IsNullOrWhiteSpace(filter.Path))
  1242. {
  1243. baseQuery = baseQuery.Where(e => e.Path == filter.Path);
  1244. }
  1245. if (!string.IsNullOrWhiteSpace(filter.PresentationUniqueKey))
  1246. {
  1247. baseQuery = baseQuery.Where(e => e.PresentationUniqueKey == filter.PresentationUniqueKey);
  1248. }
  1249. if (filter.MinCommunityRating.HasValue)
  1250. {
  1251. baseQuery = baseQuery.Where(e => e.CommunityRating >= filter.MinCommunityRating);
  1252. }
  1253. if (filter.MinIndexNumber.HasValue)
  1254. {
  1255. baseQuery = baseQuery.Where(e => e.IndexNumber >= filter.MinIndexNumber);
  1256. }
  1257. if (filter.MinParentAndIndexNumber.HasValue)
  1258. {
  1259. baseQuery = baseQuery
  1260. .Where(e => (e.ParentIndexNumber == filter.MinParentAndIndexNumber.Value.ParentIndexNumber && e.IndexNumber >= filter.MinParentAndIndexNumber.Value.IndexNumber) || e.ParentIndexNumber > filter.MinParentAndIndexNumber.Value.ParentIndexNumber);
  1261. }
  1262. if (filter.MinDateCreated.HasValue)
  1263. {
  1264. baseQuery = baseQuery.Where(e => e.DateCreated >= filter.MinDateCreated);
  1265. }
  1266. if (filter.MinDateLastSaved.HasValue)
  1267. {
  1268. baseQuery = baseQuery.Where(e => e.DateLastSaved != null && e.DateLastSaved >= filter.MinDateLastSaved.Value);
  1269. }
  1270. if (filter.MinDateLastSavedForUser.HasValue)
  1271. {
  1272. baseQuery = baseQuery.Where(e => e.DateLastSaved != null && e.DateLastSaved >= filter.MinDateLastSavedForUser.Value);
  1273. }
  1274. if (filter.IndexNumber.HasValue)
  1275. {
  1276. baseQuery = baseQuery.Where(e => e.IndexNumber == filter.IndexNumber.Value);
  1277. }
  1278. if (filter.ParentIndexNumber.HasValue)
  1279. {
  1280. baseQuery = baseQuery.Where(e => e.ParentIndexNumber == filter.ParentIndexNumber.Value);
  1281. }
  1282. if (filter.ParentIndexNumberNotEquals.HasValue)
  1283. {
  1284. baseQuery = baseQuery.Where(e => e.ParentIndexNumber != filter.ParentIndexNumberNotEquals.Value || e.ParentIndexNumber == null);
  1285. }
  1286. var minEndDate = filter.MinEndDate;
  1287. var maxEndDate = filter.MaxEndDate;
  1288. if (filter.HasAired.HasValue)
  1289. {
  1290. if (filter.HasAired.Value)
  1291. {
  1292. maxEndDate = DateTime.UtcNow;
  1293. }
  1294. else
  1295. {
  1296. minEndDate = DateTime.UtcNow;
  1297. }
  1298. }
  1299. if (minEndDate.HasValue)
  1300. {
  1301. baseQuery = baseQuery.Where(e => e.EndDate >= minEndDate);
  1302. }
  1303. if (maxEndDate.HasValue)
  1304. {
  1305. baseQuery = baseQuery.Where(e => e.EndDate <= maxEndDate);
  1306. }
  1307. if (filter.MinStartDate.HasValue)
  1308. {
  1309. baseQuery = baseQuery.Where(e => e.StartDate >= filter.MinStartDate.Value);
  1310. }
  1311. if (filter.MaxStartDate.HasValue)
  1312. {
  1313. baseQuery = baseQuery.Where(e => e.StartDate <= filter.MaxStartDate.Value);
  1314. }
  1315. if (filter.MinPremiereDate.HasValue)
  1316. {
  1317. baseQuery = baseQuery.Where(e => e.PremiereDate <= filter.MinPremiereDate.Value);
  1318. }
  1319. if (filter.MaxPremiereDate.HasValue)
  1320. {
  1321. baseQuery = baseQuery.Where(e => e.PremiereDate <= filter.MaxPremiereDate.Value);
  1322. }
  1323. if (filter.TrailerTypes.Length > 0)
  1324. {
  1325. var trailerTypes = filter.TrailerTypes.Select(e => (int)e).ToArray();
  1326. baseQuery = baseQuery.Where(e => trailerTypes.Any(f => e.TrailerTypes!.Any(w => w.Id == f)));
  1327. }
  1328. if (filter.IsAiring.HasValue)
  1329. {
  1330. if (filter.IsAiring.Value)
  1331. {
  1332. baseQuery = baseQuery.Where(e => e.StartDate <= now && e.EndDate >= now);
  1333. }
  1334. else
  1335. {
  1336. baseQuery = baseQuery.Where(e => e.StartDate > now && e.EndDate < now);
  1337. }
  1338. }
  1339. if (filter.PersonIds.Length > 0)
  1340. {
  1341. baseQuery = baseQuery
  1342. .Where(e =>
  1343. context.PeopleBaseItemMap.Where(w => context.BaseItems.Where(r => filter.PersonIds.Contains(r.Id)).Any(f => f.Name == w.People.Name))
  1344. .Any(f => f.ItemId == e.Id));
  1345. }
  1346. if (!string.IsNullOrWhiteSpace(filter.Person))
  1347. {
  1348. baseQuery = baseQuery.Where(e => e.Peoples!.Any(f => f.People.Name == filter.Person));
  1349. }
  1350. if (!string.IsNullOrWhiteSpace(filter.MinSortName))
  1351. {
  1352. // this does not makes sense.
  1353. // baseQuery = baseQuery.Where(e => e.SortName >= query.MinSortName);
  1354. // whereClauses.Add("SortName>=@MinSortName");
  1355. // statement?.TryBind("@MinSortName", query.MinSortName);
  1356. }
  1357. if (!string.IsNullOrWhiteSpace(filter.ExternalSeriesId))
  1358. {
  1359. baseQuery = baseQuery.Where(e => e.ExternalSeriesId == filter.ExternalSeriesId);
  1360. }
  1361. if (!string.IsNullOrWhiteSpace(filter.ExternalId))
  1362. {
  1363. baseQuery = baseQuery.Where(e => e.ExternalId == filter.ExternalId);
  1364. }
  1365. if (!string.IsNullOrWhiteSpace(filter.Name))
  1366. {
  1367. var cleanName = GetCleanValue(filter.Name);
  1368. baseQuery = baseQuery.Where(e => e.CleanName == cleanName);
  1369. }
  1370. // These are the same, for now
  1371. var nameContains = filter.NameContains;
  1372. if (!string.IsNullOrWhiteSpace(nameContains))
  1373. {
  1374. baseQuery = baseQuery.Where(e =>
  1375. e.CleanName == filter.NameContains
  1376. || e.OriginalTitle!.Contains(filter.NameContains!));
  1377. }
  1378. if (!string.IsNullOrWhiteSpace(filter.NameStartsWith))
  1379. {
  1380. baseQuery = baseQuery.Where(e => e.SortName!.StartsWith(filter.NameStartsWith) || e.Name!.StartsWith(filter.NameStartsWith));
  1381. }
  1382. if (!string.IsNullOrWhiteSpace(filter.NameStartsWithOrGreater))
  1383. {
  1384. // i hate this
  1385. baseQuery = baseQuery.Where(e => e.SortName!.FirstOrDefault() > filter.NameStartsWithOrGreater[0] || e.Name!.FirstOrDefault() > filter.NameStartsWithOrGreater[0]);
  1386. }
  1387. if (!string.IsNullOrWhiteSpace(filter.NameLessThan))
  1388. {
  1389. // i hate this
  1390. baseQuery = baseQuery.Where(e => e.SortName!.FirstOrDefault() < filter.NameLessThan[0] || e.Name!.FirstOrDefault() < filter.NameLessThan[0]);
  1391. }
  1392. if (filter.ImageTypes.Length > 0)
  1393. {
  1394. var imgTypes = filter.ImageTypes.Select(e => (ImageInfoImageType)e).ToArray();
  1395. baseQuery = baseQuery.Where(e => imgTypes.Any(f => e.Images!.Any(w => w.ImageType == f)));
  1396. }
  1397. if (filter.IsLiked.HasValue)
  1398. {
  1399. baseQuery = baseQuery
  1400. .Where(e => e.UserData!.FirstOrDefault(f => f.UserId == filter.User!.Id)!.Rating >= UserItemData.MinLikeValue);
  1401. }
  1402. if (filter.IsFavoriteOrLiked.HasValue)
  1403. {
  1404. baseQuery = baseQuery
  1405. .Where(e => e.UserData!.FirstOrDefault(f => f.UserId == filter.User!.Id)!.IsFavorite == filter.IsFavoriteOrLiked);
  1406. }
  1407. if (filter.IsFavorite.HasValue)
  1408. {
  1409. baseQuery = baseQuery
  1410. .Where(e => e.UserData!.FirstOrDefault(f => f.UserId == filter.User!.Id)!.IsFavorite == filter.IsFavorite);
  1411. }
  1412. if (filter.IsPlayed.HasValue)
  1413. {
  1414. // We should probably figure this out for all folders, but for right now, this is the only place where we need it
  1415. if (filter.IncludeItemTypes.Length == 1 && filter.IncludeItemTypes[0] == BaseItemKind.Series)
  1416. {
  1417. baseQuery = baseQuery.Where(e => context.BaseItems
  1418. .Where(e => e.IsFolder == false && e.IsVirtualItem == false)
  1419. .Where(f => f.UserData!.FirstOrDefault(e => e.UserId == filter.User!.Id && e.Played)!.Played)
  1420. .Any(f => f.SeriesPresentationUniqueKey == e.PresentationUniqueKey) == filter.IsPlayed);
  1421. }
  1422. else
  1423. {
  1424. baseQuery = baseQuery
  1425. .Select(e => new
  1426. {
  1427. IsPlayed = e.UserData!.Where(f => f.UserId == filter.User!.Id).Select(f => (bool?)f.Played).FirstOrDefault() ?? false,
  1428. Item = e
  1429. })
  1430. .Where(e => e.IsPlayed == filter.IsPlayed)
  1431. .Select(f => f.Item);
  1432. }
  1433. }
  1434. if (filter.IsResumable.HasValue)
  1435. {
  1436. if (filter.IsResumable.Value)
  1437. {
  1438. baseQuery = baseQuery
  1439. .Where(e => e.UserData!.FirstOrDefault(f => f.UserId == filter.User!.Id)!.PlaybackPositionTicks > 0);
  1440. }
  1441. else
  1442. {
  1443. baseQuery = baseQuery
  1444. .Where(e => e.UserData!.FirstOrDefault(f => f.UserId == filter.User!.Id)!.PlaybackPositionTicks == 0);
  1445. }
  1446. }
  1447. if (filter.ArtistIds.Length > 0)
  1448. {
  1449. baseQuery = baseQuery
  1450. .Where(e => e.ItemValues!.Any(f => f.ItemValue.Type <= ItemValueType.Artist && filter.ArtistIds.Contains(f.ItemId)));
  1451. }
  1452. if (filter.AlbumArtistIds.Length > 0)
  1453. {
  1454. baseQuery = baseQuery
  1455. .Where(e => e.ItemValues!.Any(f => f.ItemValue.Type == ItemValueType.Artist && filter.AlbumArtistIds.Contains(f.ItemId)));
  1456. }
  1457. if (filter.ContributingArtistIds.Length > 0)
  1458. {
  1459. baseQuery = baseQuery
  1460. .Where(e => e.ItemValues!.Any(f => f.ItemValue.Type == ItemValueType.Artist && filter.ContributingArtistIds.Contains(f.ItemId)));
  1461. }
  1462. if (filter.AlbumIds.Length > 0)
  1463. {
  1464. baseQuery = baseQuery.Where(e => context.BaseItems.Where(f => filter.AlbumIds.Contains(f.Id)).Any(f => f.Name == e.Album));
  1465. }
  1466. if (filter.ExcludeArtistIds.Length > 0)
  1467. {
  1468. baseQuery = baseQuery
  1469. .Where(e => !e.ItemValues!.Any(f => f.ItemValue.Type == ItemValueType.Artist && filter.ExcludeArtistIds.Contains(f.ItemId)));
  1470. }
  1471. if (filter.GenreIds.Count > 0)
  1472. {
  1473. baseQuery = baseQuery
  1474. .Where(e => e.ItemValues!.Any(f => f.ItemValue.Type == ItemValueType.Genre && filter.GenreIds.Contains(f.ItemId)));
  1475. }
  1476. if (filter.Genres.Count > 0)
  1477. {
  1478. var cleanGenres = filter.Genres.Select(e => GetCleanValue(e)).ToArray();
  1479. baseQuery = baseQuery
  1480. .Where(e => e.ItemValues!.Any(f => f.ItemValue.Type == ItemValueType.Genre && cleanGenres.Contains(f.ItemValue.CleanValue)));
  1481. }
  1482. if (tags.Count > 0)
  1483. {
  1484. var cleanValues = tags.Select(e => GetCleanValue(e)).ToArray();
  1485. baseQuery = baseQuery
  1486. .Where(e => e.ItemValues!.Any(f => f.ItemValue.Type == ItemValueType.Tags && cleanValues.Contains(f.ItemValue.CleanValue)));
  1487. }
  1488. if (excludeTags.Count > 0)
  1489. {
  1490. var cleanValues = excludeTags.Select(e => GetCleanValue(e)).ToArray();
  1491. baseQuery = baseQuery
  1492. .Where(e => !e.ItemValues!.Any(f => f.ItemValue.Type == ItemValueType.Tags && cleanValues.Contains(f.ItemValue.CleanValue)));
  1493. }
  1494. if (filter.StudioIds.Length > 0)
  1495. {
  1496. baseQuery = baseQuery
  1497. .Where(e => e.ItemValues!.Any(f => f.ItemValue.Type == ItemValueType.Studios && filter.StudioIds.Contains(f.ItemId)));
  1498. }
  1499. if (filter.OfficialRatings.Length > 0)
  1500. {
  1501. baseQuery = baseQuery
  1502. .Where(e => filter.OfficialRatings.Contains(e.OfficialRating));
  1503. }
  1504. if (filter.HasParentalRating ?? false)
  1505. {
  1506. if (filter.MinParentalRating.HasValue)
  1507. {
  1508. baseQuery = baseQuery
  1509. .Where(e => e.InheritedParentalRatingValue >= filter.MinParentalRating.Value);
  1510. }
  1511. if (filter.MaxParentalRating.HasValue)
  1512. {
  1513. baseQuery = baseQuery
  1514. .Where(e => e.InheritedParentalRatingValue < filter.MaxParentalRating.Value);
  1515. }
  1516. }
  1517. else if (filter.BlockUnratedItems.Length > 0)
  1518. {
  1519. var unratedItems = filter.BlockUnratedItems.Select(f => f.ToString()).ToArray();
  1520. if (filter.MinParentalRating.HasValue)
  1521. {
  1522. if (filter.MaxParentalRating.HasValue)
  1523. {
  1524. baseQuery = baseQuery
  1525. .Where(e => (e.InheritedParentalRatingValue == null && !unratedItems.Contains(e.UnratedType))
  1526. || (e.InheritedParentalRatingValue >= filter.MinParentalRating && e.InheritedParentalRatingValue <= filter.MaxParentalRating));
  1527. }
  1528. else
  1529. {
  1530. baseQuery = baseQuery
  1531. .Where(e => (e.InheritedParentalRatingValue == null && !unratedItems.Contains(e.UnratedType))
  1532. || e.InheritedParentalRatingValue >= filter.MinParentalRating);
  1533. }
  1534. }
  1535. else
  1536. {
  1537. baseQuery = baseQuery
  1538. .Where(e => e.InheritedParentalRatingValue != null && !unratedItems.Contains(e.UnratedType));
  1539. }
  1540. }
  1541. else if (filter.MinParentalRating.HasValue)
  1542. {
  1543. if (filter.MaxParentalRating.HasValue)
  1544. {
  1545. baseQuery = baseQuery
  1546. .Where(e => e.InheritedParentalRatingValue != null && e.InheritedParentalRatingValue >= filter.MinParentalRating.Value && e.InheritedParentalRatingValue <= filter.MaxParentalRating.Value);
  1547. }
  1548. else
  1549. {
  1550. baseQuery = baseQuery
  1551. .Where(e => e.InheritedParentalRatingValue != null && e.InheritedParentalRatingValue >= filter.MinParentalRating.Value);
  1552. }
  1553. }
  1554. else if (filter.MaxParentalRating.HasValue)
  1555. {
  1556. baseQuery = baseQuery
  1557. .Where(e => e.InheritedParentalRatingValue != null && e.InheritedParentalRatingValue >= filter.MaxParentalRating.Value);
  1558. }
  1559. else if (!filter.HasParentalRating ?? false)
  1560. {
  1561. baseQuery = baseQuery
  1562. .Where(e => e.InheritedParentalRatingValue == null);
  1563. }
  1564. if (filter.HasOfficialRating.HasValue)
  1565. {
  1566. if (filter.HasOfficialRating.Value)
  1567. {
  1568. baseQuery = baseQuery
  1569. .Where(e => e.OfficialRating != null && e.OfficialRating != string.Empty);
  1570. }
  1571. else
  1572. {
  1573. baseQuery = baseQuery
  1574. .Where(e => e.OfficialRating == null || e.OfficialRating == string.Empty);
  1575. }
  1576. }
  1577. if (filter.HasOverview.HasValue)
  1578. {
  1579. if (filter.HasOverview.Value)
  1580. {
  1581. baseQuery = baseQuery
  1582. .Where(e => e.Overview != null && e.Overview != string.Empty);
  1583. }
  1584. else
  1585. {
  1586. baseQuery = baseQuery
  1587. .Where(e => e.Overview == null || e.Overview == string.Empty);
  1588. }
  1589. }
  1590. if (filter.HasOwnerId.HasValue)
  1591. {
  1592. if (filter.HasOwnerId.Value)
  1593. {
  1594. baseQuery = baseQuery
  1595. .Where(e => e.OwnerId != null);
  1596. }
  1597. else
  1598. {
  1599. baseQuery = baseQuery
  1600. .Where(e => e.OwnerId == null);
  1601. }
  1602. }
  1603. if (!string.IsNullOrWhiteSpace(filter.HasNoAudioTrackWithLanguage))
  1604. {
  1605. baseQuery = baseQuery
  1606. .Where(e => !e.MediaStreams!.Any(f => f.StreamType == MediaStreamTypeEntity.Audio && f.Language == filter.HasNoAudioTrackWithLanguage));
  1607. }
  1608. if (!string.IsNullOrWhiteSpace(filter.HasNoInternalSubtitleTrackWithLanguage))
  1609. {
  1610. baseQuery = baseQuery
  1611. .Where(e => !e.MediaStreams!.Any(f => f.StreamType == MediaStreamTypeEntity.Subtitle && !f.IsExternal && f.Language == filter.HasNoInternalSubtitleTrackWithLanguage));
  1612. }
  1613. if (!string.IsNullOrWhiteSpace(filter.HasNoExternalSubtitleTrackWithLanguage))
  1614. {
  1615. baseQuery = baseQuery
  1616. .Where(e => !e.MediaStreams!.Any(f => f.StreamType == MediaStreamTypeEntity.Subtitle && f.IsExternal && f.Language == filter.HasNoExternalSubtitleTrackWithLanguage));
  1617. }
  1618. if (!string.IsNullOrWhiteSpace(filter.HasNoSubtitleTrackWithLanguage))
  1619. {
  1620. baseQuery = baseQuery
  1621. .Where(e => !e.MediaStreams!.Any(f => f.StreamType == MediaStreamTypeEntity.Subtitle && f.Language == filter.HasNoSubtitleTrackWithLanguage));
  1622. }
  1623. if (filter.HasSubtitles.HasValue)
  1624. {
  1625. baseQuery = baseQuery
  1626. .Where(e => e.MediaStreams!.Any(f => f.StreamType == MediaStreamTypeEntity.Subtitle) == filter.HasSubtitles.Value);
  1627. }
  1628. if (filter.HasChapterImages.HasValue)
  1629. {
  1630. baseQuery = baseQuery
  1631. .Where(e => e.Chapters!.Any(f => f.ImagePath != null) == filter.HasChapterImages.Value);
  1632. }
  1633. if (filter.HasDeadParentId.HasValue && filter.HasDeadParentId.Value)
  1634. {
  1635. baseQuery = baseQuery
  1636. .Where(e => e.ParentId.HasValue && !context.BaseItems.Any(f => f.Id == e.ParentId.Value));
  1637. }
  1638. if (filter.IsDeadArtist.HasValue && filter.IsDeadArtist.Value)
  1639. {
  1640. baseQuery = baseQuery
  1641. .Where(e => e.ItemValues!.Count(f => f.ItemValue.Type == ItemValueType.Artist || f.ItemValue.Type == ItemValueType.AlbumArtist) == 1);
  1642. }
  1643. if (filter.IsDeadStudio.HasValue && filter.IsDeadStudio.Value)
  1644. {
  1645. baseQuery = baseQuery
  1646. .Where(e => e.ItemValues!.Count(f => f.ItemValue.Type == ItemValueType.Studios) == 1);
  1647. }
  1648. if (filter.IsDeadPerson.HasValue && filter.IsDeadPerson.Value)
  1649. {
  1650. baseQuery = baseQuery
  1651. .Where(e => !context.Peoples.Any(f => f.Name == e.Name));
  1652. }
  1653. if (filter.Years.Length == 1)
  1654. {
  1655. baseQuery = baseQuery
  1656. .Where(e => e.ProductionYear == filter.Years[0]);
  1657. }
  1658. else if (filter.Years.Length > 1)
  1659. {
  1660. baseQuery = baseQuery
  1661. .Where(e => filter.Years.Any(f => f == e.ProductionYear));
  1662. }
  1663. var isVirtualItem = filter.IsVirtualItem ?? filter.IsMissing;
  1664. if (isVirtualItem.HasValue)
  1665. {
  1666. baseQuery = baseQuery
  1667. .Where(e => e.IsVirtualItem == isVirtualItem.Value);
  1668. }
  1669. if (filter.IsSpecialSeason.HasValue)
  1670. {
  1671. if (filter.IsSpecialSeason.Value)
  1672. {
  1673. baseQuery = baseQuery
  1674. .Where(e => e.IndexNumber == 0);
  1675. }
  1676. else
  1677. {
  1678. baseQuery = baseQuery
  1679. .Where(e => e.IndexNumber != 0);
  1680. }
  1681. }
  1682. if (filter.IsUnaired.HasValue)
  1683. {
  1684. if (filter.IsUnaired.Value)
  1685. {
  1686. baseQuery = baseQuery
  1687. .Where(e => e.PremiereDate >= now);
  1688. }
  1689. else
  1690. {
  1691. baseQuery = baseQuery
  1692. .Where(e => e.PremiereDate < now);
  1693. }
  1694. }
  1695. if (filter.MediaTypes.Length > 0)
  1696. {
  1697. var mediaTypes = filter.MediaTypes.Select(f => f.ToString()).ToArray();
  1698. baseQuery = baseQuery
  1699. .Where(e => mediaTypes.Contains(e.MediaType));
  1700. }
  1701. if (filter.ItemIds.Length > 0)
  1702. {
  1703. baseQuery = baseQuery
  1704. .Where(e => filter.ItemIds.Contains(e.Id));
  1705. }
  1706. if (filter.ExcludeItemIds.Length > 0)
  1707. {
  1708. baseQuery = baseQuery
  1709. .Where(e => !filter.ItemIds.Contains(e.Id));
  1710. }
  1711. if (filter.ExcludeProviderIds is not null && filter.ExcludeProviderIds.Count > 0)
  1712. {
  1713. baseQuery = baseQuery.Where(e => !e.Provider!.All(f => !filter.ExcludeProviderIds.All(w => f.ProviderId == w.Key && f.ProviderValue == w.Value)));
  1714. }
  1715. if (filter.HasAnyProviderId is not null && filter.HasAnyProviderId.Count > 0)
  1716. {
  1717. baseQuery = baseQuery.Where(e => e.Provider!.Any(f => !filter.HasAnyProviderId.Any(w => f.ProviderId == w.Key && f.ProviderValue == w.Value)));
  1718. }
  1719. if (filter.HasImdbId.HasValue)
  1720. {
  1721. baseQuery = baseQuery.Where(e => e.Provider!.Any(f => f.ProviderId == "imdb"));
  1722. }
  1723. if (filter.HasTmdbId.HasValue)
  1724. {
  1725. baseQuery = baseQuery.Where(e => e.Provider!.Any(f => f.ProviderId == "tmdb"));
  1726. }
  1727. if (filter.HasTvdbId.HasValue)
  1728. {
  1729. baseQuery = baseQuery.Where(e => e.Provider!.Any(f => f.ProviderId == "tvdb"));
  1730. }
  1731. var queryTopParentIds = filter.TopParentIds;
  1732. if (queryTopParentIds.Length > 0)
  1733. {
  1734. var includedItemByNameTypes = GetItemByNameTypesInQuery(filter);
  1735. var enableItemsByName = (filter.IncludeItemsByName ?? false) && includedItemByNameTypes.Count > 0;
  1736. if (enableItemsByName && includedItemByNameTypes.Count > 0)
  1737. {
  1738. baseQuery = baseQuery.Where(e => includedItemByNameTypes.Contains(e.Type) || queryTopParentIds.Any(w => w == e.TopParentId!.Value));
  1739. }
  1740. else
  1741. {
  1742. baseQuery = baseQuery.Where(e => queryTopParentIds.Contains(e.TopParentId!.Value));
  1743. }
  1744. }
  1745. if (filter.AncestorIds.Length > 0)
  1746. {
  1747. baseQuery = baseQuery.Where(e => e.Children!.Any(f => filter.AncestorIds.Contains(f.ParentItemId)));
  1748. }
  1749. if (!string.IsNullOrWhiteSpace(filter.AncestorWithPresentationUniqueKey))
  1750. {
  1751. baseQuery = baseQuery
  1752. .Where(e => context.BaseItems.Where(f => f.PresentationUniqueKey == filter.AncestorWithPresentationUniqueKey).Any(f => f.ParentAncestors!.Any(w => w.ItemId == f.Id)));
  1753. }
  1754. if (!string.IsNullOrWhiteSpace(filter.SeriesPresentationUniqueKey))
  1755. {
  1756. baseQuery = baseQuery
  1757. .Where(e => e.SeriesPresentationUniqueKey == filter.SeriesPresentationUniqueKey);
  1758. }
  1759. if (filter.ExcludeInheritedTags.Length > 0)
  1760. {
  1761. baseQuery = baseQuery
  1762. .Where(e => !e.ItemValues!.Where(w => w.ItemValue.Type == ItemValueType.InheritedTags)
  1763. .Any(f => filter.ExcludeInheritedTags.Contains(f.ItemValue.CleanValue)));
  1764. }
  1765. if (filter.IncludeInheritedTags.Length > 0)
  1766. {
  1767. // Episodes do not store inherit tags from their parents in the database, and the tag may be still required by the client.
  1768. // In addtion to the tags for the episodes themselves, we need to manually query its parent (the season)'s tags as well.
  1769. if (includeTypes.Length == 1 && includeTypes.FirstOrDefault() is BaseItemKind.Episode)
  1770. {
  1771. baseQuery = baseQuery
  1772. .Where(e => e.ItemValues!.Where(f => f.ItemValue.Type == ItemValueType.InheritedTags)
  1773. .Any(f => filter.IncludeInheritedTags.Contains(f.ItemValue.CleanValue))
  1774. ||
  1775. (e.ParentId.HasValue && context.ItemValuesMap.Where(w => w.ItemId == e.ParentId.Value)!.Where(w => w.ItemValue.Type == ItemValueType.InheritedTags)
  1776. .Any(f => filter.IncludeInheritedTags.Contains(f.ItemValue.CleanValue))));
  1777. }
  1778. // A playlist should be accessible to its owner regardless of allowed tags.
  1779. else if (includeTypes.Length == 1 && includeTypes.FirstOrDefault() is BaseItemKind.Playlist)
  1780. {
  1781. baseQuery = baseQuery
  1782. .Where(e =>
  1783. e.ParentAncestors!
  1784. .Any(f =>
  1785. f.ParentItem.ItemValues!.Any(w => w.ItemValue.Type == ItemValueType.Tags && filter.IncludeInheritedTags.Contains(w.ItemValue.CleanValue))
  1786. || e.Data!.Contains($"OwnerUserId\":\"{filter.User!.Id:N}\"")));
  1787. // d ^^ this is stupid it hate this.
  1788. }
  1789. else
  1790. {
  1791. baseQuery = baseQuery
  1792. .Where(e => e.ParentAncestors!.Any(f => f.ParentItem.ItemValues!.Any(w => w.ItemValue.Type == ItemValueType.Tags && filter.IncludeInheritedTags.Contains(w.ItemValue.CleanValue))));
  1793. }
  1794. }
  1795. if (filter.SeriesStatuses.Length > 0)
  1796. {
  1797. var seriesStatus = filter.SeriesStatuses.Select(e => e.ToString()).ToArray();
  1798. baseQuery = baseQuery
  1799. .Where(e => seriesStatus.Any(f => e.Data!.Contains(f)));
  1800. }
  1801. if (filter.BoxSetLibraryFolders.Length > 0)
  1802. {
  1803. var boxsetFolders = filter.BoxSetLibraryFolders.Select(e => e.ToString("N", CultureInfo.InvariantCulture)).ToArray();
  1804. baseQuery = baseQuery
  1805. .Where(e => boxsetFolders.Any(f => e.Data!.Contains(f)));
  1806. }
  1807. if (filter.VideoTypes.Length > 0)
  1808. {
  1809. var videoTypeBs = filter.VideoTypes.Select(e => $"\"VideoType\":\"{e}\"");
  1810. baseQuery = baseQuery
  1811. .Where(e => videoTypeBs.Any(f => e.Data!.Contains(f)));
  1812. }
  1813. if (filter.Is3D.HasValue)
  1814. {
  1815. if (filter.Is3D.Value)
  1816. {
  1817. baseQuery = baseQuery
  1818. .Where(e => e.Data!.Contains("Video3DFormat"));
  1819. }
  1820. else
  1821. {
  1822. baseQuery = baseQuery
  1823. .Where(e => !e.Data!.Contains("Video3DFormat"));
  1824. }
  1825. }
  1826. if (filter.IsPlaceHolder.HasValue)
  1827. {
  1828. if (filter.IsPlaceHolder.Value)
  1829. {
  1830. baseQuery = baseQuery
  1831. .Where(e => e.Data!.Contains("IsPlaceHolder\":true"));
  1832. }
  1833. else
  1834. {
  1835. baseQuery = baseQuery
  1836. .Where(e => !e.Data!.Contains("IsPlaceHolder\":true"));
  1837. }
  1838. }
  1839. if (filter.HasSpecialFeature.HasValue)
  1840. {
  1841. if (filter.HasSpecialFeature.Value)
  1842. {
  1843. baseQuery = baseQuery
  1844. .Where(e => e.ExtraIds != null);
  1845. }
  1846. else
  1847. {
  1848. baseQuery = baseQuery
  1849. .Where(e => e.ExtraIds == null);
  1850. }
  1851. }
  1852. if (filter.HasTrailer.HasValue || filter.HasThemeSong.HasValue || filter.HasThemeVideo.HasValue)
  1853. {
  1854. if (filter.HasTrailer.GetValueOrDefault() || filter.HasThemeSong.GetValueOrDefault() || filter.HasThemeVideo.GetValueOrDefault())
  1855. {
  1856. baseQuery = baseQuery
  1857. .Where(e => e.ExtraIds != null);
  1858. }
  1859. else
  1860. {
  1861. baseQuery = baseQuery
  1862. .Where(e => e.ExtraIds == null);
  1863. }
  1864. }
  1865. return baseQuery;
  1866. }
  1867. }