change message send impl

This commit is contained in:
bitfl0wer 2023-05-11 22:36:35 +02:00
parent a2819c13f0
commit ce46452a2f
2 changed files with 42 additions and 29 deletions

View File

@ -24,11 +24,10 @@ pub mod messages {
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> {
let file_attachments_static: &'static [PartialDiscordFileAttachment];
let mut requester = LimitedRequester::new().await; let mut requester = LimitedRequester::new().await;
let user_rate_limits = &mut user.limits; let user_rate_limits = &mut user.limits;
let instance_rate_limits = &mut user.belongs_to.limits; let instance_rate_limits = &mut user.belongs_to.limits;
@ -62,37 +61,47 @@ pub mod messages {
form = form.part("payload_json", payload_field); form = form.part("payload_json", payload_field);
if let Some(file_attachments) = files { for (index, attachment) in files.iter().enumerate() {
let boxed_attachments = file_attachments.into_boxed_slice(); let part_name = format!("files[{}]", index);
file_attachments_static = Box::leak(boxed_attachments); let content_disposition = format!(
for (index, attachment) in file_attachments_static.iter().enumerate() { "form-data; name=\"{}\"'; filename=\"{}\"",
let part_name = format!("files[{}]", index); part_name,
let content_disposition = format!( attachment
"form-data; name=\"{}\"'; filename=\"{}\"", .get(index)
part_name, .unwrap()
attachment.filename.as_deref().unwrap_or("file") .filename
); .as_deref()
let mut header_map = HeaderMap::new(); .unwrap_or("file")
header_map );
.insert(CONTENT_DISPOSITION, content_disposition.parse().unwrap()) let mut header_map = HeaderMap::new();
.unwrap(); header_map
.insert(CONTENT_DISPOSITION, content_disposition.parse().unwrap())
.unwrap();
let mut part = multipart::Part::bytes(Vec::new()) let mut part =
.file_name(attachment.filename.as_deref().unwrap_or("file")) 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); .headers(header_map);
part = match part.mime_str("application/octet-stream") { part = match part.mime_str("application/octet-stream") {
Ok(part) => part, Ok(part) => part,
Err(e) => { Err(e) => {
return Err(InstanceServerError::MultipartCreationError { return Err(InstanceServerError::MultipartCreationError {
error: e.to_string(), error: e.to_string(),
}) })
} }
}; };
form = form.part(part_name, part); form = form.part(part_name, part);
}
} }
let message_request = Client::new() let message_request = Client::new()
.post(format!("{}/channels/{}/messages/", url_api, channel_id)) .post(format!("{}/channels/{}/messages/", url_api, channel_id))
.bearer_auth(token) .bearer_auth(token)
@ -116,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(

View File

@ -839,6 +839,8 @@ 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)]
@ -856,6 +858,8 @@ pub struct PartialDiscordFileAttachment {
ephemeral: Option<bool>, ephemeral: Option<bool>,
duration_secs: Option<f32>, duration_secs: Option<f32>,
waveform: Option<String>, waveform: Option<String>,
#[serde(skip_serializing)]
pub content: Vec<u8>,
} }
#[derive(Debug, Serialize, Deserialize)] #[derive(Debug, Serialize, Deserialize)]