diff --git a/Shoko.Server/Filters/Logic/Strings/StringFuzzyMatchesExpression.cs b/Shoko.Server/Filters/Logic/Strings/StringFuzzyMatchesExpression.cs new file mode 100644 index 000000000..b501f35e0 --- /dev/null +++ b/Shoko.Server/Filters/Logic/Strings/StringFuzzyMatchesExpression.cs @@ -0,0 +1,86 @@ +using System; +using Shoko.Commons.Utils; +using Shoko.Server.Filters.Interfaces; + +namespace Shoko.Server.Filters.Logic.Strings; + +public class StringFuzzyMatchesExpression : FilterExpression, IWithStringSelectorParameter, IWithSecondStringSelectorParameter, IWithStringParameter +{ + public StringFuzzyMatchesExpression(FilterExpression left, FilterExpression right) + { + Left = left; + Right = right; + } + + public StringFuzzyMatchesExpression(FilterExpression left, string parameter) + { + Left = left; + Parameter = parameter; + } + + public StringFuzzyMatchesExpression() { } + + public FilterExpression Left { get; set; } + public FilterExpression Right { get; set; } + public string Parameter { get; set; } + public override bool TimeDependent => Left.TimeDependent || (Right?.TimeDependent ?? false); + public override bool UserDependent => Left.UserDependent || (Right?.UserDependent ?? false); + public override string HelpDescription => "This passes if the left selector fuzzy matches either the right selector or the parameter"; + + public override bool Evaluate(IFilterable filterable, IFilterableUserInfo userInfo) + { + var left = Left.Evaluate(filterable, userInfo); + var right = Parameter ?? Right?.Evaluate(filterable, userInfo); + if (string.IsNullOrEmpty(left) || string.IsNullOrEmpty(right)) + { + return false; + } + + return left.FuzzyMatches(right); + } + + protected bool Equals(StringFuzzyMatchesExpression other) + { + return base.Equals(other) && Equals(Left, other.Left) && Equals(Right, other.Right) && Parameter == other.Parameter; + } + + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) + { + return false; + } + + if (ReferenceEquals(this, obj)) + { + return true; + } + + if (obj.GetType() != this.GetType()) + { + return false; + } + + return Equals((StringFuzzyMatchesExpression)obj); + } + + public override int GetHashCode() + { + return HashCode.Combine(base.GetHashCode(), Left, Right, Parameter); + } + + public static bool operator ==(StringFuzzyMatchesExpression left, StringFuzzyMatchesExpression right) + { + return Equals(left, right); + } + + public static bool operator !=(StringFuzzyMatchesExpression left, StringFuzzyMatchesExpression right) + { + return !Equals(left, right); + } + + public override bool IsType(FilterExpression expression) + { + return expression is StringFuzzyMatchesExpression exp && Left.IsType(exp.Left) && (Right?.IsType(exp.Right) ?? true); + } +}