From 572ff07b93ba90daef6249dd48bf4d0ea3e939af Mon Sep 17 00:00:00 2001 From: bitfl0wer Date: Mon, 29 May 2023 18:27:15 +0200 Subject: [PATCH 1/6] Add ChannelModifySchema --- src/types/schema/channel.rs | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/src/types/schema/channel.rs b/src/types/schema/channel.rs index 458c33c..6cc16ab 100644 --- a/src/types/schema/channel.rs +++ b/src/types/schema/channel.rs @@ -25,3 +25,26 @@ pub struct ChannelCreateSchema { pub default_thread_rate_limit_per_user: Option, pub video_quality_mode: Option, } + +#[derive(Debug, Deserialize, Serialize)] +#[serde(rename_all = "snake_case")] +pub struct ChannelModifySchema { + name: Option, + channel_type: Option, + topic: Option, + icon: Option, + bitrate: Option, + user_limit: Option, + rate_limit_per_user: Option, + position: Option, + permission_overwrites: Option>, + parent_id: Option, + id: Option, + nsfw: Option, + rtc_region: Option, + default_auto_archive_duration: Option, + default_reaction_emoji: Option, + flags: Option, + default_thread_rate_limit_per_user: Option, + video_quality_mode: Option, +} From 6cb400b3d0f7d0298993a2a40c1250ad4acf11c0 Mon Sep 17 00:00:00 2001 From: bitfl0wer Date: Mon, 29 May 2023 18:27:30 +0200 Subject: [PATCH 2/6] Add modify() --- src/api/channels/channels.rs | 49 ++++++++++++++++++++++++++++++++++-- 1 file changed, 47 insertions(+), 2 deletions(-) diff --git a/src/api/channels/channels.rs b/src/api/channels/channels.rs index 4fd217d..bfe9959 100644 --- a/src/api/channels/channels.rs +++ b/src/api/channels/channels.rs @@ -1,8 +1,11 @@ use reqwest::Client; -use serde_json::from_str; +use serde_json::{from_str, to_string}; use crate::{ - api::limits::Limits, errors::InstanceServerError, limit::LimitedRequester, types::Channel, + api::limits::Limits, + errors::InstanceServerError, + limit::LimitedRequester, + types::{Channel, ChannelModifySchema}, }; impl Channel { @@ -76,4 +79,46 @@ impl Channel { Err(e) => return Some(e), } } + + /// Modifies a channel. + /// + /// # Arguments + /// + /// * `modify_data` - A `ChannelModifySchema` object that represents the modifications to be made to the channel. + /// * `token` - A string slice that holds the authorization token. + /// * `url_api` - A string slice that holds the URL of the API. + /// * `channel_id` - A string slice that holds the ID of the channel to be modified. + /// * `limits_user` - A mutable reference to a `Limits` object that represents the user's rate limits. + /// * `limits_instance` - A mutable reference to a `Limits` object that represents the instance's rate limits. + /// + /// # Returns + /// + /// A `Result` that contains a `Channel` object if the request was successful, or an `InstanceServerError` if an error occurred during the request. + pub async fn modify( + modify_data: ChannelModifySchema, + token: &str, + url_api: &str, + channel_id: &str, + limits_user: &mut Limits, + limits_instance: &mut Limits, + ) -> Result { + let request = Client::new() + .patch(format!("{}/channels/{}/", url_api, channel_id)) + .bearer_auth(token) + .body(to_string(&modify_data).unwrap()); + let channel = match LimitedRequester::new() + .await + .send_request( + request, + crate::api::limits::LimitType::Channel, + limits_instance, + limits_user, + ) + .await + { + Ok(channel) => from_str::(&channel.text().await.unwrap()).unwrap(), + Err(e) => return Err(e), + }; + Ok(channel) + } } From b4cac5a65c360adcc6982e051ccf775e42804cfa Mon Sep 17 00:00:00 2001 From: bitfl0wer Date: Mon, 29 May 2023 18:29:08 +0200 Subject: [PATCH 3/6] Teardown after delete_channel --- tests/channel.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/channel.rs b/tests/channel.rs index a162d41..03e570f 100644 --- a/tests/channel.rs +++ b/tests/channel.rs @@ -27,6 +27,7 @@ async fn delete_channel() { let mut bundle = common::setup().await; let result = bundle .channel + .clone() .delete( &bundle.user.token, bundle.instance.urls.get_api(), @@ -35,4 +36,5 @@ async fn delete_channel() { ) .await; assert!(result.is_none()); + common::teardown(bundle).await } From f8655c22bc3df749194f54ad5146a31c8959c472 Mon Sep 17 00:00:00 2001 From: bitfl0wer Date: Mon, 29 May 2023 18:34:08 +0200 Subject: [PATCH 4/6] Make all attributes pub --- src/types/schema/channel.rs | 35 +++++++++++++++++------------------ 1 file changed, 17 insertions(+), 18 deletions(-) diff --git a/src/types/schema/channel.rs b/src/types/schema/channel.rs index 6cc16ab..67d9823 100644 --- a/src/types/schema/channel.rs +++ b/src/types/schema/channel.rs @@ -29,22 +29,21 @@ pub struct ChannelCreateSchema { #[derive(Debug, Deserialize, Serialize)] #[serde(rename_all = "snake_case")] pub struct ChannelModifySchema { - name: Option, - channel_type: Option, - topic: Option, - icon: Option, - bitrate: Option, - user_limit: Option, - rate_limit_per_user: Option, - position: Option, - permission_overwrites: Option>, - parent_id: Option, - id: Option, - nsfw: Option, - rtc_region: Option, - default_auto_archive_duration: Option, - default_reaction_emoji: Option, - flags: Option, - default_thread_rate_limit_per_user: Option, - video_quality_mode: Option, + pub name: Option, + 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 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, } From b9c3f0279997c5e30a5f5edaefd4e9e1cb433169 Mon Sep 17 00:00:00 2001 From: bitfl0wer Date: Mon, 29 May 2023 18:50:09 +0200 Subject: [PATCH 5/6] ADd test for modify_channel() --- tests/channel.rs | 38 +++++++++++++++++++++++++++++++++++++- 1 file changed, 37 insertions(+), 1 deletion(-) diff --git a/tests/channel.rs b/tests/channel.rs index 03e570f..56f1ce5 100644 --- a/tests/channel.rs +++ b/tests/channel.rs @@ -1,5 +1,5 @@ mod common; -use chorus::types::Channel; +use chorus::types::{self, Channel}; #[tokio::test] async fn get_channel() { @@ -38,3 +38,39 @@ async fn delete_channel() { assert!(result.is_none()); common::teardown(bundle).await } + +#[tokio::test] +async fn modify_channel() { + let mut bundle = common::setup().await; + let modify_data: types::ChannelModifySchema = types::ChannelModifySchema { + name: Some("beepboop".to_string()), + channel_type: None, + topic: None, + icon: None, + bitrate: None, + user_limit: None, + rate_limit_per_user: None, + position: None, + permission_overwrites: None, + parent_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 result = Channel::modify( + modify_data, + &bundle.user.token, + bundle.instance.urls.get_api(), + &bundle.channel.id.to_string(), + &mut bundle.user.limits, + &mut bundle.instance.limits, + ) + .await + .unwrap(); + assert_eq!(result.name, Some("beepboop".to_string())); + common::teardown(bundle).await +} From 84b3b72928d4753fe98acc0364583e2c17813862 Mon Sep 17 00:00:00 2001 From: bitfl0wer Date: Mon, 29 May 2023 18:50:19 +0200 Subject: [PATCH 6/6] Add clone, default derives --- src/types/schema/channel.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/types/schema/channel.rs b/src/types/schema/channel.rs index 67d9823..fb626bb 100644 --- a/src/types/schema/channel.rs +++ b/src/types/schema/channel.rs @@ -26,7 +26,7 @@ pub struct ChannelCreateSchema { pub video_quality_mode: Option, } -#[derive(Debug, Deserialize, Serialize)] +#[derive(Debug, Deserialize, Serialize, Clone, Default)] #[serde(rename_all = "snake_case")] pub struct ChannelModifySchema { pub name: Option,