mirror of
https://github.com/Sheldan/abstracto.git
synced 2026-03-24 21:53:22 +00:00
[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:
@@ -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 -> {
|
||||
@@ -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 ->
|
||||
|
||||
Reference in New Issue
Block a user