contact-form/src/routes.rs

98 lines
2.7 KiB
Rust
Raw Normal View History

2022-10-28 22:42:15 +00:00
use rocket::form::{Form, Strict};
2022-10-26 00:23:55 +00:00
use rocket::response::status::BadRequest;
2022-10-27 16:44:40 +00:00
use rocket::response::Redirect;
use rocket::{get, post, uri, State};
2022-10-29 15:13:12 +00:00
use rocket_dyn_templates::{context, Template};
2022-10-27 16:44:40 +00:00
use std::mem;
2022-10-26 00:23:55 +00:00
2022-10-28 22:42:15 +00:00
use crate::captcha_solutions;
use crate::config;
2022-10-29 15:13:12 +00:00
use crate::context;
2022-10-28 22:42:15 +00:00
use crate::forms;
use crate::mailer;
2022-10-26 00:23:55 +00:00
2022-10-29 15:13:12 +00:00
#[get("/?<was_validated>&<name>&<email>&<telefon>&<message>")]
2022-10-26 00:23:55 +00:00
pub fn index(
2022-10-29 15:13:12 +00:00
was_validated: Option<bool>,
2022-10-28 23:07:43 +00:00
name: Option<&str>,
email: Option<&str>,
telefon: Option<&str>,
message: Option<&str>,
config: &State<config::Config>,
2022-10-28 22:42:15 +00:00
captcha_solutions: &State<captcha_solutions::SharedCaptchaSolutions>,
2022-10-26 00:23:55 +00:00
) -> Result<Template, BadRequest<()>> {
let captcha = captcha::by_name(captcha::Difficulty::Easy, captcha::CaptchaName::Lucy);
let captcha_base64 = match captcha.as_base64() {
Some(s) => s,
None => return Err(BadRequest(None)),
};
let id = captcha_solutions.store_solution(&captcha.chars_as_string());
2022-10-29 15:13:12 +00:00
let form_context = context::FormContext::new(
&config.path_prefix,
was_validated,
2022-10-26 00:23:55 +00:00
id,
2022-10-27 16:44:40 +00:00
name,
email,
telefon,
message,
2022-10-29 15:13:12 +00:00
&captcha_base64,
);
2022-10-26 00:23:55 +00:00
2022-10-29 15:13:12 +00:00
Ok(Template::render("form", form_context))
2022-10-26 00:23:55 +00:00
}
#[post("/submit", data = "<form>")]
pub fn submit(
2022-10-28 22:42:15 +00:00
mut form: Form<Strict<forms::ContactForm>>,
config: &State<config::Config>,
captcha_solutions: &State<captcha_solutions::SharedCaptchaSolutions>,
mailer: &State<mailer::Mailer>,
2022-10-27 16:44:40 +00:00
) -> Redirect {
2022-10-28 22:42:15 +00:00
let path_prefix = config.path_prefix.clone();
let name = mem::take(&mut form.name);
let email = mem::take(&mut form.email);
let telefon = mem::take(&mut form.telefon);
let message = mem::take(&mut form.message);
2022-10-26 00:23:55 +00:00
2022-10-27 16:44:40 +00:00
if !captcha_solutions.check_answer(form.id, &form.captcha_answer) {
2022-10-28 22:42:15 +00:00
return Redirect::to(
path_prefix
2022-10-28 23:07:43 +00:00
+ &uri!(index(
2022-10-29 15:13:12 +00:00
Some(true),
2022-10-28 23:07:43 +00:00
Some(&name),
Some(&email),
Some(&telefon),
Some(&message)
))
.to_string(),
2022-10-28 22:42:15 +00:00
);
}
match mailer.send(&name, &email, &telefon, &message) {
Ok(_) => (),
Err(_) => {
return Redirect::to(
path_prefix
2022-10-28 23:07:43 +00:00
+ &uri!(index(
2022-10-29 15:13:12 +00:00
Some(true),
2022-10-28 23:07:43 +00:00
Some(&name),
Some(&email),
Some(&telefon),
Some(&message)
))
.to_string(),
2022-10-27 16:44:40 +00:00
)
2022-10-28 22:42:15 +00:00
}
2022-10-27 16:44:40 +00:00
}
2022-10-28 22:42:15 +00:00
Redirect::to(path_prefix + &uri!(success()).to_string())
2022-10-27 16:44:40 +00:00
}
#[get("/success")]
2022-10-29 15:13:12 +00:00
pub fn success() -> Template {
Template::render("success", context! {})
2022-10-26 00:23:55 +00:00
}