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(),
)
.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)
.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_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<i32, String> {
}
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<String>)> {
fn get_temp_file(&self) -> Option<(String, String, Option<String>)> {
let first = self.first_name.clone();
let last = self.last_name.clone();
let filename_noext = format!("{}_{}", first, last);
@ -312,10 +316,11 @@ impl MtForm {
filename = String::default();
String::default()
};
let file = self.file.take();
match file.unwrap().file.persist(path.clone()) {
if let Some(file) = &self.file {
let file = file.file.path();
match fs::copy(file, &path) {
Ok(f) => {
if f.metadata().unwrap().len() <= 0 {
if f <= 0 {
return None;
}
info!(?f, "File saved successfully");
@ -326,14 +331,18 @@ impl MtForm {
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 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 <no-reply@mail.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)
.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<MtForm>) -> HttpResponse {
match form.store_form().await {
pub async fn mt_form(MultipartForm(form): MultipartForm<MtForm>) -> 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}"),
}));
}
match form.send_email().await {
Ok(_) => info!("Successfully sent email"),
Err(e) => error!("There was an error sending the 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()
))
}
HttpResponse::Ok().body("thankyou")
}
#[cfg(test)]