feat: add get_pomelo_suggestions and get_pomelo_eligibility

This commit is contained in:
kozabrada123 2024-08-08 09:57:23 +02:00
parent 80c99753c4
commit 926f89e1cf
2 changed files with 83 additions and 4 deletions

View File

@ -12,7 +12,7 @@ use crate::{
instance::{ChorusUser, Instance}, instance::{ChorusUser, Instance},
ratelimiter::ChorusRequest, ratelimiter::ChorusRequest,
types::{ 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 /// # Reference
/// See <https://docs.discord.sex/resources/user#get-user-profile> /// See <https://docs.discord.sex/resources/user#get-user-profile>
pub async fn get_user_profile(&mut self, id: Snowflake, query_parameters: GetUserProfileSchema) -> ChorusResult<UserProfile> { pub async fn get_user_profile(
&mut self,
id: Snowflake,
query_parameters: GetUserProfileSchema,
) -> ChorusResult<UserProfile> {
User::get_profile(self, id, query_parameters).await User::get_profile(self, id, query_parameters).await
} }
@ -242,6 +246,62 @@ impl ChorusUser {
.deserialize_response::<VerifyUserEmailChangeResponse>(self) .deserialize_response::<VerifyUserEmailChangeResponse>(self)
.await .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 <https://docs.discord.sex/resources/user#get-pomelo-suggestions>
pub async fn get_pomelo_suggestions(&mut self) -> ChorusResult<String> {
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::<GetPomeloSuggestionsReturn>(self)
.await
.map(|returned| returned.username)
}
/// Checks whether a unique username is available.
///
/// Returns whether the username is not taken yet.
///
/// See <https://docs.discord.sex/resources/user#get-pomelo-eligibility>
pub async fn get_pomelo_eligibility(&mut self, username: &String) -> ChorusResult<bool> {
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::<GetPomeloEligibilityReturn>(self)
.await
.map(|returned| !returned.taken)
}
} }
impl User { impl User {
@ -353,7 +413,11 @@ impl User {
/// ///
/// # Reference /// # Reference
/// See <https://docs.discord.sex/resources/user#get-user-profile> /// See <https://docs.discord.sex/resources/user#get-user-profile>
pub async fn get_profile(user: &mut ChorusUser, id: Snowflake, query_parameters: GetUserProfileSchema) -> ChorusResult<UserProfile> { pub async fn get_profile(
user: &mut ChorusUser,
id: Snowflake,
query_parameters: GetUserProfileSchema,
) -> ChorusResult<UserProfile> {
let url_api = user.belongs_to.read().unwrap().urls.api.clone(); let url_api = user.belongs_to.read().unwrap().urls.api.clone();
let request: reqwest::RequestBuilder = Client::new() let request: reqwest::RequestBuilder = Client::new()
.get(format!("{}/users/{}/profile", url_api, id)) .get(format!("{}/users/{}/profile", url_api, id))

View File

@ -213,3 +213,18 @@ pub struct GetUserProfileSchema {
pub connections_role_id: Option<Snowflake>, pub connections_role_id: Option<Snowflake>,
} }
#[derive(Debug, Deserialize, Serialize, Clone, PartialEq, Eq)]
/// Internal type for the [crate::instance::ChorusUser::get_pomelo_suggestions] endpoint.
///
/// See <https://docs.discord.sex/resources/user#get-pomelo-suggestions>
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 <https://docs.discord.sex/resources/user#get-pomelo-eligibility>
pub(crate) struct GetPomeloEligibilityReturn {
pub taken: bool
}