[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);

View File

@@ -21,7 +21,7 @@ public class CommandDisabledCondition implements CommandCondition {
@Override
public ConditionResult shouldExecute(CommandContext context, Command command) {
ACommand acommand = commandManagementService.findCommandByName(command.getConfiguration().getName());
Boolean commandEnabled = channelGroupCommandService.isCommandEnabled(acommand, context.getUserInitiatedContext().getChannel());
Boolean commandEnabled = channelGroupCommandService.isCommandEnabled(acommand, context.getUserInitiatedContext().getMessageChannel().getIdLong());
if(!commandEnabled) {
return ConditionResult.builder().result(false).conditionDetail(new CommandDisabledDetail()).build();
}

View File

@@ -38,7 +38,7 @@ public class CommandDisallowedCondition implements CommandCondition {
@Override
public ConditionResult shouldExecute(CommandContext context, Command command) {
ACommand aCommand = commandService.findCommandByName(command.getConfiguration().getName());
ACommandInAServer commandForServer = commandInServerManagementService.getCommandForServer(aCommand, context.getUserInitiatedContext().getServer());
ACommandInAServer commandForServer = commandInServerManagementService.getCommandForServer(aCommand, context.getUserInitiatedContext().getGuild().getIdLong());
if(Boolean.FALSE.equals(commandForServer.getRestricted())) {
return ConditionResult.builder().result(true).build();
}

View File

@@ -21,7 +21,7 @@ public class FeatureModeCondition implements CommandCondition {
FeatureEnum feature = command.getFeature();
if(feature != null) {
for (FeatureMode featureModeLimitation : command.getFeatureModeLimitations()) {
if(modeService.featureModeActive(feature, context.getUserInitiatedContext().getServer(), featureModeLimitation)) {
if(modeService.featureModeActive(feature, context.getUserInitiatedContext().getGuild().getIdLong(), featureModeLimitation)) {
return ConditionResult.builder().result(true).build();
}
}

View File

@@ -36,7 +36,7 @@ public class ImmuneUserCondition implements CommandCondition {
@Override
public ConditionResult shouldExecute(CommandContext context, Command command) {
ACommand aCommand = commandService.findCommandByName(command.getConfiguration().getName());
ACommandInAServer commandForServer = commandInServerManagementService.getCommandForServer(aCommand, context.getUserInitiatedContext().getServer());
ACommandInAServer commandForServer = commandInServerManagementService.getCommandForServer(aCommand, context.getUserInitiatedContext().getGuild().getIdLong());
Optional<Member> any = context.getParameters().getParameters().stream().filter(o -> o instanceof Member).map(this::toMember).findAny();
if(any.isPresent()) {
Member member = any.get();

View File

@@ -25,10 +25,6 @@ public class ContextConverter {
.guild(commandContext.getGuild())
.message(commandContext.getMessage())
.messageChannel(commandContext.getChannel())
.channel(commandContext.getUserInitiatedContext().getChannel())
.server(commandContext.getUserInitiatedContext().getServer())
.aUserInAServer(commandContext.getUserInitiatedContext().getAUserInAServer())
.user(commandContext.getUserInitiatedContext().getUser())
.build();
} catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
log.error("Failed to execute builder method", e);

View File

@@ -5,4 +5,5 @@ import dev.sheldan.abstracto.core.models.database.AChannel;
public interface ChannelGroupCommandService {
Boolean isCommandEnabled(ACommand command, AChannel channel);
Boolean isCommandEnabled(ACommand command, Long channelId);
}

View File

@@ -8,4 +8,5 @@ public interface CommandInServerManagementService {
ACommandInAServer crateCommandInServer(ACommand command, AServer server);
boolean doesCommandExistInServer(ACommand command, AServer server);
ACommandInAServer getCommandForServer(ACommand command, AServer server);
ACommandInAServer getCommandForServer(ACommand command, Long serverId);
}

View File

@@ -1,5 +1,6 @@
package dev.sheldan.abstracto.core.models.cache;
import dev.sheldan.abstracto.core.models.ServerUser;
import dev.sheldan.abstracto.core.utils.MessageUtils;
import lombok.Builder;
import lombok.Getter;
@@ -26,4 +27,8 @@ public class CachedMessage {
public String getMessageUrl() {
return MessageUtils.buildMessageUrl(this.serverId ,this.channelId, this.messageId);
}
public ServerUser getAuthorAsServerUser() {
return ServerUser.builder().serverId(serverId).userId(author.getAuthorId()).build();
}
}

View File

@@ -1,6 +1,5 @@
package dev.sheldan.abstracto.core.models.context;
import dev.sheldan.abstracto.core.models.database.AServer;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
@@ -15,6 +14,5 @@ import net.dv8tion.jda.api.entities.Guild;
@Setter
public class ServerContext {
private Guild guild;
private AServer server;
}

View File

@@ -1,8 +1,5 @@
package dev.sheldan.abstracto.core.models.context;
import dev.sheldan.abstracto.core.models.database.AChannel;
import dev.sheldan.abstracto.core.models.database.AUser;
import dev.sheldan.abstracto.core.models.database.AUserInAServer;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
@@ -16,11 +13,8 @@ import net.dv8tion.jda.api.entities.MessageChannel;
@Setter
@SuperBuilder
public class UserInitiatedServerContext extends ServerContext {
private AChannel channel;
private MessageChannel messageChannel;
private Member member;
private AUser user;
private Message message;
private AUserInAServer aUserInAServer;
}

View File

@@ -24,6 +24,7 @@ public interface PostTargetService {
List<CompletableFuture<Message>> editOrCreatedInPostTarget(Long messageId, MessageToSend messageToSend, PostTarget target);
List<CompletableFuture<Message>> editOrCreatedInPostTarget(Long messageId, MessageToSend messageToSend, PostTargetEnum postTarget, Long serverId);
void throwIfPostTargetIsNotDefined(PostTargetEnum name, Long serverId);
boolean postTargetDefinedInServer(PostTargetEnum name, Long serverId);
boolean validPostTarget(String name);
List<PostTarget> getPostTargets(AServer server);
List<String> getAvailablePostTargets();

View File

@@ -3,12 +3,14 @@ package dev.sheldan.abstracto.core.service.management;
import dev.sheldan.abstracto.core.models.database.AChannel;
import dev.sheldan.abstracto.core.models.database.AChannelType;
import dev.sheldan.abstracto.core.models.database.AServer;
import net.dv8tion.jda.api.entities.TextChannel;
import java.util.Optional;
public interface ChannelManagementService {
Optional<AChannel> loadChannelOptional(Long id);
AChannel loadChannel(Long id);
AChannel loadChannel(TextChannel textChannel);
AChannel createChannel(Long id, AChannelType type, AServer server);
AChannel markAsDeleted(Long id);
boolean channelExists(Long id);

View File

@@ -1,6 +1,7 @@
package dev.sheldan.abstracto.core.service.management;
import dev.sheldan.abstracto.core.models.database.*;
import net.dv8tion.jda.api.entities.Guild;
import java.util.List;
import java.util.Optional;
@@ -9,6 +10,7 @@ public interface ServerManagementService {
AServer createServer(Long id);
AServer loadOrCreate(Long id);
AServer loadServer(Long id);
AServer loadServer(Guild guild);
Optional<AServer> loadServerOptional(Long id);
void addChannelToServer(AServer server, AChannel channel);
AUserInAServer addUserToServer(AServer server, AUser user);

View File

@@ -1,14 +1,12 @@
package dev.sheldan.abstracto.core.utils;
import dev.sheldan.abstracto.core.exception.AbstractoRunTimeException;
import dev.sheldan.abstracto.core.models.database.AChannel;
import dev.sheldan.abstracto.core.models.GuildChannelMember;
import dev.sheldan.abstracto.core.models.cache.CachedMessage;
import dev.sheldan.abstracto.core.models.context.UserInitiatedServerContext;
import dev.sheldan.abstracto.core.service.BotService;
import dev.sheldan.abstracto.core.service.management.ChannelManagementService;
import dev.sheldan.abstracto.core.service.management.UserInServerManagementService;
import dev.sheldan.abstracto.core.models.cache.CachedMessage;
import dev.sheldan.abstracto.core.models.GuildChannelMember;
import dev.sheldan.abstracto.core.models.context.UserInitiatedServerContext;
import dev.sheldan.abstracto.core.models.database.AUserInAServer;
import dev.sheldan.abstracto.core.service.BotService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@@ -35,16 +33,10 @@ public class ContextUtils {
try {
m = clazz.getMethod("builder");
UserInitiatedServerContext.UserInitiatedServerContextBuilder<?, ?> builder = (UserInitiatedServerContext.UserInitiatedServerContextBuilder) m.invoke(null, null);
AUserInAServer aUserInAServer = userInServerManagementService.loadUser(message.getServerId(), message.getAuthor().getAuthorId());
AChannel channel = channelManagementService.loadChannel(message.getChannelId());
return builder
.member(guildChannelMember.getMember())
.guild(guildChannelMember.getGuild())
.messageChannel(guildChannelMember.getTextChannel())
.channel(channel)
.server(aUserInAServer.getServerReference())
.aUserInAServer(aUserInAServer)
.user(aUserInAServer.getUserReference())
.build();
} catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
log.error("Failed to execute builder method", e);

View File

@@ -5,9 +5,9 @@ import dev.sheldan.abstracto.core.command.execution.CommandContext;
import dev.sheldan.abstracto.core.config.FeatureEnum;
import dev.sheldan.abstracto.core.config.FeatureMode;
import dev.sheldan.abstracto.core.models.context.UserInitiatedServerContext;
import dev.sheldan.abstracto.core.models.database.AServer;
import dev.sheldan.abstracto.core.service.FeatureModeService;
import dev.sheldan.abstracto.core.test.command.CommandTestUtilities;
import net.dv8tion.jda.api.entities.Guild;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
@@ -41,11 +41,13 @@ public class FeatureModeConditionTest {
private FeatureEnum featureEnum;
@Mock
private AServer server;
private Guild server;
@Mock
private UserInitiatedServerContext userInitiatedServerContext;
private static final Long SERVER_ID = 4L;
@Test
public void testNoLimitations() {
when(command.getFeatureModeLimitations()).thenReturn(new ArrayList<>());
@@ -55,9 +57,10 @@ public class FeatureModeConditionTest {
@Test
public void testMetLimitations() {
when(commandContext.getUserInitiatedContext()).thenReturn(userInitiatedServerContext);
when(userInitiatedServerContext.getServer()).thenReturn(server);
when(server.getIdLong()).thenReturn(SERVER_ID);
when(userInitiatedServerContext.getGuild()).thenReturn(server);
when(command.getFeature()).thenReturn(featureEnum);
when(modeService.featureModeActive(featureEnum, server, featureMode)).thenReturn(true);
when(modeService.featureModeActive(featureEnum, SERVER_ID, featureMode)).thenReturn(true);
when(command.getFeatureModeLimitations()).thenReturn(Arrays.asList(featureMode));
CommandTestUtilities.checkSuccessfulCondition(testUnit.shouldExecute(commandContext, command));
}
@@ -65,9 +68,10 @@ public class FeatureModeConditionTest {
@Test
public void testLimitedCommand() {
when(commandContext.getUserInitiatedContext()).thenReturn(userInitiatedServerContext);
when(userInitiatedServerContext.getServer()).thenReturn(server);
when(server.getIdLong()).thenReturn(SERVER_ID);
when(userInitiatedServerContext.getGuild()).thenReturn(server);
when(command.getFeature()).thenReturn(featureEnum);
when(modeService.featureModeActive(featureEnum, server, featureMode)).thenReturn(false);
when(modeService.featureModeActive(featureEnum, SERVER_ID, featureMode)).thenReturn(false);
when(command.getFeatureModeLimitations()).thenReturn(Arrays.asList(featureMode));
CommandTestUtilities.checkUnmetCondition(testUnit.shouldExecute(commandContext, command));
}

View File

@@ -7,9 +7,6 @@ import dev.sheldan.abstracto.core.command.execution.CommandContext;
import dev.sheldan.abstracto.core.command.execution.CommandResult;
import dev.sheldan.abstracto.core.command.execution.ResultState;
import dev.sheldan.abstracto.core.models.context.UserInitiatedServerContext;
import dev.sheldan.abstracto.core.models.database.AServer;
import dev.sheldan.abstracto.core.models.database.AUserInAServer;
import dev.sheldan.abstracto.core.test.MockUtils;
import net.dv8tion.jda.api.entities.Guild;
import net.dv8tion.jda.api.entities.Member;
import net.dv8tion.jda.api.entities.Message;
@@ -44,8 +41,6 @@ public class CommandTestUtilities {
public static CommandContext getNoParameters() {
AServer server = MockUtils.getServer();
AUserInAServer author = MockUtils.getUserObject(3L, server);
CommandContext context = CommandContext
.builder()
.build();
@@ -53,16 +48,11 @@ public class CommandTestUtilities {
context.setGuild(guild);
Member member = Mockito.mock(Member.class);
context.setAuthor(member);
long channelId = 4L;
TextChannel mockedTextChannel = Mockito.mock(TextChannel.class);
UserInitiatedServerContext userInitiatedContext = UserInitiatedServerContext
.builder()
.server(server)
.guild(guild)
.aUserInAServer(author)
.member(member)
.user(author.getUserReference())
.channel(MockUtils.getTextChannel(server, channelId))
.messageChannel(mockedTextChannel)
.build();
context.setUserInitiatedContext(userInitiatedContext);

View File

@@ -1,67 +0,0 @@
package dev.sheldan.abstracto.core.utils;
import dev.sheldan.abstracto.core.models.GuildChannelMember;
import dev.sheldan.abstracto.core.models.cache.CachedAuthor;
import dev.sheldan.abstracto.core.models.cache.CachedMessage;
import dev.sheldan.abstracto.core.models.database.AChannel;
import dev.sheldan.abstracto.core.models.database.AServer;
import dev.sheldan.abstracto.core.models.database.AUser;
import dev.sheldan.abstracto.core.models.database.AUserInAServer;
import dev.sheldan.abstracto.core.models.template.commands.PingModel;
import dev.sheldan.abstracto.core.service.BotService;
import dev.sheldan.abstracto.core.service.management.ChannelManagementService;
import dev.sheldan.abstracto.core.service.management.UserInServerManagementService;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import static org.junit.Assert.assertEquals;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.when;
@RunWith(MockitoJUnitRunner.class)
public class ContextUtilsTest {
public static final Long CHANNEL_ID = 1L;
public static final Long SERVER_ID = 1L;
public static final Long AUTHOR_ID = 1L;
@InjectMocks
private ContextUtils classToTest;
@Mock
private ChannelManagementService channelManagementService;
@Mock
private UserInServerManagementService userInServerManagementService;
@Mock
private BotService botService;
@Before
public void setup() {
GuildChannelMember build = GuildChannelMember.builder().build();
when(botService.getServerChannelUser(eq(SERVER_ID), eq(CHANNEL_ID), eq(AUTHOR_ID))).thenReturn(build);
AServer server = AServer.builder().id(SERVER_ID).build();
AUserInAServer aUserInAServer = AUserInAServer.builder().userReference(AUser.builder().id(AUTHOR_ID).build()).serverReference(server).build();
when(userInServerManagementService.loadUser(eq(SERVER_ID), eq(AUTHOR_ID))).thenReturn(aUserInAServer);
AChannel channel = AChannel.builder().id(CHANNEL_ID).build();
when(channelManagementService.loadChannel(eq(CHANNEL_ID))).thenReturn(channel);
}
@Test
public void testFromMessage() {
PingModel pingModel = (PingModel) classToTest.fromMessage(buildCachedMessage(), PingModel.class);
assertEquals(AUTHOR_ID, pingModel.getUser().getId());
assertEquals(AUTHOR_ID, pingModel.getAUserInAServer().getUserReference().getId());
assertEquals(SERVER_ID, pingModel.getAUserInAServer().getServerReference().getId());
assertEquals(SERVER_ID, pingModel.getServer().getId());
assertEquals(CHANNEL_ID, pingModel.getChannel().getId());
}
private CachedMessage buildCachedMessage() {
return CachedMessage.builder().author(CachedAuthor.builder().authorId(AUTHOR_ID).build()).serverId(SERVER_ID).channelId(CHANNEL_ID).build();
}
}