fix: update / fix PATCH /users/@me

This commit is contained in:
kozabrada123 2024-06-05 20:44:04 +02:00
parent 5154d04d4a
commit 15dc484952
2 changed files with 86 additions and 14 deletions

View File

@ -32,7 +32,7 @@ impl ChorusUser {
/// # Notes /// # Notes
/// This function is a wrapper around [`User::get_settings`]. /// This function is a wrapper around [`User::get_settings`].
pub async fn get_settings(&mut self) -> ChorusResult<UserSettings> { pub async fn get_settings(&mut self) -> ChorusResult<UserSettings> {
User::get_settings(self).await User::get_settings(self).await
} }
/// Modifies the current user's representation. (See [`User`]) /// Modifies the current user's representation. (See [`User`])
@ -40,12 +40,18 @@ impl ChorusUser {
/// # Reference /// # Reference
/// See <https://discord-userdoccers.vercel.app/resources/user#modify-current-user> /// See <https://discord-userdoccers.vercel.app/resources/user#modify-current-user>
pub async fn modify(&mut self, modify_schema: UserModifySchema) -> ChorusResult<User> { pub async fn modify(&mut self, modify_schema: UserModifySchema) -> ChorusResult<User> {
if modify_schema.new_password.is_some()
// See <https://docs.discord.sex/resources/user#json-params>, note 1
let requires_current_password = modify_schema.username.is_some()
|| modify_schema.discriminator.is_some()
|| modify_schema.email.is_some() || modify_schema.email.is_some()
|| modify_schema.code.is_some() || modify_schema.date_of_birth.is_some()
{ || modify_schema.new_password.is_some();
if requires_current_password && modify_schema.current_password.is_none() {
return Err(ChorusError::PasswordRequired); return Err(ChorusError::PasswordRequired);
} }
let request = Client::new() let request = Client::new()
.patch(format!( .patch(format!(
"{}/users/@me", "{}/users/@me",
@ -132,4 +138,3 @@ impl User {
} }
} }
} }

View File

@ -4,24 +4,91 @@
use std::collections::HashMap; use std::collections::HashMap;
use chrono::NaiveDate;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use crate::types::Snowflake; use crate::types::Snowflake;
#[derive(Debug, Deserialize, Serialize, Clone, PartialEq, Eq)] #[derive(Debug, Default, Deserialize, Serialize, Clone, PartialEq, Eq)]
#[serde(rename_all = "snake_case")] #[serde(rename_all = "snake_case")]
/// A schema used to modify a user. /// A schema used to modify a user.
///
/// See <https://docs.discord.sex/resources/user#json-params>
pub struct UserModifySchema { pub struct UserModifySchema {
/// The user's new username (2-32 characters)
///
/// Requires that `current_password` is set.
pub username: Option<String>, pub username: Option<String>,
// TODO: Maybe add a special discriminator type?
/// Requires that `current_password` is set.
pub discriminator: Option<String>,
/// The user's display name (1-32 characters)
///
/// # Note
///
/// This is not yet implemented on Spacebar
pub global_name: Option<String>,
// TODO: Add a CDN data type
pub avatar: Option<String>, pub avatar: Option<String>,
pub bio: Option<String>, /// Note: This is not yet implemented on Spacebar
pub accent_color: Option<u64>, pub avatar_decoration_id: Option<Snowflake>,
pub banner: Option<String>, /// Note: This is not yet implemented on Spacebar
pub current_password: Option<String>, pub avatar_decoration_sku_id: Option<Snowflake>,
pub new_password: Option<String>, /// The user's email address; if changing from a verified email, email_token must be provided
pub code: Option<String>, ///
/// Requires that `current_password` is set.
// TODO: Is ^ up to date? One would think this may not be the case, since email_token exists
pub email: Option<String>, pub email: Option<String>,
pub discriminator: Option<i16>, /// The user's email token from their previous email, required if a new email is set.
///
/// See <https://docs.discord.sex/resources/user#modify-user-email> and <https://docs.discord.sex/resources/user#verify-user-email-change>
/// for changing the user's email.
///
/// # Note
///
/// This is not yet implemented on Spacebar
pub email_token: Option<String>,
/// The user's pronouns (max 40 characters)
///
/// # Note
///
/// This is not yet implemented on Spacebar
pub pronouns: Option<String>,
/// The user's banner.
///
/// Can only be changed for premium users
pub banner: Option<String>,
/// The user's bio (max 190 characters)
pub bio: Option<String>,
/// The user's accent color, as a hex integer
pub accent_color: Option<u64>,
/// The user's [UserFlags].
///
/// Only [UserFlags::PREMIUM_PROMO_DISMISSED], [UserFlags::HAS_UNREAD_URGENT_MESSAGES]
/// and DISABLE_PREMIUM can be set.
///
/// # Note
///
/// This is not yet implemented on Spacebar
pub flags: Option<u64>,
/// The user's date of birth, can only be set once
///
/// Requires that `current_password` is set.
pub date_of_birth: Option<NaiveDate>,
/// The user's current password (if the account does not have a password, this sets it)
///
/// Required for updating `username`, `discriminator`, `email`, `date_of_birth` and
/// `new_password`
#[serde(rename = "password")]
pub current_password: Option<String>,
/// The user's new password (8-72 characters)
///
/// Requires that `current_password` is set.
///
/// Regenerates the user's token
pub new_password: Option<String>,
/// Spacebar only field, potentially same as `email_token`
pub code: Option<String>,
} }
/// A schema used to create a private channel. /// A schema used to create a private channel.
@ -33,7 +100,7 @@ pub struct UserModifySchema {
/// ///
/// # Reference: /// # Reference:
/// Read: <https://discord-userdoccers.vercel.app/resources/channel#json-params> /// Read: <https://discord-userdoccers.vercel.app/resources/channel#json-params>
#[derive(Debug, Deserialize, Serialize, Clone, PartialEq, Eq)] #[derive(Debug, Default, Deserialize, Serialize, Clone, PartialEq, Eq)]
pub struct PrivateChannelCreateSchema { pub struct PrivateChannelCreateSchema {
pub recipients: Option<Vec<Snowflake>>, pub recipients: Option<Vec<Snowflake>>,
pub access_tokens: Option<Vec<String>>, pub access_tokens: Option<Vec<String>>,