mirror of
				https://github.com/Kareadita/Kavita.git
				synced 2025-11-03 19:17:05 -05:00 
			
		
		
		
	* Refactored invite user flow to separate error handling on create user flow and email flow. This should help users that have unique situations. * Switch to using files to check LastWriteTime. Debug code in for Robbie to test on rclone * Updated Parser namespace. Changed the LastWriteTime to check all files and folders.
		
			
				
	
	
		
			102 lines
		
	
	
		
			3.6 KiB
		
	
	
	
		
			C#
		
	
	
	
	
	
			
		
		
	
	
			102 lines
		
	
	
		
			3.6 KiB
		
	
	
	
		
			C#
		
	
	
	
	
	
using System;
 | 
						|
using System.Collections.Concurrent;
 | 
						|
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 = Services.Tasks.Scanner.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 == Services.Tasks.Scanner.Parser.Parser.Normalize(tag.Title));
 | 
						|
        if (existingGenre == null)
 | 
						|
        {
 | 
						|
            metadataTags.Add(tag);
 | 
						|
        }
 | 
						|
    }
 | 
						|
 | 
						|
    public static void AddTagIfNotExists(BlockingCollection<Tag> metadataTags, Tag tag)
 | 
						|
    {
 | 
						|
        var existingGenre = metadataTags.FirstOrDefault(p =>
 | 
						|
            p.NormalizedTitle == Services.Tasks.Scanner.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(Services.Tasks.Scanner.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);
 | 
						|
        }
 | 
						|
 | 
						|
    }
 | 
						|
}
 | 
						|
 |