get channel messages
This commit is contained in:
parent
da9ba9761d
commit
8d7e49f3c2
|
@ -5,7 +5,7 @@ use crate::{
|
|||
api::common,
|
||||
errors::ChorusLibError,
|
||||
instance::UserMeta,
|
||||
types::{Channel, ChannelModifySchema, Message, Snowflake},
|
||||
types::{Channel, ChannelModifySchema, GetChannelMessagesSchema, Message, Snowflake},
|
||||
};
|
||||
|
||||
impl Channel {
|
||||
|
@ -89,4 +89,21 @@ impl Channel {
|
|||
)
|
||||
.await
|
||||
}
|
||||
|
||||
pub async fn messages(
|
||||
range: GetChannelMessagesSchema,
|
||||
channel_id: Snowflake,
|
||||
user: &mut UserMeta,
|
||||
) -> Result<Vec<Message>, ChorusLibError> {
|
||||
let request = Client::new()
|
||||
.get(format!(
|
||||
"{}/channels/{}/messages",
|
||||
user.belongs_to.borrow().urls.api,
|
||||
channel_id
|
||||
))
|
||||
.bearer_auth(user.token())
|
||||
.query(&range);
|
||||
|
||||
common::deserialize_response::<Vec<Message>>(request, user, Default::default()).await
|
||||
}
|
||||
}
|
||||
|
|
|
@ -47,3 +47,50 @@ pub struct ChannelModifySchema {
|
|||
pub default_thread_rate_limit_per_user: Option<i32>,
|
||||
pub video_quality_mode: Option<i32>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize, Serialize, Clone)]
|
||||
pub struct GetChannelMessagesSchema {
|
||||
/// Between 1 and 100, defaults to 50.
|
||||
pub limit: Option<i32>,
|
||||
#[serde(flatten)]
|
||||
pub anchor: ChannelMessagesAnchor,
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize, Serialize, Clone)]
|
||||
#[serde(rename_all = "snake_case")]
|
||||
pub enum ChannelMessagesAnchor {
|
||||
Before(Snowflake),
|
||||
Around(Snowflake),
|
||||
After(Snowflake),
|
||||
}
|
||||
|
||||
impl GetChannelMessagesSchema {
|
||||
pub fn before(anchor: Snowflake) -> Self {
|
||||
Self {
|
||||
limit: None,
|
||||
anchor: ChannelMessagesAnchor::Before(anchor),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn around(anchor: Snowflake) -> Self {
|
||||
Self {
|
||||
limit: None,
|
||||
anchor: ChannelMessagesAnchor::Around(anchor),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn after(anchor: Snowflake) -> Self {
|
||||
Self {
|
||||
limit: None,
|
||||
anchor: ChannelMessagesAnchor::After(anchor),
|
||||
}
|
||||
}
|
||||
|
||||
/// Must be between 1 and 100
|
||||
pub fn limit(self, limit: i32) -> Self {
|
||||
Self {
|
||||
limit: Some(limit),
|
||||
..self
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,4 +1,7 @@
|
|||
use chorus::types::{self, Channel, PermissionFlags, PermissionOverwrite};
|
||||
use chorus::types::{
|
||||
self, Channel, GetChannelMessagesSchema, MessageSendSchema, PermissionFlags,
|
||||
PermissionOverwrite, Snowflake,
|
||||
};
|
||||
|
||||
mod common;
|
||||
|
||||
|
@ -77,3 +80,60 @@ async fn modify_channel() {
|
|||
|
||||
common::teardown(bundle).await
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn get_channel_messages() {
|
||||
let mut bundle = common::setup().await;
|
||||
|
||||
// First create some messages to read
|
||||
for _ in 0..10 {
|
||||
let _ = bundle
|
||||
.user
|
||||
.send_message(
|
||||
&mut MessageSendSchema {
|
||||
content: Some("A Message!".to_string()),
|
||||
..Default::default()
|
||||
},
|
||||
bundle.channel.id,
|
||||
None,
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
assert_eq!(
|
||||
Channel::messages(
|
||||
GetChannelMessagesSchema::before(Snowflake::generate()),
|
||||
bundle.channel.id,
|
||||
&mut bundle.user,
|
||||
)
|
||||
.await
|
||||
.unwrap()
|
||||
.len(),
|
||||
10
|
||||
);
|
||||
|
||||
// around is currently bugged in spacebar: https://github.com/spacebarchat/server/issues/1072
|
||||
// assert_eq!(
|
||||
// Channel::messages(
|
||||
// GetChannelMessagesSchema::around(Snowflake::generate()).limit(10),
|
||||
// bundle.channel.id,
|
||||
// &mut bundle.user,
|
||||
// )
|
||||
// .await
|
||||
// .unwrap()
|
||||
// .len(),
|
||||
// 5
|
||||
// );
|
||||
|
||||
assert!(Channel::messages(
|
||||
GetChannelMessagesSchema::after(Snowflake::generate()),
|
||||
bundle.channel.id,
|
||||
&mut bundle.user,
|
||||
)
|
||||
.await
|
||||
.unwrap()
|
||||
.is_empty());
|
||||
|
||||
common::teardown(bundle).await
|
||||
}
|
||||
|
|
|
@ -64,20 +64,3 @@ async fn send_message_attachment() {
|
|||
.unwrap();
|
||||
common::teardown(bundle).await
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn read_messages() {
|
||||
let mut bundle = common::setup().await;
|
||||
|
||||
// First create some messages to read
|
||||
let mut message = types::MessageSendSchema {
|
||||
content: Some("A Message!".to_string()),
|
||||
..Default::default()
|
||||
};
|
||||
let _ = bundle
|
||||
.user
|
||||
.send_message(&mut message, bundle.channel.id, None)
|
||||
.await
|
||||
.unwrap();
|
||||
common::teardown(bundle).await
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue