From 19c6ec1ff7c4a85614ab0117653e163891613316 Mon Sep 17 00:00:00 2001 From: Chris Cochrun Date: Tue, 24 Dec 2024 22:26:01 -0600 Subject: [PATCH] making health forms more asynchronous --- src/api/health_form.rs | 30 ++++++++++++++++++------------ 1 file changed, 18 insertions(+), 12 deletions(-) diff --git a/src/api/health_form.rs b/src/api/health_form.rs index ffacc35..7b17b8f 100644 --- a/src/api/health_form.rs +++ b/src/api/health_form.rs @@ -2,7 +2,11 @@ use std::{collections::HashMap, fs}; 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, @@ -110,7 +114,7 @@ impl From<&HealthForm> for HashMap { } impl HealthForm { - async fn build_email(&self) -> Markup { + fn build_email(&self) -> Markup { html! { (DOCTYPE) meta charset="utf-8"; @@ -312,12 +316,12 @@ impl HealthForm { } } - async fn send_email(&mut self) -> Result<()> { + fn send_email(&mut self) -> Result { let first = self.first_name.clone(); let last = self.last_name.clone(); let email_subject = format!("{} {} filled out a health form!", first, last); info!("{first} {last} filled out a health form!"); - 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); @@ -332,7 +336,7 @@ impl HealthForm { MultiPart::alternative_plain_html(String::from("Testing"), email.into_string()) }; - if let Ok(m) = Message::builder() + Message::builder() .from( "TFC ADMIN " .parse() @@ -342,19 +346,21 @@ impl HealthForm { // .to("Ethan Rose ".parse().unwrap()) .subject(email_subject) .multipart(multi) - { - send_email(m).await - } else { - Err(eyre!("Email incorrect")) - } + .wrap_err("Email incorrect") } } #[post("/api/health-form")] pub async fn health_form(MultipartForm(mut form): MultipartForm) -> HttpResponse { info!("Starting health form work: {:?}", form); - match form.send_email().await { - Ok(_) => info!("Successfully sent email health form"), + match form.send_email() { + Ok(m) => { + actix_rt::spawn(send_email(m).map(|r| match r { + Ok(_) => info!("Email sent successfully"), + Err(e) => error!("There was an erroring sending form to nextcloud: {e}"), + })); + info!("Successfully sent email health form") + } Err(e) => error!("There was an error sending email: {e}"), }