From 69425f18acb095597688e32f3562ceaf072c5460 Mon Sep 17 00:00:00 2001 From: bitfl0wer Date: Mon, 22 May 2023 23:22:34 +0200 Subject: [PATCH 01/20] start working on channel create --- src/api/guilds/guilds.rs | 14 +++++++++++++- src/api/schemas.rs | 25 +++++++++++++++++++++++++ 2 files changed, 38 insertions(+), 1 deletion(-) diff --git a/src/api/guilds/guilds.rs b/src/api/guilds/guilds.rs index 5bbbd23..0e0ee1a 100644 --- a/src/api/guilds/guilds.rs +++ b/src/api/guilds/guilds.rs @@ -5,7 +5,7 @@ use crate::api::schemas; use crate::api::types; use crate::errors::InstanceServerError; -impl<'a> types::Guild { +impl types::Guild { /// Creates a new guild with the given parameters. /// /// # Arguments @@ -113,4 +113,16 @@ impl<'a> types::Guild { None } } + + pub async fn create_channel() {} +} + +impl types::Channel { + pub async fn create( + token: &str, + url_api: &str, + guild_id: &str, + schema: schemas::ChannelCreateSchema, + ) { + } } diff --git a/src/api/schemas.rs b/src/api/schemas.rs index e84abdf..fb1a14f 100644 --- a/src/api/schemas.rs +++ b/src/api/schemas.rs @@ -314,6 +314,31 @@ pub struct UserModifySchema { pub discriminator: Option, } +#[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, + pub topic: Option, + pub icon: Option, + pub bitrate: Option, + pub user_limit: Option, + pub rate_limit_per_user: Option, + pub position: Option, + pub permission_overwrites: Option>, + pub parent_id: Option, + pub id: Option, + pub nsfw: Option, + pub rtc_region: Option, + pub default_auto_archive_duration: Option, + pub default_reaction_emoji: Option, + pub flags: Option, + pub default_thread_rate_limit_per_user: Option, + pub video_quality_mode: Option, +} + // 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 #[cfg(test)] From fc55a53ed5da271ab3ab22deaac91f63b3882b77 Mon Sep 17 00:00:00 2001 From: bitfl0wer Date: Tue, 23 May 2023 13:06:09 +0200 Subject: [PATCH 02/20] Implement create channel methods --- src/api/guilds/guilds.rs | 66 ++++++++++++++++++++++++++++++++++++++-- 1 file changed, 64 insertions(+), 2 deletions(-) diff --git a/src/api/guilds/guilds.rs b/src/api/guilds/guilds.rs index 0e0ee1a..04a0546 100644 --- a/src/api/guilds/guilds.rs +++ b/src/api/guilds/guilds.rs @@ -1,9 +1,12 @@ +use reqwest::Client; use serde_json::from_str; use serde_json::to_string; +use crate::api::limits::Limits; use crate::api::schemas; use crate::api::types; use crate::errors::InstanceServerError; +use crate::limit::LimitedRequester; impl types::Guild { /// Creates a new guild with the given parameters. @@ -114,15 +117,74 @@ impl types::Guild { } } - pub async fn create_channel() {} + /// 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::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 { + 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; + requester + .send_request( + request, + crate::api::limits::LimitType::Guild, + limits_instance, + limits_user, + ) + .await } } From 12095132d59bb2e4ab663825344dc029c23b0ecd Mon Sep 17 00:00:00 2001 From: bitfl0wer Date: Tue, 23 May 2023 13:15:57 +0200 Subject: [PATCH 03/20] Add instance, guild, channel to TestBundle --- tests/integration.rs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/tests/integration.rs b/tests/integration.rs index e004496..0d5389c 100644 --- a/tests/integration.rs +++ b/tests/integration.rs @@ -1,5 +1,5 @@ use chorus::{ - api::{AuthUsername, RegisterSchema, User}, + api::{AuthUsername, Channel, Guild, RegisterSchema, User}, instance::Instance, URLBundle, }; @@ -8,6 +8,9 @@ use chorus::{ struct TestBundle { urls: URLBundle, user: User, + instance: Instance, + guild: Guild, + channel: Channel, } // Set up a test by creating an Instance and a User. Reduces Test boilerplate. From 81885cffc025756746d1d0df07c8812da000be67 Mon Sep 17 00:00:00 2001 From: bitfl0wer Date: Tue, 23 May 2023 13:50:39 +0200 Subject: [PATCH 04/20] Change create channel method to return Channel Previously returned a Response object. --- src/api/guilds/guilds.rs | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/src/api/guilds/guilds.rs b/src/api/guilds/guilds.rs index 04a0546..3761140 100644 --- a/src/api/guilds/guilds.rs +++ b/src/api/guilds/guilds.rs @@ -137,7 +137,7 @@ impl types::Guild { schema: schemas::ChannelCreateSchema, limits_user: &mut Limits, limits_instance: &mut Limits, - ) -> Result { + ) -> Result { types::Channel::create( token, url_api, @@ -172,13 +172,13 @@ impl types::Channel { schema: schemas::ChannelCreateSchema, limits_user: &mut Limits, limits_instance: &mut Limits, - ) -> Result { + ) -> Result { 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; - requester + let result = match requester .send_request( request, crate::api::limits::LimitType::Guild, @@ -186,5 +186,16 @@ impl types::Channel { limits_user, ) .await + { + Ok(result) => result, + Err(e) => return Err(e), + }; + match from_str::(&result.text().await.unwrap()) { + Ok(object) => Ok(object), + Err(e) => Err(InstanceServerError::RequestErrorError { + url: url_api.to_string(), + error: e.to_string(), + }), + } } } From b05c5b122480d6a99410579ca02e12d88979cb11 Mon Sep 17 00:00:00 2001 From: bitfl0wer Date: Tue, 23 May 2023 13:57:02 +0200 Subject: [PATCH 05/20] Impl. instance, guild_id, channel to TestBundle --- tests/integration.rs | 57 ++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 53 insertions(+), 4 deletions(-) diff --git a/tests/integration.rs b/tests/integration.rs index 0d5389c..79e1d2a 100644 --- a/tests/integration.rs +++ b/tests/integration.rs @@ -1,5 +1,6 @@ use chorus::{ - api::{AuthUsername, Channel, Guild, RegisterSchema, User}, + api::schemas, + api::{AuthUsername, Channel, Guild, GuildCreateSchema, RegisterSchema, User}, instance::Instance, URLBundle, }; @@ -9,7 +10,7 @@ struct TestBundle { urls: URLBundle, user: User, instance: Instance, - guild: Guild, + guild_id: String, channel: Channel, } @@ -35,9 +36,57 @@ async fn setup() -> TestBundle { None, ) .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: None, + rtc_region: None, + default_auto_archive_duration: None, + default_reaction_emoji: None, + flags: None, + default_thread_rate_limit_per_user: None, + 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. From d48644281381f47bfa3227075e0600e727058e04 Mon Sep 17 00:00:00 2001 From: bitfl0wer Date: Tue, 23 May 2023 14:00:08 +0200 Subject: [PATCH 06/20] Delete Guild on test cleanup --- tests/integration.rs | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/tests/integration.rs b/tests/integration.rs index 79e1d2a..f84fa48 100644 --- a/tests/integration.rs +++ b/tests/integration.rs @@ -90,7 +90,13 @@ async fn setup() -> TestBundle { } // 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; } From 985e180c65af990bec58647b3d5cdd658d347fc5 Mon Sep 17 00:00:00 2001 From: bitfl0wer Date: Tue, 23 May 2023 14:28:45 +0200 Subject: [PATCH 07/20] Add teardown() call to guild_creation test --- tests/integration.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/integration.rs b/tests/integration.rs index f84fa48..7cf76cf 100644 --- a/tests/integration.rs +++ b/tests/integration.rs @@ -128,5 +128,6 @@ mod guild { None => assert!(true), Some(_) => assert!(false), } + crate::teardown(bundle).await } } From fd0442dcc9d647d6338990b9d97c5e341e36c4b7 Mon Sep 17 00:00:00 2001 From: bitfl0wer Date: Tue, 23 May 2023 14:31:21 +0200 Subject: [PATCH 08/20] Start working on get() channel route --- src/api/guilds/guilds.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/api/guilds/guilds.rs b/src/api/guilds/guilds.rs index 3761140..40b22d8 100644 --- a/src/api/guilds/guilds.rs +++ b/src/api/guilds/guilds.rs @@ -198,4 +198,6 @@ impl types::Channel { }), } } + + pub async fn get() {} } From 9a6a08bb671828fbe3206aa737fa6d0e20a2cc98 Mon Sep 17 00:00:00 2001 From: bitfl0wer Date: Tue, 23 May 2023 14:37:34 +0200 Subject: [PATCH 09/20] Implement get() for channel --- src/api/guilds/guilds.rs | 32 +++++++++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/src/api/guilds/guilds.rs b/src/api/guilds/guilds.rs index 40b22d8..a2fc119 100644 --- a/src/api/guilds/guilds.rs +++ b/src/api/guilds/guilds.rs @@ -199,5 +199,35 @@ impl types::Channel { } } - pub async fn get() {} + pub async fn get( + token: &str, + url_api: &str, + guild_id: &str, + limits_user: &mut Limits, + limits_instance: &mut Limits, + ) -> Result { + let request = Client::new() + .get(format!("{}/guilds/{}/channels/", url_api, guild_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), + }; + match from_str::(&result.text().await.unwrap()) { + Ok(object) => Ok(object), + Err(e) => Err(InstanceServerError::RequestErrorError { + url: url_api.to_string(), + error: e.to_string(), + }), + } + } } From 912d8043d0b54ecd34d063c802c167039c732e2a Mon Sep 17 00:00:00 2001 From: bitfl0wer Date: Tue, 23 May 2023 14:38:03 +0200 Subject: [PATCH 10/20] Replace URL in errors with full route URL --- src/api/guilds/guilds.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/api/guilds/guilds.rs b/src/api/guilds/guilds.rs index a2fc119..da7c503 100644 --- a/src/api/guilds/guilds.rs +++ b/src/api/guilds/guilds.rs @@ -193,7 +193,7 @@ impl types::Channel { match from_str::(&result.text().await.unwrap()) { Ok(object) => Ok(object), Err(e) => Err(InstanceServerError::RequestErrorError { - url: url_api.to_string(), + url: format!("{}/guilds/{}/channels/", url_api, guild_id), error: e.to_string(), }), } @@ -225,7 +225,7 @@ impl types::Channel { match from_str::(&result.text().await.unwrap()) { Ok(object) => Ok(object), Err(e) => Err(InstanceServerError::RequestErrorError { - url: url_api.to_string(), + url: format!("{}/guilds/{}/channels/", url_api, guild_id), error: e.to_string(), }), } From b293f5ed96c26820c1fcd51418adae6b34d60d67 Mon Sep 17 00:00:00 2001 From: bitfl0wer Date: Tue, 23 May 2023 14:50:48 +0200 Subject: [PATCH 11/20] Add ChannelType enum --- src/api/types.rs | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/src/api/types.rs b/src/api/types.rs index 5631130..e5cc3de 100644 --- a/src/api/types.rs +++ b/src/api/types.rs @@ -544,7 +544,7 @@ struct ChannelMention { id: String, guild_id: String, #[serde(rename = "type")] - channel_type: i32, + channel_type: ChannelType, name: String, } @@ -742,7 +742,7 @@ pub struct GuildMember { pub struct Channel { pub id: String, #[serde(rename = "type")] - pub channel_type: i32, + pub channel_type: ChannelType, pub guild_id: Option, pub position: Option, pub permission_overwrites: Option>, @@ -1480,3 +1480,21 @@ pub struct Webhook { pub struct GuildCreateResponse { pub id: String, } + +#[derive(Serialize, Deserialize, Debug, Default, Clone)] +#[serde(rename_all = "SCREAMING_SNAKE_CASE")] +pub enum ChannelType { + #[default] + GuildText = 0, + DM = 1, + GuildVoice = 2, + GroupDM = 3, + GuildCategory = 4, + GuildAnnouncement = 5, + AnnouncementThread = 10, + PublicThread = 11, + PrivateThread = 12, + GuildStageVoice = 13, + GuildDirectory = 14, + GuildForum = 15, +} From 64693c87706faebaa362513167165eb61aee254c Mon Sep 17 00:00:00 2001 From: bitfl0wer Date: Tue, 23 May 2023 14:51:28 +0200 Subject: [PATCH 12/20] Add todo --- src/api/types.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/api/types.rs b/src/api/types.rs index e5cc3de..5ca4f26 100644 --- a/src/api/types.rs +++ b/src/api/types.rs @@ -1483,6 +1483,7 @@ pub struct GuildCreateResponse { #[derive(Serialize, Deserialize, Debug, Default, Clone)] #[serde(rename_all = "SCREAMING_SNAKE_CASE")] +// TODO: Implement in polyphony/types pub enum ChannelType { #[default] GuildText = 0, From 420f3798f469aae893b875406b9667ada08e3013 Mon Sep 17 00:00:00 2001 From: bitfl0wer Date: Tue, 23 May 2023 15:10:56 +0200 Subject: [PATCH 13/20] try flattening the enum to fix CI --- src/api/types.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/api/types.rs b/src/api/types.rs index 5ca4f26..4e76c8b 100644 --- a/src/api/types.rs +++ b/src/api/types.rs @@ -544,6 +544,7 @@ struct ChannelMention { id: String, guild_id: String, #[serde(rename = "type")] + #[serde(flatten)] channel_type: ChannelType, name: String, } @@ -742,6 +743,7 @@ pub struct GuildMember { pub struct Channel { pub id: String, #[serde(rename = "type")] + #[serde(flatten)] pub channel_type: ChannelType, pub guild_id: Option, pub position: Option, From f4f01a00ccf404ecb41e55996168aad17b41f8ab Mon Sep 17 00:00:00 2001 From: bitfl0wer Date: Tue, 23 May 2023 15:17:04 +0200 Subject: [PATCH 14/20] Revert "Add ChannelType enum" This reverts commit b293f5ed96c26820c1fcd51418adae6b34d60d67. --- src/api/types.rs | 25 ++----------------------- 1 file changed, 2 insertions(+), 23 deletions(-) diff --git a/src/api/types.rs b/src/api/types.rs index 4e76c8b..5631130 100644 --- a/src/api/types.rs +++ b/src/api/types.rs @@ -544,8 +544,7 @@ struct ChannelMention { id: String, guild_id: String, #[serde(rename = "type")] - #[serde(flatten)] - channel_type: ChannelType, + channel_type: i32, name: String, } @@ -743,8 +742,7 @@ pub struct GuildMember { pub struct Channel { pub id: String, #[serde(rename = "type")] - #[serde(flatten)] - pub channel_type: ChannelType, + pub channel_type: i32, pub guild_id: Option, pub position: Option, pub permission_overwrites: Option>, @@ -1482,22 +1480,3 @@ pub struct Webhook { pub struct GuildCreateResponse { pub id: String, } - -#[derive(Serialize, Deserialize, Debug, Default, Clone)] -#[serde(rename_all = "SCREAMING_SNAKE_CASE")] -// TODO: Implement in polyphony/types -pub enum ChannelType { - #[default] - GuildText = 0, - DM = 1, - GuildVoice = 2, - GroupDM = 3, - GuildCategory = 4, - GuildAnnouncement = 5, - AnnouncementThread = 10, - PublicThread = 11, - PrivateThread = 12, - GuildStageVoice = 13, - GuildDirectory = 14, - GuildForum = 15, -} From 368152e09279d866741a51676833989d1903b599 Mon Sep 17 00:00:00 2001 From: bitfl0wer Date: Tue, 23 May 2023 15:25:32 +0200 Subject: [PATCH 15/20] Make Channel type derive Eq, PartialEq recursively --- src/api/types.rs | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/api/types.rs b/src/api/types.rs index 5631130..f040dd2 100644 --- a/src/api/types.rs +++ b/src/api/types.rs @@ -319,7 +319,7 @@ pub struct RoleObject { //pub tags: Option } -#[derive(Serialize, Deserialize, Debug, Default, Clone)] +#[derive(Serialize, Deserialize, Debug, Default, Clone, Eq, PartialEq)] pub struct UserObject { pub id: String, username: String, @@ -722,7 +722,7 @@ pub struct MessageInteraction { pub member: Option, } -#[derive(Debug, Deserialize, Serialize, Clone)] +#[derive(Debug, Deserialize, Serialize, Clone, Eq, PartialEq)] pub struct GuildMember { pub user: Option, pub nick: Option, @@ -738,7 +738,7 @@ pub struct GuildMember { pub communication_disabled_until: Option, } -#[derive(Default, Debug, Serialize, Deserialize, Clone)] +#[derive(Default, Debug, Serialize, Deserialize, Clone, Eq, PartialEq)] pub struct Channel { pub id: String, #[serde(rename = "type")] @@ -777,7 +777,7 @@ pub struct Channel { pub default_forum_layout: Option, } -#[derive(Debug, Deserialize, Serialize, Clone)] +#[derive(Debug, Deserialize, Serialize, Clone, Eq, PartialEq)] pub struct Tag { pub id: u64, pub name: String, @@ -786,7 +786,7 @@ pub struct Tag { pub emoji_name: Option, } -#[derive(Debug, Serialize, Deserialize, Clone)] +#[derive(Debug, Serialize, Deserialize, Clone, Eq, PartialEq)] pub struct PermissionOverwrite { pub id: String, #[serde(rename = "type")] @@ -795,7 +795,7 @@ pub struct PermissionOverwrite { pub deny: String, } -#[derive(Debug, Deserialize, Serialize, Clone)] +#[derive(Debug, Deserialize, Serialize, Clone, Eq, PartialEq)] pub struct ThreadMetadata { pub archived: bool, pub auto_archive_duration: i32, @@ -805,7 +805,7 @@ pub struct ThreadMetadata { pub create_timestamp: Option, } -#[derive(Default, Debug, Deserialize, Serialize, Clone)] +#[derive(Default, Debug, Deserialize, Serialize, Clone, Eq, PartialEq)] pub struct ThreadMember { pub id: Option, pub user_id: Option, @@ -855,7 +855,7 @@ pub struct StageInstance { pub guild_scheduled_event_id: Option, } -#[derive(Debug, Deserialize, Serialize, Clone)] +#[derive(Debug, Deserialize, Serialize, Clone, Eq, PartialEq)] pub struct DefaultReaction { pub emoji_id: Option, pub emoji_name: Option, From 797af67d3a53661705d10b0d9d465b0d41c420f3 Mon Sep 17 00:00:00 2001 From: bitfl0wer Date: Tue, 23 May 2023 15:25:47 +0200 Subject: [PATCH 16/20] Add get_channel integration test --- tests/integration.rs | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/tests/integration.rs b/tests/integration.rs index 7cf76cf..c30c3ad 100644 --- a/tests/integration.rs +++ b/tests/integration.rs @@ -101,7 +101,7 @@ async fn teardown(mut bundle: TestBundle) { } mod guild { - use chorus::api::{schemas, types}; + use chorus::api::{schemas, types, Channel}; #[tokio::test] async fn guild_creation_deletion() { @@ -130,4 +130,25 @@ mod guild { } 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.guild_id.as_str(), + &mut bundle_user.limits, + &mut bundle.instance.limits + ) + .await + .unwrap() + ); + crate::teardown(bundle).await + } } From 8136bfa9a5daa3530edef34467d43ba350c78430 Mon Sep 17 00:00:00 2001 From: bitfl0wer Date: Tue, 23 May 2023 16:25:08 +0200 Subject: [PATCH 17/20] Fix: Used wrong rout to retrieve singular channel --- src/api/guilds/guilds.rs | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/api/guilds/guilds.rs b/src/api/guilds/guilds.rs index da7c503..caeea3b 100644 --- a/src/api/guilds/guilds.rs +++ b/src/api/guilds/guilds.rs @@ -202,12 +202,12 @@ impl types::Channel { pub async fn get( token: &str, url_api: &str, - guild_id: &str, + channel_id: &str, limits_user: &mut Limits, limits_instance: &mut Limits, ) -> Result { let request = Client::new() - .get(format!("{}/guilds/{}/channels/", url_api, guild_id)) + .get(format!("{}/channels/{}/", url_api, channel_id)) .bearer_auth(token); let mut requester = LimitedRequester::new().await; let result = match requester @@ -222,10 +222,11 @@ impl types::Channel { Ok(result) => result, Err(e) => return Err(e), }; - match from_str::(&result.text().await.unwrap()) { + let result_text = result.text().await.unwrap(); + match from_str::(&result_text) { Ok(object) => Ok(object), Err(e) => Err(InstanceServerError::RequestErrorError { - url: format!("{}/guilds/{}/channels/", url_api, guild_id), + url: format!("{}/channels/{}/", url_api, channel_id), error: e.to_string(), }), } From 3f35c9d74c2f05b546da91b98e7cad3d46672d67 Mon Sep 17 00:00:00 2001 From: bitfl0wer Date: Tue, 23 May 2023 16:25:31 +0200 Subject: [PATCH 18/20] Fix get_channel test --- tests/integration.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/integration.rs b/tests/integration.rs index c30c3ad..045dc08 100644 --- a/tests/integration.rs +++ b/tests/integration.rs @@ -57,12 +57,12 @@ async fn setup() -> TestBundle { permission_overwrites: None, parent_id: None, id: None, - nsfw: None, + nsfw: Some(false), rtc_region: None, default_auto_archive_duration: None, default_reaction_emoji: None, - flags: None, - default_thread_rate_limit_per_user: None, + flags: Some(0), + default_thread_rate_limit_per_user: Some(0), video_quality_mode: None, }; let mut user = instance.register_account(®).await.unwrap(); @@ -142,7 +142,7 @@ mod guild { Channel::get( bundle_user.token.as_str(), bundle.instance.urls.get_api(), - bundle.guild_id.as_str(), + &bundle_channel.id, &mut bundle_user.limits, &mut bundle.instance.limits ) From 44f9f2cae5f688e1d7ae1679a046a95f08696795 Mon Sep 17 00:00:00 2001 From: bitfl0wer Date: Tue, 23 May 2023 16:28:09 +0200 Subject: [PATCH 19/20] Create channels.rs --- src/api/channels/mod.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/api/channels/mod.rs b/src/api/channels/mod.rs index 8f05488..72f4e3e 100644 --- a/src/api/channels/mod.rs +++ b/src/api/channels/mod.rs @@ -1,3 +1,5 @@ +pub mod channels; pub mod messages; +pub use channels::*; pub use messages::*; From 7493597f4f8bd61afa7bb7fe2967b600cd6d6319 Mon Sep 17 00:00:00 2001 From: bitfl0wer Date: Tue, 23 May 2023 16:28:25 +0200 Subject: [PATCH 20/20] Move Channels::get() to channels.rs --- src/api/channels/channels.rs | 43 ++++++++++++++++++++++++++++++++++++ src/api/guilds/guilds.rs | 33 --------------------------- 2 files changed, 43 insertions(+), 33 deletions(-) create mode 100644 src/api/channels/channels.rs diff --git a/src/api/channels/channels.rs b/src/api/channels/channels.rs new file mode 100644 index 0000000..51552a1 --- /dev/null +++ b/src/api/channels/channels.rs @@ -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 { + 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::(&result_text) { + Ok(object) => Ok(object), + Err(e) => Err(InstanceServerError::RequestErrorError { + url: format!("{}/channels/{}/", url_api, channel_id), + error: e.to_string(), + }), + } + } +} diff --git a/src/api/guilds/guilds.rs b/src/api/guilds/guilds.rs index caeea3b..21140f1 100644 --- a/src/api/guilds/guilds.rs +++ b/src/api/guilds/guilds.rs @@ -198,37 +198,4 @@ impl types::Channel { }), } } - - pub async fn get( - token: &str, - url_api: &str, - channel_id: &str, - limits_user: &mut Limits, - limits_instance: &mut Limits, - ) -> Result { - 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::(&result_text) { - Ok(object) => Ok(object), - Err(e) => Err(InstanceServerError::RequestErrorError { - url: format!("{}/channels/{}/", url_api, channel_id), - error: e.to_string(), - }), - } - } }