Get pinned, pin, unpin Message (#404)

Implements the above mentioned features, including a basic test
Closes #268, #269, #270
This commit is contained in:
Flori 2023-08-22 20:15:30 +02:00 committed by GitHub
commit 6f7c49a187
3 changed files with 151 additions and 5 deletions

View File

@ -101,6 +101,7 @@ impl Message {
endpoint endpoint
)) ))
.header("Authorization", user.token()) .header("Authorization", user.token())
.header("Content-Type", "application/json")
.body(to_string(&query).unwrap()), .body(to_string(&query).unwrap()),
}; };
let result = request.send_request(user).await?; 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: <https://discord-userdoccers.vercel.app/resources/message#get-pinned-messages>
pub async fn get_sticky(
channel_id: Snowflake,
user: &mut UserMeta,
) -> ChorusResult<Vec<Message>> {
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::<Vec<Message>>(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: <https://discord-userdoccers.vercel.app/resources/message#pin-message>
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: <https://discord-userdoccers.vercel.app/resources/message#unpin-message>
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 { fn search_error(result_text: String) -> ChorusError {

View File

@ -20,7 +20,7 @@ pub struct Message {
pub id: Snowflake, pub id: Snowflake,
pub channel_id: Snowflake, pub channel_id: Snowflake,
#[cfg_attr(feature = "sqlx", sqlx(skip))] #[cfg_attr(feature = "sqlx", sqlx(skip))]
pub author: PublicUser, pub author: Option<PublicUser>,
pub content: Option<String>, pub content: Option<String>,
pub timestamp: String, pub timestamp: String,
pub edited_timestamp: Option<String>, pub edited_timestamp: Option<String>,
@ -29,15 +29,15 @@ pub struct Message {
#[cfg_attr(feature = "sqlx", sqlx(skip))] #[cfg_attr(feature = "sqlx", sqlx(skip))]
pub mentions: Option<Vec<User>>, pub mentions: Option<Vec<User>>,
#[cfg_attr(feature = "sqlx", sqlx(skip))] #[cfg_attr(feature = "sqlx", sqlx(skip))]
pub mention_roles: Vec<Snowflake>, pub mention_roles: Option<Vec<Snowflake>>,
#[cfg_attr(feature = "sqlx", sqlx(skip))] #[cfg_attr(feature = "sqlx", sqlx(skip))]
pub mention_channels: Option<Vec<ChannelMention>>, pub mention_channels: Option<Vec<ChannelMention>>,
#[cfg_attr(feature = "sqlx", sqlx(skip))] #[cfg_attr(feature = "sqlx", sqlx(skip))]
pub attachments: Vec<Attachment>, pub attachments: Option<Vec<Attachment>>,
#[cfg(feature = "sqlx")] #[cfg(feature = "sqlx")]
pub embeds: Vec<sqlx::types::Json<Embed>>, pub embeds: Vec<sqlx::types::Json<Embed>>,
#[cfg(not(feature = "sqlx"))] #[cfg(not(feature = "sqlx"))]
pub embeds: Vec<Embed>, pub embeds: Option<Vec<Embed>>,
#[cfg(feature = "sqlx")] #[cfg(feature = "sqlx")]
pub reactions: Option<sqlx::types::Json<Vec<Reaction>>>, pub reactions: Option<sqlx::types::Json<Vec<Reaction>>>,
#[cfg(not(feature = "sqlx"))] #[cfg(not(feature = "sqlx"))]
@ -69,6 +69,41 @@ pub struct Message {
pub role_subscription_data: Option<RoleSubscriptionData>, pub role_subscription_data: Option<RoleSubscriptionData>,
} }
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)] #[derive(Debug, PartialEq, Clone, Serialize, Deserialize)]
/// # Reference /// # Reference
/// See <https://discord-userdoccers.vercel.app/resources/message#message-reference-object> /// See <https://discord-userdoccers.vercel.app/resources/message#message-reference-object>

View File

@ -1,7 +1,7 @@
use std::fs::File; use std::fs::File;
use std::io::{BufReader, Read}; use std::io::{BufReader, Read};
use chorus::types::{self, Guild, MessageSearchQuery}; use chorus::types::{self, Guild, Message, MessageSearchQuery};
mod common; mod common;
@ -99,3 +99,42 @@ async fn search_messages() {
assert!(!query_result.is_empty()); assert!(!query_result.is_empty());
assert_eq!(query_result.get(0).unwrap().id, message.id); 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::<Message>::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::<Message>::new()
);
common::teardown(bundle).await
}