Merge pull request #65 from polyphony-chat/feature/channels
Feature/channels
This commit is contained in:
commit
bf74eba827
|
@ -0,0 +1,43 @@
|
||||||
|
use reqwest::Client;
|
||||||
|
use serde_json::from_str;
|
||||||
|
|
||||||
|
use crate::{
|
||||||
|
api::{limits::Limits, types},
|
||||||
|
errors::InstanceServerError,
|
||||||
|
limit::LimitedRequester,
|
||||||
|
};
|
||||||
|
|
||||||
|
impl types::Channel {
|
||||||
|
pub async fn get(
|
||||||
|
token: &str,
|
||||||
|
url_api: &str,
|
||||||
|
channel_id: &str,
|
||||||
|
limits_user: &mut Limits,
|
||||||
|
limits_instance: &mut Limits,
|
||||||
|
) -> Result<types::Channel, InstanceServerError> {
|
||||||
|
let request = Client::new()
|
||||||
|
.get(format!("{}/channels/{}/", url_api, channel_id))
|
||||||
|
.bearer_auth(token);
|
||||||
|
let mut requester = LimitedRequester::new().await;
|
||||||
|
let result = match requester
|
||||||
|
.send_request(
|
||||||
|
request,
|
||||||
|
crate::api::limits::LimitType::Guild,
|
||||||
|
limits_instance,
|
||||||
|
limits_user,
|
||||||
|
)
|
||||||
|
.await
|
||||||
|
{
|
||||||
|
Ok(result) => result,
|
||||||
|
Err(e) => return Err(e),
|
||||||
|
};
|
||||||
|
let result_text = result.text().await.unwrap();
|
||||||
|
match from_str::<types::Channel>(&result_text) {
|
||||||
|
Ok(object) => Ok(object),
|
||||||
|
Err(e) => Err(InstanceServerError::RequestErrorError {
|
||||||
|
url: format!("{}/channels/{}/", url_api, channel_id),
|
||||||
|
error: e.to_string(),
|
||||||
|
}),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,3 +1,5 @@
|
||||||
|
pub mod channels;
|
||||||
pub mod messages;
|
pub mod messages;
|
||||||
|
|
||||||
|
pub use channels::*;
|
||||||
pub use messages::*;
|
pub use messages::*;
|
||||||
|
|
|
@ -1,11 +1,14 @@
|
||||||
|
use reqwest::Client;
|
||||||
use serde_json::from_str;
|
use serde_json::from_str;
|
||||||
use serde_json::to_string;
|
use serde_json::to_string;
|
||||||
|
|
||||||
|
use crate::api::limits::Limits;
|
||||||
use crate::api::schemas;
|
use crate::api::schemas;
|
||||||
use crate::api::types;
|
use crate::api::types;
|
||||||
use crate::errors::InstanceServerError;
|
use crate::errors::InstanceServerError;
|
||||||
|
use crate::limit::LimitedRequester;
|
||||||
|
|
||||||
impl<'a> types::Guild {
|
impl types::Guild {
|
||||||
/// Creates a new guild with the given parameters.
|
/// Creates a new guild with the given parameters.
|
||||||
///
|
///
|
||||||
/// # Arguments
|
/// # Arguments
|
||||||
|
@ -113,4 +116,86 @@ impl<'a> types::Guild {
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Sends a request to create a new channel in the guild.
|
||||||
|
///
|
||||||
|
/// # Arguments
|
||||||
|
///
|
||||||
|
/// * `url_api` - The base URL for the Discord API.
|
||||||
|
/// * `token` - A Discord bot token.
|
||||||
|
/// * `schema` - A `ChannelCreateSchema` struct containing the properties of the new channel.
|
||||||
|
/// * `limits_user` - A mutable reference to a `Limits` struct containing the user's rate limits.
|
||||||
|
/// * `limits_instance` - A mutable reference to a `Limits` struct containing the instance's rate limits.
|
||||||
|
///
|
||||||
|
/// # Returns
|
||||||
|
///
|
||||||
|
/// A `Result` containing a `reqwest::Response` if the request was successful, or an `InstanceServerError` if there was an error.
|
||||||
|
pub async fn create_channel(
|
||||||
|
&self,
|
||||||
|
url_api: &str,
|
||||||
|
token: &str,
|
||||||
|
schema: schemas::ChannelCreateSchema,
|
||||||
|
limits_user: &mut Limits,
|
||||||
|
limits_instance: &mut Limits,
|
||||||
|
) -> Result<types::Channel, InstanceServerError> {
|
||||||
|
types::Channel::create(
|
||||||
|
token,
|
||||||
|
url_api,
|
||||||
|
&self.id,
|
||||||
|
schema,
|
||||||
|
limits_user,
|
||||||
|
limits_instance,
|
||||||
|
)
|
||||||
|
.await
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl types::Channel {
|
||||||
|
/// Sends a request to create a new channel in a guild.
|
||||||
|
///
|
||||||
|
/// # Arguments
|
||||||
|
///
|
||||||
|
/// * `token` - A Discord bot token.
|
||||||
|
/// * `url_api` - The base URL for the Discord API.
|
||||||
|
/// * `guild_id` - The ID of the guild where the channel will be created.
|
||||||
|
/// * `schema` - A `ChannelCreateSchema` struct containing the properties of the new channel.
|
||||||
|
/// * `limits_user` - A mutable reference to a `Limits` struct containing the user's rate limits.
|
||||||
|
/// * `limits_instance` - A mutable reference to a `Limits` struct containing the instance's rate limits.
|
||||||
|
///
|
||||||
|
/// # Returns
|
||||||
|
///
|
||||||
|
/// A `Result` containing a `reqwest::Response` if the request was successful, or an `InstanceServerError` if there was an error.
|
||||||
|
pub async fn create(
|
||||||
|
token: &str,
|
||||||
|
url_api: &str,
|
||||||
|
guild_id: &str,
|
||||||
|
schema: schemas::ChannelCreateSchema,
|
||||||
|
limits_user: &mut Limits,
|
||||||
|
limits_instance: &mut Limits,
|
||||||
|
) -> Result<types::Channel, InstanceServerError> {
|
||||||
|
let request = Client::new()
|
||||||
|
.post(format!("{}/guilds/{}/channels/", url_api, guild_id))
|
||||||
|
.bearer_auth(token)
|
||||||
|
.body(to_string(&schema).unwrap());
|
||||||
|
let mut requester = LimitedRequester::new().await;
|
||||||
|
let result = match requester
|
||||||
|
.send_request(
|
||||||
|
request,
|
||||||
|
crate::api::limits::LimitType::Guild,
|
||||||
|
limits_instance,
|
||||||
|
limits_user,
|
||||||
|
)
|
||||||
|
.await
|
||||||
|
{
|
||||||
|
Ok(result) => result,
|
||||||
|
Err(e) => return Err(e),
|
||||||
|
};
|
||||||
|
match from_str::<types::Channel>(&result.text().await.unwrap()) {
|
||||||
|
Ok(object) => Ok(object),
|
||||||
|
Err(e) => Err(InstanceServerError::RequestErrorError {
|
||||||
|
url: format!("{}/guilds/{}/channels/", url_api, guild_id),
|
||||||
|
error: e.to_string(),
|
||||||
|
}),
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -314,6 +314,31 @@ pub struct UserModifySchema {
|
||||||
pub discriminator: Option<i16>,
|
pub discriminator: Option<i16>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Deserialize, Serialize)]
|
||||||
|
#[serde(rename_all = "snake_case")]
|
||||||
|
// TODO: Implement in polyphony/types
|
||||||
|
pub struct ChannelCreateSchema {
|
||||||
|
pub name: String,
|
||||||
|
#[serde(rename = "type")]
|
||||||
|
pub channel_type: Option<u8>,
|
||||||
|
pub topic: Option<String>,
|
||||||
|
pub icon: Option<String>,
|
||||||
|
pub bitrate: Option<i32>,
|
||||||
|
pub user_limit: Option<i32>,
|
||||||
|
pub rate_limit_per_user: Option<i32>,
|
||||||
|
pub position: Option<i32>,
|
||||||
|
pub permission_overwrites: Option<Vec<crate::api::types::PermissionOverwrite>>,
|
||||||
|
pub parent_id: Option<String>,
|
||||||
|
pub id: Option<String>,
|
||||||
|
pub nsfw: Option<bool>,
|
||||||
|
pub rtc_region: Option<String>,
|
||||||
|
pub default_auto_archive_duration: Option<i32>,
|
||||||
|
pub default_reaction_emoji: Option<String>,
|
||||||
|
pub flags: Option<i32>,
|
||||||
|
pub default_thread_rate_limit_per_user: Option<i32>,
|
||||||
|
pub video_quality_mode: Option<i32>,
|
||||||
|
}
|
||||||
|
|
||||||
// I know that some of these tests are... really really basic and unneccessary, but sometimes, I
|
// I know that some of these tests are... really really basic and unneccessary, but sometimes, I
|
||||||
// just feel like writing tests, so there you go :) -@bitfl0wer
|
// just feel like writing tests, so there you go :) -@bitfl0wer
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
|
|
|
@ -319,7 +319,7 @@ pub struct RoleObject {
|
||||||
//pub tags: Option<RoleTags>
|
//pub tags: Option<RoleTags>
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize, Debug, Default, Clone)]
|
#[derive(Serialize, Deserialize, Debug, Default, Clone, Eq, PartialEq)]
|
||||||
pub struct UserObject {
|
pub struct UserObject {
|
||||||
pub id: String,
|
pub id: String,
|
||||||
username: String,
|
username: String,
|
||||||
|
@ -722,7 +722,7 @@ pub struct MessageInteraction {
|
||||||
pub member: Option<GuildMember>,
|
pub member: Option<GuildMember>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Deserialize, Serialize, Clone)]
|
#[derive(Debug, Deserialize, Serialize, Clone, Eq, PartialEq)]
|
||||||
pub struct GuildMember {
|
pub struct GuildMember {
|
||||||
pub user: Option<UserObject>,
|
pub user: Option<UserObject>,
|
||||||
pub nick: Option<String>,
|
pub nick: Option<String>,
|
||||||
|
@ -738,7 +738,7 @@ pub struct GuildMember {
|
||||||
pub communication_disabled_until: Option<String>,
|
pub communication_disabled_until: Option<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Default, Debug, Serialize, Deserialize, Clone)]
|
#[derive(Default, Debug, Serialize, Deserialize, Clone, Eq, PartialEq)]
|
||||||
pub struct Channel {
|
pub struct Channel {
|
||||||
pub id: String,
|
pub id: String,
|
||||||
#[serde(rename = "type")]
|
#[serde(rename = "type")]
|
||||||
|
@ -777,7 +777,7 @@ pub struct Channel {
|
||||||
pub default_forum_layout: Option<i32>,
|
pub default_forum_layout: Option<i32>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Deserialize, Serialize, Clone)]
|
#[derive(Debug, Deserialize, Serialize, Clone, Eq, PartialEq)]
|
||||||
pub struct Tag {
|
pub struct Tag {
|
||||||
pub id: u64,
|
pub id: u64,
|
||||||
pub name: String,
|
pub name: String,
|
||||||
|
@ -786,7 +786,7 @@ pub struct Tag {
|
||||||
pub emoji_name: Option<String>,
|
pub emoji_name: Option<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Serialize, Deserialize, Clone)]
|
#[derive(Debug, Serialize, Deserialize, Clone, Eq, PartialEq)]
|
||||||
pub struct PermissionOverwrite {
|
pub struct PermissionOverwrite {
|
||||||
pub id: String,
|
pub id: String,
|
||||||
#[serde(rename = "type")]
|
#[serde(rename = "type")]
|
||||||
|
@ -795,7 +795,7 @@ pub struct PermissionOverwrite {
|
||||||
pub deny: String,
|
pub deny: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Deserialize, Serialize, Clone)]
|
#[derive(Debug, Deserialize, Serialize, Clone, Eq, PartialEq)]
|
||||||
pub struct ThreadMetadata {
|
pub struct ThreadMetadata {
|
||||||
pub archived: bool,
|
pub archived: bool,
|
||||||
pub auto_archive_duration: i32,
|
pub auto_archive_duration: i32,
|
||||||
|
@ -805,7 +805,7 @@ pub struct ThreadMetadata {
|
||||||
pub create_timestamp: Option<String>,
|
pub create_timestamp: Option<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Default, Debug, Deserialize, Serialize, Clone)]
|
#[derive(Default, Debug, Deserialize, Serialize, Clone, Eq, PartialEq)]
|
||||||
pub struct ThreadMember {
|
pub struct ThreadMember {
|
||||||
pub id: Option<u64>,
|
pub id: Option<u64>,
|
||||||
pub user_id: Option<u64>,
|
pub user_id: Option<u64>,
|
||||||
|
@ -855,7 +855,7 @@ pub struct StageInstance {
|
||||||
pub guild_scheduled_event_id: Option<String>,
|
pub guild_scheduled_event_id: Option<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Deserialize, Serialize, Clone)]
|
#[derive(Debug, Deserialize, Serialize, Clone, Eq, PartialEq)]
|
||||||
pub struct DefaultReaction {
|
pub struct DefaultReaction {
|
||||||
pub emoji_id: Option<String>,
|
pub emoji_id: Option<String>,
|
||||||
pub emoji_name: Option<String>,
|
pub emoji_name: Option<String>,
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
use chorus::{
|
use chorus::{
|
||||||
api::{AuthUsername, RegisterSchema, User},
|
api::schemas,
|
||||||
|
api::{AuthUsername, Channel, Guild, GuildCreateSchema, RegisterSchema, User},
|
||||||
instance::Instance,
|
instance::Instance,
|
||||||
URLBundle,
|
URLBundle,
|
||||||
};
|
};
|
||||||
|
@ -8,6 +9,9 @@ use chorus::{
|
||||||
struct TestBundle {
|
struct TestBundle {
|
||||||
urls: URLBundle,
|
urls: URLBundle,
|
||||||
user: User,
|
user: User,
|
||||||
|
instance: Instance,
|
||||||
|
guild_id: String,
|
||||||
|
channel: Channel,
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set up a test by creating an Instance and a User. Reduces Test boilerplate.
|
// Set up a test by creating an Instance and a User. Reduces Test boilerplate.
|
||||||
|
@ -32,18 +36,72 @@ async fn setup() -> TestBundle {
|
||||||
None,
|
None,
|
||||||
)
|
)
|
||||||
.unwrap();
|
.unwrap();
|
||||||
let user = instance.register_account(®).await.unwrap();
|
let guild_create_schema = GuildCreateSchema {
|
||||||
|
name: Some("Test-Guild!".to_string()),
|
||||||
|
region: None,
|
||||||
|
icon: None,
|
||||||
|
channels: None,
|
||||||
|
guild_template_code: None,
|
||||||
|
system_channel_id: None,
|
||||||
|
rules_channel_id: None,
|
||||||
|
};
|
||||||
|
let channel_create_schema = schemas::ChannelCreateSchema {
|
||||||
|
name: "testchannel".to_string(),
|
||||||
|
channel_type: Some(0),
|
||||||
|
topic: None,
|
||||||
|
icon: None,
|
||||||
|
bitrate: None,
|
||||||
|
user_limit: None,
|
||||||
|
rate_limit_per_user: None,
|
||||||
|
position: None,
|
||||||
|
permission_overwrites: None,
|
||||||
|
parent_id: None,
|
||||||
|
id: None,
|
||||||
|
nsfw: Some(false),
|
||||||
|
rtc_region: None,
|
||||||
|
default_auto_archive_duration: None,
|
||||||
|
default_reaction_emoji: None,
|
||||||
|
flags: Some(0),
|
||||||
|
default_thread_rate_limit_per_user: Some(0),
|
||||||
|
video_quality_mode: None,
|
||||||
|
};
|
||||||
|
let mut user = instance.register_account(®).await.unwrap();
|
||||||
|
let guild_id = Guild::create(&mut user, urls.get_api(), guild_create_schema)
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
|
let channel = Channel::create(
|
||||||
|
&user.token,
|
||||||
|
urls.get_api(),
|
||||||
|
guild_id.as_str(),
|
||||||
|
channel_create_schema,
|
||||||
|
&mut user.limits,
|
||||||
|
&mut instance.limits,
|
||||||
|
)
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
TestBundle { urls, user }
|
TestBundle {
|
||||||
|
urls,
|
||||||
|
user,
|
||||||
|
instance,
|
||||||
|
guild_id,
|
||||||
|
channel,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Teardown method to clean up after a test.
|
// Teardown method to clean up after a test.
|
||||||
async fn teardown(bundle: TestBundle) {
|
async fn teardown(mut bundle: TestBundle) {
|
||||||
|
Guild::delete(
|
||||||
|
&mut bundle.user,
|
||||||
|
bundle.instance.urls.get_api(),
|
||||||
|
bundle.guild_id,
|
||||||
|
)
|
||||||
|
.await;
|
||||||
bundle.user.delete().await;
|
bundle.user.delete().await;
|
||||||
}
|
}
|
||||||
|
|
||||||
mod guild {
|
mod guild {
|
||||||
use chorus::api::{schemas, types};
|
use chorus::api::{schemas, types, Channel};
|
||||||
|
|
||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
async fn guild_creation_deletion() {
|
async fn guild_creation_deletion() {
|
||||||
|
@ -70,5 +128,27 @@ mod guild {
|
||||||
None => assert!(true),
|
None => assert!(true),
|
||||||
Some(_) => assert!(false),
|
Some(_) => assert!(false),
|
||||||
}
|
}
|
||||||
|
crate::teardown(bundle).await
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn get_channel() {
|
||||||
|
let mut bundle = crate::setup().await;
|
||||||
|
let bundle_channel = bundle.channel.clone();
|
||||||
|
let bundle_user = &mut bundle.user;
|
||||||
|
|
||||||
|
assert_eq!(
|
||||||
|
bundle_channel,
|
||||||
|
Channel::get(
|
||||||
|
bundle_user.token.as_str(),
|
||||||
|
bundle.instance.urls.get_api(),
|
||||||
|
&bundle_channel.id,
|
||||||
|
&mut bundle_user.limits,
|
||||||
|
&mut bundle.instance.limits
|
||||||
|
)
|
||||||
|
.await
|
||||||
|
.unwrap()
|
||||||
|
);
|
||||||
|
crate::teardown(bundle).await
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue