Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Client properties API #590

Merged
merged 8 commits into from
Dec 30, 2024
Merged
25 changes: 12 additions & 13 deletions src/api/auth/login.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ use crate::gateway::Gateway;
use crate::instance::{ChorusUser, Instance};
use crate::ratelimiter::ChorusRequest;
use crate::types::{
MfaAuthenticationType, GatewayIdentifyPayload, LimitType, LoginResult, LoginSchema,
SendMfaSmsResponse, SendMfaSmsSchema, User, VerifyMFALoginResponse, VerifyMFALoginSchema,
ClientProperties, GatewayIdentifyPayload, LimitType, LoginResult, LoginSchema,
MfaAuthenticationType, SendMfaSmsResponse, SendMfaSmsSchema, User, VerifyMFALoginResponse,
VerifyMFALoginSchema,
};

impl Instance {
Expand All @@ -24,12 +25,11 @@ impl Instance {
pub async fn login_account(&mut self, login_schema: LoginSchema) -> ChorusResult<ChorusUser> {
let endpoint_url = self.urls.api.clone() + "/auth/login";
let chorus_request = ChorusRequest {
request: Client::new()
.post(endpoint_url)
.body(to_string(&login_schema).unwrap())
.header("Content-Type", "application/json"),
request: Client::new().post(endpoint_url).json(&login_schema),
limit_type: LimitType::AuthLogin,
};
}
// Note: yes, this is still sent even for login and register
.with_client_properties(&ClientProperties::default());

// We do not have a user yet, and the UserRateLimits will not be affected by a login
// request (since login is an instance wide limit), which is why we are just cloning the
Expand Down Expand Up @@ -58,12 +58,11 @@ impl Instance {
let endpoint_url = self.urls.api.clone() + "/auth/mfa/" + &authenticator.to_string();

let chorus_request = ChorusRequest {
request: Client::new()
.post(endpoint_url)
.header("Content-Type", "application/json")
.json(&schema),
request: Client::new().post(endpoint_url).json(&schema),
limit_type: LimitType::AuthLogin,
};
}
// Note: yes, this is still sent even for login and register
.with_client_properties(&ClientProperties::default());

let mut user = ChorusUser::shell(Arc::new(RwLock::new(self.clone())), "None").await;

Expand Down Expand Up @@ -94,7 +93,7 @@ impl Instance {
///
/// # Reference
/// See <https://docs.discord.sex/authentication#send-mfa-sms>
// FIXME: This uses ChorusUser::shell, when it *really* shoudln't, but
// FIXME: This uses ChorusUser::shell, when it *really* shouldn't, but
// there is no other way to send a ratelimited request
pub async fn send_mfa_sms(
&mut self,
Expand Down
2 changes: 1 addition & 1 deletion src/api/auth/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ impl Instance {
pub async fn login_with_token(&mut self, token: &str) -> ChorusResult<ChorusUser> {
let mut user = ChorusUser::shell(Arc::new(RwLock::new(self.clone())), token).await;

user.update_with_login_data(token.to_string(), None).await?;
user.update_with_login_data(token.to_string(), None).await?;

Ok(user)
}
Expand Down
12 changes: 6 additions & 6 deletions src/api/auth/register.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use reqwest::Client;
use serde_json::to_string;

use crate::gateway::{Gateway, GatewayHandle};
use crate::types::{GatewayIdentifyPayload, User};
use crate::types::{ClientProperties, GatewayIdentifyPayload, User};
use crate::{
errors::ChorusResult,
instance::{ChorusUser, Instance, Token},
Expand All @@ -28,12 +28,12 @@ impl Instance {
) -> ChorusResult<ChorusUser> {
let endpoint_url = self.urls.api.clone() + "/auth/register";
let chorus_request = ChorusRequest {
request: Client::new()
.post(endpoint_url)
.body(to_string(&register_schema).unwrap())
.header("Content-Type", "application/json"),
request: Client::new().post(endpoint_url).json(&register_schema),
limit_type: LimitType::AuthRegister,
};
}
// Note: yes, this is still sent even for login and register
.with_client_properties(&ClientProperties::default());

// We do not have a user yet, and the UserRateLimits will not be affected by a login
// request (since register is an instance wide limit), which is why we are just cloning
// the instances' limits to pass them on as user_rate_limits later.
Expand Down
103 changes: 44 additions & 59 deletions src/api/channels/channels.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,15 @@ impl Channel {
/// # Reference
/// See <https://discord-userdoccers.vercel.app/resources/channel#get-channel>
pub async fn get(user: &mut ChorusUser, channel_id: Snowflake) -> ChorusResult<Channel> {
let chorus_request = ChorusRequest::new(
http::Method::GET,
&format!(
let chorus_request = ChorusRequest {
request: Client::new().get(format!(
"{}/channels/{}",
user.belongs_to.read().unwrap().urls.api.clone(),
channel_id
),
None,
None,
Some(user),
LimitType::Channel(channel_id),
);
)),
limit_type: LimitType::Channel(channel_id),
}
.with_headers_for(user);

chorus_request.deserialize_response::<Channel>(user).await
}
Expand All @@ -55,14 +52,12 @@ impl Channel {
self.id,
);

let request = ChorusRequest::new(
http::Method::DELETE,
&url,
None,
audit_log_reason.as_deref(),
Some(user),
LimitType::Channel(self.id),
);
let request = ChorusRequest {
request: Client::new().delete(url),
limit_type: LimitType::Channel(self.id),
}
.with_maybe_audit_log_reason(audit_log_reason)
.with_headers_for(user);

request.handle_request_as_result(user).await
}
Expand Down Expand Up @@ -94,14 +89,12 @@ impl Channel {
channel_id
);

let request = ChorusRequest::new(
http::Method::PATCH,
&url,
Some(to_string(&modify_data).unwrap()),
audit_log_reason.as_deref(),
Some(user),
LimitType::Channel(channel_id),
);
let request = ChorusRequest {
request: Client::new().patch(url).json(&modify_data),
limit_type: LimitType::Channel(channel_id),
}
.with_maybe_audit_log_reason(audit_log_reason)
.with_headers_for(user);

request.deserialize_response::<Channel>(user).await
}
Expand All @@ -126,14 +119,12 @@ impl Channel {
channel_id
);

let mut chorus_request = ChorusRequest::new(
http::Method::GET,
&url,
None,
None,
Some(user),
Default::default(),
);
let mut chorus_request = ChorusRequest {
request: Client::new().get(url),
limit_type: Default::default(),
}
.with_headers_for(user);

chorus_request.request = chorus_request.request.query(&range);

chorus_request
Expand All @@ -151,22 +142,22 @@ impl Channel {
user: &mut ChorusUser,
add_channel_recipient_schema: Option<AddChannelRecipientSchema>,
) -> ChorusResult<()> {
let mut request = Client::new()
.put(format!(
"{}/channels/{}/recipients/{}",
user.belongs_to.read().unwrap().urls.api,
self.id,
recipient_id
))
.header("Authorization", user.token())
.header("Content-Type", "application/json");
let mut request = Client::new().put(format!(
"{}/channels/{}/recipients/{}",
user.belongs_to.read().unwrap().urls.api,
self.id,
recipient_id
));

if let Some(schema) = add_channel_recipient_schema {
request = request.body(to_string(&schema).unwrap());
request = request.json(&schema);
}

ChorusRequest {
request,
limit_type: LimitType::Channel(self.id),
}
.with_headers_for(user)
.handle_request_as_result(user)
.await
}
Expand All @@ -187,14 +178,11 @@ impl Channel {
recipient_id
);

let request = ChorusRequest::new(
http::Method::DELETE,
&url,
None,
None,
Some(user),
LimitType::Channel(self.id),
);
let request = ChorusRequest {
request: Client::new().delete(url),
limit_type: LimitType::Channel(self.id),
}
.with_headers_for(user);

request.handle_request_as_result(user).await
}
Expand All @@ -215,14 +203,11 @@ impl Channel {
guild_id
);

let request = ChorusRequest::new(
http::Method::PATCH,
&url,
Some(to_string(&schema).unwrap()),
None,
Some(user),
LimitType::Guild(guild_id),
);
let request = ChorusRequest {
request: Client::new().patch(url).json(&schema),
limit_type: LimitType::Guild(guild_id),
}
.with_headers_for(user);

request.handle_request_as_result(user).await
}
Expand Down
Loading
Loading