[AB-233] adding feature to handle removed attachments in message edited events

now cloning the cached message for the listeners, in order to not influence the objects when updating it
This commit is contained in:
Sheldan
2021-04-24 18:03:06 +02:00
parent 27763e985d
commit 53bef3fdb3
18 changed files with 105 additions and 27 deletions

View File

@@ -3,6 +3,7 @@ package dev.sheldan.abstracto.logging.listener;
import dev.sheldan.abstracto.core.config.FeatureDefinition;
import dev.sheldan.abstracto.core.listener.DefaultListenerResult;
import dev.sheldan.abstracto.core.listener.async.jda.AsyncMessageUpdatedListener;
import dev.sheldan.abstracto.core.models.cache.CachedAttachment;
import dev.sheldan.abstracto.core.models.cache.CachedMessage;
import dev.sheldan.abstracto.core.models.listener.MessageUpdatedModel;
import dev.sheldan.abstracto.core.service.ChannelService;
@@ -12,6 +13,7 @@ import dev.sheldan.abstracto.core.templating.model.MessageToSend;
import dev.sheldan.abstracto.core.templating.service.TemplateService;
import dev.sheldan.abstracto.logging.config.LoggingFeatureDefinition;
import dev.sheldan.abstracto.logging.config.LoggingPostTarget;
import dev.sheldan.abstracto.logging.model.template.MessageDeletedAttachmentLog;
import dev.sheldan.abstracto.logging.model.template.MessageEditedLog;
import lombok.extern.slf4j.Slf4j;
import net.dv8tion.jda.api.entities.Message;
@@ -19,11 +21,15 @@ import net.dv8tion.jda.api.entities.TextChannel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.stream.Collectors;
@Service
@Slf4j
public class MessageEditedListener implements AsyncMessageUpdatedListener {
public static final String MESSAGE_EDITED_TEMPLATE = "message_edited";
public static final String MESSAGE_EDITED_ATTACHMENT_REMOVED_TEMPLATE = "message_edited_attachment_removed";
@Autowired
private TemplateService templateService;
@@ -41,13 +47,16 @@ public class MessageEditedListener implements AsyncMessageUpdatedListener {
public DefaultListenerResult execute(MessageUpdatedModel model) {
Message messageAfter = model.getAfter();
CachedMessage messageBefore = model.getBefore();
if(messageBefore.getContent().equals(messageAfter.getContentRaw())) {
log.debug("Message content was the same. Possible reason was: message was not in cache.");
int attachmentCountBefore = messageBefore.getAttachments() != null ? messageBefore.getAttachments().size() : 0;
int attachmentCountAfter = messageAfter.getAttachments().size();
boolean attachmentWasRemoved = attachmentCountAfter != attachmentCountBefore;
if(messageBefore.getContent().equals(messageAfter.getContentRaw()) && attachmentCountBefore == attachmentCountAfter) {
log.info("Message content was the same and attachment count did not change. Possible reason was: message was not in cache.");
return DefaultListenerResult.IGNORED;
}
log.debug("Message {} in channel {} in guild {} was edited.", messageBefore.getMessageId(), messageBefore.getChannelId(), model.getServerId());
TextChannel textChannel = channelService.getTextChannelFromServer(model.getServerId(), messageBefore.getChannelId());
MessageEditedLog log = MessageEditedLog
MessageEditedLog lodModel = MessageEditedLog
.builder()
.messageAfter(messageAfter)
.messageBefore(messageBefore)
@@ -55,8 +64,30 @@ public class MessageEditedListener implements AsyncMessageUpdatedListener {
.guild(textChannel.getGuild())
.member(messageAfter.getMember())
.build();
MessageToSend message = templateService.renderEmbedTemplate(MESSAGE_EDITED_TEMPLATE, log, model.getServerId());
MessageToSend message = templateService.renderEmbedTemplate(MESSAGE_EDITED_TEMPLATE, lodModel, model.getServerId());
postTargetService.sendEmbedInPostTarget(message, LoggingPostTarget.EDIT_LOG, model.getServerId());
if(attachmentWasRemoved) {
log.info("Attachment count changed. Old {}, new {}.", attachmentCountBefore, attachmentCountAfter);
// this filters attachments which were present in the cached message, but arent anymore in the new one
List<CachedAttachment> removedAttachments = messageBefore.getAttachments().stream().filter(cachedAttachment ->
messageAfter.getAttachments().stream().noneMatch(attachment -> attachment.getIdLong() == cachedAttachment.getId())
).collect(Collectors.toList());
log.info("Logging deletion of {} attachments.", removedAttachments.size());
for (int i = 0; i < removedAttachments.size(); i++) {
MessageDeletedAttachmentLog log = MessageDeletedAttachmentLog
.builder()
.imageUrl(removedAttachments.get(i).getProxyUrl())
.counter(i + 1)
.guild(messageAfter.getGuild())
.channel(textChannel)
.member(messageAfter.getMember())
.build();
MessageToSend attachmentEmbed = templateService.renderEmbedTemplate(MESSAGE_EDITED_ATTACHMENT_REMOVED_TEMPLATE,
log, messageBefore.getServerId());
postTargetService.sendEmbedInPostTarget(attachmentEmbed, LoggingPostTarget.DELETE_LOG, messageBefore.getServerId());
}
}
return DefaultListenerResult.PROCESSED;
}