[AB-166] refactored conditions to not use exceptions for their regular case, split up feature mode exception into condition and exception, if the conditions need to be checked somewhere else, a separate exception is required, fixed command not being disabled properly in channels

This commit is contained in:
Sheldan
2020-11-21 22:07:34 +01:00
parent 04f1db2408
commit 448d555dba
39 changed files with 259 additions and 208 deletions

View File

@@ -4,13 +4,15 @@ 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.modmail.condition.ModMailContextCondition;
import dev.sheldan.abstracto.modmail.exception.NotInModMailThreadException;
import dev.sheldan.abstracto.modmail.condition.detail.NotInModMailThreadConditionDetail;
import dev.sheldan.abstracto.modmail.models.database.ModMailThread;
import dev.sheldan.abstracto.modmail.service.management.ModMailThreadManagementService;
import dev.sheldan.abstracto.templating.service.TemplateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.Optional;
/**
* This {@link dev.sheldan.abstracto.core.command.condition.CommandCondition} checks the channel it is executed in
* and checks if the channel is a valid and open mod mail thread.
@@ -26,10 +28,10 @@ public class RequiresModMailCondition implements ModMailContextCondition {
@Override
public ConditionResult shouldExecute(CommandContext commandContext, Command command) {
ModMailThread thread = modMailThreadManagementService.getByChannel(commandContext.getUserInitiatedContext().getChannel());
if(thread != null) {
Optional<ModMailThread> threadOptional = modMailThreadManagementService.getByChannelOptional(commandContext.getUserInitiatedContext().getChannel());
if(threadOptional.isPresent()) {
return ConditionResult.builder().result(true).build();
}
return ConditionResult.builder().result(false).exception(new NotInModMailThreadException()).build();
return ConditionResult.builder().result(false).conditionDetail(new NotInModMailThreadConditionDetail()).build();
}
}

View File

@@ -0,0 +1,16 @@
package dev.sheldan.abstracto.modmail.condition.detail;
import dev.sheldan.abstracto.core.command.condition.ConditionDetail;
public class NotInModMailThreadConditionDetail implements ConditionDetail {
@Override
public String getTemplateName() {
return "modmail_not_in_modmail_thread_condition";
}
@Override
public Object getTemplateModel() {
return new Object();
}
}

View File

@@ -1,21 +0,0 @@
package dev.sheldan.abstracto.modmail.exception;
import dev.sheldan.abstracto.core.exception.AbstractoRunTimeException;
import dev.sheldan.abstracto.templating.Templatable;
public class NoModMailServerAvailable extends AbstractoRunTimeException implements Templatable {
public NoModMailServerAvailable() {
super("No modmail server available");
}
@Override
public String getTemplateName() {
return "modmail_no_server_available_exception";
}
@Override
public Object getTemplateModel() {
return new Object();
}
}

View File

@@ -1,17 +0,0 @@
package dev.sheldan.abstracto.modmail.exception;
import dev.sheldan.abstracto.core.exception.AbstractoRunTimeException;
import dev.sheldan.abstracto.templating.Templatable;
public class NotInModMailThreadException extends AbstractoRunTimeException implements Templatable {
@Override
public String getTemplateName() {
return "modmail_not_in_modmail_thread_exception";
}
@Override
public Object getTemplateModel() {
return new Object();
}
}

View File

@@ -8,7 +8,6 @@ import dev.sheldan.abstracto.core.command.exception.IncorrectParameterException;
import dev.sheldan.abstracto.core.command.execution.CommandContext;
import dev.sheldan.abstracto.core.command.execution.CommandResult;
import dev.sheldan.abstracto.core.config.FeatureEnum;
import dev.sheldan.abstracto.core.exception.IncorrectFeatureModeException;
import dev.sheldan.abstracto.core.service.EmoteService;
import dev.sheldan.abstracto.core.service.FeatureModeService;
import dev.sheldan.abstracto.statistic.config.StatisticFeatures;
@@ -22,7 +21,6 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
@Component
@@ -52,12 +50,9 @@ public class TrackEmote extends AbstractConditionableCommand {
} else if(emoteToTrack.getEmote() != null) {
boolean external = !emoteService.emoteIsFromGuild(emoteToTrack.getEmote(), commandContext.getGuild());
if(external) {
boolean trackExternalEmotes = featureModeService.featureModeActive(StatisticFeatures.EMOTE_TRACKING, serverId, EmoteTrackingMode.EXTERNAL_EMOTES);
if(!trackExternalEmotes) {
throw new IncorrectFeatureModeException(StatisticFeatures.EMOTE_TRACKING, Arrays.asList(EmoteTrackingMode.EXTERNAL_EMOTES));
}
featureModeService.validateActiveFeatureMode(serverId, StatisticFeatures.EMOTE_TRACKING, EmoteTrackingMode.EXTERNAL_EMOTES);
}
trackedEmoteService.createFakeTrackedEmote(emoteToTrack.getEmote(), commandContext.getGuild());
trackedEmoteService.createFakeTrackedEmote(emoteToTrack.getEmote(), commandContext.getGuild(), external);
} else {
throw new IncorrectParameterException(this, getConfiguration().getParameters().get(0).getName());
}

View File

@@ -173,6 +173,11 @@ public class TrackedEmoteServiceBean implements TrackedEmoteService {
@Override
public TrackedEmote createFakeTrackedEmote(Emote emote, Guild guild) {
boolean external = !emoteService.emoteIsFromGuild(emote, guild);
return createFakeTrackedEmote(emote, guild, external);
}
@Override
public TrackedEmote createFakeTrackedEmote(Emote emote, Guild guild, boolean external) {
return trackedEmoteManagementService.createTrackedEmote(emote, guild, external);
}

View File

@@ -5,7 +5,7 @@ import dev.sheldan.abstracto.core.command.exception.IncorrectParameterTypeExcept
import dev.sheldan.abstracto.core.command.exception.InsufficientParametersException;
import dev.sheldan.abstracto.core.command.execution.CommandContext;
import dev.sheldan.abstracto.core.command.execution.CommandResult;
import dev.sheldan.abstracto.core.exception.IncorrectFeatureModeException;
import dev.sheldan.abstracto.core.command.exception.IncorrectFeatureModeException;
import dev.sheldan.abstracto.core.service.EmoteService;
import dev.sheldan.abstracto.core.service.FeatureModeService;
import dev.sheldan.abstracto.core.test.command.CommandTestUtilities;
@@ -88,7 +88,7 @@ public class TrackEmoteTest {
when(trackEmoteParameter.getEmote()).thenReturn(emoteToTrack);
when(trackEmoteParameter.getTrackedEmote()).thenReturn(trackedEmote);
when(emoteService.emoteIsFromGuild(emoteToTrack, commandContext.getGuild())).thenReturn(false);
when(featureModeService.featureModeActive(StatisticFeatures.EMOTE_TRACKING, SERVER_ID, EmoteTrackingMode.EXTERNAL_EMOTES)).thenReturn(false);
doThrow(new IncorrectFeatureModeException(null, null)).when(featureModeService).validateActiveFeatureMode(SERVER_ID, StatisticFeatures.EMOTE_TRACKING, EmoteTrackingMode.EXTERNAL_EMOTES);
testUnit.execute(commandContext);
}
@@ -103,10 +103,10 @@ public class TrackEmoteTest {
when(trackEmoteParameter.getEmote()).thenReturn(emoteToTrack);
when(trackEmoteParameter.getTrackedEmote()).thenReturn(trackedEmote);
when(emoteService.emoteIsFromGuild(emoteToTrack, commandContext.getGuild())).thenReturn(false);
when(featureModeService.featureModeActive(StatisticFeatures.EMOTE_TRACKING, SERVER_ID, EmoteTrackingMode.EXTERNAL_EMOTES)).thenReturn(true);
CommandResult result = testUnit.execute(commandContext);
CommandTestUtilities.checkSuccessfulCompletion(result);
verify(trackedEmoteService, times(1)).createFakeTrackedEmote(emoteToTrack, commandContext.getGuild());
verify(trackedEmoteService, times(1)).createFakeTrackedEmote(emoteToTrack, commandContext.getGuild(), true);
verify(featureModeService, times(1)).validateActiveFeatureMode(SERVER_ID, StatisticFeatures.EMOTE_TRACKING, EmoteTrackingMode.EXTERNAL_EMOTES);
}
@Test(expected = IncorrectParameterException.class)

View File

@@ -20,6 +20,7 @@ public interface TrackedEmoteService {
TrackedEmoteOverview loadTrackedEmoteOverview(Guild guild);
TrackedEmoteOverview loadTrackedEmoteOverview(Guild guild, Boolean showTrackingDisabled);
TrackedEmote createFakeTrackedEmote(Emote emote, Guild guild);
TrackedEmote createFakeTrackedEmote(Emote emote, Guild guild, boolean external);
void deleteTrackedEmote(TrackedEmote trackedEmote);
void resetEmoteStats(Guild guild);
void disableEmoteTracking(Guild guild);