[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

@@ -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();
}
}