added some utility methods to bot service

reworked message cache to not be jda messages, but own objects: cachedMessages, jda messages should not be kept around
added listener to listen for message links, to represent them in an embed (or embeds)
added offsetDateTime handling for gson
introduced a way on how to handle async transaction handling for suggestions and message loading (self reference with another method with transactional)
added timestamp support to embeds
This commit is contained in:
Sheldan
2020-03-24 20:36:36 +01:00
parent 8cd9068cfc
commit 7d63f6b8af
43 changed files with 696 additions and 136 deletions

View File

@@ -10,13 +10,16 @@ import dev.sheldan.abstracto.templating.TemplateService;
import dev.sheldan.abstracto.utility.models.Suggestion;
import dev.sheldan.abstracto.utility.models.SuggestionState;
import dev.sheldan.abstracto.utility.models.template.SuggestionLog;
import dev.sheldan.abstracto.utility.service.management.AsyncSuggestionServiceBean;
import dev.sheldan.abstracto.utility.service.management.SuggestionManagementService;
import lombok.extern.slf4j.Slf4j;
import net.dv8tion.jda.api.JDA;
import net.dv8tion.jda.api.entities.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import java.util.Optional;
@Component
@Slf4j
@@ -38,15 +41,15 @@ public class SuggestionServiceBean implements SuggestionService {
@Autowired
private Bot botService;
@Autowired
private AsyncSuggestionServiceBean suggestionServiceBean;
@Autowired
private EmoteManagementService emoteManagementService;
@Autowired
private MessageService messageService;
@Autowired
private SuggestionServiceBean self;
@Override
public void createSuggestion(Member member, String text, SuggestionLog suggestionLog) {
Suggestion suggestion = suggestionManagementService.createSuggestion(member, text);
@@ -93,13 +96,25 @@ public class SuggestionServiceBean implements SuggestionService {
TextChannel textChannelById = guildById.getTextChannelById(channelId);
if(textChannelById != null) {
textChannelById.retrieveMessageById(originalMessageId).queue(message -> {
suggestionServiceBean.updateSuggestionMessageText(text, suggestionLog, message);
self.updateSuggestionMessageText(text, suggestionLog, message);
});
}
}
}
}
@Transactional(propagation = Propagation.REQUIRES_NEW)
public void updateSuggestionMessageText(String text, SuggestionLog suggestionLog, Message message) {
Optional<MessageEmbed> embedOptional = message.getEmbeds().stream().filter(embed -> embed.getDescription() != null).findFirst();
if(embedOptional.isPresent()) {
MessageEmbed suggestionEmbed = embedOptional.get();
suggestionLog.setReason(text);
suggestionLog.setText(suggestionEmbed.getDescription());
MessageEmbed embed = templateService.renderEmbedTemplate(SUGGESTION_LOG_TEMPLATE, suggestionLog);
postTargetService.sendEmbedInPostTarget(embed, SUGGESTIONS_TARGET, suggestionLog.getServer().getId());
}
}
@Override
public void rejectSuggestion(Long suggestionId, String text, SuggestionLog log) {
Suggestion suggestion = suggestionManagementService.getSuggestion(suggestionId);

View File

@@ -1,41 +0,0 @@
package dev.sheldan.abstracto.utility.service.management;
import dev.sheldan.abstracto.core.service.PostTargetService;
import dev.sheldan.abstracto.templating.TemplateService;
import dev.sheldan.abstracto.utility.models.template.SuggestionLog;
import lombok.extern.slf4j.Slf4j;
import net.dv8tion.jda.api.entities.Message;
import net.dv8tion.jda.api.entities.MessageEmbed;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import java.util.Optional;
import static dev.sheldan.abstracto.utility.service.SuggestionServiceBean.SUGGESTION_LOG_TEMPLATE;
import static dev.sheldan.abstracto.utility.service.SuggestionServiceBean.SUGGESTIONS_TARGET;
@Component
@Slf4j
public class AsyncSuggestionServiceBean {
@Autowired
private TemplateService templateService;
@Autowired
private PostTargetService postTargetService;
@Transactional(propagation = Propagation.REQUIRES_NEW)
public void updateSuggestionMessageText(String text, SuggestionLog suggestionLog, Message message) {
Optional<MessageEmbed> embedOptional = message.getEmbeds().stream().filter(embed -> embed.getDescription() != null).findFirst();
if(embedOptional.isPresent()) {
MessageEmbed suggestionEmbed = embedOptional.get();
suggestionLog.setReason(text);
suggestionLog.setText(suggestionEmbed.getDescription());
MessageEmbed embed = templateService.renderEmbedTemplate(SUGGESTION_LOG_TEMPLATE, suggestionLog);
postTargetService.sendEmbedInPostTarget(embed, SUGGESTIONS_TARGET, suggestionLog.getServer().getId());
}
}
}