From 59c449f4d368e1699a4a8b891c0216608079d5ab Mon Sep 17 00:00:00 2001 From: Sheldan <5037282+Sheldan@users.noreply.github.com> Date: Tue, 12 May 2020 00:45:13 +0200 Subject: [PATCH] added evaluation of the allowed commands when executing help added a note that a few commands are only available within a mod mail thread --- .../core/command/CommandReceivedHandler.java | 31 +++++-------------- .../command/service/CommandServiceBean.java | 28 +++++++++++++++++ .../abstracto/core/commands/help/Help.java | 17 ++++++++++ .../config/SingleLevelPackedModule.java | 2 ++ .../core/command/service/CommandService.java | 4 +++ .../main/docs/asciidoc/features/modmail.adoc | 21 +++++++------ .../templating/methods/DurationMethod.java | 2 +- 7 files changed, 72 insertions(+), 33 deletions(-) diff --git a/abstracto-application/core/core-impl/src/main/java/dev/sheldan/abstracto/core/command/CommandReceivedHandler.java b/abstracto-application/core/core-impl/src/main/java/dev/sheldan/abstracto/core/command/CommandReceivedHandler.java index 01b3e0755..44051796f 100644 --- a/abstracto-application/core/core-impl/src/main/java/dev/sheldan/abstracto/core/command/CommandReceivedHandler.java +++ b/abstracto-application/core/core-impl/src/main/java/dev/sheldan/abstracto/core/command/CommandReceivedHandler.java @@ -1,13 +1,12 @@ package dev.sheldan.abstracto.core.command; -import dev.sheldan.abstracto.core.command.condition.CommandCondition; import dev.sheldan.abstracto.core.command.condition.ConditionResult; -import dev.sheldan.abstracto.core.command.condition.ConditionalCommand; import dev.sheldan.abstracto.core.command.config.Parameter; import dev.sheldan.abstracto.core.command.config.Parameters; import dev.sheldan.abstracto.core.command.exception.IncorrectParameter; import dev.sheldan.abstracto.core.command.exception.ParameterTooLong; import dev.sheldan.abstracto.core.command.service.CommandManager; +import dev.sheldan.abstracto.core.command.service.CommandService; import dev.sheldan.abstracto.core.command.service.PostCommandExecution; import dev.sheldan.abstracto.core.command.execution.*; import dev.sheldan.abstracto.core.command.execution.UnParsedCommandParameter; @@ -63,6 +62,9 @@ public class CommandReceivedHandler extends ListenerAdapter { @Autowired private RoleManagementService roleManagementService; + @Autowired + private CommandService commandService; + @Override @Transactional public void onMessageReceived(@Nonnull MessageReceivedEvent event) { @@ -89,17 +91,12 @@ public class CommandReceivedHandler extends ListenerAdapter { foundCommand = commandManager.findCommandByParameters(commandName, unparsedParameter); Parameters parsedParameters = getParsedParameters(unparsedParameter, foundCommand, event.getMessage(), userInitiatedContext); CommandContext commandContext = commandContextBuilder.parameters(parsedParameters).build(); + ConditionResult conditionResult = commandService.isCommandExecutable(foundCommand, commandContext); CommandResult commandResult; - if(foundCommand instanceof ConditionalCommand) { - ConditionalCommand castedCommand = (ConditionalCommand) foundCommand; - ConditionResult conditionResult = checkConditions(commandContext, foundCommand, castedCommand.getConditions()); - if(!conditionResult.isResult()) { - commandResult = CommandResult.fromCondition(conditionResult); - } else { - commandResult = self.executeCommand(foundCommand, commandContext); - } - } else { + if(conditionResult.isResult()) { commandResult = self.executeCommand(foundCommand, commandContext); + } else { + commandResult = CommandResult.fromCondition(conditionResult); } for (PostCommandExecution postCommandExecution : executions) { postCommandExecution.execute(commandContext, commandResult, foundCommand); @@ -118,18 +115,6 @@ public class CommandReceivedHandler extends ListenerAdapter { return foundCommand.execute(commandContext); } - public ConditionResult checkConditions(CommandContext commandContext, Command command, List conditions) { - if(conditions != null) { - for (CommandCondition condition : conditions) { - ConditionResult conditionResult = condition.shouldExecute(commandContext, command); - if(!conditionResult.isResult()) { - return conditionResult; - } - } - } - return ConditionResult.builder().result(true).build(); - } - private UserInitiatedServerContext buildTemplateParameter(MessageReceivedEvent event) { AChannel channel = channelManagementService.loadChannel(event.getChannel().getIdLong()); AServer server = serverManagementService.loadOrCreate(event.getGuild().getIdLong()); diff --git a/abstracto-application/core/core-impl/src/main/java/dev/sheldan/abstracto/core/command/service/CommandServiceBean.java b/abstracto-application/core/core-impl/src/main/java/dev/sheldan/abstracto/core/command/service/CommandServiceBean.java index 1cc23e15e..cd5ef9984 100644 --- a/abstracto-application/core/core-impl/src/main/java/dev/sheldan/abstracto/core/command/service/CommandServiceBean.java +++ b/abstracto-application/core/core-impl/src/main/java/dev/sheldan/abstracto/core/command/service/CommandServiceBean.java @@ -1,5 +1,10 @@ package dev.sheldan.abstracto.core.command.service; +import dev.sheldan.abstracto.core.command.Command; +import dev.sheldan.abstracto.core.command.condition.CommandCondition; +import dev.sheldan.abstracto.core.command.condition.ConditionResult; +import dev.sheldan.abstracto.core.command.condition.ConditionalCommand; +import dev.sheldan.abstracto.core.command.execution.CommandContext; import dev.sheldan.abstracto.core.command.models.database.ACommand; import dev.sheldan.abstracto.core.command.models.database.ACommandInAServer; import dev.sheldan.abstracto.core.command.models.database.AModule; @@ -15,6 +20,8 @@ import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; +import java.util.List; + @Component @Slf4j public class CommandServiceBean implements CommandService { @@ -105,5 +112,26 @@ public class CommandServiceBean implements CommandService { }); } + public ConditionResult isCommandExecutable(Command command, CommandContext commandContext) { + if(command instanceof ConditionalCommand) { + ConditionalCommand castedCommand = (ConditionalCommand) command; + return checkConditions(commandContext, command, castedCommand.getConditions()); + } else { + return ConditionResult.builder().result(true).build(); + } + } + + private ConditionResult checkConditions(CommandContext commandContext, Command command, List conditions) { + if(conditions != null) { + for (CommandCondition condition : conditions) { + ConditionResult conditionResult = condition.shouldExecute(commandContext, command); + if(!conditionResult.isResult()) { + return conditionResult; + } + } + } + return ConditionResult.builder().result(true).build(); + } + } diff --git a/abstracto-application/core/core-impl/src/main/java/dev/sheldan/abstracto/core/commands/help/Help.java b/abstracto-application/core/core-impl/src/main/java/dev/sheldan/abstracto/core/commands/help/Help.java index 7a18fb41f..cb4d022f7 100644 --- a/abstracto-application/core/core-impl/src/main/java/dev/sheldan/abstracto/core/commands/help/Help.java +++ b/abstracto-application/core/core-impl/src/main/java/dev/sheldan/abstracto/core/commands/help/Help.java @@ -1,11 +1,16 @@ package dev.sheldan.abstracto.core.commands.help; import dev.sheldan.abstracto.core.command.*; +import dev.sheldan.abstracto.core.command.condition.CommandCondition; +import dev.sheldan.abstracto.core.command.condition.ConditionResult; +import dev.sheldan.abstracto.core.command.condition.ConditionalCommand; import dev.sheldan.abstracto.core.command.config.*; import dev.sheldan.abstracto.core.command.execution.*; import dev.sheldan.abstracto.core.command.models.database.ACommand; import dev.sheldan.abstracto.core.command.models.database.ACommandInAServer; import dev.sheldan.abstracto.core.command.service.CommandRegistry; +import dev.sheldan.abstracto.core.command.service.CommandService; +import dev.sheldan.abstracto.core.command.service.CommandServiceBean; import dev.sheldan.abstracto.core.command.service.ModuleRegistry; import dev.sheldan.abstracto.core.command.service.management.CommandInServerManagementService; import dev.sheldan.abstracto.core.command.service.management.CommandManagementService; @@ -21,6 +26,7 @@ import dev.sheldan.abstracto.templating.service.TemplateService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; +import java.util.ArrayList; import java.util.Collections; import java.util.List; @@ -50,6 +56,9 @@ public class Help implements Command { @Autowired private RoleService roleService; + @Autowired + private CommandService commandService; + @Override public CommandResult execute(CommandContext commandContext) { List parameters = commandContext.getParameters().getParameters(); @@ -65,6 +74,14 @@ public class Help implements Command { if(moduleService.moduleExists(parameter)){ ModuleInterface moduleInterface = moduleService.getModuleByName(parameter); SingleLevelPackedModule module = moduleService.getPackedModule(moduleInterface); + List commands = module.getCommands(); + List filteredCommands = new ArrayList<>(); + commands.forEach(command -> { + if(commandService.isCommandExecutable(command, commandContext).isResult()) { + filteredCommands.add(command); + } + }); + module.setCommands(filteredCommands); List subModules = moduleService.getSubModules(moduleInterface); HelpModuleDetailsModel model = (HelpModuleDetailsModel) ContextConverter.fromCommandContext(commandContext, HelpModuleDetailsModel.class); model.setModule(module); diff --git a/abstracto-application/core/core-interface/src/main/java/dev/sheldan/abstracto/core/command/config/SingleLevelPackedModule.java b/abstracto-application/core/core-interface/src/main/java/dev/sheldan/abstracto/core/command/config/SingleLevelPackedModule.java index a3d0ab19a..f5ae58ff2 100644 --- a/abstracto-application/core/core-interface/src/main/java/dev/sheldan/abstracto/core/command/config/SingleLevelPackedModule.java +++ b/abstracto-application/core/core-interface/src/main/java/dev/sheldan/abstracto/core/command/config/SingleLevelPackedModule.java @@ -3,10 +3,12 @@ package dev.sheldan.abstracto.core.command.config; import dev.sheldan.abstracto.core.command.Command; import lombok.Builder; import lombok.Getter; +import lombok.Setter; import java.util.List; @Builder +@Setter @Getter public class SingleLevelPackedModule { private ModuleInterface moduleInterface; diff --git a/abstracto-application/core/core-interface/src/main/java/dev/sheldan/abstracto/core/command/service/CommandService.java b/abstracto-application/core/core-interface/src/main/java/dev/sheldan/abstracto/core/command/service/CommandService.java index 3ea6495f5..c7d4369b0 100644 --- a/abstracto-application/core/core-interface/src/main/java/dev/sheldan/abstracto/core/command/service/CommandService.java +++ b/abstracto-application/core/core-interface/src/main/java/dev/sheldan/abstracto/core/command/service/CommandService.java @@ -1,5 +1,8 @@ package dev.sheldan.abstracto.core.command.service; +import dev.sheldan.abstracto.core.command.Command; +import dev.sheldan.abstracto.core.command.condition.ConditionResult; +import dev.sheldan.abstracto.core.command.execution.CommandContext; import dev.sheldan.abstracto.core.command.models.database.ACommand; import dev.sheldan.abstracto.core.config.FeatureEnum; import dev.sheldan.abstracto.core.models.database.ARole; @@ -16,4 +19,5 @@ public interface CommandService { void unRestrictCommand(ACommand aCommand, AServer server); void disAllowCommandForRole(ACommand aCommand, ARole role); void disAllowFeatureForRole(FeatureEnum featureEnum, ARole role); + ConditionResult isCommandExecutable(Command command, CommandContext commandContext); } diff --git a/abstracto-application/documentation/src/main/docs/asciidoc/features/modmail.adoc b/abstracto-application/documentation/src/main/docs/asciidoc/features/modmail.adoc index 1cbfa2301..86eba788f 100644 --- a/abstracto-application/documentation/src/main/docs/asciidoc/features/modmail.adoc +++ b/abstracto-application/documentation/src/main/docs/asciidoc/features/modmail.adoc @@ -25,6 +25,18 @@ Feature key: `modmail` Opening a mod mail thread for a user:: * Usage: `contact ` * Description: Creates a new mod mail thread with the `member`. Does not send a notification about the new thread. +Adding a role to the roles responsible for managing mod mail threads:: +* Usage: `setModMailRole ` +* Description: Adds this role to the roles responsible for mod mail threads, which means: this role will be pinged when a new thread is created and this role is automatically added to the roles allowed to execute all commands related to mod mail. +Removing a role from the roles responsible for managing mod mail threads:: +* Usage: `removeModMailRole ` +* Description: Removes this role from the roles responsible for mod mail threads, which means: this role will no longer be pinged when a new thread is created and this role will also be removed from the roles allowed to execute all commands related to mod mail. +Changing the category in which the text channels are created:: +* Usage: `setModMailCategory ` +* Description: Sets the category which Abstracto uses to create the text channels containing the mod mail threads. The existing threads will not be migrated automatically. + +NOTE: The following commands are only available within a mod mail thread. + Replying to a mod mail thread:: * Usage `reply [text]` * Description: Sends `text` to the user if provided. `text` is optional, because its also possible to only send a picture. @@ -46,15 +58,6 @@ Closing the mod mail thread without notifying the user:: * Usage: `closeSilently [note]` * Description: Closes the thread, deletes the text channel containing the thread and logs the interactions between the member and the moderators in the `modmailLog` post target. When closing a thread, a closing header with general information will be send and the note will be displayed there. -Adding a role to the roles responsible for managing mod mail threads:: -* Usage: `setModMailRole ` -* Description: Adds this role to the roles responsible for mod mail threads, which means: this role will be pinged when a new thread is created and this role is automatically added to the roles allowed to execute all commands related to mod mail. -Removing a role from the roles responsible for managing mod mail threads:: -* Usage: `removeModMailRole ` -* Description: Removes this role from the roles responsible for mod mail threads, which means: this role will no longer be pinged when a new thread is created and this role will also be removed from the roles allowed to execute all commands related to mod mail. -Changing the category in which the text channels are created:: -* Usage: `setModMailCategory ` -* Description: Sets the category which Abstracto uses to create the text channels containing the mod mail threads. The existing threads will not be migrated automatically. === Mod mail thread logging diff --git a/abstracto-application/templating/templating-impl/src/main/java/dev/sheldan/abstracto/templating/methods/DurationMethod.java b/abstracto-application/templating/templating-impl/src/main/java/dev/sheldan/abstracto/templating/methods/DurationMethod.java index 4bf8c3454..f38a93361 100644 --- a/abstracto-application/templating/templating-impl/src/main/java/dev/sheldan/abstracto/templating/methods/DurationMethod.java +++ b/abstracto-application/templating/templating-impl/src/main/java/dev/sheldan/abstracto/templating/methods/DurationMethod.java @@ -27,7 +27,7 @@ public class DurationMethod implements TemplateMethodModelEx { * otherwise * @param arguments The parameters passed to this method, should be only a single duration. * @return The string representation of the {@link Duration} object - * @throws TemplateModelException + * @throws TemplateModelException In case the amount of parameters is not correct, or the first object was not a {@link Duration} */ @Override public Object exec(List arguments) throws TemplateModelException {