Add Modify Message endpoint

This commit is contained in:
bitfl0wer 2023-08-22 22:27:09 +02:00
parent c9562457d2
commit e8a02e57f8
No known key found for this signature in database
GPG Key ID: 0ACD574FCF5226CF
5 changed files with 106 additions and 14 deletions

View File

@ -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: <https://discord-userdoccers.vercel.app/resources/message#edit-message>
pub async fn modify(
channel_id: Snowflake,
message_id: Snowflake,
schema: MessageModifySchema,
user: &mut UserMeta,
) -> ChorusResult<Message> {
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::<Message>(user).await
}
}
fn search_error(result_text: String) -> ChorusError {

View File

@ -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 <https://discord.com/developers/docs/resources/channel#attachment-object>

View File

@ -26,3 +26,50 @@ pub struct Emoji {
pub animated: Option<bool>,
pub available: Option<bool>,
}
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<std::cmp::Ordering> {
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)
}
}

View File

@ -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<String>,
#[serde(rename = "type")]
@ -167,14 +167,14 @@ pub struct Embed {
fields: Option<Vec<EmbedField>>,
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, PartialOrd, Ord)]
pub struct EmbedFooter {
text: String,
icon_url: Option<String>,
proxy_icon_url: Option<String>,
}
#[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<i32>,
}
#[derive(Debug, PartialEq, Eq, Clone, Serialize, Deserialize)]
#[derive(Debug, PartialEq, Eq, Clone, Serialize, Deserialize, PartialOrd, Ord)]
pub struct EmbedThumbnail {
url: String,
proxy_url: Option<String>,
@ -190,7 +190,7 @@ pub struct EmbedThumbnail {
width: Option<i32>,
}
#[derive(Debug, PartialEq, Eq, Clone, Serialize, Deserialize)]
#[derive(Debug, PartialEq, Eq, Clone, Serialize, Deserialize, PartialOrd, Ord)]
struct EmbedVideo {
url: Option<String>,
proxy_url: Option<String>,
@ -198,13 +198,13 @@ struct EmbedVideo {
width: Option<i32>,
}
#[derive(Debug, PartialEq, Eq, Clone, Serialize, Deserialize)]
#[derive(Debug, PartialEq, Eq, Clone, Serialize, Deserialize, PartialOrd, Ord)]
pub struct EmbedProvider {
name: Option<String>,
url: Option<String>,
}
#[derive(Debug, PartialEq, Eq, Clone, Serialize, Deserialize)]
#[derive(Debug, PartialEq, Eq, Clone, Serialize, Deserialize, PartialOrd, Ord)]
pub struct EmbedAuthor {
name: String,
url: Option<String>,
@ -212,14 +212,14 @@ pub struct EmbedAuthor {
proxy_icon_url: Option<String>,
}
#[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<bool>,
}
#[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,

View File

@ -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<bool>,
pub mention_count: Option<u32>,
}
#[derive(Debug, Deserialize, Serialize, Clone, PartialEq, PartialOrd)]
pub struct MessageModifySchema {
content: Option<String>,
embeds: Option<Vec<Embed>>,
embed: Option<Embed>,
allowed_mentions: Option<AllowedMention>,
components: Option<Vec<Component>>,
flags: Option<i32>,
files: Option<Vec<u8>>,
payload_json: Option<String>,
attachments: Option<Vec<Attachment>>,
}