Merge pull request #31 from polyphony-chat/feature/sending-messages
Bring main up to date
This commit is contained in:
commit
939afac463
|
@ -1,4 +1,7 @@
|
||||||
pub mod messages {
|
pub mod messages {
|
||||||
|
use reqwest::Client;
|
||||||
|
use serde_json::to_string;
|
||||||
|
|
||||||
use crate::api::limits::Limits;
|
use crate::api::limits::Limits;
|
||||||
use crate::api::types::{Message, PartialDiscordFileAttachment, User};
|
use crate::api::types::{Message, PartialDiscordFileAttachment, User};
|
||||||
use crate::limit::LimitedRequester;
|
use crate::limit::LimitedRequester;
|
||||||
|
@ -18,18 +21,114 @@ pub mod messages {
|
||||||
|
|
||||||
pub async fn send<'a>(
|
pub async fn send<'a>(
|
||||||
url_api: &String,
|
url_api: &String,
|
||||||
message: &mut Message,
|
channel_id: &String,
|
||||||
|
message: &mut crate::api::schemas::MessageSendSchema,
|
||||||
files: Option<Vec<PartialDiscordFileAttachment>>,
|
files: Option<Vec<PartialDiscordFileAttachment>>,
|
||||||
|
token: &String,
|
||||||
user: &mut User<'a>,
|
user: &mut User<'a>,
|
||||||
limits_instance: &mut Limits,
|
) -> Result<reqwest::Response, crate::errors::InstanceServerError> {
|
||||||
requester: &mut LimitedRequester,
|
let mut requester = LimitedRequester::new().await;
|
||||||
) {
|
let user_rate_limits = &mut user.limits;
|
||||||
let token = user.token();
|
let instance_rate_limits = &mut user.belongs_to.limits;
|
||||||
let mut limits = &mut user.rate_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(),
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> User<'a> {
|
impl<'a> User<'a> {
|
||||||
pub async fn send_message() {}
|
pub async fn send_message(
|
||||||
|
&mut self,
|
||||||
|
mut message: &mut crate::api::schemas::MessageSendSchema,
|
||||||
|
channel_id: &String,
|
||||||
|
files: Option<Vec<PartialDiscordFileAttachment>>,
|
||||||
|
) -> Result<reqwest::Response, crate::errors::InstanceServerError> {
|
||||||
|
let token = self.token().clone();
|
||||||
|
Message::send(
|
||||||
|
&self.belongs_to.urls.get_api().to_string(),
|
||||||
|
channel_id,
|
||||||
|
&mut message,
|
||||||
|
files,
|
||||||
|
&token,
|
||||||
|
self,
|
||||||
|
)
|
||||||
|
.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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,6 +3,7 @@ pub mod channels;
|
||||||
pub mod policies;
|
pub mod policies;
|
||||||
pub mod schemas;
|
pub mod schemas;
|
||||||
pub mod types;
|
pub mod types;
|
||||||
|
pub mod users;
|
||||||
|
|
||||||
pub use channels::messages::*;
|
pub use channels::messages::*;
|
||||||
pub use policies::instance::instance::*;
|
pub use policies::instance::instance::*;
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
pub mod instance {
|
|
||||||
use reqwest::Client;
|
use reqwest::Client;
|
||||||
use serde_json::from_str;
|
use serde_json::from_str;
|
||||||
|
|
||||||
|
@ -11,9 +11,7 @@ pub mod instance {
|
||||||
# Errors
|
# Errors
|
||||||
[`InstanceServerError`] - If the request fails.
|
[`InstanceServerError`] - If the request fails.
|
||||||
*/
|
*/
|
||||||
pub async fn instance_policies_schema(
|
pub async fn instance_policies_schema(&self) -> Result<InstancePolicies, InstanceServerError> {
|
||||||
&self,
|
|
||||||
) -> Result<InstancePolicies, InstanceServerError> {
|
|
||||||
let client = Client::new();
|
let client = Client::new();
|
||||||
let endpoint_url = self.urls.get_api().to_string() + "/policies/instance/";
|
let endpoint_url = self.urls.get_api().to_string() + "/policies/instance/";
|
||||||
let request = match client.get(&endpoint_url).send().await {
|
let request = match client.get(&endpoint_url).send().await {
|
||||||
|
@ -37,7 +35,6 @@ pub mod instance {
|
||||||
Ok(instance_policies_schema)
|
Ok(instance_policies_schema)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod instance_policies_schema_test {
|
mod instance_policies_schema_test {
|
||||||
|
|
|
@ -244,11 +244,11 @@ pub struct TotpSchema {
|
||||||
login_source: Option<String>,
|
login_source: Option<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Deserialize)]
|
#[derive(Debug, Deserialize, Serialize)]
|
||||||
#[serde(rename_all = "snake_case")]
|
#[serde(rename_all = "snake_case")]
|
||||||
pub struct MessageSendSchema {
|
pub struct MessageSendSchema {
|
||||||
#[serde(rename = "type")]
|
#[serde(rename = "type")]
|
||||||
message_type: i32,
|
message_type: Option<i32>,
|
||||||
content: Option<String>,
|
content: Option<String>,
|
||||||
nonce: Option<String>,
|
nonce: Option<String>,
|
||||||
tts: Option<bool>,
|
tts: Option<bool>,
|
||||||
|
@ -262,6 +262,37 @@ pub struct MessageSendSchema {
|
||||||
attachments: Option<Vec<super::PartialDiscordFileAttachment>>,
|
attachments: Option<Vec<super::PartialDiscordFileAttachment>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// make a new() method for MessageSendSchema
|
||||||
|
impl MessageSendSchema {
|
||||||
|
pub fn new(
|
||||||
|
message_type: Option<i32>,
|
||||||
|
content: Option<String>,
|
||||||
|
nonce: Option<String>,
|
||||||
|
tts: Option<bool>,
|
||||||
|
embeds: Option<Vec<Embed>>,
|
||||||
|
allowed_mentions: Option<super::AllowedMention>,
|
||||||
|
message_reference: Option<super::MessageReference>,
|
||||||
|
components: Option<Vec<super::Component>>,
|
||||||
|
sticker_ids: Option<Vec<String>>,
|
||||||
|
files: Option<HashMap<String, Vec<u8>>>,
|
||||||
|
attachments: Option<Vec<super::PartialDiscordFileAttachment>>,
|
||||||
|
) -> 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
|
// 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
|
// just feel like writing tests, so there you go :) -@bitfl0wer
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
|
|
|
@ -14,8 +14,8 @@ pub trait WebSocketEvent {}
|
||||||
|
|
||||||
#[derive(Debug, Serialize, Deserialize)]
|
#[derive(Debug, Serialize, Deserialize)]
|
||||||
pub struct LoginResult {
|
pub struct LoginResult {
|
||||||
token: String,
|
pub token: String,
|
||||||
settings: UserSettings,
|
pub settings: UserSettings,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Serialize, Deserialize)]
|
#[derive(Debug, Serialize, Deserialize)]
|
||||||
|
@ -150,6 +150,7 @@ pub struct UserObject {
|
||||||
email: Option<String>,
|
email: Option<String>,
|
||||||
flags: i8,
|
flags: i8,
|
||||||
premium_type: Option<i8>,
|
premium_type: Option<i8>,
|
||||||
|
pronouns: Option<String>,
|
||||||
public_flags: Option<i8>,
|
public_flags: Option<i8>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -158,9 +159,9 @@ pub struct User<'a> {
|
||||||
pub logged_in: bool,
|
pub logged_in: bool,
|
||||||
pub belongs_to: &'a mut Instance<'a>,
|
pub belongs_to: &'a mut Instance<'a>,
|
||||||
token: String,
|
token: String,
|
||||||
pub rate_limits: Limits,
|
pub limits: Limits,
|
||||||
pub settings: UserSettings,
|
pub settings: UserSettings,
|
||||||
pub object: UserObject,
|
pub object: Option<UserObject>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> User<'a> {
|
impl<'a> User<'a> {
|
||||||
|
@ -188,15 +189,15 @@ impl<'a> User<'a> {
|
||||||
logged_in: bool,
|
logged_in: bool,
|
||||||
belongs_to: &'a mut Instance<'a>,
|
belongs_to: &'a mut Instance<'a>,
|
||||||
token: String,
|
token: String,
|
||||||
rate_limits: Limits,
|
limits: Limits,
|
||||||
settings: UserSettings,
|
settings: UserSettings,
|
||||||
object: UserObject,
|
object: Option<UserObject>,
|
||||||
) -> User<'a> {
|
) -> User<'a> {
|
||||||
User {
|
User {
|
||||||
logged_in,
|
logged_in,
|
||||||
belongs_to,
|
belongs_to,
|
||||||
token,
|
token,
|
||||||
rate_limits,
|
limits,
|
||||||
settings,
|
settings,
|
||||||
object,
|
object,
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,3 @@
|
||||||
|
pub mod users;
|
||||||
|
|
||||||
|
pub use users::*;
|
|
@ -0,0 +1 @@
|
||||||
|
pub fn doathing() {}
|
Loading…
Reference in New Issue