Merge pull request #31 from polyphony-chat/feature/sending-messages

Bring main up to date
This commit is contained in:
Flori 2023-05-07 00:47:50 +02:00 committed by GitHub
commit 939afac463
7 changed files with 180 additions and 47 deletions

View File

@ -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,18 +21,114 @@ pub mod messages {
pub async fn send<'a>(
url_api: &String,
message: &mut Message,
channel_id: &String,
message: &mut crate::api::schemas::MessageSendSchema,
files: Option<Vec<PartialDiscordFileAttachment>>,
token: &String,
user: &mut User<'a>,
limits_instance: &mut Limits,
requester: &mut LimitedRequester,
) {
let token = user.token();
let mut limits = &mut user.rate_limits;
) -> Result<reqwest::Response, crate::errors::InstanceServerError> {
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(),
});
}
}
}
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);
}
}

View File

@ -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::*;

View File

@ -1,19 +1,17 @@
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> {
use crate::errors::InstanceServerError;
use crate::{api::types::InstancePolicies, instance::Instance};
impl<'a> Instance<'a> {
/**
Gets the instance policies schema.
# Errors
[`InstanceServerError`] - If the request fails.
*/
pub async fn instance_policies_schema(
&self,
) -> Result<InstancePolicies, InstanceServerError> {
pub async fn instance_policies_schema(&self) -> Result<InstancePolicies, InstanceServerError> {
let client = Client::new();
let endpoint_url = self.urls.get_api().to_string() + "/policies/instance/";
let request = match client.get(&endpoint_url).send().await {
@ -36,7 +34,6 @@ pub mod instance {
let instance_policies_schema: InstancePolicies = from_str(&body).unwrap();
Ok(instance_policies_schema)
}
}
}
#[cfg(test)]

View File

@ -244,11 +244,11 @@ pub struct TotpSchema {
login_source: Option<String>,
}
#[derive(Debug, Deserialize)]
#[derive(Debug, Deserialize, Serialize)]
#[serde(rename_all = "snake_case")]
pub struct MessageSendSchema {
#[serde(rename = "type")]
message_type: i32,
message_type: Option<i32>,
content: Option<String>,
nonce: Option<String>,
tts: Option<bool>,
@ -262,6 +262,37 @@ pub struct MessageSendSchema {
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
// just feel like writing tests, so there you go :) -@bitfl0wer
#[cfg(test)]

View File

@ -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)]
@ -150,6 +150,7 @@ pub struct UserObject {
email: Option<String>,
flags: i8,
premium_type: Option<i8>,
pronouns: Option<String>,
public_flags: Option<i8>,
}
@ -158,9 +159,9 @@ 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,
pub object: Option<UserObject>,
}
impl<'a> User<'a> {
@ -188,15 +189,15 @@ impl<'a> User<'a> {
logged_in: bool,
belongs_to: &'a mut Instance<'a>,
token: String,
rate_limits: Limits,
limits: Limits,
settings: UserSettings,
object: UserObject,
object: Option<UserObject>,
) -> User<'a> {
User {
logged_in,
belongs_to,
token,
rate_limits,
limits,
settings,
object,
}

3
src/api/users/mod.rs Normal file
View File

@ -0,0 +1,3 @@
pub mod users;
pub use users::*;

1
src/api/users/users.rs Normal file
View File

@ -0,0 +1 @@
pub fn doathing() {}