Merge pull request #35 from polyphony-chat/feature/sending-messages
Bring main up to date
This commit is contained in:
commit
ea9d864580
|
@ -16,3 +16,4 @@ custom_error = "1.9.2"
|
||||||
native-tls = "0.2.11"
|
native-tls = "0.2.11"
|
||||||
tokio-tungstenite = {version = "0.18.0", features = ["native-tls"]}
|
tokio-tungstenite = {version = "0.18.0", features = ["native-tls"]}
|
||||||
futures-util = "0.3.28"
|
futures-util = "0.3.28"
|
||||||
|
http = "0.2.9"
|
|
@ -80,7 +80,7 @@ pub mod register {
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod test {
|
mod test {
|
||||||
use crate::api::schemas::{AuthEmail, AuthPassword, AuthUsername, RegisterSchema};
|
use crate::api::schemas::{AuthUsername, RegisterSchema};
|
||||||
use crate::instance::Instance;
|
use crate::instance::Instance;
|
||||||
use crate::limit::LimitedRequester;
|
use crate::limit::LimitedRequester;
|
||||||
use crate::URLBundle;
|
use crate::URLBundle;
|
||||||
|
@ -98,9 +98,9 @@ mod test {
|
||||||
.unwrap();
|
.unwrap();
|
||||||
let reg = RegisterSchema::new(
|
let reg = RegisterSchema::new(
|
||||||
AuthUsername::new("Hiiii".to_string()).unwrap(),
|
AuthUsername::new("Hiiii".to_string()).unwrap(),
|
||||||
Some(AuthPassword::new("mysupersecurepass123!".to_string()).unwrap()),
|
None,
|
||||||
true,
|
true,
|
||||||
Some(AuthEmail::new("four7@aaaa.xyz".to_string()).unwrap()),
|
None,
|
||||||
None,
|
None,
|
||||||
None,
|
None,
|
||||||
Some("2000-01-01".to_string()),
|
Some("2000-01-01".to_string()),
|
||||||
|
|
|
@ -1,8 +1,11 @@
|
||||||
pub mod messages {
|
pub mod messages {
|
||||||
use reqwest::Client;
|
use http::header::CONTENT_DISPOSITION;
|
||||||
|
use http::{header, HeaderMap};
|
||||||
|
use reqwest::{multipart, Client};
|
||||||
use serde_json::to_string;
|
use serde_json::to_string;
|
||||||
|
|
||||||
use crate::api::types::{Message, PartialDiscordFileAttachment, User};
|
use crate::api::types::{Message, PartialDiscordFileAttachment, User};
|
||||||
|
use crate::errors::InstanceServerError;
|
||||||
use crate::limit::LimitedRequester;
|
use crate::limit::LimitedRequester;
|
||||||
|
|
||||||
impl Message {
|
impl Message {
|
||||||
|
@ -17,12 +20,11 @@ pub mod messages {
|
||||||
# Errors
|
# Errors
|
||||||
* [`InstanceServerError`] - If the message cannot be sent.
|
* [`InstanceServerError`] - If the message cannot be sent.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
pub async fn send<'a>(
|
pub async fn send<'a>(
|
||||||
url_api: &String,
|
url_api: &String,
|
||||||
channel_id: &String,
|
channel_id: &String,
|
||||||
message: &mut crate::api::schemas::MessageSendSchema,
|
message: &mut crate::api::schemas::MessageSendSchema,
|
||||||
files: Option<Vec<PartialDiscordFileAttachment>>,
|
files: Option<&'static Vec<PartialDiscordFileAttachment>>,
|
||||||
token: &String,
|
token: &String,
|
||||||
user: &mut User<'a>,
|
user: &mut User<'a>,
|
||||||
) -> Result<reqwest::Response, crate::errors::InstanceServerError> {
|
) -> Result<reqwest::Response, crate::errors::InstanceServerError> {
|
||||||
|
@ -44,10 +46,76 @@ pub mod messages {
|
||||||
)
|
)
|
||||||
.await
|
.await
|
||||||
} else {
|
} else {
|
||||||
Err(crate::errors::InstanceServerError::InvalidFormBodyError {
|
let mut form = reqwest::multipart::Form::new();
|
||||||
error_type: "Not implemented".to_string(),
|
let payload_json = to_string(message).unwrap();
|
||||||
error: "Not implemented".to_string(),
|
let mut payload_field =
|
||||||
})
|
reqwest::multipart::Part::text(payload_json).file_name("payload_json");
|
||||||
|
payload_field = match payload_field.mime_str("application/json") {
|
||||||
|
Ok(part) => part,
|
||||||
|
Err(e) => {
|
||||||
|
return Err(InstanceServerError::MultipartCreationError {
|
||||||
|
error: e.to_string(),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
form = form.part("payload_json", payload_field);
|
||||||
|
|
||||||
|
for (index, attachment) in files.iter().enumerate() {
|
||||||
|
let part_name = format!("files[{}]", index);
|
||||||
|
let content_disposition = format!(
|
||||||
|
"form-data; name=\"{}\"'; filename=\"{}\"",
|
||||||
|
part_name,
|
||||||
|
attachment
|
||||||
|
.get(index)
|
||||||
|
.unwrap()
|
||||||
|
.filename
|
||||||
|
.as_deref()
|
||||||
|
.unwrap_or("file")
|
||||||
|
);
|
||||||
|
let mut header_map = HeaderMap::new();
|
||||||
|
header_map
|
||||||
|
.insert(CONTENT_DISPOSITION, content_disposition.parse().unwrap())
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
let mut part =
|
||||||
|
multipart::Part::bytes(attachment.get(index).unwrap().content.as_slice())
|
||||||
|
.file_name(
|
||||||
|
attachment
|
||||||
|
.get(index)
|
||||||
|
.unwrap()
|
||||||
|
.filename
|
||||||
|
.as_deref()
|
||||||
|
.unwrap_or("file"),
|
||||||
|
)
|
||||||
|
.headers(header_map);
|
||||||
|
|
||||||
|
part = match part.mime_str("application/octet-stream") {
|
||||||
|
Ok(part) => part,
|
||||||
|
Err(e) => {
|
||||||
|
return Err(InstanceServerError::MultipartCreationError {
|
||||||
|
error: e.to_string(),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
form = form.part(part_name, part);
|
||||||
|
}
|
||||||
|
|
||||||
|
let message_request = Client::new()
|
||||||
|
.post(format!("{}/channels/{}/messages/", url_api, channel_id))
|
||||||
|
.bearer_auth(token)
|
||||||
|
.multipart(form);
|
||||||
|
|
||||||
|
requester
|
||||||
|
.send_request(
|
||||||
|
message_request,
|
||||||
|
crate::api::limits::LimitType::Channel,
|
||||||
|
instance_rate_limits,
|
||||||
|
user_rate_limits,
|
||||||
|
)
|
||||||
|
.await
|
||||||
|
// TODO: Deallocate the darn memory leak!
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -57,7 +125,7 @@ pub mod messages {
|
||||||
&mut self,
|
&mut self,
|
||||||
message: &mut crate::api::schemas::MessageSendSchema,
|
message: &mut crate::api::schemas::MessageSendSchema,
|
||||||
channel_id: &String,
|
channel_id: &String,
|
||||||
files: Option<Vec<PartialDiscordFileAttachment>>,
|
files: Option<&'static Vec<PartialDiscordFileAttachment>>,
|
||||||
) -> Result<reqwest::Response, crate::errors::InstanceServerError> {
|
) -> Result<reqwest::Response, crate::errors::InstanceServerError> {
|
||||||
let token = self.token().clone();
|
let token = self.token().clone();
|
||||||
Message::send(
|
Message::send(
|
||||||
|
@ -75,6 +143,8 @@ pub mod messages {
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod test {
|
mod test {
|
||||||
|
use std::borrow::Cow;
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
api::{AuthUsername, LoginSchema},
|
api::{AuthUsername, LoginSchema},
|
||||||
instance::Instance,
|
instance::Instance,
|
||||||
|
@ -95,7 +165,6 @@ mod test {
|
||||||
None,
|
None,
|
||||||
None,
|
None,
|
||||||
None,
|
None,
|
||||||
None,
|
|
||||||
);
|
);
|
||||||
let mut instance = Instance::new(
|
let mut instance = Instance::new(
|
||||||
crate::URLBundle {
|
crate::URLBundle {
|
||||||
|
@ -126,4 +195,68 @@ mod test {
|
||||||
.await
|
.await
|
||||||
.unwrap();
|
.unwrap();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn send_message_two() {
|
||||||
|
let channel_id = "1104413094102290492".to_string();
|
||||||
|
|
||||||
|
let attachment = crate::api::types::PartialDiscordFileAttachment {
|
||||||
|
id: None,
|
||||||
|
filename: Some("test".to_string()),
|
||||||
|
description: None,
|
||||||
|
content_type: None,
|
||||||
|
size: None,
|
||||||
|
url: None,
|
||||||
|
proxy_url: None,
|
||||||
|
width: None,
|
||||||
|
height: None,
|
||||||
|
ephemeral: Some(false),
|
||||||
|
duration_secs: None,
|
||||||
|
waveform: None,
|
||||||
|
content: vec![8],
|
||||||
|
};
|
||||||
|
|
||||||
|
let mut message = crate::api::schemas::MessageSendSchema::new(
|
||||||
|
None,
|
||||||
|
Some("ashjkdhjksdfgjsdfzjkhsdvhjksdf".to_string()),
|
||||||
|
None,
|
||||||
|
None,
|
||||||
|
None,
|
||||||
|
None,
|
||||||
|
None,
|
||||||
|
None,
|
||||||
|
None,
|
||||||
|
Some(vec![attachment.clone()]),
|
||||||
|
);
|
||||||
|
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(&mut instance, token, limits, settings, None);
|
||||||
|
let vec_attach = vec![attachment.clone()];
|
||||||
|
let arg = Some(&vec_attach);
|
||||||
|
let response = user
|
||||||
|
.send_message(&mut message, &channel_id, arg)
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,11 +1,11 @@
|
||||||
use std::{collections::HashMap};
|
use std::collections::HashMap;
|
||||||
|
|
||||||
use regex::Regex;
|
use regex::Regex;
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
use crate::errors::FieldFormatError;
|
use crate::errors::FieldFormatError;
|
||||||
|
|
||||||
use super::{Embed};
|
use super::Embed;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
A struct that represents a well-formed email address.
|
A struct that represents a well-formed email address.
|
||||||
|
@ -257,8 +257,6 @@ pub struct MessageSendSchema {
|
||||||
message_reference: Option<super::MessageReference>,
|
message_reference: Option<super::MessageReference>,
|
||||||
components: Option<Vec<super::Component>>,
|
components: Option<Vec<super::Component>>,
|
||||||
sticker_ids: Option<Vec<String>>,
|
sticker_ids: Option<Vec<String>>,
|
||||||
#[serde(flatten)]
|
|
||||||
files: Option<HashMap<String, Vec<u8>>>,
|
|
||||||
attachments: Option<Vec<super::PartialDiscordFileAttachment>>,
|
attachments: Option<Vec<super::PartialDiscordFileAttachment>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -274,7 +272,6 @@ impl MessageSendSchema {
|
||||||
message_reference: Option<super::MessageReference>,
|
message_reference: Option<super::MessageReference>,
|
||||||
components: Option<Vec<super::Component>>,
|
components: Option<Vec<super::Component>>,
|
||||||
sticker_ids: Option<Vec<String>>,
|
sticker_ids: Option<Vec<String>>,
|
||||||
files: Option<HashMap<String, Vec<u8>>>,
|
|
||||||
attachments: Option<Vec<super::PartialDiscordFileAttachment>>,
|
attachments: Option<Vec<super::PartialDiscordFileAttachment>>,
|
||||||
) -> MessageSendSchema {
|
) -> MessageSendSchema {
|
||||||
MessageSendSchema {
|
MessageSendSchema {
|
||||||
|
@ -287,7 +284,6 @@ impl MessageSendSchema {
|
||||||
message_reference,
|
message_reference,
|
||||||
components,
|
components,
|
||||||
sticker_ids,
|
sticker_ids,
|
||||||
files,
|
|
||||||
attachments,
|
attachments,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -839,23 +839,27 @@ pub struct DiscordFileAttachment {
|
||||||
ephemeral: Option<bool>,
|
ephemeral: Option<bool>,
|
||||||
duration_secs: Option<f32>,
|
duration_secs: Option<f32>,
|
||||||
waveform: Option<String>,
|
waveform: Option<String>,
|
||||||
|
#[serde(skip_serializing)]
|
||||||
|
content: Vec<u8>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Serialize, Deserialize)]
|
#[derive(Debug, Serialize, Deserialize, Clone)]
|
||||||
|
|
||||||
pub struct PartialDiscordFileAttachment {
|
pub struct PartialDiscordFileAttachment {
|
||||||
pub id: Option<i16>,
|
pub id: Option<i16>,
|
||||||
pub filename: Option<String>,
|
pub filename: Option<String>,
|
||||||
description: Option<String>,
|
pub description: Option<String>,
|
||||||
content_type: Option<String>,
|
pub content_type: Option<String>,
|
||||||
size: Option<i64>,
|
pub size: Option<i64>,
|
||||||
url: Option<String>,
|
pub url: Option<String>,
|
||||||
proxy_url: Option<String>,
|
pub proxy_url: Option<String>,
|
||||||
height: Option<i32>,
|
pub height: Option<i32>,
|
||||||
width: Option<i32>,
|
pub width: Option<i32>,
|
||||||
ephemeral: Option<bool>,
|
pub ephemeral: Option<bool>,
|
||||||
duration_secs: Option<f32>,
|
pub duration_secs: Option<f32>,
|
||||||
waveform: Option<String>,
|
pub waveform: Option<String>,
|
||||||
|
#[serde(skip_serializing)]
|
||||||
|
pub content: Vec<u8>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Serialize, Deserialize)]
|
#[derive(Debug, Serialize, Deserialize)]
|
||||||
|
|
|
@ -18,6 +18,7 @@ custom_error! {
|
||||||
CantGetInfoError{error:String} = "Something seems to be wrong with the instance. Cannot get information about the instance: {error}",
|
CantGetInfoError{error:String} = "Something seems to be wrong with the instance. Cannot get information about the instance: {error}",
|
||||||
InvalidFormBodyError{error_type: String, error:String} = "The server responded with: {error_type}: {error}",
|
InvalidFormBodyError{error_type: String, error:String} = "The server responded with: {error_type}: {error}",
|
||||||
RateLimited = "Ratelimited.",
|
RateLimited = "Ratelimited.",
|
||||||
|
MultipartCreationError{error: String} = "Got an error whilst creating the form: {}",
|
||||||
}
|
}
|
||||||
|
|
||||||
custom_error! {
|
custom_error! {
|
||||||
|
|
Loading…
Reference in New Issue