Search in sources :

Example 1 with MessageImpl

use of org.javacord.core.entity.message.MessageImpl in project Javacord by BtoBastian.

the class DiscordApiImpl method getOrCreateMessage.

/**
 * Gets or creates a new message object.
 *
 * @param channel The channel of the message.
 * @param data    The data of the message.
 * @return The message for the given json object.
 */
public Message getOrCreateMessage(TextChannel channel, JsonNode data) {
    long id = Long.parseLong(data.get("id").asText());
    messageCacheLock.lock();
    try {
        return getCachedMessageById(id).orElseGet(() -> new MessageImpl(this, channel, data));
    } finally {
        messageCacheLock.unlock();
    }
}
Also used : MessageImpl(org.javacord.core.entity.message.MessageImpl)

Example 2 with MessageImpl

use of org.javacord.core.entity.message.MessageImpl in project Javacord by BtoBastian.

the class MessageUpdateHandler method handle.

@Override
public void handle(JsonNode packet) {
    long messageId = packet.get("id").asLong();
    long channelId = packet.get("channel_id").asLong();
    Optional<TextChannel> optionalChannel = api.getTextChannelById(channelId);
    if (!optionalChannel.isPresent()) {
        LoggerUtil.logMissingChannel(logger, channelId);
        return;
    }
    TextChannel channel = optionalChannel.get();
    Optional<MessageImpl> message = api.getCachedMessageById(messageId).map(msg -> (MessageImpl) msg);
    message.ifPresent(msg -> {
        boolean newPinnedFlag = packet.hasNonNull("pinned") ? packet.get("pinned").asBoolean() : msg.isPinned();
        boolean oldPinnedFlag = msg.isPinned();
        if (newPinnedFlag != oldPinnedFlag) {
            msg.setPinned(newPinnedFlag);
            if (newPinnedFlag) {
                CachedMessagePinEvent event = new CachedMessagePinEventImpl(msg);
                Optional<Server> optionalServer = msg.getChannel().asServerChannel().map(ServerChannel::getServer);
                api.getEventDispatcher().dispatchCachedMessagePinEvent(optionalServer.map(DispatchQueueSelector.class::cast).orElse(api), msg, optionalServer.orElse(null), msg.getChannel(), event);
            } else {
                CachedMessageUnpinEvent event = new CachedMessageUnpinEventImpl(msg);
                Optional<Server> optionalServer = msg.getChannel().asServerChannel().map(ServerChannel::getServer);
                api.getEventDispatcher().dispatchCachedMessageUnpinEvent(optionalServer.map(DispatchQueueSelector.class::cast).orElse(api), msg, optionalServer.orElse(null), msg.getChannel(), event);
            }
        }
    });
    MessageEditEvent editEvent = null;
    if (packet.has("edited_timestamp") && !packet.get("edited_timestamp").isNull()) {
        message.ifPresent(msg -> {
            msg.setLastEditTime(OffsetDateTime.parse(packet.get("edited_timestamp").asText()).toInstant());
            msg.setMentionsEveryone(packet.get("mention_everyone").asBoolean());
        });
        long editTimestamp = OffsetDateTime.parse(packet.get("edited_timestamp").asText()).toInstant().toEpochMilli();
        long lastKnownEditTimestamp = lastKnownEditTimestamps.getOrDefault(messageId, 0L);
        lastKnownEditTimestamps.put(messageId, editTimestamp);
        boolean isMostLikelyAnEdit = true;
        long offset = api.getTimeOffset() == null ? 0 : api.getTimeOffset();
        if (editTimestamp == lastKnownEditTimestamp) {
            isMostLikelyAnEdit = false;
        } else if (System.currentTimeMillis() + offset - editTimestamp > 5000) {
            isMostLikelyAnEdit = false;
        }
        String oldContent = message.map(Message::getContent).orElse(null);
        List<Embed> oldEmbeds = message.map(Message::getEmbeds).orElse(null);
        String newContent = null;
        if (packet.has("content")) {
            newContent = packet.get("content").asText();
            String finalNewContent = newContent;
            message.ifPresent(msg -> msg.setContent(finalNewContent));
        }
        List<Embed> newEmbeds = null;
        if (packet.has("embeds")) {
            newEmbeds = new ArrayList<>();
            for (JsonNode embedJson : packet.get("embeds")) {
                Embed embed = new EmbedImpl(embedJson);
                newEmbeds.add(embed);
            }
            List<Embed> finalNewEmbeds = newEmbeds;
            message.ifPresent(msg -> msg.setEmbeds(finalNewEmbeds));
        }
        if (oldContent != null && newContent != null && !oldContent.equals(newContent)) {
            // If the old content doesn't match the new content it's for sure an edit
            isMostLikelyAnEdit = true;
        }
        if (oldEmbeds != null && newEmbeds != null) {
            if (newEmbeds.size() != oldEmbeds.size()) {
                isMostLikelyAnEdit = true;
            } else {
                for (int i = 0; i < newEmbeds.size(); i++) {
                    if (!((EmbedBuilderDelegateImpl) newEmbeds.get(i).toBuilder().getDelegate()).toJsonNode().toString().equals(((EmbedBuilderDelegateImpl) oldEmbeds.get(i).toBuilder().getDelegate()).toJsonNode().toString())) {
                        isMostLikelyAnEdit = true;
                    }
                }
            }
        }
        if (isMostLikelyAnEdit) {
            editEvent = new MessageEditEventImpl(api, messageId, channel, newContent, newEmbeds, oldContent, oldEmbeds);
        }
    }
    if (editEvent != null) {
        dispatchEditEvent(editEvent);
    }
}
Also used : CachedMessagePinEventImpl(org.javacord.core.event.message.CachedMessagePinEventImpl) Server(org.javacord.api.entity.server.Server) CachedMessageUnpinEvent(org.javacord.api.event.message.CachedMessageUnpinEvent) Embed(org.javacord.api.entity.message.embed.Embed) CachedMessageUnpinEventImpl(org.javacord.core.event.message.CachedMessageUnpinEventImpl) MessageEditEventImpl(org.javacord.core.event.message.MessageEditEventImpl) CachedMessagePinEvent(org.javacord.api.event.message.CachedMessagePinEvent) JsonNode(com.fasterxml.jackson.databind.JsonNode) EmbedImpl(org.javacord.core.entity.message.embed.EmbedImpl) ServerChannel(org.javacord.api.entity.channel.ServerChannel) DispatchQueueSelector(org.javacord.core.util.event.DispatchQueueSelector) MessageEditEvent(org.javacord.api.event.message.MessageEditEvent) TextChannel(org.javacord.api.entity.channel.TextChannel) MessageImpl(org.javacord.core.entity.message.MessageImpl)

Aggregations

MessageImpl (org.javacord.core.entity.message.MessageImpl)2 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 ServerChannel (org.javacord.api.entity.channel.ServerChannel)1 TextChannel (org.javacord.api.entity.channel.TextChannel)1 Embed (org.javacord.api.entity.message.embed.Embed)1 Server (org.javacord.api.entity.server.Server)1 CachedMessagePinEvent (org.javacord.api.event.message.CachedMessagePinEvent)1 CachedMessageUnpinEvent (org.javacord.api.event.message.CachedMessageUnpinEvent)1 MessageEditEvent (org.javacord.api.event.message.MessageEditEvent)1 EmbedImpl (org.javacord.core.entity.message.embed.EmbedImpl)1 CachedMessagePinEventImpl (org.javacord.core.event.message.CachedMessagePinEventImpl)1 CachedMessageUnpinEventImpl (org.javacord.core.event.message.CachedMessageUnpinEventImpl)1 MessageEditEventImpl (org.javacord.core.event.message.MessageEditEventImpl)1 DispatchQueueSelector (org.javacord.core.util.event.DispatchQueueSelector)1