From e8a02e57f8dae6ff9ab4ae03c2009235c0f42fbf Mon Sep 17 00:00:00 2001 From: bitfl0wer Date: Tue, 22 Aug 2023 22:27:09 +0200 Subject: [PATCH] Add Modify Message endpoint --- src/api/channels/messages.rs | 36 ++++++++++++++++++++++-- src/types/entities/attachment.rs | 2 +- src/types/entities/emoji.rs | 47 ++++++++++++++++++++++++++++++++ src/types/entities/message.rs | 20 +++++++------- src/types/schema/message.rs | 15 +++++++++- 5 files changed, 106 insertions(+), 14 deletions(-) diff --git a/src/api/channels/messages.rs b/src/api/channels/messages.rs index 1eec276..05680b9 100644 --- a/src/api/channels/messages.rs +++ b/src/api/channels/messages.rs @@ -8,8 +8,8 @@ use crate::errors::{ChorusError, ChorusResult}; use crate::instance::UserMeta; use crate::ratelimiter::ChorusRequest; use crate::types::{ - Channel, CreateGreetMessage, Message, MessageAck, MessageSearchEndpoint, MessageSearchQuery, - MessageSendSchema, Snowflake, + Channel, CreateGreetMessage, Message, MessageAck, MessageModifySchema, MessageSearchEndpoint, + MessageSearchQuery, MessageSendSchema, Snowflake, }; impl Message { @@ -332,6 +332,38 @@ impl Message { }; chorus_request.handle_request_as_result(user).await } + + /// Edits a previously sent message. All fields can be edited by the original message author. + /// Other users can only edit flags and only if they have the MANAGE_MESSAGES permission in the corresponding channel. + /// When specifying flags, ensure to include all previously set flags/bits in addition to ones that you are modifying. + /// When the content field is edited, the mentions array in the message object will be reconstructed from scratch based on the new content. + /// The allowed_mentions field of the edit request controls how this happens. + /// If there is no explicit allowed_mentions in the edit request, the content will be parsed with default allowances, that is, + /// without regard to whether or not an allowed_mentions was present in the request that originally created the message. + /// + /// # Reference: + /// See: + pub async fn modify( + channel_id: Snowflake, + message_id: Snowflake, + schema: MessageModifySchema, + user: &mut UserMeta, + ) -> ChorusResult { + let chorus_request = ChorusRequest { + request: Client::new() + .patch(format!( + "{}/channels/{}/messages/{}", + user.belongs_to.borrow().urls.api, + channel_id, + message_id + )) + .header("Authorization", user.token()) + .header("Content-Type", "application/json") + .body(to_string(&schema).unwrap()), + limit_type: LimitType::Channel(channel_id), + }; + chorus_request.deserialize_response::(user).await + } } fn search_error(result_text: String) -> ChorusError { diff --git a/src/types/entities/attachment.rs b/src/types/entities/attachment.rs index 59ad53d..ffbc520 100644 --- a/src/types/entities/attachment.rs +++ b/src/types/entities/attachment.rs @@ -2,7 +2,7 @@ use serde::{Deserialize, Serialize}; use crate::types::utils::Snowflake; -#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] +#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, PartialOrd)] #[cfg_attr(feature = "sqlx", derive(sqlx::FromRow))] /// # Reference /// See diff --git a/src/types/entities/emoji.rs b/src/types/entities/emoji.rs index 68700e6..fddef8c 100644 --- a/src/types/entities/emoji.rs +++ b/src/types/entities/emoji.rs @@ -26,3 +26,50 @@ pub struct Emoji { pub animated: Option, pub available: Option, } + +impl PartialEq for Emoji { + fn eq(&self, other: &Self) -> bool { + self.id == other.id + && self.name == other.name + && self.roles == other.roles + && self.roles == other.roles + && self.require_colons == other.require_colons + && self.managed == other.managed + && self.animated == other.animated + && self.available == other.available + } +} + +impl PartialOrd for Emoji { + fn partial_cmp(&self, other: &Self) -> Option { + match self.id.partial_cmp(&other.id) { + Some(core::cmp::Ordering::Equal) => {} + ord => return ord, + } + match self.name.partial_cmp(&other.name) { + Some(core::cmp::Ordering::Equal) => {} + ord => return ord, + } + match self.roles.partial_cmp(&other.roles) { + Some(core::cmp::Ordering::Equal) => {} + ord => return ord, + } + match self.roles.partial_cmp(&other.roles) { + Some(core::cmp::Ordering::Equal) => {} + ord => return ord, + } + match self.require_colons.partial_cmp(&other.require_colons) { + Some(core::cmp::Ordering::Equal) => {} + ord => return ord, + } + match self.managed.partial_cmp(&other.managed) { + Some(core::cmp::Ordering::Equal) => {} + ord => return ord, + } + match self.animated.partial_cmp(&other.animated) { + Some(core::cmp::Ordering::Equal) => {} + ord => return ord, + } + self.available.partial_cmp(&other.available) + } +} diff --git a/src/types/entities/message.rs b/src/types/entities/message.rs index f59cdc5..27a8857 100644 --- a/src/types/entities/message.rs +++ b/src/types/entities/message.rs @@ -149,7 +149,7 @@ pub struct ChannelMention { name: String, } -#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)] +#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, PartialOrd)] pub struct Embed { title: Option, #[serde(rename = "type")] @@ -167,14 +167,14 @@ pub struct Embed { fields: Option>, } -#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)] +#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, PartialOrd, Ord)] pub struct EmbedFooter { text: String, icon_url: Option, proxy_icon_url: Option, } -#[derive(Debug, PartialEq, Eq, Clone, Serialize, Deserialize)] +#[derive(Debug, PartialEq, Eq, Clone, Serialize, Deserialize, PartialOrd, Ord)] pub struct EmbedImage { url: String, proxy_url: String, @@ -182,7 +182,7 @@ pub struct EmbedImage { width: Option, } -#[derive(Debug, PartialEq, Eq, Clone, Serialize, Deserialize)] +#[derive(Debug, PartialEq, Eq, Clone, Serialize, Deserialize, PartialOrd, Ord)] pub struct EmbedThumbnail { url: String, proxy_url: Option, @@ -190,7 +190,7 @@ pub struct EmbedThumbnail { width: Option, } -#[derive(Debug, PartialEq, Eq, Clone, Serialize, Deserialize)] +#[derive(Debug, PartialEq, Eq, Clone, Serialize, Deserialize, PartialOrd, Ord)] struct EmbedVideo { url: Option, proxy_url: Option, @@ -198,13 +198,13 @@ struct EmbedVideo { width: Option, } -#[derive(Debug, PartialEq, Eq, Clone, Serialize, Deserialize)] +#[derive(Debug, PartialEq, Eq, Clone, Serialize, Deserialize, PartialOrd, Ord)] pub struct EmbedProvider { name: Option, url: Option, } -#[derive(Debug, PartialEq, Eq, Clone, Serialize, Deserialize)] +#[derive(Debug, PartialEq, Eq, Clone, Serialize, Deserialize, PartialOrd, Ord)] pub struct EmbedAuthor { name: String, url: Option, @@ -212,14 +212,14 @@ pub struct EmbedAuthor { proxy_icon_url: Option, } -#[derive(Debug, PartialEq, Eq, Clone, Serialize, Deserialize)] +#[derive(Debug, PartialEq, Eq, Clone, Serialize, Deserialize, PartialOrd, Ord)] pub struct EmbedField { name: String, value: String, inline: Option, } -#[derive(Debug, Clone, Serialize, Deserialize)] +#[derive(Debug, Clone, Serialize, Deserialize, PartialOrd, PartialEq)] pub struct Reaction { pub count: u32, pub burst_count: u32, @@ -229,7 +229,7 @@ pub struct Reaction { pub emoji: Emoji, } -#[derive(Debug, PartialEq, Clone, Copy, Serialize, Deserialize)] +#[derive(Debug, PartialEq, Clone, Copy, Serialize, Deserialize, Eq, PartialOrd, Ord)] pub enum Component { ActionRow = 1, Button = 2, diff --git a/src/types/schema/message.rs b/src/types/schema/message.rs index 7603555..d430e2a 100644 --- a/src/types/schema/message.rs +++ b/src/types/schema/message.rs @@ -3,7 +3,7 @@ use serde::{Deserialize, Serialize}; use crate::types::entities::{ AllowedMention, Component, Embed, MessageReference, PartialDiscordFileAttachment, }; -use crate::types::Snowflake; +use crate::types::{Attachment, Snowflake}; #[derive(Debug, Default, Deserialize, Serialize, Clone, PartialEq)] #[serde(rename_all = "snake_case")] @@ -110,3 +110,16 @@ pub struct MessageAck { pub manual: Option, pub mention_count: Option, } + +#[derive(Debug, Deserialize, Serialize, Clone, PartialEq, PartialOrd)] +pub struct MessageModifySchema { + content: Option, + embeds: Option>, + embed: Option, + allowed_mentions: Option, + components: Option>, + flags: Option, + files: Option>, + payload_json: Option, + attachments: Option>, +}