Merge pull request #82 from polyphony-chat/feature/channel/modify

Feature: Modify channel
This commit is contained in:
Flori 2023-05-29 19:04:33 +02:00 committed by GitHub
commit f166141f62
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 108 additions and 3 deletions

View File

@ -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<Channel, InstanceServerError> {
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>(&channel.text().await.unwrap()).unwrap(),
Err(e) => return Err(e),
};
Ok(channel)
}
}

View File

@ -25,3 +25,25 @@ pub struct ChannelCreateSchema {
pub default_thread_rate_limit_per_user: Option<i32>,
pub video_quality_mode: Option<i32>,
}
#[derive(Debug, Deserialize, Serialize, Clone, Default)]
#[serde(rename_all = "snake_case")]
pub struct ChannelModifySchema {
pub name: Option<String>,
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<PermissionOverwrite>>,
pub parent_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>,
}

View File

@ -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
}