mirror of
https://github.com/Sheldan/abstracto.git
synced 2026-01-23 20:30:21 +00:00
[AB-234] allow templates have individual (additional) allowed mention configurations
adding primary keys to command disabled and command cooldown group exposing service method to retrieve the channel for a post target
This commit is contained in:
@@ -10,10 +10,7 @@ import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.*;
|
||||
|
||||
@Component
|
||||
@Slf4j
|
||||
@@ -37,7 +34,7 @@ public class AllowedMentionServiceBean implements AllowedMentionService {
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Message.MentionType> getAllowedMentionTypesForServer(Long serverId) {
|
||||
public Set<Message.MentionType> getAllowedMentionTypesForServer(Long serverId) {
|
||||
AllowedMention allowedMention = getEffectiveAllowedMention(serverId);
|
||||
return mapAllowedMentionToMentionType(allowedMention);
|
||||
}
|
||||
@@ -79,12 +76,12 @@ public class AllowedMentionServiceBean implements AllowedMentionService {
|
||||
throw new UnknownMentionTypeException();
|
||||
}
|
||||
|
||||
private List<Message.MentionType> mapAllowedMentionToMentionType(AllowedMention allowedMention) {
|
||||
private Set<Message.MentionType> mapAllowedMentionToMentionType(AllowedMention allowedMention) {
|
||||
// if all are allowed, we dont want to restrict it
|
||||
if(allowedMention.allAllowed()) {
|
||||
return null;
|
||||
}
|
||||
List<Message.MentionType> types = new ArrayList<>();
|
||||
Set<Message.MentionType> types = new HashSet<>();
|
||||
if(allowedMention.getEveryone()) {
|
||||
types.add(Message.MentionType.EVERYONE);
|
||||
}
|
||||
|
||||
@@ -6,6 +6,7 @@ import dev.sheldan.abstracto.core.metric.service.MetricService;
|
||||
import dev.sheldan.abstracto.core.metric.service.MetricTag;
|
||||
import dev.sheldan.abstracto.core.models.database.AChannel;
|
||||
import dev.sheldan.abstracto.core.models.database.AServer;
|
||||
import dev.sheldan.abstracto.core.templating.model.MessageConfig;
|
||||
import dev.sheldan.abstracto.core.templating.model.MessageToSend;
|
||||
import dev.sheldan.abstracto.core.templating.service.TemplateService;
|
||||
import dev.sheldan.abstracto.core.utils.FileService;
|
||||
@@ -22,10 +23,7 @@ import org.springframework.transaction.annotation.Transactional;
|
||||
import javax.annotation.PostConstruct;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
|
||||
import static dev.sheldan.abstracto.core.config.MetricConstants.DISCORD_API_INTERACTION_METRIC;
|
||||
@@ -126,21 +124,34 @@ public class ChannelServiceBean implements ChannelService {
|
||||
log.debug("Sending message {} from channel {} and server {} to channel {}.",
|
||||
message.getId(), message.getChannel().getId(), message.getGuild().getId(), channel.getId());
|
||||
metricService.incrementCounter(MESSAGE_SEND_METRIC);
|
||||
return channel.sendMessage(message).allowedMentions(getAllowedMentionsFor(channel)).submit();
|
||||
return channel.sendMessage(message).allowedMentions(getAllowedMentionsFor(channel, null)).submit();
|
||||
}
|
||||
|
||||
private List<Message.MentionType> getAllowedMentionsFor(MessageChannel channel) {
|
||||
private Set<Message.MentionType> getAllowedMentionsFor(MessageChannel channel, MessageToSend messageToSend) {
|
||||
Set<Message.MentionType> allowedMentions = new HashSet<>();
|
||||
if(channel instanceof GuildChannel) {
|
||||
return allowedMentionService.getAllowedMentionTypesForServer(((GuildChannel) channel).getGuild().getIdLong());
|
||||
allowedMentions.addAll(allowedMentionService.getAllowedMentionTypesForServer(((GuildChannel) channel).getGuild().getIdLong()));
|
||||
}
|
||||
return null;
|
||||
if(messageToSend != null && messageToSend.getMessageConfig() != null) {
|
||||
MessageConfig messageConfig = messageToSend.getMessageConfig();
|
||||
if(messageConfig.isAllowsEveryoneMention()) {
|
||||
allowedMentions.add(Message.MentionType.EVERYONE);
|
||||
}
|
||||
if(messageConfig.isAllowsUserMention()) {
|
||||
allowedMentions.add(Message.MentionType.USER);
|
||||
}
|
||||
if(messageConfig.isAllowsRoleMention()) {
|
||||
allowedMentions.add(Message.MentionType.ROLE);
|
||||
}
|
||||
}
|
||||
return allowedMentions;
|
||||
}
|
||||
|
||||
@Override
|
||||
public CompletableFuture<Message> sendTextToChannel(String text, MessageChannel channel) {
|
||||
log.debug("Sending text to channel {}.", channel.getId());
|
||||
metricService.incrementCounter(MESSAGE_SEND_METRIC);
|
||||
return channel.sendMessage(text).allowedMentions(getAllowedMentionsFor(channel)).submit();
|
||||
return channel.sendMessage(text).allowedMentions(getAllowedMentionsFor(channel, null)).submit();
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -169,7 +180,7 @@ public class ChannelServiceBean implements ChannelService {
|
||||
@Override
|
||||
public MessageAction sendEmbedToChannelInComplete(MessageEmbed embed, MessageChannel channel) {
|
||||
metricService.incrementCounter(MESSAGE_SEND_METRIC);
|
||||
return channel.sendMessage(embed).allowedMentions(getAllowedMentionsFor(channel));
|
||||
return channel.sendMessage(embed).allowedMentions(getAllowedMentionsFor(channel, null));
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -232,7 +243,7 @@ public class ChannelServiceBean implements ChannelService {
|
||||
allMessageActions.add(textChannel.sendFile(messageToSend.getFileToSend()));
|
||||
}
|
||||
}
|
||||
List<Message.MentionType> allowedMentions = getAllowedMentionsFor(textChannel);
|
||||
Set<Message.MentionType> allowedMentions = getAllowedMentionsFor(textChannel, messageToSend);
|
||||
allMessageActions.forEach(messageAction ->
|
||||
futures.add(messageAction.allowedMentions(allowedMentions).submit())
|
||||
);
|
||||
|
||||
@@ -80,7 +80,8 @@ public class PostTargetServiceBean implements PostTargetService {
|
||||
}
|
||||
}
|
||||
|
||||
private PostTarget getPostTarget(PostTargetEnum postTargetName, Long serverId) {
|
||||
@Override
|
||||
public PostTarget getPostTarget(PostTargetEnum postTargetName, Long serverId) {
|
||||
PostTarget postTarget = postTargetManagement.getPostTarget(postTargetName.getKey(), serverId);
|
||||
if(postTarget != null) {
|
||||
return postTarget;
|
||||
|
||||
@@ -55,8 +55,5 @@ public class EmbedConfiguration {
|
||||
* The message which is posted along the {@link net.dv8tion.jda.api.entities.MessageEmbed} as a normal message.
|
||||
*/
|
||||
private String additionalMessage;
|
||||
private Long additionalMessageLengthLimit;
|
||||
private Long messageLimit;
|
||||
|
||||
private boolean preventEmptyEmbed = false;
|
||||
private MetaEmbedConfiguration metaConfig;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,19 @@
|
||||
package dev.sheldan.abstracto.core.templating.model;
|
||||
|
||||
import lombok.Builder;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
@Builder
|
||||
public class MetaEmbedConfiguration {
|
||||
private Long additionalMessageLengthLimit;
|
||||
private Long messageLimit;
|
||||
|
||||
private boolean preventEmptyEmbed;
|
||||
private boolean allowsRoleMention;
|
||||
private boolean allowsEveryoneMention;
|
||||
@Builder.Default
|
||||
private boolean allowsUserMention = true;
|
||||
}
|
||||
@@ -123,7 +123,10 @@ public class TemplateServiceBean implements TemplateService {
|
||||
List<String> messages = new ArrayList<>();
|
||||
String additionalMessage = embedConfiguration.getAdditionalMessage();
|
||||
if(additionalMessage != null) {
|
||||
Long segmentLimit = embedConfiguration.getAdditionalMessageLengthLimit() != null ? embedConfiguration.getAdditionalMessageLengthLimit() : Long.valueOf(Message.MAX_CONTENT_LENGTH);
|
||||
Long segmentLimit = embedConfiguration.getMetaConfig() != null
|
||||
&& embedConfiguration.getMetaConfig().getAdditionalMessageLengthLimit() != null ?
|
||||
embedConfiguration.getMetaConfig().getAdditionalMessageLengthLimit() :
|
||||
Long.valueOf(Message.MAX_CONTENT_LENGTH);
|
||||
if(additionalMessage.length() > segmentLimit) {
|
||||
int segmentStart = 0;
|
||||
int segmentEnd = segmentLimit.intValue();
|
||||
@@ -150,8 +153,8 @@ public class TemplateServiceBean implements TemplateService {
|
||||
if(serverContext.getServerId() != null) {
|
||||
messageLimit = Math.min(messageLimit, configService.getLongValueOrConfigDefault(CoreFeatureConfig.MAX_MESSAGES_KEY, serverContext.getServerId()));
|
||||
}
|
||||
if(embedConfiguration.getMessageLimit() != null) {
|
||||
messageLimit = Math.min(messageLimit, embedConfiguration.getMessageLimit());
|
||||
if(embedConfiguration.getMetaConfig() != null && embedConfiguration.getMetaConfig().getMessageLimit() != null) {
|
||||
messageLimit = Math.min(messageLimit, embedConfiguration.getMetaConfig().getMessageLimit());
|
||||
}
|
||||
if(embeds.size() > messageLimit) {
|
||||
log.info("Limiting size of embeds. Max allowed: {}, currently: {}.", messageLimit, embeds.size());
|
||||
@@ -164,10 +167,23 @@ public class TemplateServiceBean implements TemplateService {
|
||||
|
||||
return MessageToSend.builder()
|
||||
.embeds(embeds)
|
||||
.messageConfig(createMessageConfig(embedConfiguration.getMetaConfig()))
|
||||
.messages(messages)
|
||||
.build();
|
||||
}
|
||||
|
||||
private MessageConfig createMessageConfig(MetaEmbedConfiguration metaEmbedConfiguration) {
|
||||
if(metaEmbedConfiguration == null) {
|
||||
return null;
|
||||
}
|
||||
return MessageConfig
|
||||
.builder()
|
||||
.allowsEveryoneMention(metaEmbedConfiguration.isAllowsEveryoneMention())
|
||||
.allowsUserMention(metaEmbedConfiguration.isAllowsUserMention())
|
||||
.allowsRoleMention(metaEmbedConfiguration.isAllowsRoleMention())
|
||||
.build();
|
||||
}
|
||||
|
||||
private void setPagingFooters(List<EmbedBuilder> embedBuilders) {
|
||||
// the first footer comes from the configuration
|
||||
for (int i = 1; i < embedBuilders.size(); i++) {
|
||||
@@ -211,7 +227,7 @@ public class TemplateServiceBean implements TemplateService {
|
||||
}
|
||||
|
||||
private boolean isEmptyEmbed(EmbedConfiguration configuration) {
|
||||
if (configuration.isPreventEmptyEmbed()) {
|
||||
if (configuration.getMetaConfig() != null && configuration.getMetaConfig().isPreventEmptyEmbed()) {
|
||||
return configuration.getFields() == null && configuration.getDescription() == null && configuration.getImageUrl() == null;
|
||||
}
|
||||
return false;
|
||||
|
||||
@@ -9,7 +9,7 @@
|
||||
<changeSet author="Sheldan" id="command_disabled_channel_group-table">
|
||||
<createTable tableName="command_disabled_channel_group">
|
||||
<column name="id" type="BIGINT">
|
||||
<constraints nullable="true"/>
|
||||
<constraints nullable="true" primaryKey="true" primaryKeyName="pk_disabled_channel_group"/>
|
||||
</column>
|
||||
<column name="created" type="TIMESTAMP WITHOUT TIME ZONE">
|
||||
<constraints nullable="true"/>
|
||||
|
||||
@@ -9,7 +9,7 @@
|
||||
<changeSet author="Sheldan" id="cool_down_channel_group-table">
|
||||
<createTable tableName="cool_down_channel_group">
|
||||
<column name="id" type="BIGINT">
|
||||
<constraints nullable="true"/>
|
||||
<constraints nullable="true" primaryKey="true" primaryKeyName="pk_command_cooldown_channel_group"/>
|
||||
</column>
|
||||
<column name="created" type="TIMESTAMP WITHOUT TIME ZONE">
|
||||
<constraints nullable="true"/>
|
||||
|
||||
@@ -16,6 +16,7 @@ import org.mockito.junit.MockitoJUnitRunner;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
@@ -68,7 +69,7 @@ public class AllowedMentionServiceBeanTest {
|
||||
public void getAllowedMentionTypesForServerEmpty() {
|
||||
allDefaultConfigAllowed();
|
||||
when(allowedMentionManagementService.getCustomAllowedMentionFor(SERVER_ID)).thenReturn(Optional.empty());
|
||||
List<Message.MentionType> allowedMentions = testUnit.getAllowedMentionTypesForServer(SERVER_ID);
|
||||
Set<Message.MentionType> allowedMentions = testUnit.getAllowedMentionTypesForServer(SERVER_ID);
|
||||
Assert.assertNull(allowedMentions);
|
||||
}
|
||||
|
||||
@@ -78,9 +79,9 @@ public class AllowedMentionServiceBeanTest {
|
||||
when(allowedMentionConfig.getRole()).thenReturn(false);
|
||||
when(allowedMentionConfig.getUser()).thenReturn(false);
|
||||
when(allowedMentionManagementService.getCustomAllowedMentionFor(SERVER_ID)).thenReturn(Optional.empty());
|
||||
List<Message.MentionType> allowedMentions = testUnit.getAllowedMentionTypesForServer(SERVER_ID);
|
||||
Set<Message.MentionType> allowedMentions = testUnit.getAllowedMentionTypesForServer(SERVER_ID);
|
||||
Assert.assertEquals(1, allowedMentions.size());
|
||||
Assert.assertEquals(Message.MentionType.EVERYONE, allowedMentions.get(0));
|
||||
Assert.assertEquals(Message.MentionType.EVERYONE, allowedMentions.iterator().next());
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -89,7 +90,7 @@ public class AllowedMentionServiceBeanTest {
|
||||
when(allowedMentionConfig.getRole()).thenReturn(false);
|
||||
when(allowedMentionConfig.getUser()).thenReturn(false);
|
||||
when(allowedMentionManagementService.getCustomAllowedMentionFor(SERVER_ID)).thenReturn(Optional.empty());
|
||||
List<Message.MentionType> allowedMentions = testUnit.getAllowedMentionTypesForServer(SERVER_ID);
|
||||
Set<Message.MentionType> allowedMentions = testUnit.getAllowedMentionTypesForServer(SERVER_ID);
|
||||
Assert.assertEquals(0, allowedMentions.size());
|
||||
}
|
||||
|
||||
|
||||
@@ -99,9 +99,11 @@ public class TemplateServiceBeanTest {
|
||||
when(configService.getLongValueOrConfigDefault(CoreFeatureConfig.MAX_MESSAGES_KEY, SERVER_ID)).thenReturn(5L);
|
||||
String templateContent = String.format("{ \"additionalMessage\": \"%s\"}", additionalMessage);
|
||||
EmbedConfiguration config = Mockito.mock(EmbedConfiguration.class);
|
||||
when(config.getAdditionalMessageLengthLimit()).thenReturn(2000L);
|
||||
MetaEmbedConfiguration metaConfig = Mockito.mock(MetaEmbedConfiguration.class);
|
||||
when(config.getMetaConfig()).thenReturn(metaConfig);
|
||||
when(metaConfig.getAdditionalMessageLengthLimit()).thenReturn(2000L);
|
||||
when(config.getAdditionalMessage()).thenReturn(additionalMessage);
|
||||
when(config.getMessageLimit()).thenReturn(5L);
|
||||
when(metaConfig.getMessageLimit()).thenReturn(5L);
|
||||
when(configuration.getTemplate(getEmbedTemplateKey(), null, SERVER_ID, null, true, false)).thenReturn(new Template(getEmbedTemplateKey(), templateContent, getNonMockedConfiguration()));
|
||||
when(gson.fromJson(templateContent, EmbedConfiguration.class)).thenReturn(config);
|
||||
MessageToSend messageToSend = templateServiceBean.renderEmbedTemplate(TEMPLATE_KEY, new Object());
|
||||
@@ -117,9 +119,11 @@ public class TemplateServiceBeanTest {
|
||||
when(configService.getLongValueOrConfigDefault(CoreFeatureConfig.MAX_MESSAGES_KEY, SERVER_ID)).thenReturn(5L);
|
||||
String templateContent = String.format("{ \"additionalMessage\": \"%s\", \"messageLimit\": 1}", additionalMessage);
|
||||
EmbedConfiguration config = Mockito.mock(EmbedConfiguration.class);
|
||||
when(config.getAdditionalMessageLengthLimit()).thenReturn(2000L);
|
||||
MetaEmbedConfiguration metaConfig = Mockito.mock(MetaEmbedConfiguration.class);
|
||||
when(config.getMetaConfig()).thenReturn(metaConfig);
|
||||
when(metaConfig.getAdditionalMessageLengthLimit()).thenReturn(2000L);
|
||||
when(config.getAdditionalMessage()).thenReturn(additionalMessage);
|
||||
when(config.getMessageLimit()).thenReturn(1L);
|
||||
when(metaConfig.getMessageLimit()).thenReturn(1L);
|
||||
when(configuration.getTemplate(getEmbedTemplateKey(), null, SERVER_ID, null, true, false)).thenReturn(new Template(getEmbedTemplateKey(), templateContent, getNonMockedConfiguration()));
|
||||
when(gson.fromJson(templateContent, EmbedConfiguration.class)).thenReturn(config);
|
||||
MessageToSend messageToSend = templateServiceBean.renderEmbedTemplate(TEMPLATE_KEY, new Object());
|
||||
@@ -134,8 +138,10 @@ public class TemplateServiceBeanTest {
|
||||
when(configService.getLongValueOrConfigDefault(CoreFeatureConfig.MAX_MESSAGES_KEY, SERVER_ID)).thenReturn(5L);
|
||||
String templateContent = String.format("{ \"additionalMessage\": \"%s\"}", additionalMessage);
|
||||
EmbedConfiguration config = Mockito.mock(EmbedConfiguration.class);
|
||||
when(config.getAdditionalMessageLengthLimit()).thenReturn(500L);
|
||||
when(config.getMessageLimit()).thenReturn(5L);
|
||||
MetaEmbedConfiguration metaConfig = Mockito.mock(MetaEmbedConfiguration.class);
|
||||
when(config.getMetaConfig()).thenReturn(metaConfig);
|
||||
when(metaConfig.getAdditionalMessageLengthLimit()).thenReturn(500L);
|
||||
when(metaConfig.getMessageLimit()).thenReturn(5L);
|
||||
when(config.getAdditionalMessage()).thenReturn(additionalMessage);
|
||||
when(configuration.getTemplate(getEmbedTemplateKey(), null, SERVER_ID, null, true, false)).thenReturn(new Template(getEmbedTemplateKey(), templateContent, getNonMockedConfiguration()));
|
||||
when(gson.fromJson(templateContent, EmbedConfiguration.class)).thenReturn(config);
|
||||
|
||||
Reference in New Issue
Block a user