created concept of auto load macros, these are macro files which are loaded automatically on startup and can be used in any template

changed assignable post config to be a full emote instead
This commit is contained in:
Sheldan
2020-08-16 12:40:41 +02:00
parent fd4d784081
commit fbb36ae9d5
12 changed files with 127 additions and 5 deletions

View File

@@ -510,7 +510,7 @@ public class AssignableRolePlaceServiceBean implements AssignableRolePlaceServic
AssignablePostConfigRole postRole = AssignablePostConfigRole
.builder()
.description(role.getDescription())
.emote(jdaEmoteForRole)
.emote(FullEmote.builder().fakeEmote(emoteForRole).emote(jdaEmoteForRole).build())
.position(role.getPosition())
.awardedRole(jdaRole)
.build();

View File

@@ -1,21 +1,19 @@
package dev.sheldan.abstracto.assignableroles.models.templates;
import dev.sheldan.abstracto.core.models.database.ARole;
import dev.sheldan.abstracto.core.models.FullEmote;
import lombok.Builder;
import lombok.Getter;
import lombok.Setter;
import net.dv8tion.jda.api.entities.Emote;
import net.dv8tion.jda.api.entities.Role;
@Getter
@Setter
@Builder
public class AssignablePostConfigRole {
private Emote emote;
private String description;
private Integer position;
private Boolean inline;
private ARole role;
private FullEmote emote;
private Role awardedRole;
}

View File

