Merge remote-tracking branch 'origin/feature/sending-messages'
This commit is contained in:
commit
66ab87db30
|
@ -8,7 +8,7 @@ pub mod login {
|
||||||
use crate::errors::InstanceServerError;
|
use crate::errors::InstanceServerError;
|
||||||
use crate::instance::Instance;
|
use crate::instance::Instance;
|
||||||
|
|
||||||
impl Instance {
|
impl<'a> Instance<'a> {
|
||||||
pub async fn login_account(
|
pub async fn login_account(
|
||||||
&mut self,
|
&mut self,
|
||||||
login_schema: &LoginSchema,
|
login_schema: &LoginSchema,
|
||||||
|
|
|
@ -8,7 +8,7 @@ pub mod register {
|
||||||
instance::{Instance, Token},
|
instance::{Instance, Token},
|
||||||
};
|
};
|
||||||
|
|
||||||
impl Instance {
|
impl<'a> Instance<'a> {
|
||||||
/**
|
/**
|
||||||
Registers a new user on the Spacebar server.
|
Registers a new user on the Spacebar server.
|
||||||
# Arguments
|
# Arguments
|
||||||
|
|
|
@ -1 +1,66 @@
|
||||||
pub mod messages {}
|
pub mod messages {
|
||||||
|
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::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_user: &mut Limits,
|
||||||
|
limits_instance: &mut Limits,
|
||||||
|
requester: &mut LimitedRequester,
|
||||||
|
) -> Result<Response, InstanceServerError> {
|
||||||
|
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) => Ok(result),
|
||||||
|
Err(e) => Err(e),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a> User<'a> {
|
||||||
|
pub async fn send_message(
|
||||||
|
&mut self,
|
||||||
|
message: &Message,
|
||||||
|
) -> Result<Response, InstanceServerError> {
|
||||||
|
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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -5,7 +5,7 @@ pub mod instance {
|
||||||
use crate::errors::InstanceServerError;
|
use crate::errors::InstanceServerError;
|
||||||
use crate::{api::types::InstancePolicies, instance::Instance};
|
use crate::{api::types::InstancePolicies, instance::Instance};
|
||||||
|
|
||||||
impl Instance {
|
impl<'a> Instance<'a> {
|
||||||
/**
|
/**
|
||||||
Gets the instance policies schema.
|
Gets the instance policies schema.
|
||||||
# Errors
|
# Errors
|
||||||
|
|
|
@ -304,6 +304,10 @@ pub mod limits {
|
||||||
map
|
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.
|
/// check_limits uses the API to get the current request limits of the instance.
|
||||||
/// It returns a `Limits` struct containing all the limits.
|
/// It returns a `Limits` struct containing all the limits.
|
||||||
/// If the rate limit is disabled, then the limit is set to `u64::MAX`.
|
/// If the rate limit is disabled, then the limit is set to `u64::MAX`.
|
||||||
|
|
|
@ -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.
|
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 serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
use crate::{api::limits::Limits, URLBundle};
|
use crate::{api::limits::Limits, instance::Instance};
|
||||||
|
|
||||||
pub trait WebSocketEvent {}
|
pub trait WebSocketEvent {}
|
||||||
|
|
||||||
|
@ -154,22 +152,22 @@ pub struct UserObject {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct User {
|
pub struct User<'a> {
|
||||||
logged_in: bool,
|
pub logged_in: bool,
|
||||||
belongs_to: URLBundle,
|
pub belongs_to: &'a mut Instance<'a>,
|
||||||
token: String,
|
token: String,
|
||||||
rate_limits: Limits,
|
pub rate_limits: Limits,
|
||||||
pub settings: UserSettings,
|
pub settings: UserSettings,
|
||||||
pub object: UserObject,
|
pub object: UserObject,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl User {
|
impl<'a> User<'a> {
|
||||||
pub fn is_logged_in(&self) -> bool {
|
pub fn is_logged_in(&self) -> bool {
|
||||||
self.logged_in
|
self.logged_in
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn belongs_to(&self) -> URLBundle {
|
pub fn belongs_to(&mut self) -> &mut Instance<'a> {
|
||||||
self.belongs_to.clone()
|
self.belongs_to
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn token(&self) -> String {
|
pub fn token(&self) -> String {
|
||||||
|
@ -186,12 +184,12 @@ impl User {
|
||||||
|
|
||||||
pub fn new(
|
pub fn new(
|
||||||
logged_in: bool,
|
logged_in: bool,
|
||||||
belongs_to: URLBundle,
|
belongs_to: &'a mut Instance<'a>,
|
||||||
token: String,
|
token: String,
|
||||||
rate_limits: Limits,
|
rate_limits: Limits,
|
||||||
settings: UserSettings,
|
settings: UserSettings,
|
||||||
object: UserObject,
|
object: UserObject,
|
||||||
) -> User {
|
) -> User<'a> {
|
||||||
User {
|
User {
|
||||||
logged_in,
|
logged_in,
|
||||||
belongs_to,
|
belongs_to,
|
||||||
|
@ -206,7 +204,7 @@ impl User {
|
||||||
#[derive(Debug, Serialize, Deserialize, Default)]
|
#[derive(Debug, Serialize, Deserialize, Default)]
|
||||||
pub struct Message {
|
pub struct Message {
|
||||||
id: String,
|
id: String,
|
||||||
channel_id: String,
|
pub channel_id: String,
|
||||||
author: UserObject,
|
author: UserObject,
|
||||||
content: String,
|
content: String,
|
||||||
timestamp: String,
|
timestamp: String,
|
||||||
|
|
|
@ -1,5 +1,7 @@
|
||||||
use std::sync::Arc;
|
|
||||||
use std::thread;
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
use crate::api::types::*;
|
use crate::api::types::*;
|
||||||
use crate::api::WebSocketEvent;
|
use crate::api::WebSocketEvent;
|
||||||
|
@ -7,14 +9,13 @@ use crate::errors::ObserverError;
|
||||||
use crate::gateway::events::Events;
|
use crate::gateway::events::Events;
|
||||||
use crate::URLBundle;
|
use crate::URLBundle;
|
||||||
|
|
||||||
use futures_util::stream::{FilterMap, SplitSink, SplitStream};
|
|
||||||
use futures_util::SinkExt;
|
|
||||||
use futures_util::StreamExt;
|
use futures_util::StreamExt;
|
||||||
use native_tls::TlsConnector;
|
use native_tls::TlsConnector;
|
||||||
use reqwest::Url;
|
use reqwest::Url;
|
||||||
use serde::Deserialize;
|
use serde::Deserialize;
|
||||||
use serde::Serialize;
|
use serde::Serialize;
|
||||||
use serde_json::from_str;
|
|
||||||
|
|
||||||
use tokio::io;
|
use tokio::io;
|
||||||
use tokio::net::TcpStream;
|
use tokio::net::TcpStream;
|
||||||
use tokio::sync::mpsc::{channel, Receiver, Sender};
|
use tokio::sync::mpsc::{channel, Receiver, Sender};
|
||||||
|
@ -94,21 +95,14 @@ impl<'a> WebSocketConnection {
|
||||||
while let Some(msg) = shared_channel_read.lock().await.recv().await {
|
while let Some(msg) = shared_channel_read.lock().await.recv().await {
|
||||||
write_tx.send(msg).await.unwrap();
|
write_tx.send(msg).await.unwrap();
|
||||||
}
|
}
|
||||||
|
|
||||||
let event = while let Some(msg) = write_rx.next().await {
|
|
||||||
shared_channel_write
|
|
||||||
.lock()
|
|
||||||
.await
|
|
||||||
.send(msg.unwrap())
|
|
||||||
.await
|
|
||||||
.unwrap();
|
|
||||||
};
|
};
|
||||||
});
|
|
||||||
|
|
||||||
WebSocketConnection {
|
Ok(Gateway {
|
||||||
tx: clone_shared_channel_write,
|
url: websocket_url,
|
||||||
rx: clone_shared_channel_read,
|
token,
|
||||||
}
|
events: Events::default(),
|
||||||
|
socket: ws_stream,
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -172,7 +166,6 @@ impl<'a, T: WebSocketEvent> GatewayEvent<'a, T> {
|
||||||
// pointer value than observable.
|
// pointer value than observable.
|
||||||
self.observers.retain(|obs| !std::ptr::eq(*obs, observable));
|
self.observers.retain(|obs| !std::ptr::eq(*obs, observable));
|
||||||
self.is_observed = !self.observers.is_empty();
|
self.is_observed = !self.observers.is_empty();
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -266,7 +259,7 @@ mod example {
|
||||||
|
|
||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
async fn test_gateway() {
|
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
|
.await
|
||||||
.unwrap();
|
.unwrap();
|
||||||
}
|
}
|
||||||
|
|
|
@ -11,16 +11,16 @@ use std::fmt;
|
||||||
/**
|
/**
|
||||||
The [`Instance`] what you will be using to perform all sorts of actions on the Spacebar server.
|
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 urls: URLBundle,
|
||||||
pub instance_info: InstancePolicies,
|
pub instance_info: InstancePolicies,
|
||||||
pub requester: LimitedRequester,
|
pub requester: LimitedRequester,
|
||||||
pub limits: Limits,
|
pub limits: Limits,
|
||||||
//pub gateway: Gateway,
|
//pub gateway: Gateway,
|
||||||
pub users: HashMap<Token, User>,
|
pub users: HashMap<Token, User<'a>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Instance {
|
impl<'a> Instance<'a> {
|
||||||
/// Creates a new [`Instance`].
|
/// Creates a new [`Instance`].
|
||||||
/// # Arguments
|
/// # Arguments
|
||||||
/// * `urls` - The [`URLBundle`] that contains all the URLs that are needed to connect to the Spacebar server.
|
/// * `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(
|
pub async fn new(
|
||||||
urls: URLBundle,
|
urls: URLBundle,
|
||||||
requester: LimitedRequester,
|
requester: LimitedRequester,
|
||||||
) -> Result<Instance, InstanceServerError> {
|
) -> Result<Instance<'a>, InstanceServerError> {
|
||||||
let users: HashMap<Token, User> = HashMap::new();
|
let users: HashMap<Token, User> = HashMap::new();
|
||||||
let mut instance = Instance {
|
let mut instance = Instance {
|
||||||
urls: urls.clone(),
|
urls: urls.clone(),
|
||||||
|
|
|
@ -261,7 +261,7 @@ mod rate_limit {
|
||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
|
|
||||||
async fn create_limited_requester() {
|
async fn create_limited_requester() {
|
||||||
let urls = URLBundle::new(
|
let _urls = URLBundle::new(
|
||||||
String::from("http://localhost:3001/api/"),
|
String::from("http://localhost:3001/api/"),
|
||||||
String::from("wss://localhost:3001/"),
|
String::from("wss://localhost:3001/"),
|
||||||
String::from("http://localhost:3001/cdn"),
|
String::from("http://localhost:3001/cdn"),
|
||||||
|
|
Loading…
Reference in New Issue