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

@@ -28,9 +28,7 @@ import java.time.Duration;
import java.time.Instant;
import java.util.Date;
import java.util.Optional;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.*;
@Component
@Slf4j
@@ -80,8 +78,14 @@ public class RemindServiceBean implements ReminderService {
log.trace("Directly scheduling the reminder, because it was below the threshold.");
ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
scheduler.schedule(() -> {
try {
self.executeReminder(reminder.getId());
} catch (Exception exception) {
log.error("Failed to remind immediately.", exception);
}
}, remindIn.toNanos(), TimeUnit.NANOSECONDS);
} else {
log.trace("Starting scheduled job to execute reminder.");
JobDataMap parameters = new JobDataMap();
@@ -109,6 +113,7 @@ public class RemindServiceBean implements ReminderService {
.builder()
.reminder(reminderToRemindFor)
.member(memberInServer)
.duration(Duration.between(reminderToRemindFor.getReminderDate(), reminderToRemindFor.getTargetDate()))
.build();
MessageToSend messageToSend = templateService.renderEmbedTemplate("remind_reminder", build);
channelService.sendMessageToEndInTextChannel(messageToSend, channelToAnswerIn.get());

View File

@@ -2,6 +2,7 @@ package dev.sheldan.abstracto.utility.service.management;
import dev.sheldan.abstracto.core.models.AServerAChannelMessage;
import dev.sheldan.abstracto.core.models.cache.CachedMessage;
import dev.sheldan.abstracto.core.models.database.AChannel;
import dev.sheldan.abstracto.core.models.database.AUserInAServer;
import dev.sheldan.abstracto.utility.models.database.StarboardPost;
import dev.sheldan.abstracto.utility.repository.StarboardPostRepository;
@@ -32,6 +33,7 @@ public class StarboardPostManagementServiceBean implements StarboardPostManageme
.postMessageId(starredMessage.getMessageId())
.starboardMessageId(starboardPost.getMessageId())
.starboardChannel(starboardPost.getChannel())
.sourceChanel(AChannel.builder().id(starredMessage.getChannelId()).build())
.starredDate(Instant.now())
.build();
repository.save(post);

View File

@@ -8,6 +8,20 @@
"g": 0,
"b": 255
},
"description": "You wanted to be reminded of: '${reminder.text}'. Original message was [here](${messageUrl}).",
"description": "You wanted to be reminded.",
"fields": [
{
"name": "Duration",
"value": "${fmtDuration(duration)}"
},
{
"name": "Note",
"value": "${reminder.text}"
},
{
"name": "Link",
"value": "[Jump!](${messageUrl})"
}
],
"additionalMessage": "${member.asMention}"
}

View File

@@ -35,6 +35,10 @@ public class StarboardPost {
@JoinColumn(name = "channelId")
private AChannel starboardChannel;
@ManyToOne
@JoinColumn(name = "sourceChannelId")
private AChannel sourceChanel;
@Transient
private Integer reactionCount;

View File

@@ -8,12 +8,15 @@ import lombok.Setter;
import lombok.experimental.SuperBuilder;
import net.dv8tion.jda.api.entities.Member;
import java.time.Duration;
@Getter
@Setter
@SuperBuilder
public class ExecutedReminderModel extends ServerContext {
private Reminder reminder;
private Member member;
private Duration duration;
public String getMessageUrl() {
return MessageUtils.buildMessageUrl(this.reminder.getServer().getId() ,this.reminder.getChannel().getId(), this.reminder.getMessageId());

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>