[AB-136] replacing some rare occurrences where fake objects where used for actual database operations

replacing a few sync commands with async commands
refactoring parameters in assignable role place service bean
fixing emote parameter handler to also consider default emojis
removing unused description in command
replacing some ARole parameters with Role parameters, so we can be sure they exists
added a few TODOs marking exception changes required
This commit is contained in:
Sheldan
2020-10-03 15:24:52 +02:00
parent 0f6f6a1e49
commit a391381ff6
37 changed files with 230 additions and 115 deletions

View File

@@ -25,7 +25,11 @@ public class AEmoteParameterHandler implements CommandParameterHandler {
@Override
public Object handle(String input, CommandParameterIterators iterators, Class clazz, Message context) {
Emote emote = (Emote) emoteParameterHandler.handle(input, iterators, Emote.class, context);
return emoteService.getFakeEmoteFromEmote(emote);
if(emote != null) {
return emoteService.getFakeEmoteFromEmote(emote);
} else {
return emoteService.getFakeEmote(input);
}
}
@Override

View File

@@ -3,6 +3,7 @@ package dev.sheldan.abstracto.core.command.handler;
import dev.sheldan.abstracto.core.command.CommandConstants;
import net.dv8tion.jda.api.entities.Emote;
import net.dv8tion.jda.api.entities.Message;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;
import java.util.regex.Matcher;
@@ -21,8 +22,12 @@ public class EmoteParameterHandler implements CommandParameterHandler {
if(matcher.matches()) {
return iterators.getEmoteIterator().next();
} else {
long emoteId = Long.parseLong(input);
return context.getGuild().getEmoteById(emoteId);
if(StringUtils.isNumeric(input)) {
long emoteId = Long.parseLong(input);
return context.getGuild().getEmoteById(emoteId);
} else {
return null;
}
}
}

View File

@@ -26,7 +26,12 @@ public class FullEmoteParameterHandler implements CommandParameterHandler {
@Override
public Object handle(String input, CommandParameterIterators iterators, Class clazz, Message context) {
Emote emote = (Emote) emoteParameterHandler.handle(input, iterators, Emote.class, context);
AEmote aEmote = emoteService.getFakeEmoteFromEmote(emote);
AEmote aEmote;
if(emote != null) {
aEmote = emoteService.getFakeEmoteFromEmote(emote);
} else {
aEmote = emoteService.getFakeEmote(input);
}
return FullEmote.builder().emote(emote).fakeEmote(aEmote).build();
}

View File

@@ -33,8 +33,8 @@ public class RemoveFromChannelGroup extends AbstractConditionableCommand {
@Override
public CommandConfiguration getConfiguration() {
Parameter channelGroupName = Parameter.builder().name("name").type(String.class).description("The name of the channel group to remove the channel from.").build();
Parameter channelToAdd = Parameter.builder().name("channel").type(TextChannel.class).description("The mention of the channel to remove from the group.").build();
Parameter channelGroupName = Parameter.builder().name("name").type(String.class).build();
Parameter channelToAdd = Parameter.builder().name("channel").type(TextChannel.class).build();
List<Parameter> parameters = Arrays.asList(channelGroupName, channelToAdd);
List<String> aliases = Arrays.asList("rmChChgrp", "chGrpCh-");
HelpInfo helpInfo = HelpInfo.builder().templated(true).hasExample(true).build();

View File

@@ -53,6 +53,7 @@ public class Allow extends AbstractConditionableCommand {
ACommand command = commandManagementService.findCommandByName(name);
commandService.unRestrictCommand(command, commandContext.getUserInitiatedContext().getServer());
} else {
// TODO refactor to use exception
return CommandResult.fromError(templateService.renderTemplate(CommandServiceBean.NO_FEATURE_COMMAND_FOUND_EXCEPTION_TEMPLATE, new Object()));
}
return CommandResult.fromSuccess();

View File

@@ -48,13 +48,14 @@ public class AllowRole extends AbstractConditionableCommand {
@Override
public CommandResult execute(CommandContext commandContext) {
String name = (String) commandContext.getParameters().getParameters().get(0);
ARole role = (ARole) commandContext.getParameters().getParameters().get(1);
ARole fakeRole = (ARole) commandContext.getParameters().getParameters().get(1);
ARole actualRole = roleManagementService.findRole(fakeRole.getId());
if(featureManagementService.featureExists(name)) {
FeatureEnum featureEnum = featureFlagService.getFeatureEnum(name);
commandService.allowFeatureForRole(featureEnum, role);
commandService.allowFeatureForRole(featureEnum, actualRole);
} else if(commandManagementService.doesCommandExist(name)) {
ACommand command = commandManagementService.findCommandByName(name);
commandService.allowCommandForRole(command, role);
commandService.allowCommandForRole(command, actualRole);
} else {
return CommandResult.fromError(templateService.renderTemplate(CommandServiceBean.NO_FEATURE_COMMAND_FOUND_EXCEPTION_TEMPLATE, new Object()));
}

View File

@@ -46,14 +46,15 @@ public class DisAllowRole extends AbstractConditionableCommand {
public CommandResult execute(CommandContext commandContext) {
String name = (String) commandContext.getParameters().getParameters().get(0);
ARole role = (ARole) commandContext.getParameters().getParameters().get(1);
ARole actualRole = roleManagementService.findRole(role.getId());
if(featureManagementService.featureExists(name)) {
AFeature feature = featureManagementService.getFeature(name);
feature.getCommands().forEach(command ->
commandService.disAllowCommandForRole(command, role)
commandService.disAllowCommandForRole(command, actualRole)
);
} else if(commandManagementService.doesCommandExist(name)) {
ACommand command = commandManagementService.findCommandByName(name);
commandService.disAllowCommandForRole(command, role);
commandService.disAllowCommandForRole(command, actualRole);
} else {
return CommandResult.fromError(templateService.renderTemplate(CommandServiceBean.NO_FEATURE_COMMAND_FOUND_EXCEPTION_TEMPLATE, new Object()));
}

View File

@@ -46,15 +46,17 @@ public class MakeAffected extends AbstractConditionableCommand {
public CommandResult execute(CommandContext commandContext) {
String name = (String) commandContext.getParameters().getParameters().get(0);
ARole role = (ARole) commandContext.getParameters().getParameters().get(1);
ARole actualRole = roleManagementService.findRole(role.getId());
if(featureManagementService.featureExists(name)) {
AFeature feature = featureManagementService.getFeature(name);
feature.getCommands().forEach(command ->
commandService.makeRoleAffectedByCommand(command, role)
commandService.makeRoleAffectedByCommand(command, actualRole)
);
} else if(commandManagementService.doesCommandExist(name)) {
ACommand command = commandManagementService.findCommandByName(name);
commandService.makeRoleAffectedByCommand(command, role);
commandService.makeRoleAffectedByCommand(command, actualRole);
} else {
// TODO refactor to use exception
return CommandResult.fromError(templateService.renderTemplate(CommandServiceBean.NO_FEATURE_COMMAND_FOUND_EXCEPTION_TEMPLATE, new Object()));
}
return CommandResult.fromSuccess();

View File

@@ -46,15 +46,17 @@ public class MakeImmune extends AbstractConditionableCommand {
public CommandResult execute(CommandContext commandContext) {
String name = (String) commandContext.getParameters().getParameters().get(0);
ARole role = (ARole) commandContext.getParameters().getParameters().get(1);
ARole actualRole = roleManagementService.findRole(role.getId());
if(featureManagementService.featureExists(name)) {
AFeature feature = featureManagementService.getFeature(name);
feature.getCommands().forEach(command ->
commandService.makeRoleImmuneForCommand(command, role)
commandService.makeRoleImmuneForCommand(command, actualRole)
);
} else if(commandManagementService.doesCommandExist(name)) {
ACommand command = commandManagementService.findCommandByName(name);
commandService.makeRoleImmuneForCommand(command, role);
commandService.makeRoleImmuneForCommand(command, actualRole);
} else {
// TODO refactor to use exception
return CommandResult.fromError(templateService.renderTemplate(CommandServiceBean.NO_FEATURE_COMMAND_FOUND_EXCEPTION_TEMPLATE, new Object()));
}
return CommandResult.fromSuccess();

View File

@@ -53,6 +53,7 @@ public class Restrict extends AbstractConditionableCommand {
ACommand command = commandManagementService.findCommandByName(name);
commandService.restrictCommand(command, commandContext.getUserInitiatedContext().getServer());
} else {
// TODO Refactor to use exception
return CommandResult.fromError(templateService.renderTemplate(CommandServiceBean.NO_FEATURE_COMMAND_FOUND_EXCEPTION_TEMPLATE, new Object()));
}
return CommandResult.fromSuccess();

View File

@@ -157,30 +157,30 @@ public class EmoteManagementServiceBean implements EmoteManagementService {
}
@Override
public AEmote setEmoteToAEmote(String name, AEmote emote, Long serverId) {
public AEmote setEmoteToAEmote(String name, AEmote fakeEmote, Long serverId) {
Optional<AEmote> emoteOptional = loadEmoteByName(name, serverId);
if(!emoteOptional.isPresent()) {
return createEmote(name, emote, serverId, true);
return createEmote(name, fakeEmote, serverId, true);
} else {
AEmote emoteBeingSet = emoteOptional.get();
if(emote.getCustom()) {
emoteBeingSet.setCustom(emote.getCustom());
emoteBeingSet.setEmoteId(emote.getEmoteId());
emoteBeingSet.setEmoteKey(emote.getEmoteKey());
if(fakeEmote.getCustom()) {
emoteBeingSet.setCustom(fakeEmote.getCustom());
emoteBeingSet.setEmoteId(fakeEmote.getEmoteId());
emoteBeingSet.setEmoteKey(fakeEmote.getEmoteKey());
} else {
emoteBeingSet.setCustom(false);
emoteBeingSet.setEmoteKey(emote.getEmoteKey());
emoteBeingSet.setEmoteKey(fakeEmote.getEmoteKey());
}
return emoteBeingSet;
}
}
@Override
public AEmote createEmote(String name, AEmote emote, Long serverId, boolean validateName) {
if(emote.getCustom()) {
return this.createCustomEmote(name, emote, serverId, validateName);
public AEmote createEmote(String name, AEmote fakeEmote, Long serverId, boolean validateName) {
if(fakeEmote.getCustom()) {
return this.createCustomEmote(name, fakeEmote, serverId, validateName);
} else {
return this.createDefaultEmote(name, emote.getEmoteKey(), serverId, validateName);
return this.createDefaultEmote(name, fakeEmote.getEmoteKey(), serverId, validateName);
}
}

View File

@@ -56,5 +56,14 @@ public class AEmoteParameterHandlerTest {
Assert.assertEquals(aEmote, parsed);
}
@Test
public void testDefaultEmoteHandling() {
String input = "test";
when(emoteParameterHandler.handle(input, iterators, Emote.class, message)).thenReturn(null);
when(emoteService.getFakeEmote(input)).thenReturn(aEmote);
AEmote parsed = (AEmote) testUnit.handle(input, iterators, AEmote.class, message);
Assert.assertEquals(aEmote, parsed);
}
}

View File

@@ -62,10 +62,9 @@ public class EmoteParameterHandlerTest {
Assert.assertEquals(parsed, emote);
}
@Test(expected = NumberFormatException.class)
@Test
public void testInvalidEmoteMention() {
String input = "test";
testUnit.handle(input, null, Emote.class, null);
Assert.assertNull(testUnit.handle("test", null, Emote.class, null));
}
private String getEmoteMention() {

View File

@@ -59,4 +59,15 @@ public class FullEmoteParameterHandlerTest {
}
@Test
public void testDefaultEmoteHandling() {
String input = "test";
when(emoteParameterHandler.handle(input, iterators, Emote.class, message)).thenReturn(null);
when(emoteService.getFakeEmote(input)).thenReturn(aEmote);
FullEmote parsed = (FullEmote) testUnit.handle(input, iterators, AEmote.class, message);
Assert.assertNull(parsed.getEmote());
Assert.assertEquals(aEmote, parsed.getFakeEmote());
}
}