From a9d2a043a6f50f9a87422f07672536afd4041bf7 Mon Sep 17 00:00:00 2001 From: Chris Cochrun Date: Tue, 24 Dec 2024 00:31:25 -0600 Subject: [PATCH] making emails and nextcloud asynchronous --- src/api/health_form.rs | 2 +- src/api/mt_form.rs | 103 ++++++++++++++++++++++++++++------------- 2 files changed, 73 insertions(+), 32 deletions(-) diff --git a/src/api/health_form.rs b/src/api/health_form.rs index 5f5f589..ffacc35 100644 --- a/src/api/health_form.rs +++ b/src/api/health_form.rs @@ -339,7 +339,7 @@ impl HealthForm { .unwrap(), ) .to("Chris Cochrun ".parse().unwrap()) - .to("Ethan Rose ".parse().unwrap()) + // .to("Ethan Rose ".parse().unwrap()) .subject(email_subject) .multipart(multi) { diff --git a/src/api/mt_form.rs b/src/api/mt_form.rs index 94ab807..355bb6d 100644 --- a/src/api/mt_form.rs +++ b/src/api/mt_form.rs @@ -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_web::{post, HttpResponse}; -use color_eyre::{eyre::eyre, Result}; +use color_eyre::{ + eyre::{eyre, Context}, + Result, +}; +use futures::FutureExt; use lettre::{ message::{header::ContentType, Attachment, MultiPart, SinglePart}, Message, @@ -113,7 +117,7 @@ impl From<&MtForm> for HashMap { } impl MtForm { - async fn build_email(&self) -> Markup { + fn build_email(&self) -> Markup { html! { (DOCTYPE) meta charset="utf-8"; @@ -289,7 +293,7 @@ impl MtForm { } } - fn get_temp_file(&mut self) -> Option<(String, String, Option)> { + fn get_temp_file(&self) -> Option<(String, String, Option)> { let first = self.first_name.clone(); let last = self.last_name.clone(); let filename_noext = format!("{}_{}", first, last); @@ -312,28 +316,33 @@ impl MtForm { filename = String::default(); String::default() }; - let file = self.file.take(); - match file.unwrap().file.persist(path.clone()) { - Ok(f) => { - if f.metadata().unwrap().len() <= 0 { - return None; + if let Some(file) = &self.file { + let file = file.file.path(); + match fs::copy(file, &path) { + Ok(f) => { + 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 { let first = self.first_name.clone(); let last = self.last_name.clone(); let email_subject = format!("{} {} signed up for mission trip!", first, last); 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 multi = if let Some((file, path, content_type)) = temp_file { let filebody = fs::read(path); @@ -348,35 +357,67 @@ impl MtForm { MultiPart::alternative_plain_html(String::from("Testing"), email.into_string()) }; - if let Ok(m) = Message::builder() + Message::builder() .from( "TFC ADMIN " .parse() .unwrap(), ) .to("Chris Cochrun ".parse().unwrap()) - .to("Ethan Rose ".parse().unwrap()) + // .to("Ethan Rose ".parse().unwrap()) .subject(email_subject) .multipart(multi) - { - crate::email::send_email(m).await - } else { - Err(eyre!("Email incorrect")) - } + .wrap_err("problemss") } } #[post("/api/mt-form")] -pub async fn mt_form(MultipartForm(mut form): MultipartForm) -> HttpResponse { - match form.store_form().await { +pub async fn mt_form(MultipartForm(form): MultipartForm) -> HttpResponse { + 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!"), 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) -> 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)]