| | 1 | | using System; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using System.Collections.Immutable; |
| | 4 | | using System.Linq; |
| | 5 | | using Jellyfin.Data.Enums; |
| | 6 | | using Jellyfin.Database.Implementations; |
| | 7 | | using Jellyfin.Database.Implementations.Entities; |
| | 8 | | using Jellyfin.Database.Implementations.Entities.Libraries; |
| | 9 | | using Jellyfin.Extensions; |
| | 10 | | using MediaBrowser.Controller.Entities; |
| | 11 | | using MediaBrowser.Controller.Persistence; |
| | 12 | | using Microsoft.EntityFrameworkCore; |
| | 13 | |
|
| | 14 | | namespace Jellyfin.Server.Implementations.Item; |
| | 15 | | #pragma warning disable RS0030 // Do not use banned APIs |
| | 16 | | #pragma warning disable CA1304 // Specify CultureInfo |
| | 17 | | #pragma warning disable CA1311 // Specify a culture or use an invariant version |
| | 18 | | #pragma warning disable CA1862 // Use the 'StringComparison' method overloads to perform case-insensitive string compari |
| | 19 | |
|
| | 20 | | /// <summary> |
| | 21 | | /// Manager for handling people. |
| | 22 | | /// </summary> |
| | 23 | | /// <param name="dbProvider">Efcore Factory.</param> |
| | 24 | | /// <param name="itemTypeLookup">Items lookup service.</param> |
| | 25 | | /// <remarks> |
| | 26 | | /// Initializes a new instance of the <see cref="PeopleRepository"/> class. |
| | 27 | | /// </remarks> |
| | 28 | | public class PeopleRepository(IDbContextFactory<JellyfinDbContext> dbProvider, IItemTypeLookup itemTypeLookup) : IPeople |
| | 29 | | { |
| 21 | 30 | | private readonly IDbContextFactory<JellyfinDbContext> _dbProvider = dbProvider; |
| | 31 | |
|
| | 32 | | /// <inheritdoc/> |
| | 33 | | public IReadOnlyList<PersonInfo> GetPeople(InternalPeopleQuery filter) |
| | 34 | | { |
| 0 | 35 | | using var context = _dbProvider.CreateDbContext(); |
| 0 | 36 | | var dbQuery = TranslateQuery(context.Peoples.AsNoTracking(), context, filter); |
| | 37 | |
|
| | 38 | | // dbQuery = dbQuery.OrderBy(e => e.ListOrder); |
| 0 | 39 | | if (filter.Limit > 0) |
| | 40 | | { |
| 0 | 41 | | dbQuery = dbQuery.Take(filter.Limit); |
| | 42 | | } |
| | 43 | |
|
| | 44 | | // Include PeopleBaseItemMap |
| 0 | 45 | | if (!filter.ItemId.IsEmpty()) |
| | 46 | | { |
| 0 | 47 | | dbQuery = dbQuery.Include(p => p.BaseItems!.Where(m => m.ItemId == filter.ItemId)); |
| | 48 | | } |
| | 49 | |
|
| 0 | 50 | | return dbQuery.AsEnumerable().Select(Map).ToArray(); |
| 0 | 51 | | } |
| | 52 | |
|
| | 53 | | /// <inheritdoc/> |
| | 54 | | public IReadOnlyList<string> GetPeopleNames(InternalPeopleQuery filter) |
| | 55 | | { |
| 0 | 56 | | using var context = _dbProvider.CreateDbContext(); |
| 0 | 57 | | var dbQuery = TranslateQuery(context.Peoples.AsNoTracking(), context, filter); |
| | 58 | |
|
| | 59 | | // dbQuery = dbQuery.OrderBy(e => e.ListOrder); |
| 0 | 60 | | if (filter.Limit > 0) |
| | 61 | | { |
| 0 | 62 | | dbQuery = dbQuery.Take(filter.Limit); |
| | 63 | | } |
| | 64 | |
|
| 0 | 65 | | return dbQuery.Select(e => e.Name).ToArray(); |
| 0 | 66 | | } |
| | 67 | |
|
| | 68 | | /// <inheritdoc /> |
| | 69 | | public void UpdatePeople(Guid itemId, IReadOnlyList<PersonInfo> people) |
| | 70 | | { |
| 0 | 71 | | using var context = _dbProvider.CreateDbContext(); |
| | 72 | |
|
| | 73 | | // TODO: yes for __SOME__ reason there can be duplicates. |
| 0 | 74 | | people = people.DistinctBy(e => e.Id).ToArray(); |
| 0 | 75 | | var personids = people.Select(f => f.Id); |
| 0 | 76 | | var existingPersons = context.Peoples.Where(p => personids.Contains(p.Id)).Select(f => f.Id).ToArray(); |
| 0 | 77 | | context.Peoples.AddRange(people.Where(e => !existingPersons.Contains(e.Id)).Select(Map)); |
| 0 | 78 | | context.SaveChanges(); |
| | 79 | |
|
| 0 | 80 | | var maps = context.PeopleBaseItemMap.Where(e => e.ItemId == itemId).ToList(); |
| 0 | 81 | | foreach (var person in people) |
| | 82 | | { |
| 0 | 83 | | var existingMap = maps.FirstOrDefault(e => e.PeopleId == person.Id); |
| 0 | 84 | | if (existingMap is null) |
| | 85 | | { |
| 0 | 86 | | context.PeopleBaseItemMap.Add(new PeopleBaseItemMap() |
| 0 | 87 | | { |
| 0 | 88 | | Item = null!, |
| 0 | 89 | | ItemId = itemId, |
| 0 | 90 | | People = null!, |
| 0 | 91 | | PeopleId = person.Id, |
| 0 | 92 | | ListOrder = person.SortOrder, |
| 0 | 93 | | SortOrder = person.SortOrder, |
| 0 | 94 | | Role = person.Role |
| 0 | 95 | | }); |
| | 96 | | } |
| | 97 | | else |
| | 98 | | { |
| | 99 | | // person mapping already exists so remove from list |
| 0 | 100 | | maps.Remove(existingMap); |
| | 101 | | } |
| | 102 | | } |
| | 103 | |
|
| 0 | 104 | | context.PeopleBaseItemMap.RemoveRange(maps); |
| | 105 | |
|
| 0 | 106 | | context.SaveChanges(); |
| 0 | 107 | | } |
| | 108 | |
|
| | 109 | | private PersonInfo Map(People people) |
| | 110 | | { |
| 0 | 111 | | var mapping = people.BaseItems?.FirstOrDefault(); |
| 0 | 112 | | var personInfo = new PersonInfo() |
| 0 | 113 | | { |
| 0 | 114 | | Id = people.Id, |
| 0 | 115 | | Name = people.Name, |
| 0 | 116 | | Role = mapping?.Role, |
| 0 | 117 | | SortOrder = mapping?.SortOrder |
| 0 | 118 | | }; |
| 0 | 119 | | if (Enum.TryParse<PersonKind>(people.PersonType, out var kind)) |
| | 120 | | { |
| 0 | 121 | | personInfo.Type = kind; |
| | 122 | | } |
| | 123 | |
|
| 0 | 124 | | return personInfo; |
| | 125 | | } |
| | 126 | |
|
| | 127 | | private People Map(PersonInfo people) |
| | 128 | | { |
| 0 | 129 | | var personInfo = new People() |
| 0 | 130 | | { |
| 0 | 131 | | Name = people.Name, |
| 0 | 132 | | PersonType = people.Type.ToString(), |
| 0 | 133 | | Id = people.Id, |
| 0 | 134 | | }; |
| | 135 | |
|
| 0 | 136 | | return personInfo; |
| | 137 | | } |
| | 138 | |
|
| | 139 | | private IQueryable<People> TranslateQuery(IQueryable<People> query, JellyfinDbContext context, InternalPeopleQuery f |
| | 140 | | { |
| 0 | 141 | | if (filter.User is not null && filter.IsFavorite.HasValue) |
| | 142 | | { |
| 0 | 143 | | var personType = itemTypeLookup.BaseItemKindNames[BaseItemKind.Person]; |
| 0 | 144 | | query = query |
| 0 | 145 | | .Where(e => context.BaseItems.Any(b => b.Type == personType && b.Name == e.Name && b.UserData!.Any(u => |
| | 146 | | } |
| | 147 | |
|
| 0 | 148 | | if (!filter.ItemId.IsEmpty()) |
| | 149 | | { |
| 0 | 150 | | query = query.Where(e => e.BaseItems!.Any(w => w.ItemId.Equals(filter.ItemId))); |
| | 151 | | } |
| | 152 | |
|
| 0 | 153 | | if (!filter.AppearsInItemId.IsEmpty()) |
| | 154 | | { |
| 0 | 155 | | query = query.Where(e => e.BaseItems!.Any(w => w.ItemId.Equals(filter.AppearsInItemId))); |
| | 156 | | } |
| | 157 | |
|
| 0 | 158 | | var queryPersonTypes = filter.PersonTypes.Where(IsValidPersonType).ToList(); |
| 0 | 159 | | if (queryPersonTypes.Count > 0) |
| | 160 | | { |
| 0 | 161 | | query = query.Where(e => queryPersonTypes.Contains(e.PersonType)); |
| | 162 | | } |
| | 163 | |
|
| 0 | 164 | | var queryExcludePersonTypes = filter.ExcludePersonTypes.Where(IsValidPersonType).ToList(); |
| | 165 | |
|
| 0 | 166 | | if (queryExcludePersonTypes.Count > 0) |
| | 167 | | { |
| 0 | 168 | | query = query.Where(e => !queryPersonTypes.Contains(e.PersonType)); |
| | 169 | | } |
| | 170 | |
|
| 0 | 171 | | if (filter.MaxListOrder.HasValue && !filter.ItemId.IsEmpty()) |
| | 172 | | { |
| 0 | 173 | | query = query.Where(e => e.BaseItems!.First(w => w.ItemId == filter.ItemId).ListOrder <= filter.MaxListOrder |
| | 174 | | } |
| | 175 | |
|
| 0 | 176 | | if (!string.IsNullOrWhiteSpace(filter.NameContains)) |
| | 177 | | { |
| 0 | 178 | | var nameContainsUpper = filter.NameContains.ToUpper(); |
| 0 | 179 | | query = query.Where(e => e.Name.ToUpper().Contains(nameContainsUpper)); |
| | 180 | | } |
| | 181 | |
|
| 0 | 182 | | return query; |
| | 183 | | } |
| | 184 | |
|
| | 185 | | private bool IsAlphaNumeric(string str) |
| | 186 | | { |
| 0 | 187 | | if (string.IsNullOrWhiteSpace(str)) |
| | 188 | | { |
| 0 | 189 | | return false; |
| | 190 | | } |
| | 191 | |
|
| 0 | 192 | | for (int i = 0; i < str.Length; i++) |
| | 193 | | { |
| 0 | 194 | | if (!char.IsLetter(str[i]) && !char.IsNumber(str[i])) |
| | 195 | | { |
| 0 | 196 | | return false; |
| | 197 | | } |
| | 198 | | } |
| | 199 | |
|
| 0 | 200 | | return true; |
| | 201 | | } |
| | 202 | |
|
| | 203 | | private bool IsValidPersonType(string value) |
| | 204 | | { |
| 0 | 205 | | return IsAlphaNumeric(value); |
| | 206 | | } |
| | 207 | | } |