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) + } } diff --git a/src/types/schema/channel.rs b/src/types/schema/channel.rs index 458c33c..fb626bb 100644 --- a/src/types/schema/channel.rs +++ b/src/types/schema/channel.rs @@ -25,3 +25,25 @@ pub struct ChannelCreateSchema { pub default_thread_rate_limit_per_user: Option, pub video_quality_mode: Option, } + +#[derive(Debug, Deserialize, Serialize, Clone, Default)] +#[serde(rename_all = "snake_case")] +pub struct ChannelModifySchema { + 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, +} diff --git a/tests/channel.rs b/tests/channel.rs index a162d41..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() { @@ -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,41 @@ async fn delete_channel() { ) .await; 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 }