jellyfin/MediaBrowser.Controller/Entities/PeopleHelper.cs

99 lines
3.5 KiB
C#
Raw Normal View History

#pragma warning disable CS1591
2018-12-27 16:27:57 -07:00
using System;
using System.Collections.Generic;
using System.Linq;
using Jellyfin.Data.Enums;
using MediaBrowser.Model.Entities;
2018-12-27 16:27:57 -07:00
namespace MediaBrowser.Controller.Entities
{
public static class PeopleHelper
{
public static void AddPerson(List<PersonInfo> people, PersonInfo person)
{
ArgumentNullException.ThrowIfNull(person);
ArgumentException.ThrowIfNullOrEmpty(person.Name);
2018-12-27 16:27:57 -07:00
// Normalize
if (string.Equals(person.Role, PersonType.GuestStar, StringComparison.OrdinalIgnoreCase))
{
person.Type = PersonKind.GuestStar;
2018-12-27 16:27:57 -07:00
}
else if (string.Equals(person.Role, PersonType.Director, StringComparison.OrdinalIgnoreCase))
{
person.Type = PersonKind.Director;
2018-12-27 16:27:57 -07:00
}
else if (string.Equals(person.Role, PersonType.Producer, StringComparison.OrdinalIgnoreCase))
{
person.Type = PersonKind.Producer;
2018-12-27 16:27:57 -07:00
}
else if (string.Equals(person.Role, PersonType.Writer, StringComparison.OrdinalIgnoreCase))
{
person.Type = PersonKind.Writer;
2018-12-27 16:27:57 -07:00
}
// If the type is GuestStar and there's already an Actor entry, then update it to avoid dupes
if (person.Type == PersonKind.GuestStar)
2018-12-27 16:27:57 -07:00
{
var existing = people.FirstOrDefault(p => p.Name.Equals(person.Name, StringComparison.OrdinalIgnoreCase) && p.Type == PersonKind.Actor);
2018-12-27 16:27:57 -07:00
2022-12-05 07:01:13 -07:00
if (existing is not null)
2018-12-27 16:27:57 -07:00
{
existing.Type = PersonKind.GuestStar;
2018-12-27 16:27:57 -07:00
MergeExisting(existing, person);
return;
}
}
if (person.Type == PersonKind.Actor)
2018-12-27 16:27:57 -07:00
{
// 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 == PersonKind.Actor || p.Type == PersonKind.GuestStar));
2022-12-05 07:00:20 -07:00
if (existing is null)
2018-12-27 16:27:57 -07: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)
&& p.Type == person.Type);
2018-12-27 16:27:57 -07:00
// Check for dupes based on the combination of Name and Type
2022-12-05 07:00:20 -07:00
if (existing is null)
2018-12-27 16:27:57 -07: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);
}
}
}
}