Merge pull request #60 from polyphony-chat/main

Rebase feature/fgc
This commit is contained in:
kozabrada123 2023-05-20 08:37:34 +02:00 committed by GitHub
commit 2bc24ed949
3 changed files with 63 additions and 1 deletions

View File

@ -299,6 +299,21 @@ pub struct GuildCreateSchema {
pub rules_channel_id: Option<String>,
}
#[derive(Debug, Deserialize, Serialize)]
#[serde(rename_all = "snake_case")]
pub struct UserModifySchema {
pub username: Option<String>,
pub avatar: Option<String>,
pub bio: Option<String>,
pub accent_color: Option<u64>,
pub banner: Option<String>,
pub current_password: Option<String>,
pub new_password: Option<String>,
pub code: Option<String>,
pub email: Option<String>,
pub discriminator: Option<i16>,
}
// 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)]

View File

@ -1,13 +1,15 @@
use reqwest::Client;
use serde_json::{from_str, to_string};
use crate::{
api::{
limits::Limits,
types::{User, UserObject},
UserSettings,
UserModifySchema, UserSettings,
},
errors::InstanceServerError,
instance::Instance,
limit::LimitedRequester,
};
impl<'a> User<'a> {
@ -76,6 +78,50 @@ impl<'a> User<'a> {
Err(e) => Err(e),
}
}
/// Modify the current user's `UserObject`.
///
/// # Arguments
///
/// * `modify_schema` - A `UserModifySchema` object containing the fields to modify.
///
/// # Errors
///
/// Returns an `InstanceServerError` if the request fails or if a password is required but not provided.
pub async fn modify(
&mut self,
modify_schema: UserModifySchema,
) -> Result<UserObject, InstanceServerError> {
if modify_schema.new_password.is_some()
|| modify_schema.email.is_some()
|| modify_schema.code.is_some()
{
return Err(InstanceServerError::PasswordRequiredError);
}
let request = Client::new()
.patch(format!("{}/users/@me/", self.belongs_to.urls.get_api()))
.body(to_string(&modify_schema).unwrap())
.bearer_auth(self.token());
let result = match LimitedRequester::new()
.await
.send_request(
request,
crate::api::limits::LimitType::Global,
&mut self.belongs_to.limits,
&mut self.limits,
)
.await
{
Ok(response) => response,
Err(e) => return Err(e),
};
let user_updated: UserObject = from_str(&result.text().await.unwrap()).unwrap();
let _ = std::mem::replace(
&mut self.object.as_mut().unwrap(),
&mut user_updated.clone(),
);
Ok(user_updated)
}
}
impl Instance {

View File

@ -22,6 +22,7 @@ custom_error! {
TokenExpired = "Token expired, invalid or not found.",
NoPermission = "You do not have the permissions needed to perform this action.",
NotFound{error: String} = "The provided resource hasn't been found: {}",
PasswordRequiredError = "You need to provide your current password to authenticate for this action.",
}
custom_error! {