mirror of
https://github.com/Sheldan/abstracto.git
synced 2026-01-26 13:46:19 +00:00
[AB-226] adding caching of referenced messages (only one element on the chain)
This commit is contained in:
@@ -3,6 +3,7 @@ package dev.sheldan.abstracto.core.service;
|
|||||||
import dev.sheldan.abstracto.core.models.ServerUser;
|
import dev.sheldan.abstracto.core.models.ServerUser;
|
||||||
import dev.sheldan.abstracto.core.models.cache.*;
|
import dev.sheldan.abstracto.core.models.cache.*;
|
||||||
import dev.sheldan.abstracto.core.service.management.UserInServerManagementService;
|
import dev.sheldan.abstracto.core.service.management.UserInServerManagementService;
|
||||||
|
import dev.sheldan.abstracto.core.utils.FutureUtils;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import net.dv8tion.jda.api.entities.*;
|
import net.dv8tion.jda.api.entities.*;
|
||||||
import net.dv8tion.jda.api.requests.restaction.pagination.ReactionPaginationAction;
|
import net.dv8tion.jda.api.requests.restaction.pagination.ReactionPaginationAction;
|
||||||
@@ -178,7 +179,7 @@ public class CacheEntityServiceBean implements CacheEntityService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public CompletableFuture<CachedMessage> buildCachedMessageFromMessage(Message message) {
|
public CompletableFuture<CachedMessage> buildCachedMessageFromMessage(Message message, boolean loadReferenced) {
|
||||||
CompletableFuture<CachedMessage> future = new CompletableFuture<>();
|
CompletableFuture<CachedMessage> future = new CompletableFuture<>();
|
||||||
List<CachedAttachment> attachments = new ArrayList<>();
|
List<CachedAttachment> attachments = new ArrayList<>();
|
||||||
log.debug("Caching {} attachments.", message.getAttachments().size());
|
log.debug("Caching {} attachments.", message.getAttachments().size());
|
||||||
@@ -197,10 +198,20 @@ public class CacheEntityServiceBean implements CacheEntityService {
|
|||||||
message.getEmotesBag().forEach(emote -> emotes.add(getCachedEmoteFromEmote(emote, message.getGuild())));
|
message.getEmotesBag().forEach(emote -> emotes.add(getCachedEmoteFromEmote(emote, message.getGuild())));
|
||||||
}
|
}
|
||||||
|
|
||||||
List<CompletableFuture<CachedReactions>> futures = new ArrayList<>();
|
CompletableFuture<CachedMessage> referencedMessageFuture;
|
||||||
|
if(loadReferenced && message.getReferencedMessage() != null) {
|
||||||
|
log.debug("Loading referenced message {}.", message.getReferencedMessage().getIdLong());
|
||||||
|
referencedMessageFuture = buildCachedMessageFromMessage(message.getReferencedMessage(), false);
|
||||||
|
} else {
|
||||||
|
referencedMessageFuture = CompletableFuture.completedFuture(null);
|
||||||
|
}
|
||||||
|
|
||||||
|
List<CompletableFuture<CachedReactions>> reactionFutures = new ArrayList<>();
|
||||||
log.debug("Caching {} reactions.", message.getReactions().size());
|
log.debug("Caching {} reactions.", message.getReactions().size());
|
||||||
message.getReactions().forEach(messageReaction -> futures.add(getCachedReactionFromReaction(messageReaction)));
|
message.getReactions().forEach(messageReaction -> reactionFutures.add(getCachedReactionFromReaction(messageReaction)));
|
||||||
CompletableFuture.allOf(futures.toArray(new CompletableFuture[0])).thenAccept(aVoid ->
|
List<CompletableFuture> allFutures = new ArrayList<>(reactionFutures);
|
||||||
|
allFutures.add(referencedMessageFuture);
|
||||||
|
FutureUtils.toSingleFuture(allFutures).thenAccept(aVoid ->
|
||||||
{
|
{
|
||||||
CachedAuthor cachedAuthor = CachedAuthor.builder().authorId(message.getAuthor().getIdLong()).isBot(message.getAuthor().isBot()).build();
|
CachedAuthor cachedAuthor = CachedAuthor.builder().authorId(message.getAuthor().getIdLong()).isBot(message.getAuthor().isBot()).build();
|
||||||
CachedMessage.CachedMessageBuilder builder = CachedMessage.builder()
|
CachedMessage.CachedMessageBuilder builder = CachedMessage.builder()
|
||||||
@@ -210,7 +221,8 @@ public class CacheEntityServiceBean implements CacheEntityService {
|
|||||||
.content(message.getContentRaw())
|
.content(message.getContentRaw())
|
||||||
.embeds(embeds)
|
.embeds(embeds)
|
||||||
.emotes(emotes)
|
.emotes(emotes)
|
||||||
.reactions(convertReactionFuturesToCachedReactions(futures))
|
.referencedMessage(referencedMessageFuture.join())
|
||||||
|
.reactions(convertReactionFuturesToCachedReactions(reactionFutures))
|
||||||
.timeCreated(Instant.from(message.getTimeCreated()))
|
.timeCreated(Instant.from(message.getTimeCreated()))
|
||||||
.attachments(attachments);
|
.attachments(attachments);
|
||||||
if(message.isFromGuild()) {
|
if(message.isFromGuild()) {
|
||||||
@@ -243,6 +255,11 @@ public class CacheEntityServiceBean implements CacheEntityService {
|
|||||||
return future;
|
return future;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public CompletableFuture<CachedMessage> buildCachedMessageFromMessage(Message message) {
|
||||||
|
return buildCachedMessageFromMessage(message, true);
|
||||||
|
}
|
||||||
|
|
||||||
private List<CachedReactions> convertReactionFuturesToCachedReactions(List<CompletableFuture<CachedReactions>> futures) {
|
private List<CachedReactions> convertReactionFuturesToCachedReactions(List<CompletableFuture<CachedReactions>> futures) {
|
||||||
return futures.stream().map(CompletableFuture::join).collect(Collectors.toList());
|
return futures.stream().map(CompletableFuture::join).collect(Collectors.toList());
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -23,6 +23,7 @@ public class CachedMessage {
|
|||||||
private List<CachedAttachment> attachments;
|
private List<CachedAttachment> attachments;
|
||||||
private List<CachedReactions> reactions;
|
private List<CachedReactions> reactions;
|
||||||
private List<CachedEmote> emotes;
|
private List<CachedEmote> emotes;
|
||||||
|
private CachedMessage referencedMessage;
|
||||||
|
|
||||||
public String getMessageUrl() {
|
public String getMessageUrl() {
|
||||||
return MessageUtils.buildMessageUrl(this.serverId ,this.channelId, this.messageId);
|
return MessageUtils.buildMessageUrl(this.serverId ,this.channelId, this.messageId);
|
||||||
|
|||||||
@@ -13,5 +13,6 @@ public interface CacheEntityService {
|
|||||||
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);
|
||||||
|
CompletableFuture<CachedMessage> buildCachedMessageFromMessage(Message message, boolean loadReferenced);
|
||||||
CompletableFuture<CachedMessage> buildCachedMessageFromMessage(Message message);
|
CompletableFuture<CachedMessage> buildCachedMessageFromMessage(Message message);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user