mirror of
https://github.com/Sheldan/abstracto.git
synced 2026-04-05 17:07:03 +00:00
moved post targets to an enum, in order to have more type safety, this might be changed in the future
added validation check when a feature is enabled, in order to notify which configuration is missing for this feature to function properly
This commit is contained in:
@@ -13,7 +13,6 @@ 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.features.CoreFeatures;
|
||||
import dev.sheldan.abstracto.core.models.database.AFeature;
|
||||
import dev.sheldan.abstracto.core.models.database.ARole;
|
||||
import dev.sheldan.abstracto.core.service.FeatureFlagService;
|
||||
import dev.sheldan.abstracto.core.service.management.RoleManagementService;
|
||||
|
||||
@@ -12,6 +12,7 @@ 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.config.features.CoreFeatures;
|
||||
import dev.sheldan.abstracto.core.models.FeatureValidationResult;
|
||||
import dev.sheldan.abstracto.core.models.template.commands.EnableModel;
|
||||
import dev.sheldan.abstracto.core.service.ChannelService;
|
||||
import dev.sheldan.abstracto.core.service.FeatureFlagService;
|
||||
@@ -44,6 +45,10 @@ public class Enable extends AbstractConditionableCommand {
|
||||
} else {
|
||||
String flagKey = (String) commandContext.getParameters().getParameters().get(0);
|
||||
FeatureConfig feature = featureFlagService.getFeatureDisplayForFeature(flagKey);
|
||||
FeatureValidationResult featureSetup = featureFlagService.validateFeatureSetup(feature, commandContext.getUserInitiatedContext().getServer());
|
||||
if(!featureSetup.getValidationResult()) {
|
||||
channelService.sendTextToChannelNoFuture(templateService.renderTemplatable(featureSetup), commandContext.getChannel());
|
||||
}
|
||||
featureFlagService.enableFeature(feature, commandContext.getUserInitiatedContext().getServer());
|
||||
if(feature.getRequiredFeatures() != null) {
|
||||
feature.getRequiredFeatures().forEach(featureDisplay -> {
|
||||
|
||||
@@ -1,16 +1,12 @@
|
||||
package dev.sheldan.abstracto.core.commands.help;
|
||||
|
||||
import dev.sheldan.abstracto.core.command.*;
|
||||
import dev.sheldan.abstracto.core.command.condition.CommandCondition;
|
||||
import dev.sheldan.abstracto.core.command.condition.ConditionResult;
|
||||
import dev.sheldan.abstracto.core.command.condition.ConditionalCommand;
|
||||
import dev.sheldan.abstracto.core.command.config.*;
|
||||
import dev.sheldan.abstracto.core.command.execution.*;
|
||||
import dev.sheldan.abstracto.core.command.models.database.ACommand;
|
||||
import dev.sheldan.abstracto.core.command.models.database.ACommandInAServer;
|
||||
import dev.sheldan.abstracto.core.command.service.CommandRegistry;
|
||||
import dev.sheldan.abstracto.core.command.service.CommandService;
|
||||
import dev.sheldan.abstracto.core.command.service.CommandServiceBean;
|
||||
import dev.sheldan.abstracto.core.command.service.ModuleRegistry;
|
||||
import dev.sheldan.abstracto.core.command.service.management.CommandInServerManagementService;
|
||||
import dev.sheldan.abstracto.core.command.service.management.CommandManagementService;
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package dev.sheldan.abstracto.core.repository;
|
||||
|
||||
import dev.sheldan.abstracto.core.models.database.AConfig;
|
||||
import dev.sheldan.abstracto.core.models.database.AServer;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.data.jpa.repository.QueryHints;
|
||||
|
||||
@@ -10,4 +11,7 @@ public interface ConfigRepository extends JpaRepository<AConfig, Long> {
|
||||
|
||||
@QueryHints(@QueryHint(name = org.hibernate.annotations.QueryHints.CACHEABLE, value = "true"))
|
||||
AConfig findAConfigByServerIdAndName(Long serverId, String name);
|
||||
|
||||
boolean existsAConfigByServerIdAndName(Long serverId, String name);
|
||||
boolean existsAConfigByServerAndName(AServer server, String name);
|
||||
}
|
||||
|
||||
@@ -14,4 +14,6 @@ public interface PostTargetRepository extends JpaRepository<PostTarget, Long> {
|
||||
@QueryHints(@QueryHint(name = org.hibernate.annotations.QueryHints.CACHEABLE, value = "true"))
|
||||
PostTarget findPostTargetByNameAndServerReference(String name, AServer server);
|
||||
|
||||
boolean existsByNameAndServerReference(String name, AServer server);
|
||||
|
||||
}
|
||||
|
||||
@@ -3,8 +3,10 @@ package dev.sheldan.abstracto.core.service;
|
||||
import dev.sheldan.abstracto.core.command.service.management.FeatureManagementService;
|
||||
import dev.sheldan.abstracto.core.config.FeatureConfig;
|
||||
import dev.sheldan.abstracto.core.config.FeatureEnum;
|
||||
import dev.sheldan.abstracto.core.config.PostTargetEnum;
|
||||
import dev.sheldan.abstracto.core.exception.AbstractoRunTimeException;
|
||||
import dev.sheldan.abstracto.core.exception.FeatureNotFoundException;
|
||||
import dev.sheldan.abstracto.core.models.FeatureValidationResult;
|
||||
import dev.sheldan.abstracto.core.models.database.AFeature;
|
||||
import dev.sheldan.abstracto.core.models.database.AFeatureFlag;
|
||||
import dev.sheldan.abstracto.core.models.database.AServer;
|
||||
@@ -15,6 +17,7 @@ import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.function.Predicate;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@Component
|
||||
@@ -32,6 +35,9 @@ public class FeatureFlagServiceBean implements FeatureFlagService {
|
||||
@Autowired
|
||||
private ServerManagementService serverManagementService;
|
||||
|
||||
@Autowired
|
||||
private FeatureValidatorService featureValidatorService;
|
||||
|
||||
|
||||
@Override
|
||||
public boolean isFeatureEnabled(FeatureConfig name, Long serverId) {
|
||||
@@ -121,6 +127,17 @@ public class FeatureFlagServiceBean implements FeatureFlagService {
|
||||
}
|
||||
throw new AbstractoRunTimeException(String.format("Feature %s not found.", key));
|
||||
}
|
||||
|
||||
@Override
|
||||
public PostTargetEnum getPostTargetEnumByKey(String key) {
|
||||
Predicate<PostTargetEnum> postTargetComparison = postTargetEnum -> postTargetEnum.getKey().equals(key);
|
||||
Optional<FeatureConfig> foundFeature = availableFeatures.stream().filter(featureDisplay -> featureDisplay.getRequiredPostTargets().stream().anyMatch(postTargetComparison)).findAny();
|
||||
if(foundFeature.isPresent()) {
|
||||
return foundFeature.get().getRequiredPostTargets().stream().filter(postTargetComparison).findAny().get();
|
||||
}
|
||||
throw new AbstractoRunTimeException(String.format("Post target %s not found.", key));
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean getFeatureFlagValue(FeatureEnum key, Long serverId) {
|
||||
AServer server = serverManagementService.loadOrCreate(serverId);
|
||||
@@ -145,4 +162,19 @@ public class FeatureFlagServiceBean implements FeatureFlagService {
|
||||
AFeature feature = featureManagementService.getFeature(key.getKey());
|
||||
return managementService.setFeatureFlagValue(feature, server, newValue);
|
||||
}
|
||||
|
||||
@Override
|
||||
public FeatureValidationResult validateFeatureSetup(FeatureConfig featureConfig, AServer server) {
|
||||
FeatureValidationResult featureValidationResult = FeatureValidationResult.validationSuccessful(featureConfig);
|
||||
featureConfig.getRequiredPostTargets().forEach(s -> {
|
||||
featureValidatorService.checkPostTarget(s, server, featureValidationResult);
|
||||
});
|
||||
featureConfig.getRequiredSystemConfigKeys().forEach(s -> {
|
||||
featureValidatorService.checkSystemConfig(s, server, featureValidationResult);
|
||||
});
|
||||
featureConfig.getAdditionalFeatureValidators().forEach(featureValidator -> {
|
||||
featureValidator.featureIsSetup(featureConfig, server, featureValidationResult);
|
||||
});
|
||||
return featureValidationResult;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,42 @@
|
||||
package dev.sheldan.abstracto.core.service;
|
||||
|
||||
import dev.sheldan.abstracto.core.config.PostTargetEnum;
|
||||
import dev.sheldan.abstracto.core.models.FeatureValidationResult;
|
||||
import dev.sheldan.abstracto.core.models.PostTargetValidationError;
|
||||
import dev.sheldan.abstracto.core.models.SystemConfigValidationError;
|
||||
import dev.sheldan.abstracto.core.models.database.AServer;
|
||||
import dev.sheldan.abstracto.core.service.management.ConfigManagementService;
|
||||
import dev.sheldan.abstracto.core.service.management.PostTargetManagement;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
public class FeatureValidationServiceBean implements FeatureValidatorService {
|
||||
|
||||
@Autowired
|
||||
private PostTargetManagement postTargetManagement;
|
||||
|
||||
@Autowired
|
||||
private ConfigManagementService configService;
|
||||
|
||||
@Override
|
||||
public void checkPostTarget(PostTargetEnum name, AServer server, FeatureValidationResult featureValidationResult) {
|
||||
if(!postTargetManagement.postTargetExists(name.getKey(), server)) {
|
||||
PostTargetValidationError validationError = PostTargetValidationError.builder().postTargetName(name.getKey()).build();
|
||||
featureValidationResult.setValidationResult(false);
|
||||
featureValidationResult.getValidationErrors().add(validationError);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void checkSystemConfig(String name, AServer server, FeatureValidationResult featureValidationResult) {
|
||||
if(!configService.configExists(server, name)) {
|
||||
SystemConfigValidationError validationError = SystemConfigValidationError.builder().configKey(name).build();
|
||||
featureValidationResult.setValidationResult(false);
|
||||
featureValidationResult.getValidationErrors().add(validationError);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
package dev.sheldan.abstracto.core.service;
|
||||
|
||||
import dev.sheldan.abstracto.core.config.DynamicKeyLoader;
|
||||
import dev.sheldan.abstracto.core.config.PostTargetEnum;
|
||||
import dev.sheldan.abstracto.core.exception.ChannelNotFoundException;
|
||||
import dev.sheldan.abstracto.core.exception.GuildException;
|
||||
import dev.sheldan.abstracto.core.exception.PostTargetNotFoundException;
|
||||
@@ -65,30 +66,30 @@ public class PostTargetServiceBean implements PostTargetService {
|
||||
}
|
||||
}
|
||||
|
||||
private PostTarget getPostTarget(String postTargetName, Long serverId) {
|
||||
PostTarget postTarget = postTargetManagement.getPostTarget(postTargetName, serverId);
|
||||
private PostTarget getPostTarget(PostTargetEnum postTargetName, Long serverId) {
|
||||
PostTarget postTarget = postTargetManagement.getPostTarget(postTargetName.getKey(), serverId);
|
||||
if(postTarget != null) {
|
||||
return postTarget;
|
||||
} else {
|
||||
log.error("PostTarget {} in server {} was not found!", postTargetName, serverId);
|
||||
throw new PostTargetNotFoundException(postTargetName);
|
||||
throw new PostTargetNotFoundException(postTargetName.getKey());
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public CompletableFuture<Message> sendTextInPostTarget(String text, String postTargetName, Long serverId) {
|
||||
PostTarget postTarget = this.getPostTarget(postTargetName, serverId);
|
||||
public CompletableFuture<Message> sendTextInPostTarget(String text, PostTargetEnum postTargetEnum, Long serverId) {
|
||||
PostTarget postTarget = this.getPostTarget(postTargetEnum, serverId);
|
||||
return this.sendTextInPostTarget(text, postTarget);
|
||||
}
|
||||
|
||||
@Override
|
||||
public CompletableFuture<Message> sendEmbedInPostTarget(MessageEmbed embed, String postTargetName, Long serverId) {
|
||||
public CompletableFuture<Message> sendEmbedInPostTarget(MessageEmbed embed, PostTargetEnum postTargetName, Long serverId) {
|
||||
PostTarget postTarget = this.getPostTarget(postTargetName, serverId);
|
||||
return this.sendEmbedInPostTarget(embed, postTarget);
|
||||
}
|
||||
|
||||
@Override
|
||||
public CompletableFuture<Message> sendMessageInPostTarget(Message message, String postTargetName, Long serverId) {
|
||||
public CompletableFuture<Message> sendMessageInPostTarget(Message message, PostTargetEnum postTargetName, Long serverId) {
|
||||
PostTarget postTarget = this.getPostTarget(postTargetName, serverId);
|
||||
return sendMessageInPostTarget(message, postTarget);
|
||||
}
|
||||
@@ -99,7 +100,7 @@ public class PostTargetServiceBean implements PostTargetService {
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<CompletableFuture<Message>> sendEmbedInPostTarget(MessageToSend message, String postTargetName, Long serverId) {
|
||||
public List<CompletableFuture<Message>> sendEmbedInPostTarget(MessageToSend message, PostTargetEnum postTargetName, Long serverId) {
|
||||
PostTarget postTarget = this.getPostTarget(postTargetName, serverId);
|
||||
return this.sendEmbedInPostTarget(message, postTarget);
|
||||
}
|
||||
@@ -163,21 +164,21 @@ public class PostTargetServiceBean implements PostTargetService {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void editOrCreatedInPostTarget(Long messageId, MessageToSend messageToSend, String postTargetName, Long serverId, List<CompletableFuture<Message>> future) {
|
||||
public void editOrCreatedInPostTarget(Long messageId, MessageToSend messageToSend, PostTargetEnum postTargetName, Long serverId, List<CompletableFuture<Message>> future) {
|
||||
PostTarget postTarget = this.getPostTarget(postTargetName, serverId);
|
||||
this.editOrCreatedInPostTarget(messageId, messageToSend, postTarget, future);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void throwIfPostTargetIsNotDefined(String name, Long serverId) {
|
||||
PostTarget postTarget = postTargetManagement.getPostTarget(name, serverId);
|
||||
public void throwIfPostTargetIsNotDefined(PostTargetEnum name, Long serverId) {
|
||||
PostTarget postTarget = postTargetManagement.getPostTarget(name.getKey(), serverId);
|
||||
if(postTarget == null) {
|
||||
throw new PostTargetNotValidException(name, dynamicKeyLoader.getPostTargetsAsList());
|
||||
throw new PostTargetNotValidException(name.getKey(), dynamicKeyLoader.getPostTargetsAsList());
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<CompletableFuture<Message>> editEmbedInPostTarget(Long messageId, MessageToSend message, String postTargetName, Long serverId) {
|
||||
public List<CompletableFuture<Message>> editEmbedInPostTarget(Long messageId, MessageToSend message, PostTargetEnum postTargetName, Long serverId) {
|
||||
PostTarget postTarget = this.getPostTarget(postTargetName, serverId);
|
||||
return editEmbedInPostTarget(messageId, message, postTarget);
|
||||
}
|
||||
|
||||
@@ -112,7 +112,12 @@ public class ConfigManagementServiceBean implements ConfigManagementService {
|
||||
|
||||
@Override
|
||||
public boolean configExists(Long serverId, String name) {
|
||||
return loadConfig(serverId, name) != null;
|
||||
return configRepository.existsAConfigByServerIdAndName(serverId, name);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean configExists(AServer server, String name) {
|
||||
return configRepository.existsAConfigByServerAndName(server, name);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -80,6 +80,17 @@ public class PostTargetManagementBean implements PostTargetManagement {
|
||||
return getPostTarget(name, server);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Boolean postTargetExists(String name, AServer server) {
|
||||
return postTargetRepository.existsByNameAndServerReference(name, server);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Boolean postTargetExists(String name, Long serverId) {
|
||||
AServer dbServer = serverManagementService.loadOrCreate(serverId);
|
||||
return postTargetExists(name, dbServer);
|
||||
}
|
||||
|
||||
@Override
|
||||
public PostTarget updatePostTarget(PostTarget target, AServer server, AChannel newTargetChannel) {
|
||||
target.setChannelReference(newTargetChannel);
|
||||
|
||||
@@ -0,0 +1,6 @@
|
||||
<#assign featureKey><#include "${featureTemplate}"></#assign><#include "feature_not_setup_message_text">
|
||||
|
||||
<#list errors as error>
|
||||
<#include "${error.templateName}">
|
||||
|
||||
</#list>
|
||||
Reference in New Issue
Block a user