| | | 1 | | using System; |
| | | 2 | | using System.Collections.Generic; |
| | | 3 | | using System.Linq; |
| | | 4 | | using Jellyfin.Data.Enums; |
| | | 5 | | using Jellyfin.Database.Implementations; |
| | | 6 | | using Jellyfin.Database.Implementations.Entities; |
| | | 7 | | using Jellyfin.Extensions; |
| | | 8 | | using MediaBrowser.Controller.Entities; |
| | | 9 | | using MediaBrowser.Controller.Persistence; |
| | | 10 | | using MediaBrowser.Model.Querying; |
| | | 11 | | using Microsoft.EntityFrameworkCore; |
| | | 12 | | |
| | | 13 | | namespace Jellyfin.Server.Implementations.Item; |
| | | 14 | | #pragma warning disable RS0030 // Do not use banned APIs |
| | | 15 | | #pragma warning disable CA1304 // Specify CultureInfo |
| | | 16 | | #pragma warning disable CA1311 // Specify a culture or use an invariant version |
| | | 17 | | #pragma warning disable CA1862 // Use the 'StringComparison' method overloads to perform case-insensitive string compari |
| | | 18 | | |
| | | 19 | | /// <summary> |
| | | 20 | | /// Manager for handling people. |
| | | 21 | | /// </summary> |
| | | 22 | | /// <param name="dbProvider">Efcore Factory.</param> |
| | | 23 | | /// <param name="itemTypeLookup">Items lookup service.</param> |
| | | 24 | | /// <param name="queryHelpers">Shared item query helpers.</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, IItemQuer |
| | | 29 | | { |
| | 49 | 30 | | private readonly IDbContextFactory<JellyfinDbContext> _dbProvider = dbProvider; |
| | | 31 | | |
| | | 32 | | /// <inheritdoc/> |
| | | 33 | | public QueryResult<PersonInfo> GetPeople(InternalPeopleQuery filter) |
| | | 34 | | { |
| | 8 | 35 | | using var context = _dbProvider.CreateDbContext(); |
| | 8 | 36 | | var dbQuery = TranslateQuery(context.Peoples.AsNoTracking(), context, filter); |
| | 8 | 37 | | int? distinctNameCount = null; |
| | | 38 | | |
| | | 39 | | // Include PeopleBaseItemMap |
| | 8 | 40 | | if (!filter.ItemId.IsEmpty()) |
| | | 41 | | { |
| | 0 | 42 | | dbQuery = dbQuery.Include(p => p.BaseItems!.Where(m => m.ItemId == filter.ItemId)) |
| | 0 | 43 | | .OrderBy(e => e.BaseItems!.Where(m => m.ItemId == filter.ItemId).Min(m => m.ListOrder)) |
| | 0 | 44 | | .ThenBy(e => e.PersonType) |
| | 0 | 45 | | .ThenBy(e => e.Name); |
| | | 46 | | } |
| | | 47 | | else |
| | | 48 | | { |
| | | 49 | | // The Peoples table has one row per (Name, PersonType), so the same person can |
| | | 50 | | // appear multiple times (e.g. as Actor and GuestStar). Collapse to one row per |
| | | 51 | | // name so /Persons doesn't return the same BaseItem id repeatedly, keeping the |
| | | 52 | | // lowest id per lowercased name so case-only duplicates collapse together. |
| | 8 | 53 | | var candidates = dbQuery; |
| | 8 | 54 | | dbQuery = candidates |
| | 8 | 55 | | .Where(p => !candidates.Any(other => other.Name.ToLower() == p.Name.ToLower() && other.Id < p.Id)) |
| | 8 | 56 | | .OrderBy(e => e.Name.ToLower()); |
| | | 57 | | |
| | 8 | 58 | | if (filter.EnableTotalRecordCount) |
| | | 59 | | { |
| | 8 | 60 | | distinctNameCount = candidates.Select(e => e.Name.ToLower()).Distinct().Count(); |
| | | 61 | | } |
| | | 62 | | } |
| | | 63 | | |
| | 8 | 64 | | var count = 0; |
| | 8 | 65 | | if (filter.EnableTotalRecordCount) |
| | | 66 | | { |
| | 8 | 67 | | count = distinctNameCount ?? dbQuery.Count(); |
| | | 68 | | } |
| | | 69 | | |
| | 8 | 70 | | if (filter.StartIndex.HasValue && filter.StartIndex > 0) |
| | | 71 | | { |
| | 0 | 72 | | dbQuery = dbQuery.Skip(filter.StartIndex.Value); |
| | | 73 | | } |
| | | 74 | | |
| | 8 | 75 | | if (filter.Limit > 0) |
| | | 76 | | { |
| | 1 | 77 | | dbQuery = dbQuery.Take(filter.Limit); |
| | | 78 | | } |
| | | 79 | | |
| | 8 | 80 | | return new QueryResult<PersonInfo> |
| | 8 | 81 | | { |
| | 8 | 82 | | StartIndex = filter.StartIndex ?? 0, |
| | 8 | 83 | | TotalRecordCount = count, |
| | 8 | 84 | | Items = dbQuery.AsEnumerable().SelectMany(MapCredits).ToArray(), |
| | 8 | 85 | | }; |
| | 8 | 86 | | } |
| | | 87 | | |
| | | 88 | | /// <inheritdoc/> |
| | | 89 | | public IReadOnlyList<string> GetPeopleNames(InternalPeopleQuery filter) |
| | | 90 | | { |
| | 0 | 91 | | using var context = _dbProvider.CreateDbContext(); |
| | | 92 | | |
| | 0 | 93 | | IQueryable<string> dbQuery = TranslateQuery(context.Peoples.AsNoTracking(), context, filter) |
| | 0 | 94 | | .Select(e => e.Name) |
| | 0 | 95 | | .Distinct() |
| | 0 | 96 | | .OrderBy(e => e); |
| | | 97 | | |
| | 0 | 98 | | if (filter.StartIndex.HasValue && filter.StartIndex > 0) |
| | | 99 | | { |
| | 0 | 100 | | dbQuery = dbQuery.Skip(filter.StartIndex.Value); |
| | | 101 | | } |
| | | 102 | | |
| | 0 | 103 | | if (filter.Limit > 0) |
| | | 104 | | { |
| | 0 | 105 | | dbQuery = dbQuery.Take(filter.Limit); |
| | | 106 | | } |
| | | 107 | | |
| | 0 | 108 | | return dbQuery.ToArray(); |
| | 0 | 109 | | } |
| | | 110 | | |
| | | 111 | | /// <inheritdoc /> |
| | | 112 | | public void UpdatePeople(Guid itemId, IReadOnlyList<PersonInfo> people) |
| | | 113 | | { |
| | 130 | 114 | | foreach (var person in people) |
| | | 115 | | { |
| | 38 | 116 | | person.Name = person.Name.Trim(); |
| | 38 | 117 | | person.Role = person.Role?.Trim() ?? string.Empty; |
| | | 118 | | } |
| | | 119 | | |
| | | 120 | | // Project the values every comparison below needs once, so neither the case folding nor the |
| | | 121 | | // enum formatting is repeated per candidate. |
| | 27 | 122 | | var credits = people.Select(e => (Person: e, LoweredName: e.Name.ToLowerInvariant(), PersonType: e.Type.ToString |
| | | 123 | | |
| | | 124 | | // multiple metadata providers can provide the _same_ credit; dedupe case-insensitively. |
| | | 125 | | // The role is part of the key because one person can hold several credits of the same type |
| | | 126 | | // on an item, e.g. a Writer credited for both the Novel and the Screenplay. |
| | 27 | 127 | | var distinctCredits = credits.DistinctBy(e => (e.LoweredName, e.PersonType, e.LoweredRole)).ToArray(); |
| | | 128 | | |
| | 27 | 129 | | var distinctPersons = distinctCredits.DistinctBy(e => (e.LoweredName, e.PersonType)).ToArray(); |
| | | 130 | | |
| | 27 | 131 | | using var context = _dbProvider.CreateDbContext(); |
| | 27 | 132 | | var existingMaps = context.PeopleBaseItemMap |
| | 27 | 133 | | .AsNoTracking() |
| | 27 | 134 | | .Include(e => e.People) |
| | 27 | 135 | | .Where(e => e.ItemId == itemId) |
| | 27 | 136 | | .ToList(); |
| | | 137 | | |
| | | 138 | | // Most library scans refresh unchanged local metadata. Avoid opening a write |
| | | 139 | | // transaction when the item's people mappings, order and roles are unchanged. |
| | 27 | 140 | | var incomingCredits = distinctCredits |
| | 27 | 141 | | .Select((credit, index) => new |
| | 27 | 142 | | { |
| | 27 | 143 | | Key = (credit.LoweredName, credit.PersonType, credit.LoweredRole), |
| | 27 | 144 | | Role = credit.Person.Role, |
| | 27 | 145 | | ListOrder = index, |
| | 27 | 146 | | SortOrder = credit.Person.SortOrder |
| | 27 | 147 | | }) |
| | 27 | 148 | | .ToDictionary(e => e.Key); |
| | 27 | 149 | | var mappingsAreUnchanged = existingMaps.Count == incomingCredits.Count |
| | 27 | 150 | | && existingMaps.All(map => |
| | 27 | 151 | | incomingCredits.TryGetValue( |
| | 27 | 152 | | (map.People.Name.ToLowerInvariant(), map.People.PersonType ?? string.Empty, map.Role?.ToLowerInvaria |
| | 27 | 153 | | out var incoming) |
| | 27 | 154 | | && map.ListOrder == incoming.ListOrder |
| | 27 | 155 | | && map.SortOrder == incoming.SortOrder |
| | 27 | 156 | | && string.Equals(map.Role ?? string.Empty, incoming.Role, StringComparison.OrdinalIgnoreCase)); |
| | | 157 | | |
| | 27 | 158 | | if (mappingsAreUnchanged) |
| | | 159 | | { |
| | 3 | 160 | | return; |
| | | 161 | | } |
| | | 162 | | |
| | 24 | 163 | | using var transaction = context.Database.BeginTransaction(); |
| | | 164 | | // The fast-path snapshot was read before acquiring the write transaction. Reload |
| | | 165 | | // tracked mappings inside it so a concurrent refresh cannot leave stale credits. |
| | 24 | 166 | | existingMaps = context.PeopleBaseItemMap |
| | 24 | 167 | | .Include(e => e.People) |
| | 24 | 168 | | .Where(e => e.ItemId == itemId) |
| | 24 | 169 | | .ToList(); |
| | | 170 | | |
| | | 171 | | // Query each person type separately so SQLite can use IX_Peoples_NameLower. |
| | | 172 | | // Combining the two fields into `lower(Name) || '-' || PersonType` forces a full |
| | | 173 | | // scan of Peoples for every media item, which is prohibitive during a large import. |
| | 24 | 174 | | var existingPersons = new List<People>(); |
| | 96 | 175 | | foreach (var personTypeGroup in distinctPersons.GroupBy(e => e.PersonType, StringComparer.Ordinal)) |
| | | 176 | | { |
| | 24 | 177 | | var names = personTypeGroup |
| | 24 | 178 | | .Select(e => e.LoweredName) |
| | 24 | 179 | | .ToArray(); |
| | | 180 | | |
| | 24 | 181 | | existingPersons.AddRange(context.Peoples |
| | 24 | 182 | | .Where(e => e.PersonType == personTypeGroup.Key && names.Contains(e.Name.ToLower())) |
| | 24 | 183 | | .ToArray()); |
| | | 184 | | } |
| | | 185 | | |
| | 24 | 186 | | var existingPersonKeys = existingPersons.Select(e => (e.Name.ToLowerInvariant(), e.PersonType ?? string.Empty)). |
| | | 187 | | |
| | 24 | 188 | | var toAdd = distinctPersons |
| | 24 | 189 | | .Where(e => !existingPersonKeys.Contains((e.LoweredName, e.PersonType))) |
| | 24 | 190 | | .Select(e => Map(e.Person)) |
| | 24 | 191 | | .ToArray(); |
| | 24 | 192 | | context.Peoples.AddRange(toAdd); |
| | 24 | 193 | | context.SaveChanges(); |
| | | 194 | | |
| | | 195 | | // The Peoples table can hold case-only duplicates, so keep the first match per key just as |
| | | 196 | | // the previous First() lookup did. |
| | 24 | 197 | | var personsEntities = new Dictionary<(string LoweredName, string PersonType), People>(); |
| | 112 | 198 | | foreach (var entity in toAdd.Concat(existingPersons)) |
| | | 199 | | { |
| | 32 | 200 | | personsEntities.TryAdd((entity.Name.ToLowerInvariant(), entity.PersonType ?? string.Empty), entity); |
| | | 201 | | } |
| | | 202 | | |
| | 24 | 203 | | var existingMapsByCredit = new Dictionary<(string LoweredName, string PersonType, string LoweredRole), PeopleBas |
| | 64 | 204 | | foreach (var map in existingMaps) |
| | | 205 | | { |
| | 8 | 206 | | existingMapsByCredit.TryAdd((map.People.Name.ToLowerInvariant(), map.People.PersonType ?? string.Empty, map. |
| | | 207 | | } |
| | | 208 | | |
| | 24 | 209 | | var listOrder = 0; |
| | | 210 | | |
| | 116 | 211 | | foreach (var credit in distinctCredits) |
| | | 212 | | { |
| | 34 | 213 | | var entityPerson = personsEntities[(credit.LoweredName, credit.PersonType)]; |
| | 34 | 214 | | if (existingMapsByCredit.TryGetValue((credit.LoweredName, credit.PersonType, credit.LoweredRole), out var ex |
| | | 215 | | { |
| | | 216 | | // Update the order for existing mappings |
| | 5 | 217 | | existingMap.ListOrder = listOrder; |
| | 5 | 218 | | existingMap.SortOrder = credit.Person.SortOrder; |
| | | 219 | | // person mapping already exists so remove from list |
| | 5 | 220 | | existingMaps.Remove(existingMap); |
| | | 221 | | } |
| | | 222 | | else |
| | | 223 | | { |
| | 29 | 224 | | context.PeopleBaseItemMap.Add(new PeopleBaseItemMap() |
| | 29 | 225 | | { |
| | 29 | 226 | | Item = null!, |
| | 29 | 227 | | ItemId = itemId, |
| | 29 | 228 | | People = null!, |
| | 29 | 229 | | PeopleId = entityPerson.Id, |
| | 29 | 230 | | ListOrder = listOrder, |
| | 29 | 231 | | SortOrder = credit.Person.SortOrder, |
| | 29 | 232 | | Role = credit.Person.Role |
| | 29 | 233 | | }); |
| | | 234 | | } |
| | | 235 | | |
| | 34 | 236 | | listOrder++; |
| | | 237 | | } |
| | | 238 | | |
| | 24 | 239 | | var droppedCredits = existingMaps.Select(e => e.PeopleId).Distinct().ToArray(); |
| | 24 | 240 | | context.PeopleBaseItemMap.RemoveRange(existingMaps); |
| | | 241 | | |
| | 24 | 242 | | context.SaveChanges(); |
| | | 243 | | |
| | | 244 | | // Nothing else ever deletes a credit row, so one left without a single mapping outlives the |
| | | 245 | | // credit it stood for: it keeps a person of that name off the dead-person sweep, which only |
| | | 246 | | // sees items no credit names, and keeps the name in every by-name list. That is how a credit |
| | | 247 | | // a provider dropped, or one a broken provider result invented, becomes impossible to clean up. |
| | 24 | 248 | | DeleteCreditsWithoutMapping(context, droppedCredits); |
| | | 249 | | |
| | 24 | 250 | | context.SaveChanges(); |
| | 24 | 251 | | transaction.Commit(); |
| | 51 | 252 | | } |
| | | 253 | | |
| | | 254 | | /// <inheritdoc/> |
| | | 255 | | public int DeleteOrphanedCredits() |
| | | 256 | | { |
| | 2 | 257 | | using var context = _dbProvider.CreateDbContext(); |
| | | 258 | | |
| | 2 | 259 | | return DeleteCreditsWithoutMapping(context, null); |
| | 2 | 260 | | } |
| | | 261 | | |
| | | 262 | | // A null candidate list sweeps every credit, anything else only the ones just unmapped. |
| | | 263 | | private int DeleteCreditsWithoutMapping(JellyfinDbContext context, IReadOnlyList<Guid>? candidates) |
| | | 264 | | { |
| | 26 | 265 | | if (candidates is not null && candidates.Count == 0) |
| | | 266 | | { |
| | 21 | 267 | | return 0; |
| | | 268 | | } |
| | | 269 | | |
| | 5 | 270 | | var credits = candidates is null |
| | 5 | 271 | | ? context.Peoples.AsQueryable() |
| | 5 | 272 | | : context.Peoples.WhereOneOrMany(candidates, e => e.Id); |
| | | 273 | | |
| | 5 | 274 | | return credits.Where(e => !context.PeopleBaseItemMap.Any(f => f.PeopleId == e.Id)).ExecuteDelete(); |
| | | 275 | | } |
| | | 276 | | |
| | | 277 | | /// <inheritdoc/> |
| | | 278 | | public IReadOnlyDictionary<Guid, IReadOnlyList<string>> GetPeopleNamesByItems(IReadOnlyList<Guid> itemIds, IReadOnly |
| | | 279 | | { |
| | 0 | 280 | | using var context = _dbProvider.CreateDbContext(); |
| | 0 | 281 | | var query = context.PeopleBaseItemMap |
| | 0 | 282 | | .AsNoTracking() |
| | 0 | 283 | | .WhereOneOrMany(itemIds, m => m.ItemId); |
| | | 284 | | |
| | 0 | 285 | | if (personTypes.Count > 0) |
| | | 286 | | { |
| | 0 | 287 | | query = query.Where(m => personTypes.Contains(m.People.PersonType)); |
| | | 288 | | } |
| | | 289 | | |
| | 0 | 290 | | var rows = query |
| | 0 | 291 | | .OrderBy(m => m.ListOrder) |
| | 0 | 292 | | .Select(m => new { m.ItemId, m.People.Name }) |
| | 0 | 293 | | .ToList(); |
| | | 294 | | |
| | 0 | 295 | | var result = new Dictionary<Guid, IReadOnlyList<string>>(); |
| | 0 | 296 | | foreach (var group in rows.GroupBy(r => r.ItemId)) |
| | | 297 | | { |
| | 0 | 298 | | var names = group |
| | 0 | 299 | | .Select(r => r.Name) |
| | 0 | 300 | | .Where(name => !string.IsNullOrEmpty(name)) |
| | 0 | 301 | | .Distinct() |
| | 0 | 302 | | .ToArray(); |
| | | 303 | | |
| | 0 | 304 | | if (names.Length > 0) |
| | | 305 | | { |
| | 0 | 306 | | result[group.Key] = names; |
| | | 307 | | } |
| | | 308 | | } |
| | | 309 | | |
| | 0 | 310 | | return result; |
| | 0 | 311 | | } |
| | | 312 | | |
| | | 313 | | /// <inheritdoc/> |
| | | 314 | | public IReadOnlyDictionary<Guid, IReadOnlyList<PersonInfo>> GetPeopleByItems(IReadOnlyList<Guid> itemIds) |
| | | 315 | | { |
| | 0 | 316 | | using var context = _dbProvider.CreateDbContext(); |
| | 0 | 317 | | var rows = context.PeopleBaseItemMap |
| | 0 | 318 | | .AsNoTracking() |
| | 0 | 319 | | .WhereOneOrMany(itemIds, m => m.ItemId) |
| | 0 | 320 | | .OrderBy(m => m.ListOrder) |
| | 0 | 321 | | .Select(m => new |
| | 0 | 322 | | { |
| | 0 | 323 | | m.ItemId, |
| | 0 | 324 | | m.Role, |
| | 0 | 325 | | m.SortOrder, |
| | 0 | 326 | | m.People.Id, |
| | 0 | 327 | | m.People.Name, |
| | 0 | 328 | | m.People.PersonType |
| | 0 | 329 | | }) |
| | 0 | 330 | | .ToList(); |
| | | 331 | | |
| | 0 | 332 | | var result = new Dictionary<Guid, IReadOnlyList<PersonInfo>>(); |
| | 0 | 333 | | foreach (var group in rows.GroupBy(r => r.ItemId)) |
| | | 334 | | { |
| | 0 | 335 | | var people = new List<PersonInfo>(); |
| | 0 | 336 | | foreach (var row in group) |
| | | 337 | | { |
| | 0 | 338 | | var personInfo = new PersonInfo |
| | 0 | 339 | | { |
| | 0 | 340 | | ItemId = row.ItemId, |
| | 0 | 341 | | Id = row.Id, |
| | 0 | 342 | | Name = row.Name, |
| | 0 | 343 | | Role = row.Role, |
| | 0 | 344 | | SortOrder = row.SortOrder |
| | 0 | 345 | | }; |
| | 0 | 346 | | if (Enum.TryParse<PersonKind>(row.PersonType, out var kind)) |
| | | 347 | | { |
| | 0 | 348 | | personInfo.Type = kind; |
| | | 349 | | } |
| | | 350 | | |
| | 0 | 351 | | people.Add(personInfo); |
| | | 352 | | } |
| | | 353 | | |
| | 0 | 354 | | result[group.Key] = people; |
| | | 355 | | } |
| | | 356 | | |
| | 0 | 357 | | return result; |
| | 0 | 358 | | } |
| | | 359 | | |
| | | 360 | | private IEnumerable<PersonInfo> MapCredits(People people) |
| | | 361 | | { |
| | 13 | 362 | | var mappings = people.BaseItems; |
| | 13 | 363 | | if (mappings is null || mappings.Count == 0) |
| | | 364 | | { |
| | 13 | 365 | | return [Map(people, null)]; |
| | | 366 | | } |
| | | 367 | | |
| | 0 | 368 | | return mappings.OrderBy(m => m.ListOrder).Select(m => Map(people, m)); |
| | | 369 | | } |
| | | 370 | | |
| | | 371 | | private PersonInfo Map(People people, PeopleBaseItemMap? mapping) |
| | | 372 | | { |
| | 13 | 373 | | var personInfo = new PersonInfo() |
| | 13 | 374 | | { |
| | 13 | 375 | | Id = people.Id, |
| | 13 | 376 | | Name = people.Name, |
| | 13 | 377 | | Role = mapping?.Role, |
| | 13 | 378 | | SortOrder = mapping?.SortOrder |
| | 13 | 379 | | }; |
| | 13 | 380 | | if (Enum.TryParse<PersonKind>(people.PersonType, out var kind)) |
| | | 381 | | { |
| | 13 | 382 | | personInfo.Type = kind; |
| | | 383 | | } |
| | | 384 | | |
| | 13 | 385 | | return personInfo; |
| | | 386 | | } |
| | | 387 | | |
| | | 388 | | private People Map(PersonInfo people) |
| | | 389 | | { |
| | 26 | 390 | | var personInfo = new People() |
| | 26 | 391 | | { |
| | 26 | 392 | | Name = people.Name, |
| | 26 | 393 | | PersonType = people.Type.ToString(), |
| | 26 | 394 | | Id = people.Id, |
| | 26 | 395 | | }; |
| | | 396 | | |
| | 26 | 397 | | return personInfo; |
| | | 398 | | } |
| | | 399 | | |
| | | 400 | | private IQueryable<People> TranslateQuery(IQueryable<People> query, JellyfinDbContext context, InternalPeopleQuery f |
| | | 401 | | { |
| | 8 | 402 | | if (filter.User is not null && filter.IsFavorite.HasValue) |
| | | 403 | | { |
| | 0 | 404 | | var personType = itemTypeLookup.BaseItemKindNames[BaseItemKind.Person]; |
| | 0 | 405 | | var userId = filter.User.Id; |
| | 0 | 406 | | var isFavorite = filter.IsFavorite.Value; |
| | 0 | 407 | | var favoriteItemIds = context.UserData |
| | 0 | 408 | | .Where(u => u.UserId.Equals(userId) && u.IsFavorite == isFavorite) |
| | 0 | 409 | | .Select(u => u.ItemId); |
| | | 410 | | |
| | 0 | 411 | | var favoriteNames = context.BaseItems |
| | 0 | 412 | | .Where(b => b.Type == personType && favoriteItemIds.Contains(b.Id)) |
| | 0 | 413 | | .Select(b => b.Name); |
| | | 414 | | |
| | 0 | 415 | | query = query.Where(e => favoriteNames.Contains(e.Name)); |
| | | 416 | | } |
| | | 417 | | |
| | 8 | 418 | | if (filter.AccessFilter is not null) |
| | | 419 | | { |
| | | 420 | | // Keep only people credited on at least one item the user can see. |
| | 0 | 421 | | var accessibleItems = queryHelpers.ApplyAccessFiltering(context, context.BaseItems.AsNoTracking(), filter.Ac |
| | 0 | 422 | | query = query.Where(e => context.PeopleBaseItemMap |
| | 0 | 423 | | .Any(m => m.PeopleId == e.Id && accessibleItems.Any(i => i.Id == m.ItemId))); |
| | | 424 | | } |
| | | 425 | | |
| | 8 | 426 | | if (filter.MustHaveItem) |
| | | 427 | | { |
| | | 428 | | // A credit with no by-name item behind it cannot be handed back as one. |
| | 3 | 429 | | var personType = itemTypeLookup.BaseItemKindNames[BaseItemKind.Person]; |
| | 3 | 430 | | query = query.Where(e => context.BaseItems.Any(b => b.Type == personType && b.Name == e.Name)); |
| | | 431 | | } |
| | | 432 | | |
| | 8 | 433 | | if (!filter.ItemId.IsEmpty()) |
| | | 434 | | { |
| | 0 | 435 | | var itemId = filter.ItemId; |
| | 0 | 436 | | query = query.Where(e => context.PeopleBaseItemMap |
| | 0 | 437 | | .Where(m => m.ItemId.Equals(itemId)) |
| | 0 | 438 | | .Select(m => m.PeopleId) |
| | 0 | 439 | | .Contains(e.Id)); |
| | | 440 | | } |
| | | 441 | | |
| | 8 | 442 | | if (filter.ParentId != null) |
| | | 443 | | { |
| | 0 | 444 | | query = query.Where(e => e.BaseItems!.Any(w => context.AncestorIds.Any(i => i.ParentItemId == filter.ParentI |
| | | 445 | | } |
| | | 446 | | |
| | 8 | 447 | | if (!filter.AppearsInItemId.IsEmpty()) |
| | | 448 | | { |
| | 0 | 449 | | var appearsInItemId = filter.AppearsInItemId; |
| | 0 | 450 | | query = query.Where(e => context.PeopleBaseItemMap |
| | 0 | 451 | | .Where(m => m.ItemId.Equals(appearsInItemId)) |
| | 0 | 452 | | .Select(m => m.PeopleId) |
| | 0 | 453 | | .Contains(e.Id)); |
| | | 454 | | } |
| | | 455 | | |
| | 8 | 456 | | var queryPersonTypes = filter.PersonTypes.Where(IsValidPersonType).ToList(); |
| | 8 | 457 | | if (queryPersonTypes.Count > 0) |
| | | 458 | | { |
| | 0 | 459 | | query = query.Where(e => queryPersonTypes.Contains(e.PersonType)); |
| | | 460 | | } |
| | | 461 | | |
| | 8 | 462 | | var queryExcludePersonTypes = filter.ExcludePersonTypes.Where(IsValidPersonType).ToList(); |
| | | 463 | | |
| | 8 | 464 | | if (queryExcludePersonTypes.Count > 0) |
| | | 465 | | { |
| | 0 | 466 | | query = query.Where(e => !queryExcludePersonTypes.Contains(e.PersonType)); |
| | | 467 | | } |
| | | 468 | | |
| | 8 | 469 | | if (filter.MaxListOrder.HasValue && !filter.ItemId.IsEmpty()) |
| | | 470 | | { |
| | 0 | 471 | | query = query.Where(e => e.BaseItems!.Any(w => w.ItemId == filter.ItemId && w.ListOrder <= filter.MaxListOrd |
| | | 472 | | } |
| | | 473 | | |
| | 8 | 474 | | if (!string.IsNullOrWhiteSpace(filter.NameContains)) |
| | | 475 | | { |
| | 0 | 476 | | var nameContainsUpper = filter.NameContains.ToUpper(); |
| | 0 | 477 | | query = query.Where(e => e.Name.ToUpper().Contains(nameContainsUpper)); |
| | | 478 | | } |
| | | 479 | | |
| | 8 | 480 | | if (!string.IsNullOrWhiteSpace(filter.NameStartsWith)) |
| | | 481 | | { |
| | 0 | 482 | | query = query.Where(e => e.Name.StartsWith(filter.NameStartsWith.ToLowerInvariant())); |
| | | 483 | | } |
| | | 484 | | |
| | 8 | 485 | | if (!string.IsNullOrWhiteSpace(filter.NameLessThan)) |
| | | 486 | | { |
| | 3 | 487 | | query = query.Where(e => e.Name.ToLower().CompareTo(filter.NameLessThan.ToLowerInvariant()) < 0); |
| | | 488 | | } |
| | | 489 | | |
| | 8 | 490 | | if (!string.IsNullOrWhiteSpace(filter.NameStartsWithOrGreater)) |
| | | 491 | | { |
| | 2 | 492 | | query = query.Where(e => e.Name.ToLower().CompareTo(filter.NameStartsWithOrGreater.ToLowerInvariant()) >= 0) |
| | | 493 | | } |
| | | 494 | | |
| | 8 | 495 | | return query; |
| | | 496 | | } |
| | | 497 | | |
| | | 498 | | private bool IsAlphaNumeric(string str) |
| | | 499 | | { |
| | 0 | 500 | | if (string.IsNullOrWhiteSpace(str)) |
| | | 501 | | { |
| | 0 | 502 | | return false; |
| | | 503 | | } |
| | | 504 | | |
| | 0 | 505 | | for (int i = 0; i < str.Length; i++) |
| | | 506 | | { |
| | 0 | 507 | | if (!char.IsLetter(str[i]) && !char.IsNumber(str[i])) |
| | | 508 | | { |
| | 0 | 509 | | return false; |
| | | 510 | | } |
| | | 511 | | } |
| | | 512 | | |
| | 0 | 513 | | return true; |
| | | 514 | | } |
| | | 515 | | |
| | | 516 | | private bool IsValidPersonType(string value) |
| | | 517 | | { |
| | 0 | 518 | | return IsAlphaNumeric(value); |
| | | 519 | | } |
| | | 520 | | } |