Kavita/API/Helpers/TagHelper.cs
Joseph Milazzo fb71d54fe6
Misc Fixes and Changes (#927)
* Cleaned up a ton of warnings/suggestions from the IDE.

* Fixed a bug when clearing the filters some presets could be undone.

* Renamed a class in the OPDS spec

* Simplified logic for when Fit To Screen rendering logic occurs. It now works always rather than only on cover images.

* Give some additional info to the user on what the differences between Library Types are

* Don't scan .qpkg folders (QNAP devices)

* Refactored some code to enable ability to test CoverImage Test. This is a broken test, test.zip is waiting on an issue in NetVips.

* Fixed an issue where Extra might get flagged as special too early, if in a word like Extraordinary

* Cleaned up the regex for the extra issue to be more flexible
2022-01-12 15:00:00 -08:00

91 lines
3.1 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using API.Data;
using API.Entities;
namespace API.Helpers;
public static class TagHelper
{
/// <summary>
///
/// </summary>
/// <param name="allTags"></param>
/// <param name="names"></param>
/// <param name="isExternal"></param>
/// <param name="action">Callback for every item. Will give said item back and a bool if item was added</param>
public static void UpdateTag(ICollection<Tag> allTags, IEnumerable<string> names, bool isExternal, Action<Tag, bool> action)
{
foreach (var name in names)
{
if (string.IsNullOrEmpty(name.Trim())) continue;
var added = false;
var normalizedName = Parser.Parser.Normalize(name);
var genre = allTags.FirstOrDefault(p =>
p.NormalizedTitle.Equals(normalizedName) && p.ExternalTag == isExternal);
if (genre == null)
{
added = true;
genre = DbFactory.Tag(name, false);
allTags.Add(genre);
}
action(genre, added);
}
}
public static void KeepOnlySameTagBetweenLists(ICollection<Tag> existingTags, ICollection<Tag> removeAllExcept, Action<Tag> action = null)
{
var existing = existingTags.ToList();
foreach (var genre in existing)
{
var existingPerson = removeAllExcept.FirstOrDefault(g => g.ExternalTag == genre.ExternalTag && genre.NormalizedTitle.Equals(g.NormalizedTitle));
if (existingPerson != null) continue;
existingTags.Remove(genre);
action?.Invoke(genre);
}
}
/// <summary>
/// Adds the tag to the list if it's not already in there. This will ignore the ExternalTag.
/// </summary>
/// <param name="metadataTags"></param>
/// <param name="tag"></param>
public static void AddTagIfNotExists(ICollection<Tag> metadataTags, Tag tag)
{
var existingGenre = metadataTags.FirstOrDefault(p =>
p.NormalizedTitle == Parser.Parser.Normalize(tag.Title));
if (existingGenre == null)
{
metadataTags.Add(tag);
}
}
/// <summary>
/// Remove tags on a list
/// </summary>
/// <remarks>Used to remove before we update/add new tags</remarks>
/// <param name="existingTags">Existing tags on Entity</param>
/// <param name="tags">Tags from metadata</param>
/// <param name="isExternal">Remove external tags?</param>
/// <param name="action">Callback which will be executed for each tag removed</param>
public static void RemoveTags(ICollection<Tag> existingTags, IEnumerable<string> tags, bool isExternal, Action<Tag> action = null)
{
var normalizedTags = tags.Select(Parser.Parser.Normalize).ToList();
foreach (var person in normalizedTags)
{
var existingTag = existingTags.FirstOrDefault(p => p.ExternalTag == isExternal && person.Equals(p.NormalizedTitle));
if (existingTag == null) continue;
existingTags.Remove(existingTag);
action?.Invoke(existingTag);
}
}
}