Remove failed_submission and unneeded async

This commit is contained in:
Mo 2023-02-23 21:08:36 +01:00
parent aef4a809d6
commit 0e26e36e46

View file

@ -16,6 +16,7 @@ use crate::{
templates, templates,
}; };
/// Parameters to render the contact form.
pub struct ContactFormParams<'a> { pub struct ContactFormParams<'a> {
config: Arc<Config>, config: Arc<Config>,
captcha_solutions: Arc<Mutex<CaptchaSolutions>>, captcha_solutions: Arc<Mutex<CaptchaSolutions>>,
@ -35,10 +36,9 @@ pub async fn index(
persistant_fields: None, persistant_fields: None,
error_message: None, error_message: None,
}) })
.await
} }
pub async fn render_contact_form(params: ContactFormParams<'_>) -> Result<Response, AppError> { pub fn render_contact_form(params: ContactFormParams<'_>) -> Result<Response, AppError> {
let captcha = captcha::by_name(captcha::Difficulty::Easy, captcha::CaptchaName::Lucy); let captcha = captcha::by_name(captcha::Difficulty::Easy, captcha::CaptchaName::Lucy);
let captcha_base64 = captcha.as_base64().context("Failed to create a captcha!")?; let captcha_base64 = captcha.as_base64().context("Failed to create a captcha!")?;
@ -62,22 +62,6 @@ pub async fn render_contact_form(params: ContactFormParams<'_>) -> Result<Respon
Ok(template.into_response()) Ok(template.into_response())
} }
async fn failed_submission(
config: Arc<Config>,
captcha_solutions: Arc<Mutex<CaptchaSolutions>>,
error_message: &str,
form: ContactForm,
) -> Result<Response, AppError> {
let params = ContactFormParams {
config,
captcha_solutions,
persistant_fields: Some(form.persistant_fields),
error_message: Some(error_message),
};
render_contact_form(params).await
}
pub async fn submit( pub async fn submit(
State(config): State<Arc<Config>>, State(config): State<Arc<Config>>,
State(captcha_solutions): State<Arc<Mutex<CaptchaSolutions>>>, State(captcha_solutions): State<Arc<Mutex<CaptchaSolutions>>>,
@ -92,13 +76,12 @@ pub async fn submit(
if !right_captcha_answer { if !right_captcha_answer {
info!("Wrong CAPTCHA"); info!("Wrong CAPTCHA");
return failed_submission( return render_contact_form(ContactFormParams {
Arc::clone(&config), config: Arc::clone(&config),
captcha_solutions, captcha_solutions,
&config.error_messages.captcha_error, persistant_fields: Some(form.persistant_fields),
form, error_message: Some(&config.error_messages.captcha_error),
) });
.await;
} }
match mailer match mailer
@ -114,20 +97,19 @@ pub async fn submit(
Err(e) => { Err(e) => {
error!("{e:?}"); error!("{e:?}");
return failed_submission( return render_contact_form(ContactFormParams {
Arc::clone(&config), config: Arc::clone(&config),
captcha_solutions, captcha_solutions,
&config.error_messages.email_error, persistant_fields: Some(form.persistant_fields),
form, error_message: Some(&config.error_messages.email_error),
) });
.await;
} }
} }
success(config).await success(config)
} }
pub async fn success(config: Arc<Config>) -> Result<Response, AppError> { pub fn success(config: Arc<Config>) -> Result<Response, AppError> {
info!("Successful contact form submission"); info!("Successful contact form submission");
let template = templates::Success { let template = templates::Success {