PeopleHelper.cs 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. // Normalize
  16. if (string.Equals(person.Role, PersonType.GuestStar, StringComparison.OrdinalIgnoreCase))
  17. {
  18. person.Type = PersonKind.GuestStar;
  19. }
  20. else if (string.Equals(person.Role, PersonType.Director, StringComparison.OrdinalIgnoreCase))
  21. {
  22. person.Type = PersonKind.Director;
  23. }
  24. else if (string.Equals(person.Role, PersonType.Producer, StringComparison.OrdinalIgnoreCase))
  25. {
  26. person.Type = PersonKind.Producer;
  27. }
  28. else if (string.Equals(person.Role, PersonType.Writer, StringComparison.OrdinalIgnoreCase))
  29. {
  30. person.Type = PersonKind.Writer;
  31. }
  32. // If the type is GuestStar and there's already an Actor entry, then update it to avoid dupes
  33. if (person.Type == PersonKind.GuestStar)
  34. {
  35. var existing = people.FirstOrDefault(p => p.Name.Equals(person.Name, StringComparison.OrdinalIgnoreCase) && p.Type == PersonKind.Actor);
  36. if (existing is not null)
  37. {
  38. existing.Type = PersonKind.GuestStar;
  39. MergeExisting(existing, person);
  40. return;
  41. }
  42. }
  43. if (person.Type == PersonKind.Actor)
  44. {
  45. // If the actor already exists without a role and we have one, fill it in
  46. var existing = people.FirstOrDefault(p => p.Name.Equals(person.Name, StringComparison.OrdinalIgnoreCase) && (p.Type == PersonKind.Actor || p.Type == PersonKind.GuestStar));
  47. if (existing is null)
  48. {
  49. // Wasn't there - add it
  50. people.Add(person);
  51. }
  52. else
  53. {
  54. // Was there, if no role and we have one - fill it in
  55. if (string.IsNullOrEmpty(existing.Role) && !string.IsNullOrEmpty(person.Role))
  56. {
  57. existing.Role = person.Role;
  58. }
  59. MergeExisting(existing, person);
  60. }
  61. }
  62. else
  63. {
  64. var existing = people.FirstOrDefault(p =>
  65. string.Equals(p.Name, person.Name, StringComparison.OrdinalIgnoreCase)
  66. && p.Type == person.Type);
  67. // Check for dupes based on the combination of Name and Type
  68. if (existing is null)
  69. {
  70. people.Add(person);
  71. }
  72. else
  73. {
  74. MergeExisting(existing, person);
  75. }
  76. }
  77. }
  78. private static void MergeExisting(PersonInfo existing, PersonInfo person)
  79. {
  80. existing.SortOrder = person.SortOrder ?? existing.SortOrder;
  81. existing.ImageUrl = person.ImageUrl ?? existing.ImageUrl;
  82. foreach (var id in person.ProviderIds)
  83. {
  84. existing.SetProviderId(id.Key, id.Value);
  85. }
  86. }
  87. }
  88. }