mirror of
https://github.com/Sheldan/abstracto.git
synced 2026-04-13 03:26:31 +00:00
[AB-295] adding hard limitations for field values, names, embed description and additional message lengths
This commit is contained in:
@@ -25,4 +25,12 @@ public class EmbedField {
|
|||||||
*/
|
*/
|
||||||
private Boolean inline;
|
private Boolean inline;
|
||||||
private Boolean forceNewMessage;
|
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;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,8 +8,11 @@ import lombok.Setter;
|
|||||||
@Setter
|
@Setter
|
||||||
@Builder
|
@Builder
|
||||||
public class MetaEmbedConfiguration {
|
public class MetaEmbedConfiguration {
|
||||||
private Long additionalMessageLengthLimit;
|
private Integer additionalMessageLengthLimit;
|
||||||
private Long messageLimit;
|
private Integer additionalMessageSplitLength;
|
||||||
|
private Integer descriptionMessageLengthLimit;
|
||||||
|
private Integer messageLimit;
|
||||||
|
|
||||||
|
|
||||||
private boolean preventEmptyEmbed;
|
private boolean preventEmptyEmbed;
|
||||||
private boolean allowsRoleMention;
|
private boolean allowsRoleMention;
|
||||||
|
|||||||
@@ -78,6 +78,14 @@ public class TemplateServiceBean implements TemplateService {
|
|||||||
EmbedConfiguration embedConfiguration = gson.fromJson(embedConfig, EmbedConfiguration.class);
|
EmbedConfiguration embedConfiguration = gson.fromJson(embedConfig, EmbedConfiguration.class);
|
||||||
List<EmbedBuilder> embedBuilders = new ArrayList<>();
|
List<EmbedBuilder> embedBuilders = new ArrayList<>();
|
||||||
embedBuilders.add(new EmbedBuilder());
|
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();
|
String description = embedConfiguration.getDescription();
|
||||||
if (description != null) {
|
if (description != null) {
|
||||||
handleEmbedDescription(embedBuilders, description);
|
handleEmbedDescription(embedBuilders, description);
|
||||||
@@ -121,11 +129,20 @@ public class TemplateServiceBean implements TemplateService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
List<String> messages = new ArrayList<>();
|
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();
|
String additionalMessage = embedConfiguration.getAdditionalMessage();
|
||||||
if(additionalMessage != null) {
|
if(additionalMessage != null) {
|
||||||
Long segmentLimit = embedConfiguration.getMetaConfig() != null
|
Long segmentLimit = embedConfiguration.getMetaConfig() != null
|
||||||
&& embedConfiguration.getMetaConfig().getAdditionalMessageLengthLimit() != null ?
|
&& embedConfiguration.getMetaConfig().getAdditionalMessageSplitLength() != null ?
|
||||||
embedConfiguration.getMetaConfig().getAdditionalMessageLengthLimit() :
|
embedConfiguration.getMetaConfig().getAdditionalMessageSplitLength() :
|
||||||
Long.valueOf(Message.MAX_CONTENT_LENGTH);
|
Long.valueOf(Message.MAX_CONTENT_LENGTH);
|
||||||
if(additionalMessage.length() > segmentLimit) {
|
if(additionalMessage.length() > segmentLimit) {
|
||||||
int segmentStart = 0;
|
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
|
// and we need to insert the "first" element last, so it actually gets the correct field index
|
||||||
comparator = comparator.thenComparing(additionalEmbedField -> additionalEmbedField.innerFieldIndex).reversed();
|
comparator = comparator.thenComparing(additionalEmbedField -> additionalEmbedField.innerFieldIndex).reversed();
|
||||||
TreeSet<AdditionalEmbedField> toInsert = new TreeSet<>(comparator);
|
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++) {
|
for (int i = 0; i < configuration.getFields().size(); i++) {
|
||||||
EmbedField field = configuration.getFields().get(i);
|
EmbedField field = configuration.getFields().get(i);
|
||||||
if (field != null && field.getValue() != null && field.getValue().length() > MessageEmbed.VALUE_MAX_LENGTH) {
|
if (field != null && field.getValue() != null && field.getValue().length() > MessageEmbed.VALUE_MAX_LENGTH) {
|
||||||
|
|||||||
@@ -101,9 +101,9 @@ public class TemplateServiceBeanTest {
|
|||||||
EmbedConfiguration config = Mockito.mock(EmbedConfiguration.class);
|
EmbedConfiguration config = Mockito.mock(EmbedConfiguration.class);
|
||||||
MetaEmbedConfiguration metaConfig = Mockito.mock(MetaEmbedConfiguration.class);
|
MetaEmbedConfiguration metaConfig = Mockito.mock(MetaEmbedConfiguration.class);
|
||||||
when(config.getMetaConfig()).thenReturn(metaConfig);
|
when(config.getMetaConfig()).thenReturn(metaConfig);
|
||||||
when(metaConfig.getAdditionalMessageLengthLimit()).thenReturn(2000L);
|
when(metaConfig.getAdditionalMessageSplitLength()).thenReturn(2000);
|
||||||
when(config.getAdditionalMessage()).thenReturn(additionalMessage);
|
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(configuration.getTemplate(getEmbedTemplateKey(), null, SERVER_ID, null, true, false)).thenReturn(new Template(getEmbedTemplateKey(), templateContent, getNonMockedConfiguration()));
|
||||||
when(gson.fromJson(templateContent, EmbedConfiguration.class)).thenReturn(config);
|
when(gson.fromJson(templateContent, EmbedConfiguration.class)).thenReturn(config);
|
||||||
MessageToSend messageToSend = templateServiceBean.renderEmbedTemplate(TEMPLATE_KEY, new Object());
|
MessageToSend messageToSend = templateServiceBean.renderEmbedTemplate(TEMPLATE_KEY, new Object());
|
||||||
@@ -121,9 +121,9 @@ public class TemplateServiceBeanTest {
|
|||||||
EmbedConfiguration config = Mockito.mock(EmbedConfiguration.class);
|
EmbedConfiguration config = Mockito.mock(EmbedConfiguration.class);
|
||||||
MetaEmbedConfiguration metaConfig = Mockito.mock(MetaEmbedConfiguration.class);
|
MetaEmbedConfiguration metaConfig = Mockito.mock(MetaEmbedConfiguration.class);
|
||||||
when(config.getMetaConfig()).thenReturn(metaConfig);
|
when(config.getMetaConfig()).thenReturn(metaConfig);
|
||||||
when(metaConfig.getAdditionalMessageLengthLimit()).thenReturn(2000L);
|
when(metaConfig.getAdditionalMessageSplitLength()).thenReturn(2000);
|
||||||
when(config.getAdditionalMessage()).thenReturn(additionalMessage);
|
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(configuration.getTemplate(getEmbedTemplateKey(), null, SERVER_ID, null, true, false)).thenReturn(new Template(getEmbedTemplateKey(), templateContent, getNonMockedConfiguration()));
|
||||||
when(gson.fromJson(templateContent, EmbedConfiguration.class)).thenReturn(config);
|
when(gson.fromJson(templateContent, EmbedConfiguration.class)).thenReturn(config);
|
||||||
MessageToSend messageToSend = templateServiceBean.renderEmbedTemplate(TEMPLATE_KEY, new Object());
|
MessageToSend messageToSend = templateServiceBean.renderEmbedTemplate(TEMPLATE_KEY, new Object());
|
||||||
@@ -140,8 +140,8 @@ public class TemplateServiceBeanTest {
|
|||||||
EmbedConfiguration config = Mockito.mock(EmbedConfiguration.class);
|
EmbedConfiguration config = Mockito.mock(EmbedConfiguration.class);
|
||||||
MetaEmbedConfiguration metaConfig = Mockito.mock(MetaEmbedConfiguration.class);
|
MetaEmbedConfiguration metaConfig = Mockito.mock(MetaEmbedConfiguration.class);
|
||||||
when(config.getMetaConfig()).thenReturn(metaConfig);
|
when(config.getMetaConfig()).thenReturn(metaConfig);
|
||||||
when(metaConfig.getAdditionalMessageLengthLimit()).thenReturn(500L);
|
when(metaConfig.getAdditionalMessageSplitLength()).thenReturn(500);
|
||||||
when(metaConfig.getMessageLimit()).thenReturn(5L);
|
when(metaConfig.getMessageLimit()).thenReturn(5);
|
||||||
when(config.getAdditionalMessage()).thenReturn(additionalMessage);
|
when(config.getAdditionalMessage()).thenReturn(additionalMessage);
|
||||||
when(configuration.getTemplate(getEmbedTemplateKey(), null, SERVER_ID, null, true, false)).thenReturn(new Template(getEmbedTemplateKey(), templateContent, getNonMockedConfiguration()));
|
when(configuration.getTemplate(getEmbedTemplateKey(), null, SERVER_ID, null, true, false)).thenReturn(new Template(getEmbedTemplateKey(), templateContent, getNonMockedConfiguration()));
|
||||||
when(gson.fromJson(templateContent, EmbedConfiguration.class)).thenReturn(config);
|
when(gson.fromJson(templateContent, EmbedConfiguration.class)).thenReturn(config);
|
||||||
|
|||||||
Reference in New Issue
Block a user