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

fix send() function errors
This commit is contained in:
Flori 2023-05-12 13:08:57 +02:00 committed by GitHub
commit 0fc5e19767
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 94 additions and 36 deletions

View File

@ -109,6 +109,6 @@ mod test {
None, None,
) )
.unwrap(); .unwrap();
let token = test_instance.register_account(&reg).await.unwrap().token; let _ = test_instance.register_account(&reg).await.unwrap().token;
} }
} }

View File

@ -1,6 +1,6 @@
pub mod messages { pub mod messages {
use http::header::CONTENT_DISPOSITION; use http::header::CONTENT_DISPOSITION;
use http::{header, HeaderMap}; use http::HeaderMap;
use reqwest::{multipart, Client}; use reqwest::{multipart, Client};
use serde_json::to_string; use serde_json::to_string;
@ -24,7 +24,7 @@ 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<&'static Vec<PartialDiscordFileAttachment>>, files: Option<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> {
@ -61,36 +61,30 @@ pub mod messages {
form = form.part("payload_json", payload_field); form = form.part("payload_json", payload_field);
for (index, attachment) in files.iter().enumerate() { for (index, attachment) in files.unwrap().into_iter().enumerate() {
let (attachment_content, current_attachment) = attachment.move_content();
let (attachment_filename, current_attachment) =
current_attachment.move_filename();
let (attachment_content_type, _) = current_attachment.move_content_type();
let part_name = format!("files[{}]", index); let part_name = format!("files[{}]", index);
let content_disposition = format!( let content_disposition = format!(
"form-data; name=\"{}\"'; filename=\"{}\"", "form-data; name=\"{}\"'; filename=\"{}\"",
part_name, part_name, &attachment_filename
attachment
.get(index)
.unwrap()
.filename
.as_deref()
.unwrap_or("file")
); );
let mut header_map = HeaderMap::new(); let mut header_map = HeaderMap::new();
header_map header_map
.insert(CONTENT_DISPOSITION, content_disposition.parse().unwrap()) .insert(CONTENT_DISPOSITION, content_disposition.parse().unwrap())
.unwrap(); .unwrap();
let mut part = let mut part = multipart::Part::bytes(attachment_content)
multipart::Part::bytes(attachment.get(index).unwrap().content.as_slice()) .file_name(attachment_filename)
.file_name( .headers(header_map);
attachment
.get(index)
.unwrap()
.filename
.as_deref()
.unwrap_or("file"),
)
.headers(header_map);
part = match part.mime_str("application/octet-stream") { part = match part.mime_str(
attachment_content_type
.unwrap_or("application/octet-stream".to_string())
.as_str(),
) {
Ok(part) => part, Ok(part) => part,
Err(e) => { Err(e) => {
return Err(InstanceServerError::MultipartCreationError { return Err(InstanceServerError::MultipartCreationError {
@ -115,7 +109,6 @@ pub mod messages {
user_rate_limits, user_rate_limits,
) )
.await .await
// TODO: Deallocate the darn memory leak!
} }
} }
} }
@ -125,7 +118,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<&'static Vec<PartialDiscordFileAttachment>>, files: Option<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(
@ -143,8 +136,6 @@ 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,
@ -190,7 +181,7 @@ mod test {
let settings = login_result.settings; let settings = login_result.settings;
let limits = instance.limits.clone(); let limits = instance.limits.clone();
let mut user = crate::api::types::User::new(&mut instance, token, limits, settings, None); let mut user = crate::api::types::User::new(&mut instance, token, limits, settings, None);
let response = user let _ = user
.send_message(&mut message, &channel_id, None) .send_message(&mut message, &channel_id, None)
.await .await
.unwrap(); .unwrap();
@ -202,7 +193,7 @@ mod test {
let attachment = crate::api::types::PartialDiscordFileAttachment { let attachment = crate::api::types::PartialDiscordFileAttachment {
id: None, id: None,
filename: Some("test".to_string()), filename: "test".to_string(),
description: None, description: None,
content_type: None, content_type: None,
size: None, size: None,
@ -253,9 +244,9 @@ mod test {
let limits = instance.limits.clone(); let limits = instance.limits.clone();
let mut user = crate::api::types::User::new(&mut instance, token, limits, settings, None); let mut user = crate::api::types::User::new(&mut instance, token, limits, settings, None);
let vec_attach = vec![attachment.clone()]; let vec_attach = vec![attachment.clone()];
let arg = Some(&vec_attach); let _arg = Some(&vec_attach);
let response = user let _response = user
.send_message(&mut message, &channel_id, arg) .send_message(&mut message, &channel_id, Some(vec![attachment.clone()]))
.await .await
.unwrap(); .unwrap();
} }

View File

@ -51,6 +51,6 @@ mod instance_policies_schema_test {
.await .await
.unwrap(); .unwrap();
let schema = test_instance.instance_policies_schema().await.unwrap(); let _schema = test_instance.instance_policies_schema().await.unwrap();
} }
} }

View File

@ -1,4 +1,4 @@
use std::collections::HashMap;
use regex::Regex; use regex::Regex;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};

View File

@ -847,7 +847,7 @@ pub struct DiscordFileAttachment {
pub struct PartialDiscordFileAttachment { pub struct PartialDiscordFileAttachment {
pub id: Option<i16>, pub id: Option<i16>,
pub filename: Option<String>, pub filename: String,
pub description: Option<String>, pub description: Option<String>,
pub content_type: Option<String>, pub content_type: Option<String>,
pub size: Option<i64>, pub size: Option<i64>,
@ -862,6 +862,73 @@ pub struct PartialDiscordFileAttachment {
pub content: Vec<u8>, pub content: Vec<u8>,
} }
impl PartialDiscordFileAttachment {
/**
Moves `self.content` out of `self` and returns it.
# Returns
Vec<u8>
*/
pub fn move_content(self) -> (Vec<u8>, PartialDiscordFileAttachment) {
let content = self.content;
let updated_struct = PartialDiscordFileAttachment {
id: self.id,
filename: self.filename,
description: self.description,
content_type: self.content_type,
size: self.size,
url: self.url,
proxy_url: self.proxy_url,
height: self.height,
width: self.width,
ephemeral: self.ephemeral,
duration_secs: self.duration_secs,
waveform: self.waveform,
content: Vec::new(),
};
(content, updated_struct)
}
pub fn move_filename(self) -> (String, PartialDiscordFileAttachment) {
let filename = self.filename;
let updated_struct = PartialDiscordFileAttachment {
id: self.id,
filename: String::new(),
description: self.description,
content_type: self.content_type,
size: self.size,
url: self.url,
proxy_url: self.proxy_url,
height: self.height,
width: self.width,
ephemeral: self.ephemeral,
duration_secs: self.duration_secs,
waveform: self.waveform,
content: self.content,
};
(filename, updated_struct)
}
pub fn move_content_type(self) -> (Option<String>, PartialDiscordFileAttachment) {
let content_type = self.content_type;
let updated_struct = PartialDiscordFileAttachment {
id: self.id,
filename: self.filename,
description: self.description,
content_type: None,
size: self.size,
url: self.url,
proxy_url: self.proxy_url,
height: self.height,
width: self.width,
ephemeral: self.ephemeral,
duration_secs: self.duration_secs,
waveform: self.waveform,
content: self.content,
};
(content_type, updated_struct)
}
}
#[derive(Debug, Serialize, Deserialize)] #[derive(Debug, Serialize, Deserialize)]
pub struct AllowedMention { pub struct AllowedMention {
parse: Vec<AllowedMentionType>, parse: Vec<AllowedMentionType>,

View File

@ -329,6 +329,6 @@ mod rate_limit {
Ok(result) => result, Ok(result) => result,
Err(_) => panic!("Request failed"), Err(_) => panic!("Request failed"),
}; };
let config: Config = from_str(result.text().await.unwrap().as_str()).unwrap(); let _config: Config = from_str(result.text().await.unwrap().as_str()).unwrap();
} }
} }