mirror of
https://github.com/Sheldan/abstracto.git
synced 2026-04-15 04:02:53 +00:00
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:
@@ -28,9 +28,7 @@ import java.time.Duration;
|
|||||||
import java.time.Instant;
|
import java.time.Instant;
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
import java.util.concurrent.Executors;
|
import java.util.concurrent.*;
|
||||||
import java.util.concurrent.ScheduledExecutorService;
|
|
||||||
import java.util.concurrent.TimeUnit;
|
|
||||||
|
|
||||||
@Component
|
@Component
|
||||||
@Slf4j
|
@Slf4j
|
||||||
@@ -80,8 +78,14 @@ public class RemindServiceBean implements ReminderService {
|
|||||||
log.trace("Directly scheduling the reminder, because it was below the threshold.");
|
log.trace("Directly scheduling the reminder, because it was below the threshold.");
|
||||||
ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
|
ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
|
||||||
scheduler.schedule(() -> {
|
scheduler.schedule(() -> {
|
||||||
|
try {
|
||||||
self.executeReminder(reminder.getId());
|
self.executeReminder(reminder.getId());
|
||||||
|
} catch (Exception exception) {
|
||||||
|
log.error("Failed to remind immediately.", exception);
|
||||||
|
}
|
||||||
}, remindIn.toNanos(), TimeUnit.NANOSECONDS);
|
}, remindIn.toNanos(), TimeUnit.NANOSECONDS);
|
||||||
|
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
log.trace("Starting scheduled job to execute reminder.");
|
log.trace("Starting scheduled job to execute reminder.");
|
||||||
JobDataMap parameters = new JobDataMap();
|
JobDataMap parameters = new JobDataMap();
|
||||||
@@ -109,6 +113,7 @@ public class RemindServiceBean implements ReminderService {
|
|||||||
.builder()
|
.builder()
|
||||||
.reminder(reminderToRemindFor)
|
.reminder(reminderToRemindFor)
|
||||||
.member(memberInServer)
|
.member(memberInServer)
|
||||||
|
.duration(Duration.between(reminderToRemindFor.getReminderDate(), reminderToRemindFor.getTargetDate()))
|
||||||
.build();
|
.build();
|
||||||
MessageToSend messageToSend = templateService.renderEmbedTemplate("remind_reminder", build);
|
MessageToSend messageToSend = templateService.renderEmbedTemplate("remind_reminder", build);
|
||||||
channelService.sendMessageToEndInTextChannel(messageToSend, channelToAnswerIn.get());
|
channelService.sendMessageToEndInTextChannel(messageToSend, channelToAnswerIn.get());
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ package dev.sheldan.abstracto.utility.service.management;
|
|||||||
|
|
||||||
import dev.sheldan.abstracto.core.models.AServerAChannelMessage;
|
import dev.sheldan.abstracto.core.models.AServerAChannelMessage;
|
||||||
import dev.sheldan.abstracto.core.models.cache.CachedMessage;
|
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.core.models.database.AUserInAServer;
|
||||||
import dev.sheldan.abstracto.utility.models.database.StarboardPost;
|
import dev.sheldan.abstracto.utility.models.database.StarboardPost;
|
||||||
import dev.sheldan.abstracto.utility.repository.StarboardPostRepository;
|
import dev.sheldan.abstracto.utility.repository.StarboardPostRepository;
|
||||||
@@ -32,6 +33,7 @@ public class StarboardPostManagementServiceBean implements StarboardPostManageme
|
|||||||
.postMessageId(starredMessage.getMessageId())
|
.postMessageId(starredMessage.getMessageId())
|
||||||
.starboardMessageId(starboardPost.getMessageId())
|
.starboardMessageId(starboardPost.getMessageId())
|
||||||
.starboardChannel(starboardPost.getChannel())
|
.starboardChannel(starboardPost.getChannel())
|
||||||
|
.sourceChanel(AChannel.builder().id(starredMessage.getChannelId()).build())
|
||||||
.starredDate(Instant.now())
|
.starredDate(Instant.now())
|
||||||
.build();
|
.build();
|
||||||
repository.save(post);
|
repository.save(post);
|
||||||
|
|||||||
@@ -8,6 +8,20 @@
|
|||||||
"g": 0,
|
"g": 0,
|
||||||
"b": 255
|
"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}"
|
"additionalMessage": "${member.asMention}"
|
||||||
}
|
}
|
||||||
@@ -35,6 +35,10 @@ public class StarboardPost {
|
|||||||
@JoinColumn(name = "channelId")
|
@JoinColumn(name = "channelId")
|
||||||
private AChannel starboardChannel;
|
private AChannel starboardChannel;
|
||||||
|
|
||||||
|
@ManyToOne
|
||||||
|
@JoinColumn(name = "sourceChannelId")
|
||||||
|
private AChannel sourceChanel;
|
||||||
|
|
||||||
@Transient
|
@Transient
|
||||||
private Integer reactionCount;
|
private Integer reactionCount;
|
||||||
|
|
||||||
|
|||||||
@@ -8,12 +8,15 @@ import lombok.Setter;
|
|||||||
import lombok.experimental.SuperBuilder;
|
import lombok.experimental.SuperBuilder;
|
||||||
import net.dv8tion.jda.api.entities.Member;
|
import net.dv8tion.jda.api.entities.Member;
|
||||||
|
|
||||||
|
import java.time.Duration;
|
||||||
|
|
||||||
@Getter
|
@Getter
|
||||||
@Setter
|
@Setter
|
||||||
@SuperBuilder
|
@SuperBuilder
|
||||||
public class ExecutedReminderModel extends ServerContext {
|
public class ExecutedReminderModel extends ServerContext {
|
||||||
private Reminder reminder;
|
private Reminder reminder;
|
||||||
private Member member;
|
private Member member;
|
||||||
|
private Duration duration;
|
||||||
|
|
||||||
public String getMessageUrl() {
|
public String getMessageUrl() {
|
||||||
return MessageUtils.buildMessageUrl(this.reminder.getServer().getId() ,this.reminder.getChannel().getId(), this.reminder.getMessageId());
|
return MessageUtils.buildMessageUrl(this.reminder.getServer().getId() ,this.reminder.getChannel().getId(), this.reminder.getMessageId());
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package dev.sheldan.abstracto.templating.config;
|
package dev.sheldan.abstracto.templating.config;
|
||||||
|
|
||||||
import dev.sheldan.abstracto.templating.loading.DatabaseTemplateLoader;
|
import dev.sheldan.abstracto.templating.loading.DatabaseTemplateLoader;
|
||||||
|
import dev.sheldan.abstracto.templating.methods.DurationMethod;
|
||||||
import freemarker.template.Configuration;
|
import freemarker.template.Configuration;
|
||||||
import freemarker.template.TemplateException;
|
import freemarker.template.TemplateException;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
@@ -16,11 +17,15 @@ public class FreemarkerConfiguration {
|
|||||||
@Autowired
|
@Autowired
|
||||||
private DatabaseTemplateLoader templateLoader;
|
private DatabaseTemplateLoader templateLoader;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private DurationMethod durationMethod;
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
public Configuration freeMarkerConfiguration() throws IOException, TemplateException {
|
public Configuration freeMarkerConfiguration() throws IOException, TemplateException {
|
||||||
FreeMarkerConfigurationFactory factory = new FreeMarkerConfigurationFactory();
|
FreeMarkerConfigurationFactory factory = new FreeMarkerConfigurationFactory();
|
||||||
factory.setPreTemplateLoaders(templateLoader);
|
factory.setPreTemplateLoaders(templateLoader);
|
||||||
Configuration configuration = factory.createConfiguration();
|
Configuration configuration = factory.createConfiguration();
|
||||||
|
configuration.setSharedVariable("fmtDuration", durationMethod);
|
||||||
configuration.setEncoding(Locale.getDefault(), "utf-8");
|
configuration.setEncoding(Locale.getDefault(), "utf-8");
|
||||||
// needed to support default methods in interfaces
|
// needed to support default methods in interfaces
|
||||||
configuration.setIncompatibleImprovements(Configuration.VERSION_2_3_29);
|
configuration.setIncompatibleImprovements(Configuration.VERSION_2_3_29);
|
||||||
|
|||||||
@@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
<#if amount gt 1>
|
||||||
|
${amount} days
|
||||||
|
<#else>
|
||||||
|
1 day
|
||||||
|
</#if>
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
<#if amount gt 1>
|
||||||
|
${amount} hours
|
||||||
|
<#else>
|
||||||
|
1 hour
|
||||||
|
</#if>
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
<#if amount gt 1>
|
||||||
|
${amount} minutes
|
||||||
|
<#else>
|
||||||
|
1 minute
|
||||||
|
</#if>
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
<#if amount gt 1>
|
||||||
|
${amount} seconds
|
||||||
|
<#else>
|
||||||
|
1 second
|
||||||
|
</#if>
|
||||||
Reference in New Issue
Block a user