From 01bb5ea214111815c3c2ce134c7832413b01b55d Mon Sep 17 00:00:00 2001 From: Pedro <3602279+Doc94@users.noreply.github.com> Date: Sat, 19 Oct 2024 06:20:19 -0300 Subject: [PATCH] Support Message forwarding data (#187) * Support Message forwarding data * Remove redundant field * Add MessageReferenceData.type with default state * Improvements for Forward and PartialMessageData - Created PartialMessageUpdateData for the Update Message response - Include test for forward message response - Fixes for PartialMessageData used in Forward --- .../discordjson/json/MessageData.java | 3 + .../json/MessageReferenceData.java | 4 ++ .../json/MessageSnapshotsData.java | 18 ++++++ .../discordjson/json/PartialMessageData.java | 7 +-- .../json/PartialMessageUpdateData.java | 23 ++++++++ .../json/gateway/MessageUpdate.java | 4 +- .../json/rest/RestDeserializationTest.java | 6 ++ .../CreateMessageWithForwardResponse.json | 55 +++++++++++++++++++ 8 files changed, 113 insertions(+), 7 deletions(-) create mode 100644 src/main/java/discord4j/discordjson/json/MessageSnapshotsData.java create mode 100644 src/main/java/discord4j/discordjson/json/PartialMessageUpdateData.java create mode 100644 src/test/resources/rest/CreateMessageWithForwardResponse.json diff --git a/src/main/java/discord4j/discordjson/json/MessageData.java b/src/main/java/discord4j/discordjson/json/MessageData.java index d506e7aa..f489c967 100644 --- a/src/main/java/discord4j/discordjson/json/MessageData.java +++ b/src/main/java/discord4j/discordjson/json/MessageData.java @@ -76,6 +76,9 @@ static ImmutableMessageData.Builder builder() { @JsonProperty("message_reference") Possible messageReference(); + @JsonProperty("message_snapshots") + Possible> messageSnapshots(); + Possible flags(); @Deprecated diff --git a/src/main/java/discord4j/discordjson/json/MessageReferenceData.java b/src/main/java/discord4j/discordjson/json/MessageReferenceData.java index c46e199b..42293efa 100644 --- a/src/main/java/discord4j/discordjson/json/MessageReferenceData.java +++ b/src/main/java/discord4j/discordjson/json/MessageReferenceData.java @@ -16,6 +16,10 @@ static ImmutableMessageReferenceData.Builder builder() { return ImmutableMessageReferenceData.builder(); } + default int type() { + return 0; + } + @JsonProperty("message_id") Possible messageId(); diff --git a/src/main/java/discord4j/discordjson/json/MessageSnapshotsData.java b/src/main/java/discord4j/discordjson/json/MessageSnapshotsData.java new file mode 100644 index 00000000..4fcf0bbb --- /dev/null +++ b/src/main/java/discord4j/discordjson/json/MessageSnapshotsData.java @@ -0,0 +1,18 @@ +package discord4j.discordjson.json; + +import com.fasterxml.jackson.databind.annotation.JsonDeserialize; +import com.fasterxml.jackson.databind.annotation.JsonSerialize; +import org.immutables.value.Value; + +@Value.Immutable +@JsonSerialize(as = ImmutableMessageSnapshotsData.class) +@JsonDeserialize(as = ImmutableMessageSnapshotsData.class) +public interface MessageSnapshotsData { + + static ImmutableMessageSnapshotsData.Builder builder() { + return ImmutableMessageSnapshotsData.builder(); + } + + PartialMessageData message(); + +} diff --git a/src/main/java/discord4j/discordjson/json/PartialMessageData.java b/src/main/java/discord4j/discordjson/json/PartialMessageData.java index 7f9ec8b7..eb9ed505 100644 --- a/src/main/java/discord4j/discordjson/json/PartialMessageData.java +++ b/src/main/java/discord4j/discordjson/json/PartialMessageData.java @@ -19,11 +19,6 @@ static ImmutablePartialMessageData.Builder builder() { return ImmutablePartialMessageData.builder(); } - Id id(); - - @JsonProperty("channel_id") - Id channelId(); - @JsonProperty("guild_id") Possible guildId(); @@ -86,4 +81,6 @@ static ImmutablePartialMessageData.Builder builder() { Possible interaction(); + Possible> components(); + } diff --git a/src/main/java/discord4j/discordjson/json/PartialMessageUpdateData.java b/src/main/java/discord4j/discordjson/json/PartialMessageUpdateData.java new file mode 100644 index 00000000..09a47b83 --- /dev/null +++ b/src/main/java/discord4j/discordjson/json/PartialMessageUpdateData.java @@ -0,0 +1,23 @@ +package discord4j.discordjson.json; + +import com.fasterxml.jackson.annotation.JsonProperty; +import com.fasterxml.jackson.databind.annotation.JsonDeserialize; +import com.fasterxml.jackson.databind.annotation.JsonSerialize; +import discord4j.discordjson.Id; +import org.immutables.value.Value; + +@Value.Immutable +@JsonSerialize(as = ImmutablePartialMessageUpdateData.class) +@JsonDeserialize(as = ImmutablePartialMessageUpdateData.class) +public interface PartialMessageUpdateData extends PartialMessageData { + + static ImmutablePartialMessageUpdateData.Builder builder() { + return ImmutablePartialMessageUpdateData.builder(); + } + + Id id(); + + @JsonProperty("channel_id") + Id channelId(); + +} diff --git a/src/main/java/discord4j/discordjson/json/gateway/MessageUpdate.java b/src/main/java/discord4j/discordjson/json/gateway/MessageUpdate.java index c31e4a69..95fb5bd8 100644 --- a/src/main/java/discord4j/discordjson/json/gateway/MessageUpdate.java +++ b/src/main/java/discord4j/discordjson/json/gateway/MessageUpdate.java @@ -3,7 +3,7 @@ import com.fasterxml.jackson.annotation.JsonUnwrapped; import com.fasterxml.jackson.databind.annotation.JsonDeserialize; import com.fasterxml.jackson.databind.annotation.JsonSerialize; -import discord4j.discordjson.json.PartialMessageData; +import discord4j.discordjson.json.PartialMessageUpdateData; import org.immutables.value.Value; @Value.Immutable @@ -16,5 +16,5 @@ static ImmutableMessageUpdate.Builder builder() { } @JsonUnwrapped - PartialMessageData message(); + PartialMessageUpdateData message(); } diff --git a/src/test/java/discord4j/discordjson/json/rest/RestDeserializationTest.java b/src/test/java/discord4j/discordjson/json/rest/RestDeserializationTest.java index 85ccbf04..13b979ca 100644 --- a/src/test/java/discord4j/discordjson/json/rest/RestDeserializationTest.java +++ b/src/test/java/discord4j/discordjson/json/rest/RestDeserializationTest.java @@ -86,6 +86,12 @@ public void testCreateMessageResponse() throws IOException { log.info("{}", res); } + @Test + public void testCreateMessageWithForwardResponse() throws IOException { + MessageData res = read("/rest/CreateMessageWithForwardResponse.json", MessageData.class); + log.info("{}", res); + } + @Test public void testMessageCopyingCollectionMethods() throws IOException { MessageData res = read("/rest/CreateMessageResponse.json", MessageData.class); diff --git a/src/test/resources/rest/CreateMessageWithForwardResponse.json b/src/test/resources/rest/CreateMessageWithForwardResponse.json new file mode 100644 index 00000000..72a123a6 --- /dev/null +++ b/src/test/resources/rest/CreateMessageWithForwardResponse.json @@ -0,0 +1,55 @@ +{ + "type": 0, + "content": "", + "mentions": [], + "mention_roles": [], + "attachments": [], + "embeds": [], + "timestamp": "2024-08-31T02:38:50.292000+00:00", + "edited_timestamp": null, + "flags": 16384, + "components": [], + "id": "1279269138510118913", + "channel_id": "1088465252804481025", + "author": { + "id": "719574575557509202", + "username": "DoggoFN", + "avatar": "ca9e2a158768a8e7e38861c16c575ffb", + "discriminator": "9425", + "public_flags": 0, + "flags": 0, + "bot": true, + "banner": null, + "accent_color": null, + "global_name": null, + "avatar_decoration_data": null, + "banner_color": null, + "clan": null + }, + "pinned": false, + "mention_everyone": false, + "tts": false, + "message_reference": { + "type": 1, + "channel_id": "1088465252804481025", + "message_id": "1265424582311936060", + "guild_id": "405699020564987914" + }, + "position": 0, + "message_snapshots": [ + { + "message": { + "type": 0, + "content": "What is Lorem Ipsum?\nLorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.", + "mentions": [], + "mention_roles": [], + "attachments": [], + "embeds": [], + "timestamp": "2024-07-23T21:45:30.875000+00:00", + "edited_timestamp": null, + "flags": 0, + "components": [] + } + } + ] +}