| | | 1 | | #pragma warning disable CS1591 |
| | | 2 | | |
| | | 3 | | using System; |
| | | 4 | | using System.Collections.Generic; |
| | | 5 | | using System.Linq; |
| | | 6 | | using Jellyfin.Data.Enums; |
| | | 7 | | using MediaBrowser.Model.Entities; |
| | | 8 | | |
| | | 9 | | namespace MediaBrowser.Controller.Entities |
| | | 10 | | { |
| | | 11 | | public static class PeopleHelper |
| | | 12 | | { |
| | | 13 | | public static void AddPerson(ICollection<PersonInfo> people, PersonInfo person) |
| | | 14 | | { |
| | 63 | 15 | | ArgumentNullException.ThrowIfNull(person); |
| | 63 | 16 | | ArgumentException.ThrowIfNullOrEmpty(person.Name); |
| | | 17 | | |
| | 63 | 18 | | person.Name = person.Name.Trim(); |
| | | 19 | | |
| | | 20 | | // Normalize |
| | 63 | 21 | | if (string.Equals(person.Role, PersonType.GuestStar, StringComparison.OrdinalIgnoreCase)) |
| | | 22 | | { |
| | 0 | 23 | | person.Type = PersonKind.GuestStar; |
| | | 24 | | } |
| | 63 | 25 | | else if (string.Equals(person.Role, PersonType.Director, StringComparison.OrdinalIgnoreCase)) |
| | | 26 | | { |
| | 0 | 27 | | person.Type = PersonKind.Director; |
| | | 28 | | } |
| | 63 | 29 | | else if (string.Equals(person.Role, PersonType.Producer, StringComparison.OrdinalIgnoreCase)) |
| | | 30 | | { |
| | 0 | 31 | | person.Type = PersonKind.Producer; |
| | | 32 | | } |
| | 63 | 33 | | else if (string.Equals(person.Role, PersonType.Writer, StringComparison.OrdinalIgnoreCase)) |
| | | 34 | | { |
| | 0 | 35 | | person.Type = PersonKind.Writer; |
| | | 36 | | } |
| | | 37 | | |
| | | 38 | | // Check for dupes based on the combination of Name, Type and Role. |
| | 63 | 39 | | var existing = people.FirstOrDefault(p => IsSameCredit(p, person) |
| | 63 | 40 | | && string.Equals(p.Role ?? string.Empty, person.Role ?? string.Empty, StringComparison.OrdinalIgnoreCase |
| | | 41 | | |
| | 63 | 42 | | if (existing is null) |
| | | 43 | | { |
| | 63 | 44 | | if (string.IsNullOrEmpty(person.Role)) |
| | | 45 | | { |
| | 9 | 46 | | existing = people.FirstOrDefault(p => IsSameCredit(p, person)); |
| | | 47 | | } |
| | | 48 | | else |
| | | 49 | | { |
| | | 50 | | // If the person already exists without a role and we have one, fill it in |
| | 54 | 51 | | existing = people.FirstOrDefault(p => IsSameCredit(p, person) && string.IsNullOrEmpty(p.Role)); |
| | 54 | 52 | | if (existing is not null) |
| | | 53 | | { |
| | 0 | 54 | | existing.Role = person.Role; |
| | | 55 | | } |
| | | 56 | | } |
| | | 57 | | } |
| | | 58 | | |
| | 63 | 59 | | if (existing is null) |
| | | 60 | | { |
| | 63 | 61 | | people.Add(person); |
| | 63 | 62 | | return; |
| | | 63 | | } |
| | | 64 | | |
| | | 65 | | // If the type is GuestStar and there's already an Actor entry, then promote it to avoid dupes |
| | 0 | 66 | | if (person.Type == PersonKind.GuestStar) |
| | | 67 | | { |
| | 0 | 68 | | existing.Type = PersonKind.GuestStar; |
| | | 69 | | } |
| | | 70 | | |
| | 0 | 71 | | MergeExisting(existing, person); |
| | 0 | 72 | | } |
| | | 73 | | |
| | | 74 | | private static bool IsSameCredit(PersonInfo existing, PersonInfo person) |
| | | 75 | | { |
| | 694 | 76 | | if (!string.Equals(existing.Name, person.Name, StringComparison.OrdinalIgnoreCase)) |
| | | 77 | | { |
| | 694 | 78 | | return false; |
| | | 79 | | } |
| | | 80 | | |
| | | 81 | | // Actor and GuestStar describe the same credit, a guest star is just a promoted actor. |
| | 0 | 82 | | if (IsCastKind(existing.Type) && IsCastKind(person.Type)) |
| | | 83 | | { |
| | 0 | 84 | | return true; |
| | | 85 | | } |
| | | 86 | | |
| | 0 | 87 | | return existing.Type == person.Type; |
| | | 88 | | } |
| | | 89 | | |
| | | 90 | | private static bool IsCastKind(PersonKind kind) |
| | 0 | 91 | | => kind is PersonKind.Actor or PersonKind.GuestStar; |
| | | 92 | | |
| | | 93 | | private static void MergeExisting(PersonInfo existing, PersonInfo person) |
| | | 94 | | { |
| | 0 | 95 | | existing.SortOrder = person.SortOrder ?? existing.SortOrder; |
| | 0 | 96 | | existing.ImageUrl = person.ImageUrl ?? existing.ImageUrl; |
| | | 97 | | |
| | 0 | 98 | | foreach (var id in person.ProviderIds) |
| | | 99 | | { |
| | 0 | 100 | | existing.SetProviderId(id.Key, id.Value); |
| | | 101 | | } |
| | 0 | 102 | | } |
| | | 103 | | } |
| | | 104 | | } |