Skip to content

Commit

Permalink
Filters: Hopefully Fix Group Name Filter Legacy Mapping
Browse files Browse the repository at this point in the history
  • Loading branch information
da3dsoul committed Dec 7, 2023
1 parent a286b58 commit 9123fc1
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 12 deletions.
25 changes: 23 additions & 2 deletions Shoko.Server/Filters/Legacy/LegacyConditionConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -68,12 +67,12 @@ public static bool TryConvertToConditions(FilterPreset filter, out List<GroupFil

private static bool TryGetSingleCondition(FilterExpression expression, out GroupFilterCondition condition)
{
if (TryGetGroupCondition(expression, out condition)) return true;
if (TryGetIncludeCondition(expression, out condition)) return true;
if (TryGetInCondition(expression, out condition)) return true;
return TryGetComparatorCondition(expression, out condition);
}


private static bool TryGetConditionsRecursive<T>(FilterExpression expression, List<GroupFilterCondition> conditions) where T : IWithExpressionParameter, IWithSecondExpressionParameter
{
// Do this first, as compound expressions can throw off the following logic
Expand All @@ -87,6 +86,28 @@ private static bool TryGetConditionsRecursive<T>(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;
Expand Down
44 changes: 34 additions & 10 deletions Shoko.Server/Filters/Legacy/LegacyMappings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -309,27 +308,52 @@ public static FilterExpression<bool> GetAnimeTypeExpression(GroupFilterOperator
public static FilterExpression<bool> 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<bool> first = new HasNameExpression(tags[0]);
return tags.Skip(1).Aggregate(first, (a, b) => new OrExpression(a, new HasNameExpression(b)));
FilterExpression<bool> 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<bool> 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<bool> 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");
Expand Down

0 comments on commit 9123fc1

Please sign in to comment.