From 9123fc138c0b5b3501d7eae0993794e6dd2c500d Mon Sep 17 00:00:00 2001 From: da3dsoul Date: Thu, 7 Dec 2023 11:05:48 -0500 Subject: [PATCH] Filters: Hopefully Fix Group Name Filter Legacy Mapping --- .../Legacy/LegacyConditionConverter.cs | 25 ++++++++++- Shoko.Server/Filters/Legacy/LegacyMappings.cs | 44 ++++++++++++++----- 2 files changed, 57 insertions(+), 12 deletions(-) diff --git a/Shoko.Server/Filters/Legacy/LegacyConditionConverter.cs b/Shoko.Server/Filters/Legacy/LegacyConditionConverter.cs index c39738ea2..8a1f6bf04 100644 --- a/Shoko.Server/Filters/Legacy/LegacyConditionConverter.cs +++ b/Shoko.Server/Filters/Legacy/LegacyConditionConverter.cs @@ -10,7 +10,6 @@ using Shoko.Server.Filters.Logic.DateTimes; using Shoko.Server.Filters.Logic.Expressions; using Shoko.Server.Filters.Logic.Numbers; -using Shoko.Server.Filters.Selectors; using Shoko.Server.Filters.Selectors.DateSelectors; using Shoko.Server.Filters.Selectors.NumberSelectors; using Shoko.Server.Filters.SortingSelectors; @@ -68,12 +67,12 @@ public static bool TryConvertToConditions(FilterPreset filter, out List(FilterExpression expression, List conditions) where T : IWithExpressionParameter, IWithSecondExpressionParameter { // Do this first, as compound expressions can throw off the following logic @@ -87,6 +86,28 @@ private static bool TryGetConditionsRecursive(FilterExpression expression, Li return false; } + private static bool TryGetGroupCondition(FilterExpression expression, out GroupFilterCondition condition) + { + var conditionOperator = GroupFilterOperator.Equals; + if (expression is NotExpression not) + { + conditionOperator = GroupFilterOperator.Exclude; + expression = not.Left; + } + + if (expression is not HasNameExpression nameExpression) + { + condition = null; + return false; + } + + condition = new GroupFilterCondition + { + ConditionType = (int)GroupFilterConditionType.AnimeGroup, ConditionOperator = (int)conditionOperator, ConditionParameter = nameExpression.Parameter + }; + return true; + } + private static bool TryGetIncludeCondition(FilterExpression expression, out GroupFilterCondition condition) { var conditionOperator = GroupFilterOperator.Include; diff --git a/Shoko.Server/Filters/Legacy/LegacyMappings.cs b/Shoko.Server/Filters/Legacy/LegacyMappings.cs index b89bf5d20..72685f839 100644 --- a/Shoko.Server/Filters/Legacy/LegacyMappings.cs +++ b/Shoko.Server/Filters/Legacy/LegacyMappings.cs @@ -4,14 +4,13 @@ using Shoko.Server.Filters.Files; using Shoko.Server.Filters.Functions; using Shoko.Server.Filters.Info; -using Shoko.Server.Filters.Logic; using Shoko.Server.Filters.Logic.DateTimes; using Shoko.Server.Filters.Logic.Expressions; using Shoko.Server.Filters.Logic.Numbers; -using Shoko.Server.Filters.Selectors; using Shoko.Server.Filters.Selectors.DateSelectors; using Shoko.Server.Filters.Selectors.NumberSelectors; using Shoko.Server.Filters.User; +using Shoko.Server.Repositories; namespace Shoko.Server.Filters.Legacy; @@ -309,27 +308,52 @@ public static FilterExpression GetAnimeTypeExpression(GroupFilterOperator public static FilterExpression GetGroupExpression(GroupFilterOperator op, string parameter, bool suppressErrors = false) { if (string.IsNullOrEmpty(parameter)) return suppressErrors ? null : throw new ArgumentNullException(nameof(parameter)); - var tags = parameter.Split(new[] + var groups = parameter.Split(new[] { '|', ',' - }, StringSplitOptions.TrimEntries | StringSplitOptions.RemoveEmptyEntries); + }, StringSplitOptions.TrimEntries | StringSplitOptions.RemoveEmptyEntries).Select(a => + { + if (!int.TryParse(a, out var groupID)) + { + if (RepoFactory.AnimeGroup.GetAll().Any(b => b.GroupName.Equals(a, StringComparison.InvariantCultureIgnoreCase))) return a; + throw new ArgumentOutOfRangeException(nameof(op), $@"ID {a} not found for Group"); + } + var group = RepoFactory.AnimeGroup.GetByID(groupID); + if (group == null) throw new ArgumentOutOfRangeException(nameof(op), $@"ID {a} not found for Group"); + return group.GroupName; + }).ToArray(); switch (op) { case GroupFilterOperator.In: case GroupFilterOperator.Include: { - if (tags.Length <= 1) return new HasNameExpression(tags[0]); + if (groups.Length <= 1) return new HasNameExpression(groups[0]); - FilterExpression first = new HasNameExpression(tags[0]); - return tags.Skip(1).Aggregate(first, (a, b) => new OrExpression(a, new HasNameExpression(b))); + FilterExpression first = new HasNameExpression(groups[0]); + return groups.Skip(1).Aggregate(first, (a, b) => new OrExpression(a, new HasNameExpression(b))); } case GroupFilterOperator.NotIn: case GroupFilterOperator.Exclude: { - if (tags.Length <= 1) return new NotExpression(new HasNameExpression(tags[0])); + if (groups.Length <= 1) return new NotExpression(new HasNameExpression(groups[0])); + + FilterExpression first = new HasNameExpression(groups[0]); + return new NotExpression(groups.Skip(1).Aggregate(first, (a, b) => new OrExpression(a, new HasNameExpression(b)))); + } + case GroupFilterOperator.Equals: + { + if (groups.Length > 1) throw new ArgumentOutOfRangeException(nameof(op), $@"ConditionOperator {op} not applicable for Group Name and more than one value"); + return new HasNameExpression(groups[0]); + } + case GroupFilterOperator.NotEquals: + { + if (groups.Length > 1) throw new ArgumentOutOfRangeException(nameof(op), $@"ConditionOperator {op} not applicable for Group Name and more than one value"); + + if (!int.TryParse(groups[0], out var groupID)) throw new ArgumentOutOfRangeException(nameof(op), $@"ID {groups[0]} not found for Group"); + var group = RepoFactory.AnimeGroup.GetByID(groupID); + if (group == null) throw new ArgumentOutOfRangeException(nameof(op), $@"ID {groups[0]} not found for Group"); - FilterExpression first = new HasNameExpression(tags[0]); - return new NotExpression(tags.Skip(1).Aggregate(first, (a, b) => new OrExpression(a, new HasNameExpression(b)))); + return new NotExpression(new HasNameExpression(groups[0])); } default: return suppressErrors ? null : throw new ArgumentOutOfRangeException(nameof(op), $@"ConditionOperator {op} not applicable for Group Name");