mirror of
https://github.com/Sheldan/abstracto.git
synced 2026-03-20 04:31:06 +00:00
upgraded spring boot starter version
added gson dependency added logging the exception in the exception post executor added configuration exception in case the configuration for posttargets is not correct added support to send embeds to posttargets changed interface to be a warnlog instead of a server context for warning users, because the template changed freemarker Incompatible Improvements to support default methods in interfaces added ability to define embed configurations to be used, in order for a template to only be configurable as a template changed template content size to 4000 characters
This commit is contained in:
@@ -22,10 +22,17 @@
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-freemarker</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>commons-io</groupId>
|
||||
<artifactId>commons-io</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.google.code.gson</groupId>
|
||||
<artifactId>gson</artifactId>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
@@ -19,6 +19,9 @@ public class FreemarkerConfiguration {
|
||||
public Configuration freeMarkerConfiguration() throws IOException, TemplateException {
|
||||
FreeMarkerConfigurationFactory factory = new FreeMarkerConfigurationFactory();
|
||||
factory.setPreTemplateLoaders(templateLoader);
|
||||
return factory.createConfiguration();
|
||||
Configuration configuration = factory.createConfiguration();
|
||||
// needed to support default methods in interfaces
|
||||
configuration.setIncompatibleImprovements(Configuration.VERSION_2_3_29);
|
||||
return configuration;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
package dev.sheldan.abstracto.templating.embed;
|
||||
|
||||
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
|
||||
@Getter @Setter
|
||||
public class EmbedAuthor {
|
||||
private String name;
|
||||
private String url;
|
||||
private String avatar;
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
package dev.sheldan.abstracto.templating.embed;
|
||||
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
|
||||
@Setter @Getter
|
||||
public class EmbedColor {
|
||||
private Integer r;
|
||||
private Integer g;
|
||||
private Integer b;
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package dev.sheldan.abstracto.templating.embed;
|
||||
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Getter @Setter
|
||||
public class EmbedConfiguration {
|
||||
private EmbedAuthor author;
|
||||
private EmbedTitle title;
|
||||
private EmbedColor color;
|
||||
private String description;
|
||||
private String thumbnail;
|
||||
private List<EmbedField> fields;
|
||||
private EmbedFooter footer;
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
package dev.sheldan.abstracto.templating.embed;
|
||||
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
|
||||
@Getter @Setter
|
||||
public class EmbedField {
|
||||
private String name;
|
||||
private String value;
|
||||
private Boolean inline;
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
package dev.sheldan.abstracto.templating.embed;
|
||||
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
|
||||
@Getter @Setter
|
||||
public class EmbedFooter {
|
||||
private String text;
|
||||
private String icon;
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
package dev.sheldan.abstracto.templating.embed;
|
||||
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
|
||||
@Getter @Setter
|
||||
public class EmbedTitle {
|
||||
private String title;
|
||||
private String url;
|
||||
}
|
||||
@@ -1,18 +1,24 @@
|
||||
package dev.sheldan.abstracto.templating.loading;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import dev.sheldan.abstracto.core.models.ContextAware;
|
||||
import dev.sheldan.abstracto.core.models.ServerContext;
|
||||
import dev.sheldan.abstracto.templating.TemplateDto;
|
||||
import dev.sheldan.abstracto.templating.TemplateService;
|
||||
import dev.sheldan.abstracto.templating.embed.*;
|
||||
import freemarker.template.Configuration;
|
||||
import freemarker.template.Template;
|
||||
import freemarker.template.TemplateException;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import net.dv8tion.jda.api.EmbedBuilder;
|
||||
import net.dv8tion.jda.api.entities.MessageEmbed;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.cache.annotation.Cacheable;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.ui.freemarker.FreeMarkerTemplateUtils;
|
||||
|
||||
import java.awt.*;
|
||||
import java.io.IOException;
|
||||
import java.io.StringReader;
|
||||
import java.time.Instant;
|
||||
import java.util.HashMap;
|
||||
|
||||
@@ -26,8 +32,10 @@ public class TemplateServiceBean implements TemplateService {
|
||||
@Autowired
|
||||
private Configuration configuration;
|
||||
|
||||
@Autowired
|
||||
private Gson gson;
|
||||
|
||||
@Override
|
||||
@Cacheable("template")
|
||||
public TemplateDto getTemplateByKey(String key) {
|
||||
return repository.getOne(key);
|
||||
}
|
||||
@@ -42,6 +50,58 @@ public class TemplateServiceBean implements TemplateService {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public MessageEmbed renderEmbedTemplate(String key, Object model) {
|
||||
String embedConfig = this.renderTemplate(key + "_embed", model);
|
||||
EmbedBuilder builder = new EmbedBuilder();
|
||||
EmbedConfiguration configuration = gson.fromJson(embedConfig, EmbedConfiguration.class);
|
||||
String description = configuration.getDescription();
|
||||
if(description != null) {
|
||||
builder.setDescription(description);
|
||||
}
|
||||
EmbedAuthor author = configuration.getAuthor();
|
||||
if(author != null) {
|
||||
builder.setAuthor(author.getName(), author.getUrl(), author.getAvatar());
|
||||
}
|
||||
|
||||
String thumbnail = configuration.getThumbnail();
|
||||
if(thumbnail != null) {
|
||||
builder.setThumbnail(thumbnail);
|
||||
}
|
||||
EmbedTitle title = configuration.getTitle();
|
||||
if(title != null) {
|
||||
builder.setTitle(title.getTitle(), title.getUrl());
|
||||
}
|
||||
EmbedFooter footer = configuration.getFooter();
|
||||
if(footer != null) {
|
||||
builder.setFooter(footer.getText(), footer.getIcon());
|
||||
}
|
||||
configuration.getFields().forEach(embedField -> {
|
||||
Boolean inline = embedField.getInline() != null ? embedField.getInline() : Boolean.FALSE;
|
||||
builder.addField(embedField.getName(), embedField.getValue(), inline);
|
||||
});
|
||||
|
||||
EmbedColor color = configuration.getColor();
|
||||
if(color != null) {
|
||||
builder.setColor(new Color(color.getR(), color.getG(), color.getB()).getRGB());
|
||||
}
|
||||
|
||||
return builder.build();
|
||||
}
|
||||
|
||||
private String impromptu(String templateStr, Object model) {
|
||||
try {
|
||||
Template t = new Template("name", new StringReader(templateStr),
|
||||
new Configuration(Configuration.VERSION_2_3_29));
|
||||
return FreeMarkerTemplateUtils.processTemplateIntoString(t, model);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
} catch (TemplateException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String renderTemplate(String key, HashMap<String, Object> parameters) {
|
||||
try {
|
||||
@@ -58,8 +118,8 @@ public class TemplateServiceBean implements TemplateService {
|
||||
return FreeMarkerTemplateUtils.processTemplateIntoString(configuration.getTemplate(key), model);
|
||||
} catch (IOException | TemplateException e) {
|
||||
log.warn("Failed to render template: {}", e.getMessage());
|
||||
throw new RuntimeException("Failed to render template", e);
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package dev.sheldan.abstracto.templating.seeding;
|
||||
|
||||
import dev.sheldan.abstracto.templating.TemplateService;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.io.FilenameUtils;
|
||||
import org.apache.commons.io.IOUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
@@ -16,6 +17,7 @@ import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
@Component
|
||||
@Slf4j
|
||||
public class TemplateSeedDataLoader {
|
||||
|
||||
@Value("classpath*:**/templates/**/*.ftl")
|
||||
@@ -31,6 +33,7 @@ public class TemplateSeedDataLoader {
|
||||
try {
|
||||
String templateKey = FilenameUtils.getBaseName(resource.getFilename());
|
||||
String templateContent = IOUtils.toString(resource.getURI(), Charset.defaultCharset());
|
||||
log.debug("Creating template {}", templateKey);
|
||||
service.createTemplate(templateKey, templateContent);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
|
||||
Reference in New Issue
Block a user