From 514ae0e441a1684d75a81031a2e17de58796a72f Mon Sep 17 00:00:00 2001 From: bitfl0wer Date: Wed, 3 May 2023 15:45:07 +0200 Subject: [PATCH 01/10] Add message.rs --- src/api/channels/messages.rs | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/api/channels/messages.rs b/src/api/channels/messages.rs index cbfcdc5..b0a5412 100644 --- a/src/api/channels/messages.rs +++ b/src/api/channels/messages.rs @@ -1 +1,6 @@ -pub mod messages {} +pub mod messages { + use crate::api::types::Message; + use crate::instance::Instance; + + impl Message {} +} From 7938addf67b2466e4a32fc9b06e07295521f0a30 Mon Sep 17 00:00:00 2001 From: bitfl0wer Date: Wed, 3 May 2023 15:55:01 +0200 Subject: [PATCH 02/10] Start working on message send body --- src/api/channels/messages.rs | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/src/api/channels/messages.rs b/src/api/channels/messages.rs index b0a5412..c9211ef 100644 --- a/src/api/channels/messages.rs +++ b/src/api/channels/messages.rs @@ -1,6 +1,24 @@ pub mod messages { + use reqwest::Client; + use serde_json::to_string; + + use crate::api::limits::Limits; use crate::api::types::Message; use crate::instance::Instance; + use crate::limit::LimitedRequester; - impl Message {} + impl Message { + pub async fn send( + url_api: &String, + message: &Message, + limits: &Limits, + requester: &mut LimitedRequester, + ) { + let client = Client::new() + .post(url_api) + .body(to_string(message).unwrap()) + .send() + .await; + } + } } From 6858b1eb0d5e1c72960961f12059a0992c5aa67c Mon Sep 17 00:00:00 2001 From: bitfl0wer Date: Wed, 3 May 2023 16:27:04 +0200 Subject: [PATCH 03/10] Add Message send() --- src/api/channels/messages.rs | 45 +++++++++++++++++++++++++++++------- 1 file changed, 37 insertions(+), 8 deletions(-) diff --git a/src/api/channels/messages.rs b/src/api/channels/messages.rs index c9211ef..a00fa79 100644 --- a/src/api/channels/messages.rs +++ b/src/api/channels/messages.rs @@ -1,24 +1,53 @@ pub mod messages { - use reqwest::Client; + use reqwest::{Client, Response}; use serde_json::to_string; use crate::api::limits::Limits; use crate::api::types::Message; + use crate::api::User; + use crate::errors::InstanceServerError; use crate::instance::Instance; use crate::limit::LimitedRequester; impl Message { + /** + Sends a message to the Spacebar server. + # Arguments + * `url_api` - The URL of the Spacebar server's API. + * `message` - The [`Message`] that will be sent to the Spacebar server. + * `limits_user` - The [`Limits`] of the user. + * `limits_instance` - The [`Limits`] of the instance. + * `requester` - The [`LimitedRequester`] that will be used to make requests to the Spacebar server. + # Errors + * [`InstanceServerError`] - If the message cannot be sent. + */ pub async fn send( url_api: &String, message: &Message, - limits: &Limits, + limits_user: &mut Limits, + limits_instance: &mut Limits, requester: &mut LimitedRequester, - ) { - let client = Client::new() - .post(url_api) - .body(to_string(message).unwrap()) - .send() - .await; + ) -> Result { + let request = Client::new() + .post(format!( + "{}/channels/{}/messages", + url_api, message.channel_id + )) + .body(to_string(message).unwrap()); + match requester + .send_request( + request, + crate::api::limits::LimitType::Channel, + limits_instance, + limits_user, + ) + .await + { + Ok(result) => return Ok(result), + Err(e) => return Err(e), + }; } } + + impl User {} } From 5edc92524c7fe2d2c6b72a5be581f3a1300f31b6 Mon Sep 17 00:00:00 2001 From: bitfl0wer Date: Wed, 3 May 2023 16:37:10 +0200 Subject: [PATCH 04/10] Add lifetime to Instance --- src/api/auth/login.rs | 2 +- src/api/auth/register.rs | 2 +- src/api/channels/messages.rs | 2 +- src/api/policies/instance/instance.rs | 2 +- src/api/types.rs | 20 +++++++++----------- src/instance.rs | 8 ++++---- 6 files changed, 17 insertions(+), 19 deletions(-) diff --git a/src/api/auth/login.rs b/src/api/auth/login.rs index f7c4fe6..23add11 100644 --- a/src/api/auth/login.rs +++ b/src/api/auth/login.rs @@ -8,7 +8,7 @@ pub mod login { use crate::errors::InstanceServerError; use crate::instance::Instance; - impl Instance { + impl<'a> Instance<'a> { pub async fn login_account( &mut self, login_schema: &LoginSchema, diff --git a/src/api/auth/register.rs b/src/api/auth/register.rs index b932b9f..b4d4fd1 100644 --- a/src/api/auth/register.rs +++ b/src/api/auth/register.rs @@ -8,7 +8,7 @@ pub mod register { instance::{Instance, Token}, }; - impl Instance { + impl<'a> Instance<'a> { /** Registers a new user on the Spacebar server. # Arguments diff --git a/src/api/channels/messages.rs b/src/api/channels/messages.rs index a00fa79..5dfe532 100644 --- a/src/api/channels/messages.rs +++ b/src/api/channels/messages.rs @@ -49,5 +49,5 @@ pub mod messages { } } - impl User {} + impl<'a> User<'a> {} } diff --git a/src/api/policies/instance/instance.rs b/src/api/policies/instance/instance.rs index 7c1a48a..f7a5653 100644 --- a/src/api/policies/instance/instance.rs +++ b/src/api/policies/instance/instance.rs @@ -5,7 +5,7 @@ pub mod instance { use crate::errors::InstanceServerError; use crate::{api::types::InstancePolicies, instance::Instance}; - impl Instance { + impl<'a> Instance<'a> { /** Gets the instance policies schema. # Errors diff --git a/src/api/types.rs b/src/api/types.rs index 4bdb6c4..c6c9a13 100644 --- a/src/api/types.rs +++ b/src/api/types.rs @@ -4,11 +4,9 @@ https://discord.com/developers/docs . I do not feel like re-documenting all of this, as everything is already perfectly explained there. */ -use std::fmt; - use serde::{Deserialize, Serialize}; -use crate::{api::limits::Limits, URLBundle}; +use crate::{api::limits::Limits, instance::Instance}; pub trait WebSocketEvent {} @@ -154,22 +152,22 @@ pub struct UserObject { } #[derive(Debug)] -pub struct User { +pub struct User<'a> { logged_in: bool, - belongs_to: URLBundle, + belongs_to: &'a Instance<'a>, token: String, rate_limits: Limits, pub settings: UserSettings, pub object: UserObject, } -impl User { +impl<'a> User<'a> { pub fn is_logged_in(&self) -> bool { self.logged_in } - pub fn belongs_to(&self) -> URLBundle { - self.belongs_to.clone() + pub fn belongs_to(&self) -> &Instance { + self.belongs_to } pub fn token(&self) -> String { @@ -186,12 +184,12 @@ impl User { pub fn new( logged_in: bool, - belongs_to: URLBundle, + belongs_to: &'a Instance<'a>, token: String, rate_limits: Limits, settings: UserSettings, object: UserObject, - ) -> User { + ) -> User<'a> { User { logged_in, belongs_to, @@ -206,7 +204,7 @@ impl User { #[derive(Debug, Serialize, Deserialize, Default)] pub struct Message { id: String, - channel_id: String, + pub channel_id: String, author: UserObject, content: String, timestamp: String, diff --git a/src/instance.rs b/src/instance.rs index 3e52e88..ad877f0 100644 --- a/src/instance.rs +++ b/src/instance.rs @@ -11,16 +11,16 @@ use std::fmt; /** The [`Instance`] what you will be using to perform all sorts of actions on the Spacebar server. */ -pub struct Instance { +pub struct Instance<'a> { pub urls: URLBundle, pub instance_info: InstancePolicies, pub requester: LimitedRequester, pub limits: Limits, //pub gateway: Gateway, - pub users: HashMap, + pub users: HashMap>, } -impl Instance { +impl<'a> Instance<'a> { /// Creates a new [`Instance`]. /// # Arguments /// * `urls` - The [`URLBundle`] that contains all the URLs that are needed to connect to the Spacebar server. @@ -30,7 +30,7 @@ impl Instance { pub async fn new( urls: URLBundle, requester: LimitedRequester, - ) -> Result { + ) -> Result, InstanceServerError> { let users: HashMap = HashMap::new(); let mut instance = Instance { urls: urls.clone(), From efb68a607a0785b69ab49880e2ea4d4c6c70ca42 Mon Sep 17 00:00:00 2001 From: bitfl0wer Date: Wed, 3 May 2023 16:38:42 +0200 Subject: [PATCH 05/10] cargo clippy --fix --- src/api/channels/messages.rs | 8 ++++---- src/gateway.rs | 21 ++++++++++----------- src/limit.rs | 2 +- 3 files changed, 15 insertions(+), 16 deletions(-) diff --git a/src/api/channels/messages.rs b/src/api/channels/messages.rs index 5dfe532..67e1404 100644 --- a/src/api/channels/messages.rs +++ b/src/api/channels/messages.rs @@ -6,7 +6,7 @@ pub mod messages { use crate::api::types::Message; use crate::api::User; use crate::errors::InstanceServerError; - use crate::instance::Instance; + use crate::limit::LimitedRequester; impl Message { @@ -43,9 +43,9 @@ pub mod messages { ) .await { - Ok(result) => return Ok(result), - Err(e) => return Err(e), - }; + Ok(result) => Ok(result), + Err(e) => Err(e), + } } } diff --git a/src/gateway.rs b/src/gateway.rs index 61f51ec..fa2fc8d 100644 --- a/src/gateway.rs +++ b/src/gateway.rs @@ -1,20 +1,20 @@ -use std::sync::Arc; -use std::sync::Mutex; -use std::thread::JoinHandle; -use std::time::Duration; + + + + use crate::api::types::*; use crate::api::WebSocketEvent; use crate::errors::ObserverError; use crate::gateway::events::Events; use crate::URLBundle; -use futures_util::SinkExt; + use futures_util::StreamExt; use reqwest::Url; use serde::Deserialize; use serde::Serialize; -use serde_json::from_str; -use serde_json::to_string; + + use tokio::io; use tokio::net::TcpStream; use tokio_tungstenite::connect_async; @@ -60,12 +60,12 @@ impl<'a> Gateway<'a> { } }; - return Ok(Gateway { + Ok(Gateway { url: websocket_url, token, events: Events::default(), socket: ws_stream, - }); + }) } } @@ -129,7 +129,6 @@ impl<'a, T: WebSocketEvent> GatewayEvent<'a, T> { // pointer value than observable. self.observers.retain(|obs| !std::ptr::eq(*obs, observable)); self.is_observed = !self.observers.is_empty(); - return; } /** @@ -223,7 +222,7 @@ mod example { #[tokio::test] async fn test_gateway() { - let gateway = Gateway::new("ws://localhost:3001/".to_string(), "none".to_string()) + let _gateway = Gateway::new("ws://localhost:3001/".to_string(), "none".to_string()) .await .unwrap(); } diff --git a/src/limit.rs b/src/limit.rs index 84e6d17..0dfbbd3 100644 --- a/src/limit.rs +++ b/src/limit.rs @@ -261,7 +261,7 @@ mod rate_limit { #[tokio::test] async fn create_limited_requester() { - let urls = URLBundle::new( + let _urls = URLBundle::new( String::from("http://localhost:3001/api/"), String::from("wss://localhost:3001/"), String::from("http://localhost:3001/cdn"), From fc9032c03813700ef1dc1229b48900978f820656 Mon Sep 17 00:00:00 2001 From: bitfl0wer Date: Wed, 3 May 2023 17:11:48 +0200 Subject: [PATCH 06/10] change visibilities to pub --- 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 c6c9a13..1ab64b1 100644 --- a/src/api/types.rs +++ b/src/api/types.rs @@ -153,10 +153,10 @@ pub struct UserObject { #[derive(Debug)] pub struct User<'a> { - logged_in: bool, - belongs_to: &'a Instance<'a>, + pub logged_in: bool, + pub belongs_to: &'a Instance<'a>, token: String, - rate_limits: Limits, + pub rate_limits: Limits, pub settings: UserSettings, pub object: UserObject, } From a0df444f872af14c568ff178413b932ea9cc072d Mon Sep 17 00:00:00 2001 From: bitfl0wer Date: Wed, 3 May 2023 17:12:23 +0200 Subject: [PATCH 07/10] Remove blank line --- src/api/channels/messages.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/api/channels/messages.rs b/src/api/channels/messages.rs index 67e1404..5a47b0b 100644 --- a/src/api/channels/messages.rs +++ b/src/api/channels/messages.rs @@ -6,7 +6,6 @@ pub mod messages { use crate::api::types::Message; use crate::api::User; use crate::errors::InstanceServerError; - use crate::limit::LimitedRequester; impl Message { From 5d545d7fc94f8e9a0f4aa4c2cb9c835b91e30f63 Mon Sep 17 00:00:00 2001 From: bitfl0wer Date: Wed, 3 May 2023 17:12:32 +0200 Subject: [PATCH 08/10] Add get_as_mut to Limits --- src/api/policies/instance/limits.rs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/api/policies/instance/limits.rs b/src/api/policies/instance/limits.rs index 7c4b624..58d55c5 100644 --- a/src/api/policies/instance/limits.rs +++ b/src/api/policies/instance/limits.rs @@ -304,6 +304,10 @@ pub mod limits { map } + pub fn get_as_mut(&mut self) -> &mut Limits { + self + } + /// check_limits uses the API to get the current request limits of the instance. /// It returns a `Limits` struct containing all the limits. /// If the rate limit is disabled, then the limit is set to `u64::MAX`. From 7560361bf56ca8e6bec4f6be756d57b3136afae6 Mon Sep 17 00:00:00 2001 From: bitfl0wer Date: Wed, 3 May 2023 17:29:20 +0200 Subject: [PATCH 09/10] add send_message to user --- src/api/channels/messages.rs | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/src/api/channels/messages.rs b/src/api/channels/messages.rs index 5a47b0b..dae8a9e 100644 --- a/src/api/channels/messages.rs +++ b/src/api/channels/messages.rs @@ -48,5 +48,19 @@ pub mod messages { } } - impl<'a> User<'a> {} + impl<'a> User<'a> { + pub async fn send_message( + &mut self, + message: &Message, + ) -> Result { + Message::send( + &self.belongs_to().urls.get_api().to_string(), + message, + self.rate_limits.get_as_mut(), + &mut self.belongs_to.limits.get_as_mut(), + &mut LimitedRequester::new().await, + ) + .await + } + } } From c4b15b5bf055026a55c9fe8627eada1a2cb7b26f Mon Sep 17 00:00:00 2001 From: bitfl0wer Date: Wed, 3 May 2023 17:29:32 +0200 Subject: [PATCH 10/10] change reference to be mutable --- 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 1ab64b1..a070587 100644 --- a/src/api/types.rs +++ b/src/api/types.rs @@ -154,7 +154,7 @@ pub struct UserObject { #[derive(Debug)] pub struct User<'a> { pub logged_in: bool, - pub belongs_to: &'a Instance<'a>, + pub belongs_to: &'a mut Instance<'a>, token: String, pub rate_limits: Limits, pub settings: UserSettings, @@ -166,7 +166,7 @@ impl<'a> User<'a> { self.logged_in } - pub fn belongs_to(&self) -> &Instance { + pub fn belongs_to(&mut self) -> &mut Instance<'a> { self.belongs_to } @@ -184,7 +184,7 @@ impl<'a> User<'a> { pub fn new( logged_in: bool, - belongs_to: &'a Instance<'a>, + belongs_to: &'a mut Instance<'a>, token: String, rate_limits: Limits, settings: UserSettings,