added duration field to reminder and restructured remind text

added general duration formatting to freemarker (its a builtin method currently
added source channel attribute to starboard post
added exception logging to immediate reminders
This commit is contained in:
Sheldan
2020-04-21 21:54:47 +02:00
parent 7b56b89157
commit ff8817f765
11 changed files with 117 additions and 5 deletions

View File

@@ -1,6 +1,7 @@
package dev.sheldan.abstracto.templating.config;
import dev.sheldan.abstracto.templating.loading.DatabaseTemplateLoader;
import dev.sheldan.abstracto.templating.methods.DurationMethod;
import freemarker.template.Configuration;
import freemarker.template.TemplateException;
import org.springframework.beans.factory.annotation.Autowired;
@@ -16,11 +17,15 @@ public class FreemarkerConfiguration {
@Autowired
private DatabaseTemplateLoader templateLoader;
@Autowired
private DurationMethod durationMethod;
@Bean
public Configuration freeMarkerConfiguration() throws IOException, TemplateException {
FreeMarkerConfigurationFactory factory = new FreeMarkerConfigurationFactory();
factory.setPreTemplateLoaders(templateLoader);
Configuration configuration = factory.createConfiguration();
configuration.setSharedVariable("fmtDuration", durationMethod);
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,59 @@
package dev.sheldan.abstracto.templating.methods;
import dev.sheldan.abstracto.templating.service.TemplateService;
import freemarker.ext.beans.StringModel;
import freemarker.template.TemplateMethodModelEx;
import freemarker.template.TemplateModelException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.time.Duration;
import java.time.temporal.ChronoUnit;
import java.util.HashMap;
import java.util.List;
@Component
public class DurationMethod implements TemplateMethodModelEx {
@Autowired
private TemplateService service;
@Override
public Object exec(List arguments) throws TemplateModelException {
if (arguments.size() != 1) {
throw new TemplateModelException("Incorrect parameters passed.");
}
Object wrappedObject = ((StringModel) arguments.get(0)).getWrappedObject();
if(!(wrappedObject instanceof Duration)) {
throw new TemplateModelException("Passed argument was not a duration object");
}
Duration duration = (Duration) wrappedObject;
StringBuilder stringBuilder = new StringBuilder();
// upgrading to java 9 makes this nicer
long days = duration.toDays();
if(days > 0) {
stringBuilder.append(service.renderTemplate("day", getParam(days)));
}
long hours = duration.toHours() % 24;
if(hours > 0) {
stringBuilder.append(service.renderTemplate("hour", getParam(hours)));
}
long minutes = duration.toMinutes() % 60;
if(minutes > 0) {
stringBuilder.append(service.renderTemplate("minute", getParam(minutes)));
}
long seconds = duration.get(ChronoUnit.SECONDS) % 60;
if(seconds > 0) {
stringBuilder.append(service.renderTemplate("second", getParam(seconds)));
}
return stringBuilder.toString();
}
private HashMap<String, Object> getParam(Long value) {
HashMap<String, Object> params = new HashMap<>();
params.put("amount", value);
return params;
}
}

View File

@@ -0,0 +1,5 @@
<#if amount gt 1>
${amount} days
<#else>
1 day
</#if>

View File

@@ -0,0 +1,5 @@
<#if amount gt 1>
${amount} hours
<#else>
1 hour
</#if>

View File

@@ -0,0 +1,5 @@
<#if amount gt 1>
${amount} minutes
<#else>
1 minute
</#if>

View File

@@ -0,0 +1,5 @@
<#if amount gt 1>
${amount} seconds
<#else>
1 second
</#if>