[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.config.FeatureDefinition;
import dev.sheldan.abstracto.core.listener.DefaultListenerResult; import dev.sheldan.abstracto.core.listener.DefaultListenerResult;
import dev.sheldan.abstracto.core.listener.async.jda.AsyncMessageUpdatedListener; 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.cache.CachedMessage;
import dev.sheldan.abstracto.core.models.listener.MessageUpdatedModel; import dev.sheldan.abstracto.core.models.listener.MessageUpdatedModel;
import dev.sheldan.abstracto.core.service.ChannelService; 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.core.templating.service.TemplateService;
import dev.sheldan.abstracto.logging.config.LoggingFeatureDefinition; import dev.sheldan.abstracto.logging.config.LoggingFeatureDefinition;
import dev.sheldan.abstracto.logging.config.LoggingPostTarget; import dev.sheldan.abstracto.logging.config.LoggingPostTarget;
import dev.sheldan.abstracto.logging.model.template.MessageDeletedAttachmentLog;
import dev.sheldan.abstracto.logging.model.template.MessageEditedLog; import dev.sheldan.abstracto.logging.model.template.MessageEditedLog;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import net.dv8tion.jda.api.entities.Message; 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.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.util.List;
import java.util.stream.Collectors;
@Service @Service
@Slf4j @Slf4j
public class MessageEditedListener implements AsyncMessageUpdatedListener { public class MessageEditedListener implements AsyncMessageUpdatedListener {
public static final String MESSAGE_EDITED_TEMPLATE = "message_edited"; public static final String MESSAGE_EDITED_TEMPLATE = "message_edited";
public static final String MESSAGE_EDITED_ATTACHMENT_REMOVED_TEMPLATE = "message_edited_attachment_removed";
@Autowired @Autowired
private TemplateService templateService; private TemplateService templateService;
@@ -41,13 +47,16 @@ public class MessageEditedListener implements AsyncMessageUpdatedListener {
public DefaultListenerResult execute(MessageUpdatedModel model) { public DefaultListenerResult execute(MessageUpdatedModel model) {
Message messageAfter = model.getAfter(); Message messageAfter = model.getAfter();
CachedMessage messageBefore = model.getBefore(); CachedMessage messageBefore = model.getBefore();
if(messageBefore.getContent().equals(messageAfter.getContentRaw())) { int attachmentCountBefore = messageBefore.getAttachments() != null ? messageBefore.getAttachments().size() : 0;
log.debug("Message content was the same. Possible reason was: message was not in cache."); 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; return DefaultListenerResult.IGNORED;
} }
log.debug("Message {} in channel {} in guild {} was edited.", messageBefore.getMessageId(), messageBefore.getChannelId(), model.getServerId()); log.debug("Message {} in channel {} in guild {} was edited.", messageBefore.getMessageId(), messageBefore.getChannelId(), model.getServerId());
TextChannel textChannel = channelService.getTextChannelFromServer(model.getServerId(), messageBefore.getChannelId()); TextChannel textChannel = channelService.getTextChannelFromServer(model.getServerId(), messageBefore.getChannelId());
MessageEditedLog log = MessageEditedLog MessageEditedLog lodModel = MessageEditedLog
.builder() .builder()
.messageAfter(messageAfter) .messageAfter(messageAfter)
.messageBefore(messageBefore) .messageBefore(messageBefore)
@@ -55,8 +64,30 @@ public class MessageEditedListener implements AsyncMessageUpdatedListener {
.guild(textChannel.getGuild()) .guild(textChannel.getGuild())
.member(messageAfter.getMember()) .member(messageAfter.getMember())
.build(); .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()); 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; return DefaultListenerResult.PROCESSED;
} }

View File

@@ -1,18 +1,22 @@
package dev.sheldan.abstracto.core.listener; package dev.sheldan.abstracto.core.listener.combo;
import dev.sheldan.abstracto.core.listener.ListenerService;
import dev.sheldan.abstracto.core.listener.async.jda.AsyncMessageUpdatedListener; import dev.sheldan.abstracto.core.listener.async.jda.AsyncMessageUpdatedListener;
import dev.sheldan.abstracto.core.listener.sync.jda.MessageUpdatedListener; import dev.sheldan.abstracto.core.listener.sync.jda.MessageUpdatedListener;
import dev.sheldan.abstracto.core.metric.service.CounterMetric; import dev.sheldan.abstracto.core.metric.service.CounterMetric;
import dev.sheldan.abstracto.core.metric.service.MetricService; import dev.sheldan.abstracto.core.metric.service.MetricService;
import dev.sheldan.abstracto.core.metric.service.MetricTag; import dev.sheldan.abstracto.core.metric.service.MetricTag;
import dev.sheldan.abstracto.core.models.cache.CachedAttachment;
import dev.sheldan.abstracto.core.models.cache.CachedMessage; import dev.sheldan.abstracto.core.models.cache.CachedMessage;
import dev.sheldan.abstracto.core.models.listener.MessageUpdatedModel; import dev.sheldan.abstracto.core.models.listener.MessageUpdatedModel;
import dev.sheldan.abstracto.core.service.CacheEntityService;
import dev.sheldan.abstracto.core.service.MessageCache; import dev.sheldan.abstracto.core.service.MessageCache;
import dev.sheldan.abstracto.core.utils.BeanUtils; import dev.sheldan.abstracto.core.utils.BeanUtils;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import net.dv8tion.jda.api.entities.Message; import net.dv8tion.jda.api.entities.Message;
import net.dv8tion.jda.api.events.message.guild.GuildMessageUpdateEvent; import net.dv8tion.jda.api.events.message.guild.GuildMessageUpdateEvent;
import net.dv8tion.jda.api.hooks.ListenerAdapter; import net.dv8tion.jda.api.hooks.ListenerAdapter;
import org.apache.commons.lang3.SerializationUtils;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.core.task.TaskExecutor; import org.springframework.core.task.TaskExecutor;
@@ -23,6 +27,7 @@ import javax.annotation.Nonnull;
import javax.annotation.PostConstruct; import javax.annotation.PostConstruct;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
import java.util.stream.Collectors;
import static dev.sheldan.abstracto.core.listener.sync.jda.MessageReceivedListenerBean.ACTION; import static dev.sheldan.abstracto.core.listener.sync.jda.MessageReceivedListenerBean.ACTION;
import static dev.sheldan.abstracto.core.listener.sync.jda.MessageReceivedListenerBean.MESSAGE_METRIC; import static dev.sheldan.abstracto.core.listener.sync.jda.MessageReceivedListenerBean.MESSAGE_METRIC;
@@ -53,6 +58,9 @@ public class MessageUpdatedListenerBean extends ListenerAdapter {
@Autowired @Autowired
private MetricService metricService; private MetricService metricService;
@Autowired
private CacheEntityService cacheEntityService;
private static final CounterMetric MESSAGE_UPDATED_COUNTER = CounterMetric private static final CounterMetric MESSAGE_UPDATED_COUNTER = CounterMetric
.builder() .builder()
.name(MESSAGE_METRIC) .name(MESSAGE_METRIC)
@@ -66,12 +74,16 @@ public class MessageUpdatedListenerBean extends ListenerAdapter {
Message message = event.getMessage(); Message message = event.getMessage();
messageCache.getMessageFromCache(message.getGuild().getIdLong(), message.getTextChannel().getIdLong(), event.getMessageIdLong()).thenAccept(cachedMessage -> { messageCache.getMessageFromCache(message.getGuild().getIdLong(), message.getTextChannel().getIdLong(), event.getMessageIdLong()).thenAccept(cachedMessage -> {
try { try {
executeAsyncListeners(event, cachedMessage); // we need to provide a copy of the object, so modifications here dont influence the async execution
if (listenerList != null) { // because we do modify it, as we are the one responsible for caching it
self.executeListener(cachedMessage, event); executeAsyncListeners(event, SerializationUtils.clone(cachedMessage));
} self.executeListener(cachedMessage, event);
} finally { } finally {
cachedMessage.setContent(message.getContentRaw()); cachedMessage.setContent(message.getContentRaw());
List<CachedAttachment> remainingAttachments = cachedMessage.getAttachments().stream().filter(cachedAttachment ->
message.getAttachments().stream().anyMatch(attachment -> attachment.getIdLong() == cachedAttachment.getId())
).collect(Collectors.toList());
cachedMessage.setAttachments(remainingAttachments);
messageCache.putMessageInCache(cachedMessage); messageCache.putMessageInCache(cachedMessage);
} }
}).exceptionally(throwable -> { }).exceptionally(throwable -> {

View File

@@ -67,6 +67,7 @@ public class CacheEntityServiceBean implements CacheEntityService {
public CachedAttachment getCachedAttachment(Message.Attachment attachment) { public CachedAttachment getCachedAttachment(Message.Attachment attachment) {
return CachedAttachment return CachedAttachment
.builder() .builder()
.id(attachment.getIdLong())
.fileName(attachment.getFileName()) .fileName(attachment.getFileName())
.height(attachment.getHeight()) .height(attachment.getHeight())
.proxyUrl(attachment.getProxyUrl()) .proxyUrl(attachment.getProxyUrl())
@@ -131,6 +132,15 @@ public class CacheEntityServiceBean implements CacheEntityService {
return builder.build(); return builder.build();
} }
@Override
public List<CachedAttachment> getCachedAttachments(List<Message.Attachment> attachments) {
List<CachedAttachment> cachedAttachments = new ArrayList<>();
attachments.forEach(attachment ->
cachedAttachments.add(getCachedAttachment(attachment))
);
return cachedAttachments;
}
@Override @Override
public CachedThumbnail buildCachedThumbnail(MessageEmbed.Thumbnail thumbnail) { public CachedThumbnail buildCachedThumbnail(MessageEmbed.Thumbnail thumbnail) {
return CachedThumbnail return CachedThumbnail
@@ -181,11 +191,8 @@ public class CacheEntityServiceBean implements CacheEntityService {
@Override @Override
public CompletableFuture<CachedMessage> buildCachedMessageFromMessage(Message message, boolean loadReferenced) { public CompletableFuture<CachedMessage> buildCachedMessageFromMessage(Message message, boolean loadReferenced) {
CompletableFuture<CachedMessage> future = new CompletableFuture<>(); CompletableFuture<CachedMessage> future = new CompletableFuture<>();
List<CachedAttachment> attachments = new ArrayList<>();
log.debug("Caching {} attachments.", message.getAttachments().size()); log.debug("Caching {} attachments.", message.getAttachments().size());
message.getAttachments().forEach(attachment -> List<CachedAttachment> attachments = getCachedAttachments(message.getAttachments());
attachments.add(getCachedAttachment(attachment))
);
log.debug("Caching {} embeds.", message.getEmbeds().size()); log.debug("Caching {} embeds.", message.getEmbeds().size());
List<CachedEmbed> embeds = new ArrayList<>(); List<CachedEmbed> embeds = new ArrayList<>();
message.getEmbeds().forEach(embed -> message.getEmbeds().forEach(embed ->

View File

@@ -6,11 +6,13 @@ import lombok.EqualsAndHashCode;
import lombok.Getter; import lombok.Getter;
import lombok.Setter; import lombok.Setter;
import java.io.Serializable;
@Getter @Getter
@Setter @Setter
@Builder @Builder
@EqualsAndHashCode @EqualsAndHashCode
public class ServerUser { public class ServerUser implements Serializable {
private Long serverId; private Long serverId;
private Long userId; private Long userId;

View File

@@ -4,11 +4,14 @@ import lombok.Builder;
import lombok.Getter; import lombok.Getter;
import lombok.Setter; import lombok.Setter;
import java.io.Serializable;
@Getter @Getter
@Setter @Setter
@Builder @Builder
public class CachedAttachment { public class CachedAttachment implements Serializable {
private String url; private String url;
private Long id;
private String proxyUrl; private String proxyUrl;
private String fileName; private String fileName;
private Integer size; private Integer size;

View File

@@ -4,10 +4,12 @@ import lombok.Builder;
import lombok.Getter; import lombok.Getter;
import lombok.Setter; import lombok.Setter;
import java.io.Serializable;
@Getter @Getter
@Setter @Setter
@Builder @Builder
public class CachedAuthor { public class CachedAuthor implements Serializable {
private Long authorId; private Long authorId;
private Boolean isBot; private Boolean isBot;
} }

View File

@@ -5,13 +5,14 @@ import lombok.Getter;
import lombok.Setter; import lombok.Setter;
import net.dv8tion.jda.api.entities.EmbedType; import net.dv8tion.jda.api.entities.EmbedType;
import java.io.Serializable;
import java.time.OffsetDateTime; import java.time.OffsetDateTime;
import java.util.List; import java.util.List;
@Getter @Getter
@Setter @Setter
@Builder @Builder
public class CachedEmbed { public class CachedEmbed implements Serializable {
private CachedEmbedAuthor author; private CachedEmbedAuthor author;
private CachedEmbedTitle title; private CachedEmbedTitle title;
private CachedEmbedColor color; private CachedEmbedColor color;

View File

@@ -5,10 +5,12 @@ import lombok.Builder;
import lombok.Getter; import lombok.Getter;
import lombok.Setter; import lombok.Setter;
import java.io.Serializable;
@Getter @Getter
@Setter @Setter
@Builder @Builder
public class CachedEmbedAuthor { public class CachedEmbedAuthor implements Serializable {
private String name; private String name;
private String url; private String url;
private String avatar; private String avatar;

View File

@@ -4,10 +4,12 @@ import lombok.Builder;
import lombok.Getter; import lombok.Getter;
import lombok.Setter; import lombok.Setter;
import java.io.Serializable;
@Setter @Setter
@Getter @Getter
@Builder @Builder
public class CachedEmbedColor { public class CachedEmbedColor implements Serializable {
private Integer r; private Integer r;
private Integer g; private Integer g;
private Integer b; private Integer b;

View File

@@ -4,10 +4,12 @@ import lombok.Builder;
import lombok.Getter; import lombok.Getter;
import lombok.Setter; import lombok.Setter;
import java.io.Serializable;
@Getter @Getter
@Setter @Setter
@Builder @Builder
public class CachedEmbedField { public class CachedEmbedField implements Serializable {
private String name; private String name;
private String value; private String value;
private Boolean inline; private Boolean inline;

View File

@@ -4,10 +4,12 @@ import lombok.Builder;
import lombok.Getter; import lombok.Getter;
import lombok.Setter; import lombok.Setter;
import java.io.Serializable;
@Getter @Getter
@Setter @Setter
@Builder @Builder
public class CachedEmbedFooter { public class CachedEmbedFooter implements Serializable {
private String text; private String text;
private String icon; private String icon;
} }

View File

@@ -3,8 +3,10 @@ package dev.sheldan.abstracto.core.models.cache;
import lombok.Getter; import lombok.Getter;
import lombok.Setter; import lombok.Setter;
import java.io.Serializable;
@Getter @Setter @Getter @Setter
public class CachedEmbedTitle { public class CachedEmbedTitle implements Serializable {
private String title; private String title;
private String url; private String url;
} }

View File

@@ -5,11 +5,13 @@ import lombok.EqualsAndHashCode;
import lombok.Getter; import lombok.Getter;
import lombok.Setter; import lombok.Setter;
import java.io.Serializable;
@Getter @Getter
@Setter @Setter
@Builder @Builder
@EqualsAndHashCode @EqualsAndHashCode
public class CachedEmote { public class CachedEmote implements Serializable {
private String emoteName; private String emoteName;
private Long emoteId; private Long emoteId;
private Boolean external; private Boolean external;

View File

@@ -4,10 +4,12 @@ import lombok.Builder;
import lombok.Getter; import lombok.Getter;
import lombok.Setter; import lombok.Setter;
import java.io.Serializable;
@Getter @Getter
@Setter @Setter
@Builder @Builder
public class CachedImageInfo { public class CachedImageInfo implements Serializable {
protected String url; protected String url;
protected String proxyUrl; protected String proxyUrl;
protected Integer width; protected Integer width;

View File

@@ -6,13 +6,14 @@ import lombok.Builder;
import lombok.Getter; import lombok.Getter;
import lombok.Setter; import lombok.Setter;
import java.io.Serializable;
import java.time.Instant; import java.time.Instant;
import java.util.List; import java.util.List;
@Getter @Getter
@Setter @Setter
@Builder @Builder
public class CachedMessage { public class CachedMessage implements Serializable {
private Long serverId; private Long serverId;
private Long channelId; private Long channelId;
private Long messageId; private Long messageId;

View File

@@ -6,12 +6,13 @@ import lombok.Builder;
import lombok.Getter; import lombok.Getter;
import lombok.Setter; import lombok.Setter;
import java.io.Serializable;
import java.util.List; import java.util.List;
@Getter @Getter
@Setter @Setter
@Builder @Builder
public class CachedReactions { public class CachedReactions implements Serializable {
private CachedEmote emote; private CachedEmote emote;
private Boolean self; private Boolean self;
private List<ServerUser> users; private List<ServerUser> users;

View File

@@ -4,10 +4,12 @@ import lombok.Builder;
import lombok.Getter; import lombok.Getter;
import lombok.Setter; import lombok.Setter;
import java.io.Serializable;
@Getter @Getter
@Setter @Setter
@Builder @Builder
public class CachedThumbnail { public class CachedThumbnail implements Serializable {
protected String url; protected String url;
protected String proxyUrl; protected String proxyUrl;
protected Integer width; protected Integer width;

View File

@@ -3,6 +3,7 @@ package dev.sheldan.abstracto.core.service;
import dev.sheldan.abstracto.core.models.cache.*; import dev.sheldan.abstracto.core.models.cache.*;
import net.dv8tion.jda.api.entities.*; import net.dv8tion.jda.api.entities.*;
import java.util.List;
import java.util.concurrent.CompletableFuture; import java.util.concurrent.CompletableFuture;
public interface CacheEntityService { public interface CacheEntityService {
@@ -10,6 +11,7 @@ public interface CacheEntityService {
CachedEmote getCachedEmoteFromEmote(MessageReaction.ReactionEmote emote, Guild guild); CachedEmote getCachedEmoteFromEmote(MessageReaction.ReactionEmote emote, Guild guild);
CachedAttachment getCachedAttachment(Message.Attachment attachment); CachedAttachment getCachedAttachment(Message.Attachment attachment);
CachedEmbed getCachedEmbedFromEmbed(MessageEmbed embed); CachedEmbed getCachedEmbedFromEmbed(MessageEmbed embed);
List<CachedAttachment> getCachedAttachments(List<Message.Attachment> attachments);
CachedThumbnail buildCachedThumbnail(MessageEmbed.Thumbnail thumbnail); CachedThumbnail buildCachedThumbnail(MessageEmbed.Thumbnail thumbnail);
CachedImageInfo buildCachedImage(MessageEmbed.ImageInfo image); CachedImageInfo buildCachedImage(MessageEmbed.ImageInfo image);
CompletableFuture<CachedReactions> getCachedReactionFromReaction(MessageReaction reaction); CompletableFuture<CachedReactions> getCachedReactionFromReaction(MessageReaction reaction);