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,
)
.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 {
use http::header::CONTENT_DISPOSITION;
use http::{header, HeaderMap};
use http::HeaderMap;
use reqwest::{multipart, Client};
use serde_json::to_string;
@ -24,7 +24,7 @@ pub mod messages {
url_api: &String,
channel_id: &String,
message: &mut crate::api::schemas::MessageSendSchema,
files: Option<&'static Vec<PartialDiscordFileAttachment>>,
files: Option<Vec<PartialDiscordFileAttachment>>,
token: &String,
user: &mut User<'a>,
) -> Result<reqwest::Response, crate::errors::InstanceServerError> {
@ -61,36 +61,30 @@ pub mod messages {
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 content_disposition = format!(
"form-data; name=\"{}\"'; filename=\"{}\"",
part_name,
attachment
.get(index)
.unwrap()
.filename
.as_deref()
.unwrap_or("file")
part_name, &attachment_filename
);
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"),
)
let mut part = multipart::Part::bytes(attachment_content)
.file_name(attachment_filename)
.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,
Err(e) => {
return Err(InstanceServerError::MultipartCreationError {
@ -115,7 +109,6 @@ pub mod messages {
user_rate_limits,
)
.await
// TODO: Deallocate the darn memory leak!
}
}
}
@ -125,7 +118,7 @@ pub mod messages {
&mut self,
message: &mut crate::api::schemas::MessageSendSchema,
channel_id: &String,
files: Option<&'static Vec<PartialDiscordFileAttachment>>,
files: Option<Vec<PartialDiscordFileAttachment>>,
) -> Result<reqwest::Response, crate::errors::InstanceServerError> {
let token = self.token().clone();
Message::send(
@ -143,8 +136,6 @@ pub mod messages {
#[cfg(test)]
mod test {
use std::borrow::Cow;
use crate::{
api::{AuthUsername, LoginSchema},
instance::Instance,
@ -190,7 +181,7 @@ mod test {
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 response = user
let _ = user
.send_message(&mut message, &channel_id, None)
.await
.unwrap();
@ -202,7 +193,7 @@ mod test {
let attachment = crate::api::types::PartialDiscordFileAttachment {
id: None,
filename: Some("test".to_string()),
filename: "test".to_string(),
description: None,
content_type: None,
size: None,
@ -253,9 +244,9 @@ mod test {
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)
let _arg = Some(&vec_attach);
let _response = user
.send_message(&mut message, &channel_id, Some(vec![attachment.clone()]))
.await
.unwrap();
}

View File

@ -51,6 +51,6 @@ mod instance_policies_schema_test {
.await
.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 serde::{Deserialize, Serialize};

View File

@ -847,7 +847,7 @@ pub struct DiscordFileAttachment {
pub struct PartialDiscordFileAttachment {
pub id: Option<i16>,
pub filename: Option<String>,
pub filename: String,
pub description: Option<String>,
pub content_type: Option<String>,
pub size: Option<i64>,
@ -862,6 +862,73 @@ pub struct PartialDiscordFileAttachment {
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)]
pub struct AllowedMention {
parse: Vec<AllowedMentionType>,

View File

@ -329,6 +329,6 @@ mod rate_limit {
Ok(result) => result,
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();
}
}