making emails and nextcloud asynchronous

This commit is contained in:
Chris Cochrun 2024-12-24 00:31:25 -06:00
parent 6b3b64d76f
commit a9d2a043a6
2 changed files with 73 additions and 32 deletions

View file

@ -339,7 +339,7 @@ impl HealthForm {
.unwrap(), .unwrap(),
) )
.to("Chris Cochrun <chris@tfcconnection.org>".parse().unwrap()) .to("Chris Cochrun <chris@tfcconnection.org>".parse().unwrap())
.to("Ethan Rose <ethan@tfcconnection.org>".parse().unwrap()) // .to("Ethan Rose <ethan@tfcconnection.org>".parse().unwrap())
.subject(email_subject) .subject(email_subject)
.multipart(multi) .multipart(multi)
{ {

View file

@ -1,8 +1,12 @@
use std::{collections::HashMap, fs}; use std::{collections::HashMap, fs, io::Write};
use actix_multipart::form::{tempfile::TempFile, text::Text, MultipartForm}; use actix_multipart::form::{tempfile::TempFile, text::Text, MultipartForm};
use actix_web::{post, HttpResponse}; use actix_web::{post, HttpResponse};
use color_eyre::{eyre::eyre, Result}; use color_eyre::{
eyre::{eyre, Context},
Result,
};
use futures::FutureExt;
use lettre::{ use lettre::{
message::{header::ContentType, Attachment, MultiPart, SinglePart}, message::{header::ContentType, Attachment, MultiPart, SinglePart},
Message, Message,
@ -113,7 +117,7 @@ impl From<&MtForm> for HashMap<i32, String> {
} }
impl MtForm { impl MtForm {
async fn build_email(&self) -> Markup { fn build_email(&self) -> Markup {
html! { html! {
(DOCTYPE) (DOCTYPE)
meta charset="utf-8"; meta charset="utf-8";
@ -289,7 +293,7 @@ impl MtForm {
} }
} }
fn get_temp_file(&mut self) -> Option<(String, String, Option<String>)> { fn get_temp_file(&self) -> Option<(String, String, Option<String>)> {
let first = self.first_name.clone(); let first = self.first_name.clone();
let last = self.last_name.clone(); let last = self.last_name.clone();
let filename_noext = format!("{}_{}", first, last); let filename_noext = format!("{}_{}", first, last);
@ -312,28 +316,33 @@ impl MtForm {
filename = String::default(); filename = String::default();
String::default() String::default()
}; };
let file = self.file.take(); if let Some(file) = &self.file {
match file.unwrap().file.persist(path.clone()) { let file = file.file.path();
Ok(f) => { match fs::copy(file, &path) {
if f.metadata().unwrap().len() <= 0 { Ok(f) => {
return None; if f <= 0 {
return None;
}
info!(?f, "File saved successfully");
Some((filename, path, content_type.clone()))
}
Err(e) => {
error!("{:?}: Probably a missing image", e);
None
} }
info!(?f, "File saved successfully");
Some((filename, path, content_type.clone()))
}
Err(e) => {
error!("{:?}: Probably a missing image", e);
None
} }
} else {
error!("Error in tempfile");
None
} }
} }
async fn send_email(&mut self) -> Result<()> { fn send_email(&self) -> Result<Message> {
let first = self.first_name.clone(); let first = self.first_name.clone();
let last = self.last_name.clone(); let last = self.last_name.clone();
let email_subject = format!("{} {} signed up for mission trip!", first, last); let email_subject = format!("{} {} signed up for mission trip!", first, last);
info!("{first} {last} signed up for mission trip!"); info!("{first} {last} signed up for mission trip!");
let email = self.build_email().await; let email = self.build_email();
let temp_file = self.get_temp_file(); let temp_file = self.get_temp_file();
let multi = if let Some((file, path, content_type)) = temp_file { let multi = if let Some((file, path, content_type)) = temp_file {
let filebody = fs::read(path); let filebody = fs::read(path);
@ -348,35 +357,67 @@ impl MtForm {
MultiPart::alternative_plain_html(String::from("Testing"), email.into_string()) MultiPart::alternative_plain_html(String::from("Testing"), email.into_string())
}; };
if let Ok(m) = Message::builder() Message::builder()
.from( .from(
"TFC ADMIN <no-reply@mail.tfcconnection.org>" "TFC ADMIN <no-reply@mail.tfcconnection.org>"
.parse() .parse()
.unwrap(), .unwrap(),
) )
.to("Chris Cochrun <chris@tfcconnection.org>".parse().unwrap()) .to("Chris Cochrun <chris@tfcconnection.org>".parse().unwrap())
.to("Ethan Rose <ethan@tfcconnection.org>".parse().unwrap()) // .to("Ethan Rose <ethan@tfcconnection.org>".parse().unwrap())
.subject(email_subject) .subject(email_subject)
.multipart(multi) .multipart(multi)
{ .wrap_err("problemss")
crate::email::send_email(m).await
} else {
Err(eyre!("Email incorrect"))
}
} }
} }
#[post("/api/mt-form")] #[post("/api/mt-form")]
pub async fn mt_form(MultipartForm(mut form): MultipartForm<MtForm>) -> HttpResponse { pub async fn mt_form(MultipartForm(form): MultipartForm<MtForm>) -> HttpResponse {
match form.store_form().await { let name = format!("{} {}", form.first_name.0, form.last_name.0);
let map = HashMap::from(&form);
let store = store_form(map);
actix_rt::spawn(store.map(|s| match s {
Ok(_) => info!("Successfully sent form to nextcloud!"), Ok(_) => info!("Successfully sent form to nextcloud!"),
Err(e) => error!("There was an erroring sending form to nextcloud: {e}"), Err(e) => error!("There was an erroring sending form to nextcloud: {e}"),
}));
let email = form.send_email();
match email {
Ok(m) => {
let sent = crate::email::send_email(m);
actix_rt::spawn(sent.map(|s| match s {
Ok(_) => info!("Successfully sent form to email!"),
Err(e) => error!("There was an erroring sending form to email: {e}"),
}));
}
Err(e) => error!("error sending email {e}"),
};
HttpResponse::Ok().body(format!("Thank you {}, you can ", name))
}
async fn store_form(map: HashMap<i32, String>) -> Result<()> {
let client = Client::new();
// let map = HashMap::from(self);
let mut json = HashMap::new();
json.insert("data", map);
let link = r#"https://staff.tfcconnection.org/apps/tables/#/table/9/row/757"#;
let res = client
.post("https://staff.tfcconnection.org/ocs/v2.php/apps/tables/api/2/tables/9/rows")
.basic_auth("chris", Some("2VHeGxeC^Zf9KqFK^G@Pt!zu2q^6@b"))
.header("OCS-APIRequest", "true")
.header("Content-Type", "application/json")
.json(&json)
.send()
.await?;
if res.status().is_success() {
let res = res.text().await.unwrap();
Ok(())
} else {
Err(eyre!(
"Problem in storing data: {:?}",
res.error_for_status()
))
} }
match form.send_email().await {
Ok(_) => info!("Successfully sent email"),
Err(e) => error!("There was an error sending the email: {e}"),
}
HttpResponse::Ok().body("thankyou")
} }
#[cfg(test)] #[cfg(test)]