[AB-68] adding invite filter with commands to allow/disallow invites, remove stored filtered invite links and show filtered invite links

removing database entities from command context
This commit is contained in:
Sheldan
2021-01-23 15:33:00 +01:00
parent fb3ed69650
commit 2a2a3aea70
182 changed files with 2571 additions and 325 deletions

View File

@@ -212,16 +212,9 @@ public class CommandReceivedHandler extends ListenerAdapter {
}
private UserInitiatedServerContext buildTemplateParameter(MessageReceivedEvent event) {
AChannel channel = channelManagementService.loadChannel(event.getChannel().getIdLong());
AServer server = serverManagementService.loadOrCreate(event.getGuild().getIdLong());
AUserInAServer user = userInServerManagementService.loadUser(event.getMember());
return UserInitiatedServerContext
.builder()
.channel(channel)
.server(server)
.member(event.getMember())
.aUserInAServer(user)
.user(user.getUserReference())
.messageChannel(event.getTextChannel())
.guild(event.getGuild())
.build();

View File

@@ -14,4 +14,7 @@ public interface CommandInServerRepository extends JpaRepository<ACommandInAServ
@QueryHints(@QueryHint(name = org.hibernate.annotations.QueryHints.CACHEABLE, value = "true"))
ACommandInAServer findByServerReferenceAndCommandReference(AServer server, ACommand command);
@QueryHints(@QueryHint(name = org.hibernate.annotations.QueryHints.CACHEABLE, value = "true"))
ACommandInAServer findByServerReference_IdAndCommandReference(Long serverId, ACommand command);
}

View File

@@ -4,6 +4,7 @@ import dev.sheldan.abstracto.core.command.models.database.ACommand;
import dev.sheldan.abstracto.core.command.service.management.ChannelGroupCommandManagementService;
import dev.sheldan.abstracto.core.models.database.AChannel;
import dev.sheldan.abstracto.core.models.database.AChannelGroupCommand;
import dev.sheldan.abstracto.core.service.management.ChannelManagementService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@@ -18,6 +19,9 @@ public class ChannelGroupCommandServiceBean implements ChannelGroupCommandServic
@Autowired
private ChannelGroupCommandManagementService channelGroupCommandService;
@Autowired
private ChannelManagementService channelManagementService;
@Override
public Boolean isCommandEnabled(ACommand command, AChannel channel) {
List<AChannelGroupCommand> allChannelGroupsOfCommand = channelGroupCommandService.getAllGroupCommandsForCommand(command);
@@ -33,4 +37,9 @@ public class ChannelGroupCommandServiceBean implements ChannelGroupCommandServic
// not empty -> has groups, command is disabled in all
return allChannelGroupsOfCommand.isEmpty();
}
@Override
public Boolean isCommandEnabled(ACommand command, Long channelId) {
return isCommandEnabled(command, channelManagementService.loadChannel(channelId));
}
}

View File

@@ -94,7 +94,7 @@ public class ExceptionServiceBean implements ExceptionService {
}
private GenericExceptionModel buildCommandModel(Throwable throwable, CommandContext context) {
FullUserInServer fullUser = FullUserInServer.builder().member(context.getAuthor()).aUserInAServer(context.getUserInitiatedContext().getAUserInAServer()).build();
FullUserInServer fullUser = FullUserInServer.builder().member(context.getAuthor()).aUserInAServer(userInServerManagementService.loadUser(context.getAuthor())).build();
return GenericExceptionModel
.builder()
.user(fullUser)

View File

@@ -37,4 +37,9 @@ public class CommandInServerManagementServiceBean implements CommandInServerMana
public ACommandInAServer getCommandForServer(ACommand command, AServer server) {
return repository.findByServerReferenceAndCommandReference(server, command);
}
@Override
public ACommandInAServer getCommandForServer(ACommand command, Long serverId) {
return null;
}
}

View File

