[AB-295] adding hard limitations for field values, names, embed description and additional message lengths

This commit is contained in:
Sheldan
2021-06-27 16:05:40 +02:00
parent ebf659fb14
commit e655adf95e
4 changed files with 47 additions and 10 deletions

View File

@@ -25,4 +25,12 @@ public class EmbedField {
*/
private Boolean inline;
private Boolean forceNewMessage;
/**
* this will actively limit the length, not create another field
*/
private Integer valueLengthLimit;
/**
* this will actively limit the length, not create another field
*/
private Integer nameLengthLimit;
}

View File

@@ -8,8 +8,11 @@ import lombok.Setter;
@Setter
@Builder
public class MetaEmbedConfiguration {
private Long additionalMessageLengthLimit;
private Long messageLimit;
private Integer additionalMessageLengthLimit;
private Integer additionalMessageSplitLength;
private Integer descriptionMessageLengthLimit;
private Integer messageLimit;
private boolean preventEmptyEmbed;
private boolean allowsRoleMention;

View File

@@ -78,6 +78,14 @@ public class TemplateServiceBean implements TemplateService {
EmbedConfiguration embedConfiguration = gson.fromJson(embedConfig, EmbedConfiguration.class);
List<EmbedBuilder> embedBuilders = new ArrayList<>();
embedBuilders.add(new EmbedBuilder());
if(embedConfiguration.getMetaConfig() != null &&
embedConfiguration.getDescription() != null &&
embedConfiguration.getMetaConfig().getDescriptionMessageLengthLimit() != null &&
embedConfiguration.getDescription().length() > embedConfiguration.getMetaConfig().getDescriptionMessageLengthLimit())
{
embedConfiguration.setDescription(embedConfiguration.getDescription().substring(0,
embedConfiguration.getMetaConfig().getDescriptionMessageLengthLimit()));
}
String description = embedConfiguration.getDescription();
if (description != null) {
handleEmbedDescription(embedBuilders, description);
@@ -121,11 +129,20 @@ public class TemplateServiceBean implements TemplateService {
}
List<String> messages = new ArrayList<>();
if(
embedConfiguration.getMetaConfig() != null &&
embedConfiguration.getMetaConfig().getAdditionalMessageLengthLimit() != null &&
embedConfiguration.getAdditionalMessage().length() > embedConfiguration.getMetaConfig().getAdditionalMessageLengthLimit()
) {
embedConfiguration.setAdditionalMessage(embedConfiguration.getAdditionalMessage().substring(0, embedConfiguration.getMetaConfig().getAdditionalMessageLengthLimit()));
}
String additionalMessage = embedConfiguration.getAdditionalMessage();
if(additionalMessage != null) {
Long segmentLimit = embedConfiguration.getMetaConfig() != null
&& embedConfiguration.getMetaConfig().getAdditionalMessageLengthLimit() != null ?
embedConfiguration.getMetaConfig().getAdditionalMessageLengthLimit() :
&& embedConfiguration.getMetaConfig().getAdditionalMessageSplitLength() != null ?
embedConfiguration.getMetaConfig().getAdditionalMessageSplitLength() :
Long.valueOf(Message.MAX_CONTENT_LENGTH);
if(additionalMessage.length() > segmentLimit) {
int segmentStart = 0;
@@ -257,6 +274,15 @@ public class TemplateServiceBean implements TemplateService {
// and we need to insert the "first" element last, so it actually gets the correct field index
comparator = comparator.thenComparing(additionalEmbedField -> additionalEmbedField.innerFieldIndex).reversed();
TreeSet<AdditionalEmbedField> toInsert = new TreeSet<>(comparator);
configuration.getFields().forEach(embedField -> {
if(embedField.getValueLengthLimit() != null && embedField.getValueLengthLimit() < embedField.getValue().length()) {
embedField.setValue(embedField.getValue().substring(0, embedField.getValueLengthLimit()));
}
if(embedField.getNameLengthLimit() != null && embedField.getNameLengthLimit() < embedField.getName().length()) {
embedField.setName(embedField.getName().substring(0, embedField.getNameLengthLimit()));
}
});
for (int i = 0; i < configuration.getFields().size(); i++) {
EmbedField field = configuration.getFields().get(i);
if (field != null && field.getValue() != null && field.getValue().length() > MessageEmbed.VALUE_MAX_LENGTH) {

View File

@@ -101,9 +101,9 @@ public class TemplateServiceBeanTest {
EmbedConfiguration config = Mockito.mock(EmbedConfiguration.class);
MetaEmbedConfiguration metaConfig = Mockito.mock(MetaEmbedConfiguration.class);
when(config.getMetaConfig()).thenReturn(metaConfig);
when(metaConfig.getAdditionalMessageLengthLimit()).thenReturn(2000L);
when(metaConfig.getAdditionalMessageSplitLength()).thenReturn(2000);
when(config.getAdditionalMessage()).thenReturn(additionalMessage);
when(metaConfig.getMessageLimit()).thenReturn(5L);
when(metaConfig.getMessageLimit()).thenReturn(5);
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());
@@ -121,9 +121,9 @@ public class TemplateServiceBeanTest {
EmbedConfiguration config = Mockito.mock(EmbedConfiguration.class);
MetaEmbedConfiguration metaConfig = Mockito.mock(MetaEmbedConfiguration.class);
when(config.getMetaConfig()).thenReturn(metaConfig);
when(metaConfig.getAdditionalMessageLengthLimit()).thenReturn(2000L);
when(metaConfig.getAdditionalMessageSplitLength()).thenReturn(2000);
when(config.getAdditionalMessage()).thenReturn(additionalMessage);
when(metaConfig.getMessageLimit()).thenReturn(1L);
when(metaConfig.getMessageLimit()).thenReturn(1);
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());
@@ -140,8 +140,8 @@ public class TemplateServiceBeanTest {
EmbedConfiguration config = Mockito.mock(EmbedConfiguration.class);
MetaEmbedConfiguration metaConfig = Mockito.mock(MetaEmbedConfiguration.class);
when(config.getMetaConfig()).thenReturn(metaConfig);
when(metaConfig.getAdditionalMessageLengthLimit()).thenReturn(500L);
when(metaConfig.getMessageLimit()).thenReturn(5L);
when(metaConfig.getAdditionalMessageSplitLength()).thenReturn(500);
when(metaConfig.getMessageLimit()).thenReturn(5);
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);