From 926f89e1cf0ed2c104c52a0ec7ddc43f0c4e7afe Mon Sep 17 00:00:00 2001 From: kozabrada123 Date: Thu, 8 Aug 2024 09:57:23 +0200 Subject: [PATCH] feat: add get_pomelo_suggestions and get_pomelo_eligibility --- src/api/users/users.rs | 72 +++++++++++++++++++++++++++++++++++++--- src/types/schema/user.rs | 15 +++++++++ 2 files changed, 83 insertions(+), 4 deletions(-) diff --git a/src/api/users/users.rs b/src/api/users/users.rs index b49f4a6..1c07b8c 100644 --- a/src/api/users/users.rs +++ b/src/api/users/users.rs @@ -12,7 +12,7 @@ use crate::{ instance::{ChorusUser, Instance}, ratelimiter::ChorusRequest, types::{ - DeleteDisableUserSchema, 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 }, }; @@ -172,7 +172,11 @@ impl ChorusUser { /// /// # Reference /// See - pub async fn get_user_profile(&mut self, id: Snowflake, query_parameters: GetUserProfileSchema) -> ChorusResult { + pub async fn get_user_profile( + &mut self, + id: Snowflake, + query_parameters: GetUserProfileSchema, + ) -> ChorusResult { User::get_profile(self, id, query_parameters).await } @@ -242,6 +246,62 @@ impl ChorusUser { .deserialize_response::(self) .await } + + /// Returns a suggested unique username based on the current user's username. + /// + /// 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]) + /// + /// See + pub async fn get_pomelo_suggestions(&mut self) -> ChorusResult { + let request = Client::new() + .get(format!( + "{}/users/@me/pomelo-suggestions", + self.belongs_to.read().unwrap().urls.api + )) + .header("Authorization", self.token()); + + let chorus_request = ChorusRequest { + request, + limit_type: LimitType::default(), + }; + chorus_request + .deserialize_response::(self) + .await + .map(|returned| returned.username) + } + + /// Checks whether a unique username is available. + /// + /// Returns whether the username is not taken yet. + /// + /// See + pub async fn get_pomelo_eligibility(&mut self, username: &String) -> ChorusResult { + let request = Client::new() + .post(format!( + "{}/users/@me/pomelo-attempt", + 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(), + }; + chorus_request + .deserialize_response::(self) + .await + .map(|returned| !returned.taken) + } } impl User { @@ -353,12 +413,16 @@ impl User { /// /// # Reference /// See - pub async fn get_profile(user: &mut ChorusUser, id: Snowflake, query_parameters: GetUserProfileSchema) -> ChorusResult { + pub async fn get_profile( + user: &mut ChorusUser, + id: Snowflake, + query_parameters: GetUserProfileSchema, + ) -> ChorusResult { let url_api = user.belongs_to.read().unwrap().urls.api.clone(); let request: reqwest::RequestBuilder = Client::new() .get(format!("{}/users/{}/profile", url_api, id)) .header("Authorization", user.token()) - .query(&query_parameters); + .query(&query_parameters); let chorus_request = ChorusRequest { request, diff --git a/src/types/schema/user.rs b/src/types/schema/user.rs index a53298a..b336a63 100644 --- a/src/types/schema/user.rs +++ b/src/types/schema/user.rs @@ -213,3 +213,18 @@ pub struct GetUserProfileSchema { pub connections_role_id: Option, } +#[derive(Debug, Deserialize, Serialize, Clone, PartialEq, Eq)] +/// Internal type for the [crate::instance::ChorusUser::get_pomelo_suggestions] endpoint. +/// +/// See +pub(crate) struct GetPomeloSuggestionsReturn { + pub username: String +} + +#[derive(Debug, Deserialize, Serialize, Clone, PartialEq, Eq)] +/// Internal type for the [crate::instance::ChorusUser::get_pomelo_eligibility] endpoint. +/// +/// See +pub(crate) struct GetPomeloEligibilityReturn { + pub taken: bool +}