@@ -4,6 +4,8 @@ import dev.sheldan.abstracto.templating.loading.DatabaseTemplateLoader;
import dev.sheldan.abstracto.templating.methods.DurationMethod;
import dev.sheldan.abstracto.templating.methods.DateMethod;
import dev.sheldan.abstracto.templating.methods.SafeFieldIterations;
import dev.sheldan.abstracto.templating.model.database.AutoLoadMacro;
import dev.sheldan.abstracto.templating.service.management.AutoLoadMacroManagementService;
import freemarker.template.Configuration;
import freemarker.template.TemplateException;
import org.springframework.beans.factory.annotation.Autowired;
@@ -11,7 +13,10 @@ import org.springframework.context.annotation.Bean;
import org.springframework.ui.freemarker.FreeMarkerConfigurationFactory;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.Locale;
import java.util.stream.Collectors;
/**
* Configuration bean used to provide the {@link Configuration} bean to the spring context.
@@ -31,6 +36,9 @@ public class FreemarkerConfiguration {
@Autowired
private SafeFieldIterations safeFieldIterations;
@Autowired
private AutoLoadMacroManagementService macroManagementService;
/**
* Creates a {@link Configuration} bean with the appropriate configuration which includes:
* The correct compatibility version and the provided formatter methods to be used in the templates.
@@ -45,6 +53,9 @@ public class FreemarkerConfiguration {
configuration.setSharedVariable("fmtDuration", durationMethod);
configuration.setSharedVariable("formatDate", instantMethod);
configuration.setSharedVariable("safeFieldLength", safeFieldIterations);
List<String> macrosToLoad = macroManagementService.loadAllMacros().stream()
.map(AutoLoadMacro::getKey).collect(Collectors.toList());
configuration.setAutoIncludes(macrosToLoad);
configuration.setEncoding(Locale.getDefault(), "utf-8");
// needed to support default methods in interfaces
configuration.setIncompatibleImprovements(Configuration.VERSION_2_3_29);

View File

@@ -0,0 +1,9 @@
package dev.sheldan.abstracto.templating.repository;
import dev.sheldan.abstracto.templating.model.database.AutoLoadMacro;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface AutoLoadMacroRepository extends JpaRepository<AutoLoadMacro, String> {
}

View File

@@ -0,0 +1,20 @@
package dev.sheldan.abstracto.templating.service.management;
import dev.sheldan.abstracto.templating.model.database.AutoLoadMacro;
import dev.sheldan.abstracto.templating.repository.AutoLoadMacroRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.List;
@Component
public class AutoLoadMacroManagementServiceBean implements AutoLoadMacroManagementService {
@Autowired
private AutoLoadMacroRepository repository;
@Override
public List<AutoLoadMacro> loadAllMacros() {
return repository.findAll();
}
}

View File

@@ -7,4 +7,5 @@
http://www.liquibase.org/xml/ns/dbchangelog-ext ../dbchangelog-3.8.xsd
http://www.liquibase.org/xml/ns/pro ../dbchangelog-3.8.xsd" >
<include file="templating-tables/tables.xml" relativeToChangelogFile="true"/>
<include file="templating-seedData/data.xml" relativeToChangelogFile="true"/>
</databaseChangeLog>

View File

@@ -0,0 +1,18 @@
<?xml version="1.1" encoding="UTF-8" standalone="no"?>
<databaseChangeLog xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
xmlns:ext="http://www.liquibase.org/xml/ns/dbchangelog-ext"
xmlns:pro="http://www.liquibase.org/xml/ns/pro"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog ../../dbchangelog-3.8.xsd
http://www.liquibase.org/xml/ns/dbchangelog-ext ../../dbchangelog-3.8.xsd
http://www.liquibase.org/xml/ns/pro ../../dbchangelog-3.8.xsd" >
<changeSet author="Sheldan" id="templates-auto_load_macro">
<insert tableName="auto_load_macro">
<column name="key" value="safe_macro"/>
</insert>
<insert tableName="auto_load_macro">
<column name="key" value="member_user_name"/>
</insert>
</changeSet>
</databaseChangeLog>

View File

@@ -0,0 +1,10 @@
<?xml version="1.1" encoding="UTF-8" standalone="no"?>
<databaseChangeLog xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
xmlns:ext="http://www.liquibase.org/xml/ns/dbchangelog-ext"
xmlns:pro="http://www.liquibase.org/xml/ns/pro"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog ../../dbchangelog-3.8.xsd
http://www.liquibase.org/xml/ns/dbchangelog-ext ../../dbchangelog-3.8.xsd
http://www.liquibase.org/xml/ns/pro ../../dbchangelog-3.8.xsd" >
<include file="auto_load_macro.xml" relativeToChangelogFile="true"/>
</databaseChangeLog>

View File

@@ -0,0 +1,21 @@
<?xml version="1.1" encoding="UTF-8" standalone="no"?>
<databaseChangeLog xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
xmlns:ext="http://www.liquibase.org/xml/ns/dbchangelog-ext"
xmlns:pro="http://www.liquibase.org/xml/ns/pro"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog ../../dbchangelog-3.8.xsd
http://www.liquibase.org/xml/ns/dbchangelog-ext ../../dbchangelog-3.8.xsd
http://www.liquibase.org/xml/ns/pro ../../dbchangelog-3.8.xsd" >
<changeSet author="Sheldan" id="auto_load_macro-table">
<createTable tableName="auto_load_macro">
<column name="key" type="VARCHAR(255)">
<constraints nullable="false" primaryKey="true" primaryKeyName="auto_load_macro_pkey"/>
</column>
</createTable>
</changeSet>
<changeSet author="Sheldan" id="auto_load_macro-fk_assignable_role_emote">
<addForeignKeyConstraint baseColumnNames="key" baseTableName="auto_load_macro" constraintName="fk_auto_load_macro_template" deferrable="false" initiallyDeferred="false" onDelete="NO ACTION" onUpdate="NO ACTION" referencedColumnNames="key" referencedTableName="template" validate="true"/>
</changeSet>
</databaseChangeLog>

View File

@@ -7,4 +7,5 @@
http://www.liquibase.org/xml/ns/dbchangelog-ext ../../dbchangelog-3.8.xsd
http://www.liquibase.org/xml/ns/pro ../../dbchangelog-3.8.xsd" >
<include file="template.xml" relativeToChangelogFile="true"/>
<include file="auto_load_macro.xml" relativeToChangelogFile="true"/>
</databaseChangeLog>

View File

@@ -0,0 +1,24 @@
package dev.sheldan.abstracto.templating.model.database;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import javax.persistence.*;
@Builder
@Entity
@NoArgsConstructor
@AllArgsConstructor
@Table(name = "auto_load_macro")
@Getter
public class AutoLoadMacro {
@Id
private String key;
@OneToOne(fetch = FetchType.LAZY, cascade = {CascadeType.PERSIST, CascadeType.MERGE})
@PrimaryKeyJoinColumn
private Template template;
}

View File

@@ -0,0 +1,9 @@
package dev.sheldan.abstracto.templating.service.management;
import dev.sheldan.abstracto.templating.model.database.AutoLoadMacro;
import java.util.List;
public interface AutoLoadMacroManagementService {
List<AutoLoadMacro> loadAllMacros();
}