2020-08-22 19:56:24 +00:00
|
|
|
#pragma warning disable CS1591
|
|
|
|
|
2018-12-27 23:27:57 +00:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Linq;
|
2019-01-13 19:25:32 +00:00
|
|
|
using MediaBrowser.Model.Entities;
|
2018-12-27 23:27:57 +00:00
|
|
|
|
|
|
|
namespace MediaBrowser.Controller.Entities
|
|
|
|
{
|
|
|
|
public static class PeopleHelper
|
|
|
|
{
|
|
|
|
public static void AddPerson(List<PersonInfo> people, PersonInfo person)
|
|
|
|
{
|
2022-10-06 18:21:23 +00:00
|
|
|
ArgumentNullException.ThrowIfNull(person);
|
2018-12-27 23:27:57 +00:00
|
|
|
|
|
|
|
if (string.IsNullOrEmpty(person.Name))
|
|
|
|
{
|
2019-01-12 20:41:08 +00:00
|
|
|
throw new ArgumentException("The person's name was empty or null.", nameof(person));
|
2018-12-27 23:27:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Normalize
|
|
|
|
if (string.Equals(person.Role, PersonType.GuestStar, StringComparison.OrdinalIgnoreCase))
|
|
|
|
{
|
|
|
|
person.Type = PersonType.GuestStar;
|
|
|
|
}
|
|
|
|
else if (string.Equals(person.Role, PersonType.Director, StringComparison.OrdinalIgnoreCase))
|
|
|
|
{
|
|
|
|
person.Type = PersonType.Director;
|
|
|
|
}
|
|
|
|
else if (string.Equals(person.Role, PersonType.Producer, StringComparison.OrdinalIgnoreCase))
|
|
|
|
{
|
|
|
|
person.Type = PersonType.Producer;
|
|
|
|
}
|
|
|
|
else if (string.Equals(person.Role, PersonType.Writer, StringComparison.OrdinalIgnoreCase))
|
|
|
|
{
|
|
|
|
person.Type = PersonType.Writer;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the type is GuestStar and there's already an Actor entry, then update it to avoid dupes
|
|
|
|
if (string.Equals(person.Type, PersonType.GuestStar, StringComparison.OrdinalIgnoreCase))
|
|
|
|
{
|
|
|
|
var existing = people.FirstOrDefault(p => p.Name.Equals(person.Name, StringComparison.OrdinalIgnoreCase) && p.Type.Equals(PersonType.Actor, StringComparison.OrdinalIgnoreCase));
|
|
|
|
|
2022-12-05 14:01:13 +00:00
|
|
|
if (existing is not null)
|
2018-12-27 23:27:57 +00:00
|
|
|
{
|
|
|
|
existing.Type = PersonType.GuestStar;
|
|
|
|
MergeExisting(existing, person);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (string.Equals(person.Type, PersonType.Actor, StringComparison.OrdinalIgnoreCase))
|
|
|
|
{
|
|
|
|
// If the actor already exists without a role and we have one, fill it in
|
|
|
|
var existing = people.FirstOrDefault(p => p.Name.Equals(person.Name, StringComparison.OrdinalIgnoreCase) && (p.Type.Equals(PersonType.Actor, StringComparison.OrdinalIgnoreCase) || p.Type.Equals(PersonType.GuestStar, StringComparison.OrdinalIgnoreCase)));
|
2022-12-05 14:00:20 +00:00
|
|
|
if (existing is null)
|
2018-12-27 23:27:57 +00:00
|
|
|
{
|
|
|
|
// Wasn't there - add it
|
|
|
|
people.Add(person);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Was there, if no role and we have one - fill it in
|
|
|
|
if (string.IsNullOrEmpty(existing.Role) && !string.IsNullOrEmpty(person.Role))
|
|
|
|
{
|
|
|
|
existing.Role = person.Role;
|
|
|
|
}
|
|
|
|
|
|
|
|
MergeExisting(existing, person);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
var existing = people.FirstOrDefault(p =>
|
|
|
|
string.Equals(p.Name, person.Name, StringComparison.OrdinalIgnoreCase) &&
|
|
|
|
string.Equals(p.Type, person.Type, StringComparison.OrdinalIgnoreCase));
|
|
|
|
|
|
|
|
// Check for dupes based on the combination of Name and Type
|
2022-12-05 14:00:20 +00:00
|
|
|
if (existing is null)
|
2018-12-27 23:27:57 +00:00
|
|
|
{
|
|
|
|
people.Add(person);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
MergeExisting(existing, person);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private static void MergeExisting(PersonInfo existing, PersonInfo person)
|
|
|
|
{
|
|
|
|
existing.SortOrder = person.SortOrder ?? existing.SortOrder;
|
|
|
|
existing.ImageUrl = person.ImageUrl ?? existing.ImageUrl;
|
|
|
|
|
|
|
|
foreach (var id in person.ProviderIds)
|
|
|
|
{
|
|
|
|
existing.SetProviderId(id.Key, id.Value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|