[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;
}

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.sync.jda.MessageUpdatedListener;
import dev.sheldan.abstracto.core.metric.service.CounterMetric;
import dev.sheldan.abstracto.core.metric.service.MetricService;
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.listener.MessageUpdatedModel;
import dev.sheldan.abstracto.core.service.CacheEntityService;
import dev.sheldan.abstracto.core.service.MessageCache;
import dev.sheldan.abstracto.core.utils.BeanUtils;
import lombok.extern.slf4j.Slf4j;
import net.dv8tion.jda.api.entities.Message;
import net.dv8tion.jda.api.events.message.guild.GuildMessageUpdateEvent;
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.Qualifier;
import org.springframework.core.task.TaskExecutor;
@@ -23,6 +27,7 @@ import javax.annotation.Nonnull;
import javax.annotation.PostConstruct;
import java.util.Arrays;
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.MESSAGE_METRIC;
@@ -53,6 +58,9 @@ public class MessageUpdatedListenerBean extends ListenerAdapter {
@Autowired
private MetricService metricService;
@Autowired
private CacheEntityService cacheEntityService;
private static final CounterMetric MESSAGE_UPDATED_COUNTER = CounterMetric
.builder()
.name(MESSAGE_METRIC)
@@ -66,12 +74,16 @@ public class MessageUpdatedListenerBean extends ListenerAdapter {
Message message = event.getMessage();
messageCache.getMessageFromCache(message.getGuild().getIdLong(), message.getTextChannel().getIdLong(), event.getMessageIdLong()).thenAccept(cachedMessage -> {
try {
executeAsyncListeners(event, cachedMessage);
if (listenerList != null) {
self.executeListener(cachedMessage, event);
}
// we need to provide a copy of the object, so modifications here dont influence the async execution
// because we do modify it, as we are the one responsible for caching it
executeAsyncListeners(event, SerializationUtils.clone(cachedMessage));
self.executeListener(cachedMessage, event);
} finally {
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);
}
}).exceptionally(throwable -> {

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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