From 7683ce49a39e65f78c438520bd571fc10806b332 Mon Sep 17 00:00:00 2001 From: kozabrada123 Date: Thu, 8 Aug 2024 19:14:39 +0200 Subject: [PATCH] feat: add create_pomelo_migration --- src/api/users/users.rs | 78 ++++++++++++++++++++++++++++++++++++------ 1 file changed, 67 insertions(+), 11 deletions(-) diff --git a/src/api/users/users.rs b/src/api/users/users.rs index 1c07b8c..6d47303 100644 --- a/src/api/users/users.rs +++ b/src/api/users/users.rs @@ -12,7 +12,10 @@ use crate::{ instance::{ChorusUser, Instance}, ratelimiter::ChorusRequest, types::{ - DeleteDisableUserSchema, GetPomeloEligibilityReturn, GetPomeloSuggestionsReturn, GetUserProfileSchema, LimitType, PublicUser, Snowflake, User, UserModifyProfileSchema, UserModifySchema, UserProfile, UserProfileMetadata, UserSettings, VerifyUserEmailChangeResponse, VerifyUserEmailChangeSchema + DeleteDisableUserSchema, GetPomeloEligibilityReturn, GetPomeloSuggestionsReturn, + GetUserProfileSchema, LimitType, PublicUser, Snowflake, User, UserModifyProfileSchema, + UserModifySchema, UserProfile, UserProfileMetadata, UserSettings, + VerifyUserEmailChangeResponse, VerifyUserEmailChangeSchema, }, }; @@ -225,6 +228,9 @@ impl ChorusUser { /// This endpoint returns a token which can be used with [Self::modify] /// to set a new email address (email_token). /// + /// As of 2024/08/08, Spacebar does not yet implement this endpoint. + // FIXME: Does this mean PUT users/@me/email is different? + /// /// # Reference /// See pub async fn verify_email_change( @@ -254,9 +260,11 @@ impl ChorusUser { /// "This endpoint is used during the pomelo migration flow. /// /// The user must be in the rollout to use this endpoint." - /// - /// If a user has already migrated, this endpoint will likely return a 401 Unauthorized - /// ([ChorusError::NoPermission]) + /// + /// If a user has already migrated, this endpoint will likely return a 401 Unauthorized + /// ([ChorusError::NoPermission]) + /// + /// As of 2024/08/08, Spacebar does not yet implement this endpoint. /// /// See pub async fn get_pomelo_suggestions(&mut self) -> ChorusResult { @@ -274,12 +282,14 @@ impl ChorusUser { chorus_request .deserialize_response::(self) .await - .map(|returned| returned.username) + .map(|returned| returned.username) } - /// Checks whether a unique username is available. - /// - /// Returns whether the username is not taken yet. + /// Checks whether a unique username is available. + /// + /// Returns whether the username is not taken yet. + /// + /// As of 2024/08/08, Spacebar does not yet implement this endpoint. /// /// See pub async fn get_pomelo_eligibility(&mut self, username: &String) -> ChorusResult { @@ -289,8 +299,8 @@ impl ChorusUser { self.belongs_to.read().unwrap().urls.api )) .header("Authorization", self.token()) - // FIXME: should we create a type for this? - .body(format!(r#"{{ "username": {:?} }}"#, username)) + // FIXME: should we create a type for this? + .body(format!(r#"{{ "username": {:?} }}"#, username)) .header("Content-Type", "application/json"); let chorus_request = ChorusRequest { @@ -300,7 +310,53 @@ impl ChorusUser { chorus_request .deserialize_response::(self) .await - .map(|returned| !returned.taken) + .map(|returned| !returned.taken) + } + + /// Migrates the user from the username#discriminator to the unique username system. + /// + /// Fires a [UserUpdate](crate::types::UserUpdate) gateway event. + /// + /// Updates [Self::object] to an updated representation returned by the server. + // FIXME: Is this appropriate behaviour? + /// + /// Note: + /// + /// "This endpoint is used during the pomelo migration flow. + /// + /// The user must be in the rollout to use this endpoint." + /// + /// If a user has already migrated, this endpoint will likely return a 401 Unauthorized + /// ([ChorusError::NoPermission]) + // + /// As of 2024/08/08, Spacebar does not yet implement this endpoint. + /// + /// See + pub async fn create_pomelo_migration(&mut self, username: &String) -> ChorusResult<()> { + let request = Client::new() + .post(format!( + "{}/users/@me/pomelo", + self.belongs_to.read().unwrap().urls.api + )) + .header("Authorization", self.token()) + // FIXME: should we create a type for this? + .body(format!(r#"{{ "username": {:?} }}"#, username)) + .header("Content-Type", "application/json"); + + let chorus_request = ChorusRequest { + request, + limit_type: LimitType::default(), + }; + + let result = chorus_request.deserialize_response::(self).await; + + // FIXME: Does UserUpdate do this automatically? or would a user need to manually observe ChorusUser::object + if let Ok(new_object) = result { + *self.object.write().unwrap() = new_object; + return ChorusResult::Ok(()); + } + + ChorusResult::Err(result.err().unwrap()) } }