@@ -8,6 +8,8 @@ import dev.sheldan.abstracto.core.command.execution.CommandResult;
import dev.sheldan.abstracto.core.command.execution.ContextConverter;
import dev.sheldan.abstracto.core.config.FeatureEnum;
import dev.sheldan.abstracto.core.command.config.features.CoreFeatures;
import dev.sheldan.abstracto.core.models.database.AServer;
import dev.sheldan.abstracto.core.service.management.ServerManagementService;
import dev.sheldan.abstracto.templating.model.MessageToSend;
import dev.sheldan.abstracto.core.models.template.commands.ChannelGroupChannelModel;
import dev.sheldan.abstracto.core.models.template.commands.ChannelGroupModel;
@@ -37,9 +39,13 @@ public class ListChannelGroups extends AbstractConditionableCommand {
@Autowired
private ChannelService channelService;
@Autowired
private ServerManagementService serverManagementService;
@Override
public CommandResult execute(CommandContext commandContext) {
List<AChannelGroup> channelGroups = channelGroupManagementService.findAllInServer(commandContext.getUserInitiatedContext().getServer());
AServer server = serverManagementService.loadServer(commandContext.getGuild());
List<AChannelGroup> channelGroups = channelGroupManagementService.findAllInServer(server);
ListChannelGroupsModel template = (ListChannelGroupsModel) ContextConverter.fromCommandContext(commandContext, ListChannelGroupsModel.class);
template.setGroups(convertAChannelGroupToChannelGroupChannel(channelGroups));
MessageToSend response = templateService.renderEmbedTemplate("listChannelGroups_response", template);

View File

@@ -15,6 +15,7 @@ import dev.sheldan.abstracto.core.models.template.commands.PostTargetModelEntry;
import dev.sheldan.abstracto.core.service.ChannelService;
import dev.sheldan.abstracto.core.service.PostTargetService;
import dev.sheldan.abstracto.core.service.management.PostTargetManagement;
import dev.sheldan.abstracto.core.service.management.ServerManagementService;
import dev.sheldan.abstracto.core.utils.FutureUtils;
import dev.sheldan.abstracto.templating.service.TemplateService;
import lombok.extern.slf4j.Slf4j;
@@ -48,12 +49,15 @@ public class PostTargetCommand extends AbstractConditionableCommand {
@Autowired
private ChannelService channelService;
@Autowired
private ServerManagementService serverManagementService;
@Override
public CompletableFuture<CommandResult> executeAsync(CommandContext commandContext) {
if(commandContext.getParameters().getParameters().isEmpty()) {
log.trace("Displaying existing post targets for guild {}.", commandContext.getGuild().getId());
PostTargetDisplayModel posttargetDisplayModel = (PostTargetDisplayModel) ContextConverter.fromCommandContext(commandContext, PostTargetDisplayModel.class);
AServer server = commandContext.getUserInitiatedContext().getServer();
AServer server = serverManagementService.loadServer(commandContext.getGuild());
List<PostTarget> postTargets = postTargetService.getPostTargets(server);
posttargetDisplayModel.setPostTargets(new ArrayList<>());
List<PostTargetModelEntry> postTargetEntries = posttargetDisplayModel.getPostTargets();

View File

@@ -15,7 +15,9 @@ import dev.sheldan.abstracto.core.commands.config.ConfigModuleInterface;
import dev.sheldan.abstracto.core.config.FeatureEnum;
import dev.sheldan.abstracto.core.command.config.features.CoreFeatures;
import dev.sheldan.abstracto.core.models.database.AFeature;
import dev.sheldan.abstracto.core.models.database.AServer;
import dev.sheldan.abstracto.core.service.management.RoleManagementService;
import dev.sheldan.abstracto.core.service.management.ServerManagementService;
import dev.sheldan.abstracto.templating.service.TemplateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@@ -41,17 +43,21 @@ public class Allow extends AbstractConditionableCommand {
@Autowired
private TemplateService templateService;
@Autowired
private ServerManagementService serverManagementService;
@Override
public CommandResult execute(CommandContext commandContext) {
String name = (String) commandContext.getParameters().getParameters().get(0);
AServer server = serverManagementService.loadServer(commandContext.getGuild());
if(featureManagementService.featureExists(name)) {
AFeature feature = featureManagementService.getFeature(name);
feature.getCommands().forEach(command ->
commandService.unRestrictCommand(command, commandContext.getUserInitiatedContext().getServer())
commandService.unRestrictCommand(command, server)
);
} else if(commandManagementService.doesCommandExist(name)) {
ACommand command = commandManagementService.findCommandByName(name);
commandService.unRestrictCommand(command, commandContext.getUserInitiatedContext().getServer());
commandService.unRestrictCommand(command, server);
} else {
// TODO refactor to use exception
return CommandResult.fromError(templateService.renderTemplate(CommandServiceBean.NO_FEATURE_COMMAND_FOUND_EXCEPTION_TEMPLATE, new Object()));

View File

@@ -12,10 +12,12 @@ import dev.sheldan.abstracto.core.commands.config.ConfigModuleInterface;
import dev.sheldan.abstracto.core.config.FeatureConfig;
import dev.sheldan.abstracto.core.config.FeatureEnum;
import dev.sheldan.abstracto.core.command.config.features.CoreFeatures;
import dev.sheldan.abstracto.core.models.database.AServer;
import dev.sheldan.abstracto.core.models.template.commands.EnableModel;
import dev.sheldan.abstracto.core.service.ChannelService;
import dev.sheldan.abstracto.core.service.FeatureConfigService;
import dev.sheldan.abstracto.core.service.FeatureFlagService;
import dev.sheldan.abstracto.core.service.management.ServerManagementService;
import dev.sheldan.abstracto.templating.service.TemplateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@@ -39,6 +41,8 @@ public class DisableFeature extends AbstractConditionableCommand {
@Autowired
private TemplateService templateService;
@Autowired
private ServerManagementService serverManagementService;
@Override
public CompletableFuture<CommandResult> executeAsync(CommandContext commandContext) {
@@ -53,8 +57,9 @@ public class DisableFeature extends AbstractConditionableCommand {
FeatureConfig feature = featureConfigService.getFeatureDisplayForFeature(flagKey);
featureFlagService.disableFeature(feature, commandContext.getGuild().getIdLong());
if(feature.getDependantFeatures() != null) {
AServer server = serverManagementService.loadServer(commandContext.getGuild());
feature.getDependantFeatures().forEach(featureDisplay ->
featureFlagService.disableFeature(featureDisplay, commandContext.getUserInitiatedContext().getServer())
featureFlagService.disableFeature(featureDisplay, server)
);
}
return CompletableFuture.completedFuture(CommandResult.fromSuccess());

View File

@@ -11,9 +11,11 @@ import dev.sheldan.abstracto.core.command.service.management.FeatureManagementSe
import dev.sheldan.abstracto.core.commands.config.ConfigModuleInterface;
import dev.sheldan.abstracto.core.config.FeatureEnum;
import dev.sheldan.abstracto.core.config.FeatureMode;
import dev.sheldan.abstracto.core.models.database.AServer;
import dev.sheldan.abstracto.core.service.FeatureConfigService;
import dev.sheldan.abstracto.core.service.FeatureModeService;
import dev.sheldan.abstracto.core.service.management.FeatureModeManagementService;
import dev.sheldan.abstracto.core.service.management.ServerManagementService;
import dev.sheldan.abstracto.templating.service.TemplateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@@ -39,13 +41,17 @@ public class DisableMode extends AbstractConditionableCommand {
@Autowired
private TemplateService templateService;
@Autowired
private ServerManagementService serverManagementService;
@Override
public CommandResult execute(CommandContext commandContext) {
String featureName = (String) commandContext.getParameters().getParameters().get(0);
String modeName = (String) commandContext.getParameters().getParameters().get(1);
FeatureEnum featureEnum = featureConfigService.getFeatureEnum(featureName);
FeatureMode featureMode = featureModeService.getFeatureModeForKey(modeName);
featureModeService.disableFeatureModeForFeature(featureEnum, commandContext.getUserInitiatedContext().getServer(), featureMode);
AServer server = serverManagementService.loadServer(commandContext.getGuild());
featureModeService.disableFeatureModeForFeature(featureEnum, server, featureMode);
return CommandResult.fromSuccess();
}

View File

@@ -13,10 +13,12 @@ import dev.sheldan.abstracto.core.config.FeatureConfig;
import dev.sheldan.abstracto.core.config.FeatureEnum;
import dev.sheldan.abstracto.core.command.config.features.CoreFeatures;
import dev.sheldan.abstracto.core.models.FeatureValidationResult;
import dev.sheldan.abstracto.core.models.database.AServer;
import dev.sheldan.abstracto.core.models.template.commands.EnableModel;
import dev.sheldan.abstracto.core.service.ChannelService;
import dev.sheldan.abstracto.core.service.FeatureConfigService;
import dev.sheldan.abstracto.core.service.FeatureFlagService;
import dev.sheldan.abstracto.core.service.management.ServerManagementService;
import dev.sheldan.abstracto.templating.service.TemplateService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
@@ -42,6 +44,9 @@ public class EnableFeature extends AbstractConditionableCommand {
@Autowired
private TemplateService templateService;
@Autowired
private ServerManagementService serverManagementService;
@Override
public CompletableFuture<CommandResult> executeAsync(CommandContext commandContext) {
if(commandContext.getParameters().getParameters().isEmpty()) {
@@ -53,16 +58,17 @@ public class EnableFeature extends AbstractConditionableCommand {
} else {
String flagKey = (String) commandContext.getParameters().getParameters().get(0);
FeatureConfig feature = featureConfigService.getFeatureDisplayForFeature(flagKey);
FeatureValidationResult featureSetup = featureConfigService.validateFeatureSetup(feature, commandContext.getUserInitiatedContext().getServer());
AServer server = serverManagementService.loadServer(commandContext.getUserInitiatedContext().getGuild().getIdLong());
FeatureValidationResult featureSetup = featureConfigService.validateFeatureSetup(feature, server);
if(Boolean.FALSE.equals(featureSetup.getValidationResult())) {
log.info("Feature {} has failed the setup validation. Notifying user.", flagKey);
channelService.sendTextToChannelNotAsync(templateService.renderTemplatable(featureSetup), commandContext.getChannel());
}
featureFlagService.enableFeature(feature, commandContext.getUserInitiatedContext().getServer());
featureFlagService.enableFeature(feature, server);
if(feature.getRequiredFeatures() != null) {
feature.getRequiredFeatures().forEach(featureDisplay -> {
log.info("Also enabling required feature {}.", featureDisplay.getFeature().getKey());
featureFlagService.enableFeature(featureDisplay, commandContext.getUserInitiatedContext().getServer());
featureFlagService.enableFeature(featureDisplay, server);
});
}
return CompletableFuture.completedFuture(CommandResult.fromSuccess());

View File

@@ -11,9 +11,11 @@ import dev.sheldan.abstracto.core.command.service.management.FeatureManagementSe
import dev.sheldan.abstracto.core.commands.config.ConfigModuleInterface;
import dev.sheldan.abstracto.core.config.FeatureEnum;
import dev.sheldan.abstracto.core.config.FeatureMode;
import dev.sheldan.abstracto.core.models.database.AServer;
import dev.sheldan.abstracto.core.service.FeatureConfigService;
import dev.sheldan.abstracto.core.service.FeatureModeService;
import dev.sheldan.abstracto.core.service.management.FeatureModeManagementService;
import dev.sheldan.abstracto.core.service.management.ServerManagementService;
import dev.sheldan.abstracto.templating.service.TemplateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@@ -39,13 +41,17 @@ public class EnableMode extends AbstractConditionableCommand {
@Autowired
private TemplateService templateService;
@Autowired
private ServerManagementService serverManagementService;
@Override
public CommandResult execute(CommandContext commandContext) {
String featureName = (String) commandContext.getParameters().getParameters().get(0);
String modeName = (String) commandContext.getParameters().getParameters().get(1);
FeatureEnum featureEnum = featureConfigService.getFeatureEnum(featureName);
FeatureMode featureMode = featureModeService.getFeatureModeForKey(modeName);
featureModeService.enableFeatureModeForFeature(featureEnum, commandContext.getUserInitiatedContext().getServer(), featureMode);
AServer server = serverManagementService.loadServer(commandContext.getGuild().getIdLong());
featureModeService.enableFeatureModeForFeature(featureEnum, server, featureMode);
return CommandResult.fromSuccess();
}

View File

@@ -11,11 +11,13 @@ import dev.sheldan.abstracto.core.command.service.management.FeatureManagementSe
import dev.sheldan.abstracto.core.commands.config.ConfigModuleInterface;
import dev.sheldan.abstracto.core.config.FeatureEnum;
import dev.sheldan.abstracto.core.models.database.AFeature;
import dev.sheldan.abstracto.core.models.database.AServer;
import dev.sheldan.abstracto.core.models.template.commands.FeatureModeDisplay;
import dev.sheldan.abstracto.core.models.template.commands.FeatureModesModel;
import dev.sheldan.abstracto.core.service.ChannelService;
import dev.sheldan.abstracto.core.service.FeatureConfigService;
import dev.sheldan.abstracto.core.service.FeatureModeService;
import dev.sheldan.abstracto.core.service.management.ServerManagementService;
import dev.sheldan.abstracto.core.utils.FutureUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@@ -40,16 +42,20 @@ public class FeatureModes extends AbstractConditionableCommand {
@Autowired
private ChannelService channelService;
@Autowired
private ServerManagementService serverManagementService;
@Override
public CompletableFuture<CommandResult> executeAsync(CommandContext commandContext) {
List<FeatureModeDisplay> featureModes;
AServer server = serverManagementService.loadServer(commandContext.getGuild());
if(commandContext.getParameters().getParameters().isEmpty()) {
featureModes = featureModeService.getEffectiveFeatureModes(commandContext.getUserInitiatedContext().getServer());
featureModes = featureModeService.getEffectiveFeatureModes(server);
} else {
String featureName = (String) commandContext.getParameters().getParameters().get(0);
FeatureEnum featureEnum = featureConfigService.getFeatureEnum(featureName);
AFeature feature = featureManagementService.getFeature(featureEnum.getKey());
featureModes = featureModeService.getEffectiveFeatureModes(commandContext.getUserInitiatedContext().getServer(), feature);
featureModes = featureModeService.getEffectiveFeatureModes(server, feature);
}
FeatureModesModel model = FeatureModesModel.builder().featureModes(featureModes).build();
return FutureUtils.toSingleFutureGeneric(channelService.sendEmbedTemplateInChannel(FEATURE_MODES_RESPONSE_TEMPLATE_KEY, model, commandContext.getChannel()))

View File

@@ -11,9 +11,11 @@ import dev.sheldan.abstracto.core.config.FeatureEnum;
import dev.sheldan.abstracto.core.command.config.features.CoreFeatures;
import dev.sheldan.abstracto.core.converter.FeatureFlagConverter;
import dev.sheldan.abstracto.core.models.database.AFeatureFlag;
import dev.sheldan.abstracto.core.models.database.AServer;
import dev.sheldan.abstracto.core.models.template.commands.FeaturesModel;
import dev.sheldan.abstracto.core.service.ChannelService;
import dev.sheldan.abstracto.core.service.management.FeatureFlagManagementService;
import dev.sheldan.abstracto.core.service.management.ServerManagementService;
import dev.sheldan.abstracto.core.utils.FutureUtils;
import dev.sheldan.abstracto.templating.model.MessageToSend;
import dev.sheldan.abstracto.templating.service.TemplateService;
@@ -38,9 +40,13 @@ public class Features extends AbstractConditionableCommand {
@Autowired
private FeatureFlagConverter featureFlagConverter;
@Autowired
private ServerManagementService serverManagementService;
@Override
public CompletableFuture<CommandResult> executeAsync(CommandContext commandContext) {
List<AFeatureFlag> features = featureFlagManagementService.getFeatureFlagsOfServer(commandContext.getUserInitiatedContext().getServer());
AServer server = serverManagementService.loadServer(commandContext.getGuild());
List<AFeatureFlag> features = featureFlagManagementService.getFeatureFlagsOfServer(server);
FeaturesModel featuresModel = (FeaturesModel) ContextConverter.fromCommandContext(commandContext, FeaturesModel.class);
featuresModel.setFeatures(featureFlagConverter.fromFeatureFlags(features));
MessageToSend messageToSend = templateService.renderEmbedTemplate("features_response", featuresModel);

View File

@@ -15,7 +15,9 @@ import dev.sheldan.abstracto.core.commands.config.ConfigModuleInterface;
import dev.sheldan.abstracto.core.config.FeatureEnum;
import dev.sheldan.abstracto.core.command.config.features.CoreFeatures;
import dev.sheldan.abstracto.core.models.database.AFeature;
import dev.sheldan.abstracto.core.models.database.AServer;
import dev.sheldan.abstracto.core.service.management.RoleManagementService;
import dev.sheldan.abstracto.core.service.management.ServerManagementService;
import dev.sheldan.abstracto.templating.service.TemplateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@@ -41,17 +43,21 @@ public class Restrict extends AbstractConditionableCommand {
@Autowired
private TemplateService templateService;
@Autowired
private ServerManagementService serverManagementService;
@Override
public CommandResult execute(CommandContext commandContext) {
String name = (String) commandContext.getParameters().getParameters().get(0);
AServer server = serverManagementService.loadServer(commandContext.getGuild());
if(featureManagementService.featureExists(name)) {
AFeature feature = featureManagementService.getFeature(name);
feature.getCommands().forEach(command ->
commandService.restrictCommand(command, commandContext.getUserInitiatedContext().getServer())
commandService.restrictCommand(command, server)
);
} else if(commandManagementService.doesCommandExist(name)) {
ACommand command = commandManagementService.findCommandByName(name);
commandService.restrictCommand(command, commandContext.getUserInitiatedContext().getServer());
commandService.restrictCommand(command, server);
} else {
// TODO Refactor to use exception
return CommandResult.fromError(templateService.renderTemplate(CommandServiceBean.NO_FEATURE_COMMAND_FOUND_EXCEPTION_TEMPLATE, new Object()));

View File

@@ -89,7 +89,7 @@ public class Help implements Command {
Command command = commandRegistry.getCommandByName(parameter);
log.trace("Displaying help for command {}.", command.getConfiguration().getName());
ACommand aCommand = commandManagementService.findCommandByName(parameter);
ACommandInAServer aCommandInAServer = commandInServerManagementService.getCommandForServer(aCommand, commandContext.getUserInitiatedContext().getServer());
ACommandInAServer aCommandInAServer = commandInServerManagementService.getCommandForServer(aCommand, commandContext.getGuild().getIdLong());
HelpCommandDetailsModel model = (HelpCommandDetailsModel) ContextConverter.fromCommandContext(commandContext, HelpCommandDetailsModel.class);
if(Boolean.TRUE.equals(aCommandInAServer.getRestricted())) {
model.setImmuneRoles(roleService.getRolesFromGuild(aCommandInAServer.getImmuneRoles()));

View File

@@ -23,6 +23,9 @@ public interface UserInServerRepository extends JpaRepository<AUserInAServer, Lo
@QueryHints(@QueryHint(name = org.hibernate.annotations.QueryHints.CACHEABLE, value = "true"))
boolean existsByServerReferenceAndUserReference(AServer server, AUser user);
@QueryHints(@QueryHint(name = org.hibernate.annotations.QueryHints.CACHEABLE, value = "true"))
boolean existsByServerReference_IdAndUserReference_Id(Long serverId, Long userId);
@QueryHints(@QueryHint(name = org.hibernate.annotations.QueryHints.CACHEABLE, value = "true"))
List<AUserInAServer> findByUserReference(AUser user);
}

View File

@@ -202,6 +202,11 @@ public class PostTargetServiceBean implements PostTargetService {
}
}
@Override
public boolean postTargetDefinedInServer(PostTargetEnum name, Long serverId) {
return postTargetManagement.postTargetExists(name.getKey(), serverId);
}
@Override
public List<CompletableFuture<Message>> editEmbedInPostTarget(Long messageId, MessageToSend message, PostTargetEnum postTargetName, Long serverId) {
PostTarget postTarget = this.getPostTarget(postTargetName, serverId);

View File

@@ -8,6 +8,7 @@ import dev.sheldan.abstracto.core.models.database.AServer;
import dev.sheldan.abstracto.core.repository.ChannelRepository;
import dev.sheldan.abstracto.core.service.LockService;
import lombok.extern.slf4j.Slf4j;
import net.dv8tion.jda.api.entities.TextChannel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@@ -33,6 +34,11 @@ public class ChannelManagementServiceBean implements ChannelManagementService {
return loadChannelOptional(id).orElseThrow(() -> new ChannelNotFoundException(id));
}
@Override
public AChannel loadChannel(TextChannel textChannel) {
return loadChannel(textChannel.getIdLong());
}
@Override
public AChannel createChannel(Long id, AChannelType type, AServer server) {
lockService.lockTable(TableLocks.CHANNELS);

View File

@@ -4,6 +4,7 @@ import dev.sheldan.abstracto.core.exception.GuildNotFoundException;
import dev.sheldan.abstracto.core.models.database.*;
import dev.sheldan.abstracto.core.repository.ServerRepository;
import lombok.extern.slf4j.Slf4j;
import net.dv8tion.jda.api.entities.Guild;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@@ -41,6 +42,11 @@ public class ServerManagementServiceBean implements ServerManagementService {
return loadServerOptional(id).orElseThrow(() -> new GuildNotFoundException(id));
}
@Override
public AServer loadServer(Guild guild) {
return loadServer(guild.getIdLong());
}
@Override
public Optional<AServer> loadServerOptional(Long id) {
return repository.findById(id);

View File

@@ -30,7 +30,11 @@ public class UserInServerManagementServiceBean implements UserInServerManagement
@Override
public AUserInAServer loadUser(Long serverId, Long userId) {
return userInServerRepository.findByServerReference_IdAndUserReference_Id(serverId, userId).orElseThrow(() -> new UserInServerNotFoundException(0L));
if(userInServerRepository.existsByServerReference_IdAndUserReference_Id(serverId, userId)) {
return userInServerRepository.findByServerReference_IdAndUserReference_Id(serverId, userId).orElseThrow(() -> new UserInServerNotFoundException(0L));
} else {
return this.createUserInServer(serverId, userId);
}
}
@Override

View File

@@ -6,8 +6,10 @@ import dev.sheldan.abstracto.core.config.FeatureEnum;
import dev.sheldan.abstracto.core.config.FeatureMode;
import dev.sheldan.abstracto.core.exception.FeatureModeNotFoundException;
import dev.sheldan.abstracto.core.exception.FeatureNotFoundException;
import dev.sheldan.abstracto.core.models.database.AServer;
import dev.sheldan.abstracto.core.service.FeatureConfigService;
import dev.sheldan.abstracto.core.service.FeatureModeService;
import dev.sheldan.abstracto.core.service.management.ServerManagementService;
import dev.sheldan.abstracto.core.test.command.CommandConfigValidator;
import dev.sheldan.abstracto.core.test.command.CommandTestUtilities;
import org.junit.Test;
@@ -34,6 +36,9 @@ public class DisableFeatureModeTest {
@Mock
private FeatureModeService featureModeService;
@Mock
private ServerManagementService serverManagementService;
@Test
public void testExecuteDisable() {
String featureName = "text";
@@ -43,9 +48,11 @@ public class DisableFeatureModeTest {
FeatureMode featureMode = Mockito.mock(FeatureMode.class);
when(featureModeService.getFeatureModeForKey(modeName)).thenReturn(featureMode);
CommandContext context = CommandTestUtilities.getWithParameters(Arrays.asList(featureName, modeName));
AServer server = Mockito.mock(AServer.class);
when(serverManagementService.loadServer(context.getGuild())).thenReturn(server);
CommandResult commandResultCompletableFuture = testUnit.execute(context);
CommandTestUtilities.checkSuccessfulCompletion(commandResultCompletableFuture);
verify(featureModeService, times(1)).disableFeatureModeForFeature(featureEnum, context.getUserInitiatedContext().getServer(), featureMode);
verify(featureModeService, times(1)).disableFeatureModeForFeature(featureEnum, server, featureMode);
}
@Test(expected = FeatureNotFoundException.class)

View File

@@ -6,8 +6,10 @@ import dev.sheldan.abstracto.core.config.FeatureEnum;
import dev.sheldan.abstracto.core.config.FeatureMode;
import dev.sheldan.abstracto.core.exception.FeatureModeNotFoundException;
import dev.sheldan.abstracto.core.exception.FeatureNotFoundException;
import dev.sheldan.abstracto.core.models.database.AServer;
import dev.sheldan.abstracto.core.service.FeatureConfigService;
import dev.sheldan.abstracto.core.service.FeatureModeService;
import dev.sheldan.abstracto.core.service.management.ServerManagementService;
import dev.sheldan.abstracto.core.test.command.CommandConfigValidator;
import dev.sheldan.abstracto.core.test.command.CommandTestUtilities;
import org.junit.Test;
@@ -36,6 +38,11 @@ public class EnableFeatureModeTest {
@Mock
private FeatureModeService featureModeService;
@Mock
private ServerManagementService serverManagementService;
private static final Long SERVER_ID = 3L;
@Test
public void testExecuteDisable() {
String featureName = "text";
@@ -45,9 +52,12 @@ public class EnableFeatureModeTest {
FeatureMode featureMode = Mockito.mock(FeatureMode.class);
when(featureModeService.getFeatureModeForKey(modeName)).thenReturn(featureMode);
CommandContext context = CommandTestUtilities.getWithParameters(Arrays.asList(featureName, modeName));
AServer server = Mockito.mock(AServer.class);
when(context.getGuild().getIdLong()).thenReturn(SERVER_ID);
when(serverManagementService.loadServer(SERVER_ID)).thenReturn(server);
CommandResult commandResultCompletableFuture = testUnit.execute(context);
CommandTestUtilities.checkSuccessfulCompletion(commandResultCompletableFuture);
verify(featureModeService, times(1)).enableFeatureModeForFeature(featureEnum, context.getUserInitiatedContext().getServer(), featureMode);
verify(featureModeService, times(1)).enableFeatureModeForFeature(featureEnum, server, featureMode);
}
@Test(expected = FeatureNotFoundException.class)

View File

@@ -6,11 +6,13 @@ import dev.sheldan.abstracto.core.command.service.management.FeatureManagementSe
import dev.sheldan.abstracto.core.config.FeatureEnum;
import dev.sheldan.abstracto.core.exception.FeatureNotFoundException;
import dev.sheldan.abstracto.core.models.database.AFeature;
import dev.sheldan.abstracto.core.models.database.AServer;
import dev.sheldan.abstracto.core.models.template.commands.FeatureModeDisplay;
import dev.sheldan.abstracto.core.models.template.commands.FeatureModesModel;
import dev.sheldan.abstracto.core.service.ChannelService;
import dev.sheldan.abstracto.core.service.FeatureConfigService;
import dev.sheldan.abstracto.core.service.FeatureModeService;
import dev.sheldan.abstracto.core.service.management.ServerManagementService;
import dev.sheldan.abstracto.core.test.command.CommandConfigValidator;
import dev.sheldan.abstracto.core.test.command.CommandTestUtilities;
import org.junit.Assert;
@@ -43,6 +45,9 @@ public class FeatureModesTest {
@Mock
private FeatureConfigService featureConfigService;
@Mock
private ServerManagementService serverManagementService;
@Mock
private FeatureManagementService featureManagementService;
@@ -57,7 +62,9 @@ public class FeatureModesTest {
FeatureModeDisplay display1 = Mockito.mock(FeatureModeDisplay.class);
FeatureModeDisplay display2 = Mockito.mock(FeatureModeDisplay.class);
List<FeatureModeDisplay> featureModeDisplays = Arrays.asList(display1, display2);
when(featureModeService.getEffectiveFeatureModes(noParameters.getUserInitiatedContext().getServer())).thenReturn(featureModeDisplays);
AServer server = Mockito.mock(AServer.class);
when(serverManagementService.loadServer(noParameters.getGuild())).thenReturn(server);
when(featureModeService.getEffectiveFeatureModes(server)).thenReturn(featureModeDisplays);
when(channelService.sendEmbedTemplateInChannel(eq(FeatureModes.FEATURE_MODES_RESPONSE_TEMPLATE_KEY), modelCaptor.capture(), eq(noParameters.getChannel()))).thenReturn(new ArrayList<>());
CompletableFuture<CommandResult> commandResultCompletableFuture = testUnit.executeAsync(noParameters);
CommandTestUtilities.checkSuccessfulCompletionAsync(commandResultCompletableFuture);
@@ -76,7 +83,9 @@ public class FeatureModesTest {
AFeature feature = Mockito.mock(AFeature.class);
when(featureManagementService.getFeature(FEATURE_NAME)).thenReturn(feature);
List<FeatureModeDisplay> featureModeDisplays = Arrays.asList(display1);
when(featureModeService.getEffectiveFeatureModes(noParameters.getUserInitiatedContext().getServer(), feature)).thenReturn(featureModeDisplays);
AServer server = Mockito.mock(AServer.class);
when(serverManagementService.loadServer(noParameters.getGuild())).thenReturn(server);
when(featureModeService.getEffectiveFeatureModes(server, feature)).thenReturn(featureModeDisplays);
when(channelService.sendEmbedTemplateInChannel(eq(FeatureModes.FEATURE_MODES_RESPONSE_TEMPLATE_KEY), modelCaptor.capture(), eq(noParameters.getChannel()))).thenReturn(new ArrayList<>());
CompletableFuture<CommandResult> commandResultCompletableFuture = testUnit.executeAsync(noParameters);
CommandTestUtilities.checkSuccessfulCompletionAsync(commandResultCompletableFuture);