| | | 1 | | #pragma warning disable RS0030 // Do not use banned APIs |
| | | 2 | | |
| | | 3 | | using System; |
| | | 4 | | using System.Collections.Generic; |
| | | 5 | | using System.Linq; |
| | | 6 | | using Jellyfin.Data.Enums; |
| | | 7 | | using Jellyfin.Database.Implementations; |
| | | 8 | | using MediaBrowser.Controller.Entities.Audio; |
| | | 9 | | using MediaBrowser.Controller.Persistence; |
| | | 10 | | using Microsoft.EntityFrameworkCore; |
| | | 11 | | using DbLinkedChildType = Jellyfin.Database.Implementations.Entities.LinkedChildType; |
| | | 12 | | using LinkedChildType = MediaBrowser.Controller.Entities.LinkedChildType; |
| | | 13 | | |
| | | 14 | | namespace Jellyfin.Server.Implementations.Item; |
| | | 15 | | |
| | | 16 | | /// <summary> |
| | | 17 | | /// Provides linked children query and manipulation operations. |
| | | 18 | | /// </summary> |
| | | 19 | | public class LinkedChildrenService : ILinkedChildrenService |
| | | 20 | | { |
| | | 21 | | private readonly IDbContextFactory<JellyfinDbContext> _dbProvider; |
| | | 22 | | private readonly IItemTypeLookup _itemTypeLookup; |
| | | 23 | | private readonly IItemQueryHelpers _queryHelpers; |
| | | 24 | | |
| | | 25 | | /// <summary> |
| | | 26 | | /// Initializes a new instance of the <see cref="LinkedChildrenService"/> class. |
| | | 27 | | /// </summary> |
| | | 28 | | /// <param name="dbProvider">The database context factory.</param> |
| | | 29 | | /// <param name="itemTypeLookup">The item type lookup.</param> |
| | | 30 | | /// <param name="queryHelpers">The shared query helpers.</param> |
| | | 31 | | public LinkedChildrenService( |
| | | 32 | | IDbContextFactory<JellyfinDbContext> dbProvider, |
| | | 33 | | IItemTypeLookup itemTypeLookup, |
| | | 34 | | IItemQueryHelpers queryHelpers) |
| | | 35 | | { |
| | 21 | 36 | | _dbProvider = dbProvider; |
| | 21 | 37 | | _itemTypeLookup = itemTypeLookup; |
| | 21 | 38 | | _queryHelpers = queryHelpers; |
| | 21 | 39 | | } |
| | | 40 | | |
| | | 41 | | /// <inheritdoc/> |
| | | 42 | | public IReadOnlyList<Guid> GetLinkedChildrenIds(Guid parentId, int? childType = null) |
| | | 43 | | { |
| | 0 | 44 | | using var dbContext = _dbProvider.CreateDbContext(); |
| | | 45 | | |
| | 0 | 46 | | var query = dbContext.LinkedChildren |
| | 0 | 47 | | .Where(lc => lc.ParentId.Equals(parentId)); |
| | | 48 | | |
| | 0 | 49 | | if (childType.HasValue) |
| | | 50 | | { |
| | 0 | 51 | | query = query.Where(lc => (int)lc.ChildType == childType.Value); |
| | | 52 | | } |
| | | 53 | | |
| | 0 | 54 | | return query |
| | 0 | 55 | | .OrderBy(lc => lc.SortOrder) |
| | 0 | 56 | | .Select(lc => lc.ChildId) |
| | 0 | 57 | | .ToArray(); |
| | 0 | 58 | | } |
| | | 59 | | |
| | | 60 | | /// <inheritdoc/> |
| | | 61 | | public IReadOnlyDictionary<string, MusicArtist[]> FindArtists(IReadOnlyList<string> artistNames) |
| | | 62 | | { |
| | 17 | 63 | | using var dbContext = _dbProvider.CreateDbContext(); |
| | | 64 | | |
| | 17 | 65 | | var artists = dbContext.BaseItems |
| | 17 | 66 | | .AsNoTracking() |
| | 17 | 67 | | .Where(e => e.Type == _itemTypeLookup.BaseItemKindNames[BaseItemKind.MusicArtist]!) |
| | 17 | 68 | | .Where(e => artistNames.Contains(e.Name)) |
| | 17 | 69 | | .ToArray(); |
| | | 70 | | |
| | 17 | 71 | | var lookup = artists |
| | 17 | 72 | | .GroupBy(e => e.Name!) |
| | 17 | 73 | | .ToDictionary( |
| | 17 | 74 | | g => g.Key, |
| | 17 | 75 | | g => g.Select(f => _queryHelpers.DeserializeBaseItem(f)).Where(dto => dto is not null).Cast<MusicArtist> |
| | | 76 | | |
| | 17 | 77 | | var result = new Dictionary<string, MusicArtist[]>(artistNames.Count); |
| | 34 | 78 | | foreach (var name in artistNames) |
| | | 79 | | { |
| | 0 | 80 | | if (lookup.TryGetValue(name, out var artistArray)) |
| | | 81 | | { |
| | 0 | 82 | | result[name] = artistArray; |
| | | 83 | | } |
| | | 84 | | } |
| | | 85 | | |
| | 17 | 86 | | return result; |
| | 17 | 87 | | } |
| | | 88 | | |
| | | 89 | | /// <inheritdoc/> |
| | | 90 | | public IReadOnlyList<Guid> GetManualLinkedParentIds(Guid childId) |
| | | 91 | | { |
| | 0 | 92 | | using var context = _dbProvider.CreateDbContext(); |
| | 0 | 93 | | return context.LinkedChildren |
| | 0 | 94 | | .Where(lc => lc.ChildId == childId && lc.ChildType == DbLinkedChildType.Manual) |
| | 0 | 95 | | .Select(lc => lc.ParentId) |
| | 0 | 96 | | .Distinct() |
| | 0 | 97 | | .ToList(); |
| | 0 | 98 | | } |
| | | 99 | | |
| | | 100 | | /// <inheritdoc/> |
| | | 101 | | public IReadOnlyList<Guid> RerouteLinkedChildren(Guid fromChildId, Guid toChildId) |
| | | 102 | | { |
| | 0 | 103 | | using var context = _dbProvider.CreateDbContext(); |
| | | 104 | | |
| | 0 | 105 | | var affectedParentIds = context.LinkedChildren |
| | 0 | 106 | | .Where(lc => lc.ChildId == fromChildId && lc.ChildType == DbLinkedChildType.Manual) |
| | 0 | 107 | | .Select(lc => lc.ParentId) |
| | 0 | 108 | | .Distinct() |
| | 0 | 109 | | .ToList(); |
| | | 110 | | |
| | 0 | 111 | | if (affectedParentIds.Count == 0) |
| | | 112 | | { |
| | 0 | 113 | | return affectedParentIds; |
| | | 114 | | } |
| | | 115 | | |
| | 0 | 116 | | var parentsWithTarget = context.LinkedChildren |
| | 0 | 117 | | .Where(lc => lc.ChildId == toChildId && lc.ChildType == DbLinkedChildType.Manual) |
| | 0 | 118 | | .Select(lc => lc.ParentId) |
| | 0 | 119 | | .ToHashSet(); |
| | | 120 | | |
| | 0 | 121 | | context.LinkedChildren |
| | 0 | 122 | | .Where(lc => lc.ChildId == fromChildId |
| | 0 | 123 | | && lc.ChildType == DbLinkedChildType.Manual |
| | 0 | 124 | | && !parentsWithTarget.Contains(lc.ParentId)) |
| | 0 | 125 | | .ExecuteUpdate(s => s.SetProperty(e => e.ChildId, toChildId)); |
| | | 126 | | |
| | 0 | 127 | | context.LinkedChildren |
| | 0 | 128 | | .Where(lc => lc.ChildId == fromChildId |
| | 0 | 129 | | && lc.ChildType == DbLinkedChildType.Manual |
| | 0 | 130 | | && parentsWithTarget.Contains(lc.ParentId)) |
| | 0 | 131 | | .ExecuteDelete(); |
| | | 132 | | |
| | 0 | 133 | | return affectedParentIds; |
| | 0 | 134 | | } |
| | | 135 | | |
| | | 136 | | /// <inheritdoc/> |
| | | 137 | | public void UpsertLinkedChild(Guid parentId, Guid childId, LinkedChildType childType) |
| | | 138 | | { |
| | 0 | 139 | | using var context = _dbProvider.CreateDbContext(); |
| | | 140 | | |
| | 0 | 141 | | var dbChildType = (DbLinkedChildType)childType; |
| | 0 | 142 | | var existingLink = context.LinkedChildren |
| | 0 | 143 | | .FirstOrDefault(lc => lc.ParentId == parentId && lc.ChildId == childId); |
| | | 144 | | |
| | 0 | 145 | | if (existingLink is null) |
| | | 146 | | { |
| | 0 | 147 | | context.LinkedChildren.Add(new Jellyfin.Database.Implementations.Entities.LinkedChildEntity |
| | 0 | 148 | | { |
| | 0 | 149 | | ParentId = parentId, |
| | 0 | 150 | | ChildId = childId, |
| | 0 | 151 | | ChildType = dbChildType, |
| | 0 | 152 | | SortOrder = null |
| | 0 | 153 | | }); |
| | | 154 | | } |
| | | 155 | | else |
| | | 156 | | { |
| | 0 | 157 | | existingLink.ChildType = dbChildType; |
| | | 158 | | } |
| | | 159 | | |
| | 0 | 160 | | context.SaveChanges(); |
| | 0 | 161 | | } |
| | | 162 | | } |