From 3cf326348d177c22499e136aa0a645e86735974e Mon Sep 17 00:00:00 2001 From: Suleman Date: Fri, 8 Mar 2024 00:06:59 +0000 Subject: [PATCH 1/2] Added Logging for disabled features --- .../tjbot/config/FeatureBlacklist.java | 26 +++++++++++++++++++ .../togetherjava/tjbot/features/Features.java | 2 +- .../features/code/CodeMessageHandler.java | 8 +++--- 3 files changed, 31 insertions(+), 5 deletions(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/config/FeatureBlacklist.java b/application/src/main/java/org/togetherjava/tjbot/config/FeatureBlacklist.java index be69163841..6e4efd00ba 100644 --- a/application/src/main/java/org/togetherjava/tjbot/config/FeatureBlacklist.java +++ b/application/src/main/java/org/togetherjava/tjbot/config/FeatureBlacklist.java @@ -1,8 +1,12 @@ package org.togetherjava.tjbot.config; import com.fasterxml.jackson.annotation.JsonCreator; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import java.util.Set; +import java.util.function.Function; +import java.util.stream.Stream; /** * Blacklist of features, use {@link FeatureBlacklist#isEnabled(T)} to test if a feature is enabled. @@ -13,6 +17,8 @@ public class FeatureBlacklist { private final Set featureIdentifierBlacklist; + private static final Logger logger = LoggerFactory.getLogger(FeatureBlacklist.class); + /** * Creates a feature blacklist * @@ -32,4 +38,24 @@ public FeatureBlacklist(Set featureIdentifierBlacklist) { public boolean isEnabled(T featureId) { return !featureIdentifierBlacklist.contains(featureId); } + + /** + * Filters features stream to only having enabled features and logs any disabled features + * + * @param features the feature stream to be filtered + * @param idExtractor function to get the class name of an object + * @return stream of features that are enabled + */ + public Stream disableMatching(Stream features, Function idExtractor) { + return features.filter(f -> { + T id = idExtractor.apply(f); + if (!this.isEnabled(id)) { + logger.info("Feature {} is disabled", id); + return false; + } + return true; + }); + } + + } diff --git a/application/src/main/java/org/togetherjava/tjbot/features/Features.java b/application/src/main/java/org/togetherjava/tjbot/features/Features.java index 7dbc19d447..6b7ee702fe 100644 --- a/application/src/main/java/org/togetherjava/tjbot/features/Features.java +++ b/application/src/main/java/org/togetherjava/tjbot/features/Features.java @@ -166,6 +166,6 @@ public static Collection createFeatures(JDA jda, Database database, Con features.add(new JShellCommand(jshellEval)); FeatureBlacklist> blacklist = blacklistConfig.normal(); - return features.stream().filter(f -> blacklist.isEnabled(f.getClass())).toList(); + return blacklist.disableMatching(features.stream(), Object::getClass).toList(); } } diff --git a/application/src/main/java/org/togetherjava/tjbot/features/code/CodeMessageHandler.java b/application/src/main/java/org/togetherjava/tjbot/features/code/CodeMessageHandler.java index 916cef8bb6..8dd8d4527a 100644 --- a/application/src/main/java/org/togetherjava/tjbot/features/code/CodeMessageHandler.java +++ b/application/src/main/java/org/togetherjava/tjbot/features/code/CodeMessageHandler.java @@ -73,10 +73,10 @@ public final class CodeMessageHandler extends MessageReceiverAdapter implements public CodeMessageHandler(FeatureBlacklist blacklist, JShellEval jshellEval) { componentIdInteractor = new ComponentIdInteractor(getInteractionType(), getName()); - List codeActions = - Stream.of(new FormatCodeCommand(), new EvalCodeCommand(jshellEval)) - .filter(a -> blacklist.isEnabled(a.getClass().getSimpleName())) - .toList(); + List codeActions = blacklist + .disableMatching(Stream.of(new FormatCodeCommand(), new EvalCodeCommand(jshellEval)), + e -> e.getClass().getName()) + .toList(); labelToCodeAction = codeActions.stream() .collect(Collectors.toMap(CodeAction::getLabel, Function.identity(), (x, y) -> y, From 94cffc2e541bebea46a3f6e148835467e16019bb Mon Sep 17 00:00:00 2001 From: Suleman Date: Fri, 8 Mar 2024 11:25:36 +0000 Subject: [PATCH 2/2] Added @christolis change requests --- .../org/togetherjava/tjbot/config/FeatureBlacklist.java | 5 +++-- .../main/java/org/togetherjava/tjbot/features/Features.java | 2 +- .../tjbot/features/code/CodeMessageHandler.java | 6 +++--- 3 files changed, 7 insertions(+), 6 deletions(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/config/FeatureBlacklist.java b/application/src/main/java/org/togetherjava/tjbot/config/FeatureBlacklist.java index 6e4efd00ba..cca383ed19 100644 --- a/application/src/main/java/org/togetherjava/tjbot/config/FeatureBlacklist.java +++ b/application/src/main/java/org/togetherjava/tjbot/config/FeatureBlacklist.java @@ -40,13 +40,14 @@ public boolean isEnabled(T featureId) { } /** - * Filters features stream to only having enabled features and logs any disabled features + * Filters features stream to only having enabled features and logs any disabled features. * * @param features the feature stream to be filtered * @param idExtractor function to get the class name of an object * @return stream of features that are enabled */ - public Stream disableMatching(Stream features, Function idExtractor) { + public Stream filterStream(Stream features, + Function idExtractor) { return features.filter(f -> { T id = idExtractor.apply(f); if (!this.isEnabled(id)) { diff --git a/application/src/main/java/org/togetherjava/tjbot/features/Features.java b/application/src/main/java/org/togetherjava/tjbot/features/Features.java index 6b7ee702fe..c3ed8ee19e 100644 --- a/application/src/main/java/org/togetherjava/tjbot/features/Features.java +++ b/application/src/main/java/org/togetherjava/tjbot/features/Features.java @@ -166,6 +166,6 @@ public static Collection createFeatures(JDA jda, Database database, Con features.add(new JShellCommand(jshellEval)); FeatureBlacklist> blacklist = blacklistConfig.normal(); - return blacklist.disableMatching(features.stream(), Object::getClass).toList(); + return blacklist.filterStream(features.stream(), Object::getClass).toList(); } } diff --git a/application/src/main/java/org/togetherjava/tjbot/features/code/CodeMessageHandler.java b/application/src/main/java/org/togetherjava/tjbot/features/code/CodeMessageHandler.java index 8dd8d4527a..7927a2b035 100644 --- a/application/src/main/java/org/togetherjava/tjbot/features/code/CodeMessageHandler.java +++ b/application/src/main/java/org/togetherjava/tjbot/features/code/CodeMessageHandler.java @@ -74,9 +74,9 @@ public CodeMessageHandler(FeatureBlacklist blacklist, JShellEval jshellE componentIdInteractor = new ComponentIdInteractor(getInteractionType(), getName()); List codeActions = blacklist - .disableMatching(Stream.of(new FormatCodeCommand(), new EvalCodeCommand(jshellEval)), - e -> e.getClass().getName()) - .toList(); + .filterStream(Stream.of(new FormatCodeCommand(), new EvalCodeCommand(jshellEval)), + codeAction -> codeAction.getClass().getName()) + .toList(); labelToCodeAction = codeActions.stream() .collect(Collectors.toMap(CodeAction::getLabel, Function.identity(), (x, y) -> y,