2
0

PeopleHelper.cs 3.6 KB

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