From 8cf1f84d73062e5966d43abf231ec74a6b2453df Mon Sep 17 00:00:00 2001 From: bitfl0wer Date: Tue, 22 Aug 2023 20:09:44 +0200 Subject: [PATCH] Closes #269, #268, #270 --- src/api/channels/messages.rs | 72 +++++++++++++++++++++++++++++++++++ src/types/entities/message.rs | 43 +++++++++++++++++++-- tests/messages.rs | 41 +++++++++++++++++++- 3 files changed, 151 insertions(+), 5 deletions(-) diff --git a/src/api/channels/messages.rs b/src/api/channels/messages.rs index cc5aa16..0c3f35b 100644 --- a/src/api/channels/messages.rs +++ b/src/api/channels/messages.rs @@ -101,6 +101,7 @@ impl Message { endpoint )) .header("Authorization", user.token()) + .header("Content-Type", "application/json") .body(to_string(&query).unwrap()), }; let result = request.send_request(user).await?; @@ -128,6 +129,77 @@ impl Message { ), }) } + + /// Returns all pinned messages in the channel as a Vector of message objects without the reactions key. + /// # Reference: + /// See: + pub async fn get_sticky( + channel_id: Snowflake, + user: &mut UserMeta, + ) -> ChorusResult> { + let chorus_request = ChorusRequest { + request: Client::new() + .get(format!( + "{}/channels/{}/pins", + user.belongs_to.borrow().urls.api, + channel_id + )) + .header("Authorization", user.token()) + .header("Content-Type", "application/json"), + limit_type: LimitType::Channel(channel_id), + }; + chorus_request + .deserialize_response::>(user) + .await + } + + /// Pins a message in a channel. Requires the `MANAGE_MESSAGES` permission. Returns a 204 empty response on success. + /// The max pinned messages is 50. + /// + /// # Reference: + /// See: + pub async fn sticky( + channel_id: Snowflake, + message_id: Snowflake, + user: &mut UserMeta, + ) -> ChorusResult<()> { + let chorus_request = ChorusRequest { + request: Client::new() + .put(format!( + "{}/channels/{}/pins/{}", + user.belongs_to.borrow().urls.api, + channel_id, + message_id + )) + .header("Authorization", user.token()) + .header("Content-Type", "application/json"), + limit_type: LimitType::Channel(channel_id), + }; + chorus_request.handle_request_as_result(user).await + } + + /// Unpins a message in a channel. Requires the `MANAGE_MESSAGES` permission. Returns a 204 empty response on success. + /// # Reference: + /// See: + pub async fn unsticky( + channel_id: Snowflake, + message_id: Snowflake, + user: &mut UserMeta, + ) -> ChorusResult<()> { + let chorus_request = ChorusRequest { + request: Client::new() + .delete(format!( + "{}/channels/{}/pins/{}", + user.belongs_to.borrow().urls.api, + channel_id, + message_id + )) + .header("Authorization", user.token()) + .header("Content-Type", "application/json"), + limit_type: LimitType::Channel(channel_id), + }; + chorus_request.handle_request_as_result(user).await + } } fn search_error(result_text: String) -> ChorusError { diff --git a/src/types/entities/message.rs b/src/types/entities/message.rs index 4b03649..2522268 100644 --- a/src/types/entities/message.rs +++ b/src/types/entities/message.rs @@ -20,7 +20,7 @@ pub struct Message { pub id: Snowflake, pub channel_id: Snowflake, #[cfg_attr(feature = "sqlx", sqlx(skip))] - pub author: PublicUser, + pub author: Option, pub content: Option, pub timestamp: String, pub edited_timestamp: Option, @@ -29,15 +29,15 @@ pub struct Message { #[cfg_attr(feature = "sqlx", sqlx(skip))] pub mentions: Option>, #[cfg_attr(feature = "sqlx", sqlx(skip))] - pub mention_roles: Vec, + pub mention_roles: Option>, #[cfg_attr(feature = "sqlx", sqlx(skip))] pub mention_channels: Option>, #[cfg_attr(feature = "sqlx", sqlx(skip))] - pub attachments: Vec, + pub attachments: Option>, #[cfg(feature = "sqlx")] pub embeds: Vec>, #[cfg(not(feature = "sqlx"))] - pub embeds: Vec, + pub embeds: Option>, #[cfg(feature = "sqlx")] pub reactions: Option>>, #[cfg(not(feature = "sqlx"))] @@ -69,6 +69,41 @@ pub struct Message { pub role_subscription_data: Option, } +impl PartialEq for Message { + fn eq(&self, other: &Self) -> bool { + self.id == other.id + && self.channel_id == other.channel_id + && self.author == other.author + && self.content == other.content + && self.timestamp == other.timestamp + && self.edited_timestamp == other.edited_timestamp + && self.tts == other.tts + && self.mention_everyone == other.mention_everyone + && self.mentions == other.mentions + && self.mention_roles == other.mention_roles + && self.mention_channels == other.mention_channels + && self.attachments == other.attachments + && self.embeds == other.embeds + && self.embeds == other.embeds + && self.nonce == other.nonce + && self.pinned == other.pinned + && self.webhook_id == other.webhook_id + && self.message_type == other.message_type + && self.activity == other.activity + && self.activity == other.activity + && self.application_id == other.application_id + && self.message_reference == other.message_reference + && self.message_reference == other.message_reference + && self.flags == other.flags + && self.referenced_message == other.referenced_message + && self.thread == other.thread + && self.components == other.components + && self.sticker_items == other.sticker_items + && self.position == other.position + && self.role_subscription_data == other.role_subscription_data + } +} + #[derive(Debug, PartialEq, Clone, Serialize, Deserialize)] /// # Reference /// See diff --git a/tests/messages.rs b/tests/messages.rs index 2854b1d..43f8c9a 100644 --- a/tests/messages.rs +++ b/tests/messages.rs @@ -1,7 +1,7 @@ use std::fs::File; use std::io::{BufReader, Read}; -use chorus::types::{self, Guild, MessageSearchQuery}; +use chorus::types::{self, Guild, Message, MessageSearchQuery}; mod common; @@ -99,3 +99,42 @@ async fn search_messages() { assert!(!query_result.is_empty()); assert_eq!(query_result.get(0).unwrap().id, message.id); } + +#[tokio::test] +async fn test_stickies() { + let mut bundle = common::setup().await; + let message = types::MessageSendSchema { + content: Some("A Message!".to_string()), + ..Default::default() + }; + let channel = bundle.channel.read().unwrap().clone(); + let message = bundle.user.send_message(message, channel.id).await.unwrap(); + assert_eq!( + Message::get_sticky(channel.id, &mut bundle.user) + .await + .unwrap(), + Vec::::new() + ); + Message::sticky(channel.id, message.id, &mut bundle.user) + .await + .unwrap(); + assert_eq!( + Message::get_sticky(channel.id, &mut bundle.user) + .await + .unwrap() + .get(0) + .unwrap() + .id, + message.id + ); + Message::unsticky(channel.id, message.id, &mut bundle.user) + .await + .unwrap(); + assert_eq!( + Message::get_sticky(channel.id, &mut bundle.user) + .await + .unwrap(), + Vec::::new() + ); + common::teardown(bundle).await +}