From 63ece12636d218d60c1887a057427d3aa1c97838 Mon Sep 17 00:00:00 2001 From: bitfl0wer Date: Sat, 6 May 2023 18:34:38 +0200 Subject: [PATCH 01/12] change message to messagesendschema --- src/api/channels/messages.rs | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/api/channels/messages.rs b/src/api/channels/messages.rs index 357e245..d1c22de 100644 --- a/src/api/channels/messages.rs +++ b/src/api/channels/messages.rs @@ -18,7 +18,7 @@ pub mod messages { pub async fn send<'a>( url_api: &String, - message: &mut Message, + message: &mut crate::api::schemas::MessageSendSchema, files: Option>, user: &mut User<'a>, limits_instance: &mut Limits, @@ -30,6 +30,11 @@ pub mod messages { } impl<'a> User<'a> { - pub async fn send_message() {} + pub async fn send_message( + &mut self, + message: crate::api::schemas::MessageSendSchema, + files: Option>, + ) { + } } } From 0efdc8bfc275152fae50702b62a247631822d7e2 Mon Sep 17 00:00:00 2001 From: bitfl0wer Date: Sat, 6 May 2023 18:52:09 +0200 Subject: [PATCH 02/12] Construct new message send method basics --- src/api/channels/messages.rs | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/src/api/channels/messages.rs b/src/api/channels/messages.rs index d1c22de..80481f5 100644 --- a/src/api/channels/messages.rs +++ b/src/api/channels/messages.rs @@ -20,21 +20,31 @@ pub mod messages { url_api: &String, message: &mut crate::api::schemas::MessageSendSchema, files: Option>, + token: &String, user: &mut User<'a>, - limits_instance: &mut Limits, requester: &mut LimitedRequester, ) { - let token = user.token(); - let mut limits = &mut user.rate_limits; + let user_limits = &mut user.limits; + let instance_limits = &mut user.belongs_to.limits; } } impl<'a> User<'a> { pub async fn send_message( &mut self, - message: crate::api::schemas::MessageSendSchema, + mut message: &mut crate::api::schemas::MessageSendSchema, files: Option>, ) { + let token = self.token().clone(); + Message::send( + &self.belongs_to.urls.get_api().to_string(), + &mut message, + files, + &token, + self, + &mut LimitedRequester::new().await, + ) + .await; } } } From f943f57f035041bbcc78d4970e0d20379c741cbb Mon Sep 17 00:00:00 2001 From: bitfl0wer Date: Sat, 6 May 2023 18:52:24 +0200 Subject: [PATCH 03/12] rename user rate_limits to limits for consistency --- src/api/types.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/api/types.rs b/src/api/types.rs index 0cb7ba0..8be72da 100644 --- a/src/api/types.rs +++ b/src/api/types.rs @@ -158,7 +158,7 @@ pub struct User<'a> { pub logged_in: bool, pub belongs_to: &'a mut Instance<'a>, token: String, - pub rate_limits: Limits, + pub limits: Limits, pub settings: UserSettings, pub object: UserObject, } @@ -188,7 +188,7 @@ impl<'a> User<'a> { logged_in: bool, belongs_to: &'a mut Instance<'a>, token: String, - rate_limits: Limits, + limits: Limits, settings: UserSettings, object: UserObject, ) -> User<'a> { @@ -196,7 +196,7 @@ impl<'a> User<'a> { logged_in, belongs_to, token, - rate_limits, + limits, settings, object, } From 4aaaee3e1e7f2431fb3de788c170898b450e26be Mon Sep 17 00:00:00 2001 From: bitfl0wer Date: Sat, 6 May 2023 22:23:34 +0200 Subject: [PATCH 04/12] impl SendMessageSchema --- src/api/schemas.rs | 35 +++++++++++++++++++++++++++++++++-- 1 file changed, 33 insertions(+), 2 deletions(-) diff --git a/src/api/schemas.rs b/src/api/schemas.rs index 7672e55..215d7a6 100644 --- a/src/api/schemas.rs +++ b/src/api/schemas.rs @@ -244,11 +244,11 @@ pub struct TotpSchema { login_source: Option, } -#[derive(Debug, Deserialize)] +#[derive(Debug, Deserialize, Serialize)] #[serde(rename_all = "snake_case")] pub struct MessageSendSchema { #[serde(rename = "type")] - message_type: i32, + message_type: Option, content: Option, nonce: Option, tts: Option, @@ -262,6 +262,37 @@ pub struct MessageSendSchema { attachments: Option>, } +// make a new() method for MessageSendSchema +impl MessageSendSchema { + pub fn new( + message_type: Option, + content: Option, + nonce: Option, + tts: Option, + embeds: Option>, + allowed_mentions: Option, + message_reference: Option, + components: Option>, + sticker_ids: Option>, + files: Option>>, + attachments: Option>, + ) -> MessageSendSchema { + MessageSendSchema { + message_type, + content, + nonce, + tts, + embeds, + allowed_mentions, + message_reference, + components, + sticker_ids, + files, + attachments, + } + } +} + // I know that some of these tests are... really really basic and unneccessary, but sometimes, I // just feel like writing tests, so there you go :) -@bitfl0wer #[cfg(test)] From 9855465fe4649508f2a8e9bdfbcf922290e0550b Mon Sep 17 00:00:00 2001 From: bitfl0wer Date: Sat, 6 May 2023 22:23:48 +0200 Subject: [PATCH 05/12] change visibilities --- src/api/types.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/api/types.rs b/src/api/types.rs index 8be72da..5dafe1e 100644 --- a/src/api/types.rs +++ b/src/api/types.rs @@ -14,8 +14,8 @@ pub trait WebSocketEvent {} #[derive(Debug, Serialize, Deserialize)] pub struct LoginResult { - token: String, - settings: UserSettings, + pub token: String, + pub settings: UserSettings, } #[derive(Debug, Serialize, Deserialize)] @@ -160,7 +160,7 @@ pub struct User<'a> { token: String, pub limits: Limits, pub settings: UserSettings, - pub object: UserObject, + pub object: Option, } impl<'a> User<'a> { @@ -190,7 +190,7 @@ impl<'a> User<'a> { token: String, limits: Limits, settings: UserSettings, - object: UserObject, + object: Option, ) -> User<'a> { User { logged_in, From 7920dc62e6361f532a07706838ea357c2f079cda Mon Sep 17 00:00:00 2001 From: bitfl0wer Date: Sat, 6 May 2023 22:24:23 +0200 Subject: [PATCH 06/12] Implement message sending (without attachments) --- src/api/channels/messages.rs | 98 +++++++++++++++++++++++++++++++++--- 1 file changed, 91 insertions(+), 7 deletions(-) diff --git a/src/api/channels/messages.rs b/src/api/channels/messages.rs index 80481f5..1fa840a 100644 --- a/src/api/channels/messages.rs +++ b/src/api/channels/messages.rs @@ -1,4 +1,7 @@ pub mod messages { + use reqwest::Client; + use serde_json::to_string; + use crate::api::limits::Limits; use crate::api::types::{Message, PartialDiscordFileAttachment, User}; use crate::limit::LimitedRequester; @@ -18,14 +21,35 @@ pub mod messages { pub async fn send<'a>( url_api: &String, + channel_id: &String, message: &mut crate::api::schemas::MessageSendSchema, files: Option>, token: &String, user: &mut User<'a>, - requester: &mut LimitedRequester, - ) { - let user_limits = &mut user.limits; - let instance_limits = &mut user.belongs_to.limits; + ) -> Result { + let mut requester = LimitedRequester::new().await; + let user_rate_limits = &mut user.limits; + let instance_rate_limits = &mut user.belongs_to.limits; + + if files.is_none() { + let message_request = Client::new() + .post(format!("{}/channels/{}/messages/", url_api, channel_id)) + .bearer_auth(token) + .body(to_string(message).unwrap()); + requester + .send_request( + message_request, + crate::api::limits::LimitType::Channel, + instance_rate_limits, + user_rate_limits, + ) + .await + } else { + return Err(crate::errors::InstanceServerError::InvalidFormBodyError { + error_type: "Not implemented".to_string(), + error: "Not implemented".to_string(), + }); + } } } @@ -33,18 +57,78 @@ pub mod messages { pub async fn send_message( &mut self, mut message: &mut crate::api::schemas::MessageSendSchema, + channel_id: &String, files: Option>, - ) { + ) -> Result { let token = self.token().clone(); Message::send( &self.belongs_to.urls.get_api().to_string(), + channel_id, &mut message, files, &token, self, - &mut LimitedRequester::new().await, ) - .await; + .await } } } + +#[cfg(test)] +mod test { + use crate::{ + api::{AuthUsername, LoginSchema, MessageSendSchema, UserObject}, + instance::Instance, + limit::LimitedRequester, + }; + + use super::*; + + #[tokio::test] + async fn send_message() { + let channel_id = "1104413094102290492".to_string(); + let mut message = crate::api::schemas::MessageSendSchema::new( + None, + Some("ashjkdhjksdfgjsdfzjkhsdvhjksdf".to_string()), + None, + None, + None, + None, + None, + None, + None, + None, + None, + ); + let mut instance = Instance::new( + crate::URLBundle { + api: "http://localhost:3001/api".to_string(), + wss: "ws://localhost:3001/".to_string(), + cdn: "http://localhost:3001".to_string(), + }, + LimitedRequester::new().await, + ) + .await + .unwrap(); + let login_schema: LoginSchema = LoginSchema::new( + AuthUsername::new("user1@gmail.com".to_string()).unwrap(), + "user".to_string(), + None, + None, + None, + None, + ) + .unwrap(); + let login_result = instance.login_account(&login_schema).await.unwrap(); + let token = login_result.token; + let settings = login_result.settings; + let limits = instance.limits.clone(); + let mut user = + crate::api::types::User::new(true, &mut instance, token, limits, settings, None); + let response = user + .send_message(&mut message, &channel_id, None) + .await + .unwrap(); + println!("{:?}", response); + } +} From aa89168928c0e513ef84defe30afc4e90b0534c9 Mon Sep 17 00:00:00 2001 From: bitfl0wer Date: Sat, 6 May 2023 22:47:26 +0200 Subject: [PATCH 07/12] add mod.rs --- src/api/users/mod.rs | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 src/api/users/mod.rs diff --git a/src/api/users/mod.rs b/src/api/users/mod.rs new file mode 100644 index 0000000..e69de29 From 7211ff5eb30129de00978581d414482ee0915f4a Mon Sep 17 00:00:00 2001 From: bitfl0wer Date: Sat, 6 May 2023 22:47:31 +0200 Subject: [PATCH 08/12] add pronouns --- src/api/types.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/api/types.rs b/src/api/types.rs index 5dafe1e..e218a83 100644 --- a/src/api/types.rs +++ b/src/api/types.rs @@ -150,6 +150,7 @@ pub struct UserObject { email: Option, flags: i8, premium_type: Option, + pronouns: Option, public_flags: Option, } From 70a426c75491a2e0f2533aacd324b1f9381ec039 Mon Sep 17 00:00:00 2001 From: bitfl0wer Date: Sun, 7 May 2023 00:17:54 +0200 Subject: [PATCH 09/12] Add users to mod.rs --- src/api/mod.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/api/mod.rs b/src/api/mod.rs index 882f9bd..eb12765 100644 --- a/src/api/mod.rs +++ b/src/api/mod.rs @@ -3,6 +3,7 @@ pub mod channels; pub mod policies; pub mod schemas; pub mod types; +pub mod users; pub use channels::messages::*; pub use policies::instance::instance::*; From 45d5505e2c25a0106fda57c8641fc8493a27d049 Mon Sep 17 00:00:00 2001 From: bitfl0wer Date: Sun, 7 May 2023 00:18:06 +0200 Subject: [PATCH 10/12] remove nesting layer --- src/api/policies/instance/instance.rs | 59 +++++++++++++-------------- 1 file changed, 28 insertions(+), 31 deletions(-) diff --git a/src/api/policies/instance/instance.rs b/src/api/policies/instance/instance.rs index f7a5653..19e841e 100644 --- a/src/api/policies/instance/instance.rs +++ b/src/api/policies/instance/instance.rs @@ -1,41 +1,38 @@ -pub mod instance { - use reqwest::Client; - use serde_json::from_str; - use crate::errors::InstanceServerError; - use crate::{api::types::InstancePolicies, instance::Instance}; +use reqwest::Client; +use serde_json::from_str; - impl<'a> Instance<'a> { - /** - Gets the instance policies schema. - # Errors - [`InstanceServerError`] - If the request fails. - */ - pub async fn instance_policies_schema( - &self, - ) -> Result { - let client = Client::new(); - let endpoint_url = self.urls.get_api().to_string() + "/policies/instance/"; - let request = match client.get(&endpoint_url).send().await { - Ok(result) => result, - Err(e) => { - return Err(InstanceServerError::RequestErrorError { - url: endpoint_url, - error: e.to_string(), - }); - } - }; +use crate::errors::InstanceServerError; +use crate::{api::types::InstancePolicies, instance::Instance}; - if !request.status().as_str().starts_with('2') { - return Err(InstanceServerError::ReceivedErrorCodeError { - error_code: request.status().to_string(), +impl<'a> Instance<'a> { + /** + Gets the instance policies schema. + # Errors + [`InstanceServerError`] - If the request fails. + */ + pub async fn instance_policies_schema(&self) -> Result { + let client = Client::new(); + let endpoint_url = self.urls.get_api().to_string() + "/policies/instance/"; + let request = match client.get(&endpoint_url).send().await { + Ok(result) => result, + Err(e) => { + return Err(InstanceServerError::RequestErrorError { + url: endpoint_url, + error: e.to_string(), }); } + }; - let body = request.text().await.unwrap(); - let instance_policies_schema: InstancePolicies = from_str(&body).unwrap(); - Ok(instance_policies_schema) + if !request.status().as_str().starts_with('2') { + return Err(InstanceServerError::ReceivedErrorCodeError { + error_code: request.status().to_string(), + }); } + + let body = request.text().await.unwrap(); + let instance_policies_schema: InstancePolicies = from_str(&body).unwrap(); + Ok(instance_policies_schema) } } From 3e6e8b78125f59807a8e14fb01fff35a21d01ea0 Mon Sep 17 00:00:00 2001 From: bitfl0wer Date: Sun, 7 May 2023 00:18:14 +0200 Subject: [PATCH 11/12] add users to mod.rs --- src/api/users/mod.rs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/api/users/mod.rs b/src/api/users/mod.rs index e69de29..3bad41b 100644 --- a/src/api/users/mod.rs +++ b/src/api/users/mod.rs @@ -0,0 +1,3 @@ +pub mod users; + +pub use users::*; From df850b33d5239b36f46f3cc80d443e1643da5353 Mon Sep 17 00:00:00 2001 From: bitfl0wer Date: Sun, 7 May 2023 00:18:21 +0200 Subject: [PATCH 12/12] add test fn --- src/api/users/users.rs | 1 + 1 file changed, 1 insertion(+) create mode 100644 src/api/users/users.rs diff --git a/src/api/users/users.rs b/src/api/users/users.rs new file mode 100644 index 0000000..0190944 --- /dev/null +++ b/src/api/users/users.rs @@ -0,0 +1 @@ +pub fn doathing() {}