From 7dddedd7e0a70c59d8891875c482931afa28b4cc Mon Sep 17 00:00:00 2001 From: Alathreon Date: Thu, 21 Sep 2023 20:23:12 +0200 Subject: [PATCH] [FeatureBlacklist] Done --- application/config.json.template | 6 ++++ .../org/togetherjava/tjbot/config/Config.java | 15 +++++++- .../tjbot/config/FeatureBlacklist.java | 35 +++++++++++++++++++ .../tjbot/config/FeatureBlacklistConfig.java | 28 +++++++++++++++ .../togetherjava/tjbot/features/Features.java | 13 +++++-- .../features/code/CodeMessageHandler.java | 12 +++++-- 6 files changed, 102 insertions(+), 7 deletions(-) create mode 100644 application/src/main/java/org/togetherjava/tjbot/config/FeatureBlacklist.java create mode 100644 application/src/main/java/org/togetherjava/tjbot/config/FeatureBlacklistConfig.java diff --git a/application/config.json.template b/application/config.json.template index 5391037110..56a02ecc39 100644 --- a/application/config.json.template +++ b/application/config.json.template @@ -101,5 +101,11 @@ "pruneMemberAmount": 7, "inactivateAfterDays": 90, "recentlyJoinedDays": 4 + }, + "feature_blacklist": { + "normal": [ + ], + "special": [ + ] } } diff --git a/application/src/main/java/org/togetherjava/tjbot/config/Config.java b/application/src/main/java/org/togetherjava/tjbot/config/Config.java index 4697573b06..161bfe75fd 100644 --- a/application/src/main/java/org/togetherjava/tjbot/config/Config.java +++ b/application/src/main/java/org/togetherjava/tjbot/config/Config.java @@ -40,6 +40,7 @@ public final class Config { private final String sourceCodeBaseUrl; private final JShellConfig jshell; private final HelperPruneConfig helperPruneConfig; + private final FeatureBlacklistConfig featureBlacklistConfig; @SuppressWarnings("ConstructorWithTooManyParameters") @JsonCreator(mode = JsonCreator.Mode.PROPERTIES) @@ -79,7 +80,9 @@ private Config(@JsonProperty(value = "token", required = true) String token, @JsonProperty(value = "sourceCodeBaseUrl", required = true) String sourceCodeBaseUrl, @JsonProperty(value = "jshell", required = true) JShellConfig jshell, @JsonProperty(value = "helperPruneConfig", - required = true) HelperPruneConfig helperPruneConfig) { + required = true) HelperPruneConfig helperPruneConfig, + @JsonProperty(value = "featureBlacklist", + required = true) FeatureBlacklistConfig featureBlacklistConfig) { this.token = Objects.requireNonNull(token); this.gistApiKey = Objects.requireNonNull(gistApiKey); this.databasePath = Objects.requireNonNull(databasePath); @@ -106,6 +109,7 @@ private Config(@JsonProperty(value = "token", required = true) String token, this.sourceCodeBaseUrl = Objects.requireNonNull(sourceCodeBaseUrl); this.jshell = Objects.requireNonNull(jshell); this.helperPruneConfig = Objects.requireNonNull(helperPruneConfig); + this.featureBlacklistConfig = Objects.requireNonNull(featureBlacklistConfig); } /** @@ -355,4 +359,13 @@ public JShellConfig getJshell() { public HelperPruneConfig getHelperPruneConfig() { return helperPruneConfig; } + + /** + * The configuration of blacklisted features. + * + * @return configuration of blacklisted features + */ + public FeatureBlacklistConfig getFeatureBlacklistConfig() { + return featureBlacklistConfig; + } } diff --git a/application/src/main/java/org/togetherjava/tjbot/config/FeatureBlacklist.java b/application/src/main/java/org/togetherjava/tjbot/config/FeatureBlacklist.java new file mode 100644 index 0000000000..be69163841 --- /dev/null +++ b/application/src/main/java/org/togetherjava/tjbot/config/FeatureBlacklist.java @@ -0,0 +1,35 @@ +package org.togetherjava.tjbot.config; + +import com.fasterxml.jackson.annotation.JsonCreator; + +import java.util.Set; + +/** + * Blacklist of features, use {@link FeatureBlacklist#isEnabled(T)} to test if a feature is enabled. + * If a feature is blacklisted, it won't be enabled by the bot, and so will be ignored. + * + * @param the type of the feature identifier + */ +public class FeatureBlacklist { + private final Set featureIdentifierBlacklist; + + /** + * Creates a feature blacklist + * + * @param featureIdentifierBlacklist a set of identifiers which are blacklisted + */ + @JsonCreator + public FeatureBlacklist(Set featureIdentifierBlacklist) { + this.featureIdentifierBlacklist = Set.copyOf(featureIdentifierBlacklist); + } + + /** + * Returns if a feature is enabled or not. + * + * @param featureId the identifier of the feature + * @return true if a feature is enabled, false otherwise + */ + public boolean isEnabled(T featureId) { + return !featureIdentifierBlacklist.contains(featureId); + } +} diff --git a/application/src/main/java/org/togetherjava/tjbot/config/FeatureBlacklistConfig.java b/application/src/main/java/org/togetherjava/tjbot/config/FeatureBlacklistConfig.java new file mode 100644 index 0000000000..231cbdb4f8 --- /dev/null +++ b/application/src/main/java/org/togetherjava/tjbot/config/FeatureBlacklistConfig.java @@ -0,0 +1,28 @@ +package org.togetherjava.tjbot.config; + +import com.fasterxml.jackson.annotation.JsonProperty; + +import java.util.Objects; + +/** + * Configuration of the feature blacklist, any feature present here will be disabled. + * + * @param normal the normal features, which are present in + * {@link org.togetherjava.tjbot.features.Features} + * @param special the special features, which require special code + */ +public record FeatureBlacklistConfig( + @JsonProperty(value = "normal", required = true) FeatureBlacklist> normal, + @JsonProperty(value = "special", required = true) FeatureBlacklist special) { + + /** + * Creates a FeatureBlacklistConfig. + * + * @param normal the list of normal features, must be not null + * @param special the list of special features, must be not null + */ + public FeatureBlacklistConfig { + Objects.requireNonNull(normal); + Objects.requireNonNull(special); + } +} 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 e5186ad087..b98fc75d12 100644 --- a/application/src/main/java/org/togetherjava/tjbot/features/Features.java +++ b/application/src/main/java/org/togetherjava/tjbot/features/Features.java @@ -3,6 +3,8 @@ import net.dv8tion.jda.api.JDA; import org.togetherjava.tjbot.config.Config; +import org.togetherjava.tjbot.config.FeatureBlacklist; +import org.togetherjava.tjbot.config.FeatureBlacklistConfig; import org.togetherjava.tjbot.db.Database; import org.togetherjava.tjbot.features.basic.PingCommand; import org.togetherjava.tjbot.features.basic.RoleSelectCommand; @@ -19,6 +21,7 @@ import org.togetherjava.tjbot.features.code.CodeMessageManualDetection; import org.togetherjava.tjbot.features.filesharing.FileSharingMessageListener; import org.togetherjava.tjbot.features.help.*; +import org.togetherjava.tjbot.features.jshell.JShellCommand; import org.togetherjava.tjbot.features.jshell.JShellEval; import org.togetherjava.tjbot.features.mathcommands.TeXCommand; import org.togetherjava.tjbot.features.mathcommands.wolframalpha.WolframAlphaCommand; @@ -73,6 +76,7 @@ private Features() { * @return a collection of all features */ public static Collection createFeatures(JDA jda, Database database, Config config) { + FeatureBlacklistConfig blacklistConfig = config.getFeatureBlacklistConfig(); JShellEval jshellEval = new JShellEval(config.getJshell()); TagSystem tagSystem = new TagSystem(database); @@ -80,7 +84,8 @@ public static Collection createFeatures(JDA jda, Database database, Con ModerationActionsStore actionsStore = new ModerationActionsStore(database); ModAuditLogWriter modAuditLogWriter = new ModAuditLogWriter(config); ScamHistoryStore scamHistoryStore = new ScamHistoryStore(database); - CodeMessageHandler codeMessageHandler = new CodeMessageHandler(jshellEval); + CodeMessageHandler codeMessageHandler = + new CodeMessageHandler(blacklistConfig.special(), jshellEval); ChatGptService chatGptService = new ChatGptService(config); HelpSystemHelper helpSystemHelper = new HelpSystemHelper(config, database, chatGptService); @@ -151,7 +156,9 @@ public static Collection createFeatures(JDA jda, Database database, Con features.add(new ReportCommand(config)); features.add(new BookmarksCommand(bookmarksSystem)); features.add(new ChatGptCommand(chatGptService)); - // features.add(new JShellCommand(jshellEval)); - return features; + features.add(new JShellCommand(jshellEval)); + + FeatureBlacklist> blacklist = blacklistConfig.normal(); + return features.stream().filter(f -> blacklist.isEnabled(f.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 f4d27e85d5..916cef8bb6 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 @@ -15,6 +15,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.togetherjava.tjbot.config.FeatureBlacklist; import org.togetherjava.tjbot.features.MessageReceiverAdapter; import org.togetherjava.tjbot.features.UserInteractionType; import org.togetherjava.tjbot.features.UserInteractor; @@ -30,6 +31,7 @@ import java.util.*; import java.util.function.Function; import java.util.stream.Collectors; +import java.util.stream.Stream; /** * Handles code in registered messages and offers code actions to the user, such as formatting their @@ -63,14 +65,18 @@ public final class CodeMessageHandler extends MessageReceiverAdapter implements /** * Creates a new instance. - * + * + * @param blacklist the feature blacklist, used to test if certain code actions should be + * disabled * @param jshellEval used to execute java code and build visual result */ - public CodeMessageHandler(JShellEval jshellEval) { + public CodeMessageHandler(FeatureBlacklist blacklist, JShellEval jshellEval) { componentIdInteractor = new ComponentIdInteractor(getInteractionType(), getName()); List codeActions = - List.of(new FormatCodeCommand()/* , new EvalCodeCommand(jshellEval) */); + Stream.of(new FormatCodeCommand(), new EvalCodeCommand(jshellEval)) + .filter(a -> blacklist.isEnabled(a.getClass().getSimpleName())) + .toList(); labelToCodeAction = codeActions.stream() .collect(Collectors.toMap(CodeAction::getLabel, Function.identity(), (x, y) -> y,