Fix list filtering and matching issues

This commit is contained in:
Noi 2022-07-27 18:03:12 -07:00
parent 584a55cd60
commit 8e05a00136
2 changed files with 23 additions and 19 deletions

View file

@ -78,7 +78,7 @@ public class EntityList : IEnumerable<EntityName> {
foreach (var entry in this) {
if (entry.Type == EntityType.Role) {
if (entry.Id.HasValue) {
return authorRoles.Any(r => r.Id == entry.Id.Value);
if (authorRoles.Any(r => r.Id == entry.Id.Value)) return true;
} else {
foreach (var r in authorRoles) {
if (!string.Equals(r.Name, entry.Name, StringComparison.OrdinalIgnoreCase)) break;
@ -88,16 +88,15 @@ public class EntityList : IEnumerable<EntityName> {
}
} else if (entry.Type == EntityType.Channel) {
if (entry.Id.HasValue) {
return entry.Id.Value == channel.Id;
if (entry.Id.Value == channel.Id) return true;
} else {
if (!string.Equals(channel.Name, entry.Name, StringComparison.OrdinalIgnoreCase)) break;
if (keepId) entry.SetId(channel.Id);
return true;
}
} else // User
{
} else { // User
if (entry.Id.HasValue) {
return entry.Id.Value == author.Id;
if (entry.Id.Value == author.Id) return true;
} else {
if (!string.Equals(author.Username, entry.Name, StringComparison.OrdinalIgnoreCase)) break;
if (keepId) entry.SetId(author.Id);

View file

@ -49,22 +49,23 @@ public class FilterList {
public FilterList(JObject config, string whitelistKey = "Whitelist", string blacklistKey = "Blacklist", string exemptKey = "Exempt") {
if (whitelistKey != null && config[whitelistKey] != null &&
blacklistKey != null && config[blacklistKey] != null) {
// User has defined both keys at once, which is not allowed
throw new FormatException($"Cannot have both '{whitelistKey}' and '{blacklistKey}' defined at once.");
}
JToken? valueSrc = null;
JToken? incoming = null;
if (whitelistKey != null) {
// Try getting a whitelist
valueSrc = config[whitelistKey];
incoming = config[whitelistKey];
Mode = FilterMode.Whitelist;
}
if (valueSrc != null && blacklistKey != null) {
if (incoming == null && blacklistKey != null) {
// Try getting a blacklist
valueSrc = config[blacklistKey];
incoming = config[blacklistKey];
Mode = FilterMode.Blacklist;
}
if (valueSrc == null) {
if (incoming == null) {
// Got neither. Have an empty list.
Mode = FilterMode.None;
FilteredList = new EntityList();
@ -72,18 +73,22 @@ public class FilterList {
return;
}
// Verify that the specified array is actually an array.
if (valueSrc != null && valueSrc.Type != JTokenType.Array)
throw new ArgumentException("Given list must be a JSON array.");
FilteredList = new EntityList((JArray)valueSrc!, true);
if (incoming.Type != JTokenType.Array)
throw new FormatException("Filtering list must be a JSON array.");
FilteredList = new EntityList((JArray)incoming, true);
// Verify the same for the exemption list.
FilterExemptions = new EntityList();
if (exemptKey != null) {
var exc = config[exemptKey];
if (exc != null && exc.Type == JTokenType.Array) {
FilterExemptions = new EntityList(exc, true);
var incomingEx = config[exemptKey];
if (incomingEx == null) {
FilterExemptions = new EntityList();
} else if (incomingEx.Type != JTokenType.Array) {
throw new FormatException("Filtering exemption list must be a JSON array.");
} else {
FilterExemptions = new EntityList(incomingEx, true);
}
} else {
FilterExemptions = new EntityList();
}
}