making emails and nextcloud asynchronous
This commit is contained in:
parent
6b3b64d76f
commit
a9d2a043a6
|
@ -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)
|
||||
{
|
||||
|
|
|
@ -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,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<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}"),
|
||||
}));
|
||||
}
|
||||
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)]
|
||||
|
|
Loading…
Reference in a new issue