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-26 00:23:55 +00:00
|
|
|
use rocket_dyn_templates::Template;
|
|
|
|
use serde::Serialize;
|
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;
|
|
|
|
use crate::forms;
|
|
|
|
use crate::mailer;
|
2022-10-26 00:23:55 +00:00
|
|
|
|
|
|
|
#[derive(Serialize)]
|
|
|
|
struct FormContext {
|
|
|
|
id: u16,
|
2022-10-27 16:44:40 +00:00
|
|
|
name: Option<String>,
|
|
|
|
email: Option<String>,
|
|
|
|
telefon: Option<String>,
|
|
|
|
message: Option<String>,
|
2022-10-26 00:23:55 +00:00
|
|
|
captcha: String,
|
|
|
|
}
|
|
|
|
|
2022-10-27 16:44:40 +00:00
|
|
|
#[get("/?<name>&<email>&<telefon>&<message>")]
|
2022-10-26 00:23:55 +00:00
|
|
|
pub fn index(
|
2022-10-27 16:44:40 +00:00
|
|
|
name: Option<String>,
|
|
|
|
email: Option<String>,
|
|
|
|
telefon: Option<String>,
|
|
|
|
message: Option<String>,
|
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());
|
|
|
|
|
|
|
|
let form_context = FormContext {
|
|
|
|
id,
|
2022-10-27 16:44:40 +00:00
|
|
|
name,
|
|
|
|
email,
|
|
|
|
telefon,
|
|
|
|
message,
|
2022-10-26 00:23:55 +00:00
|
|
|
captcha: captcha_base64,
|
|
|
|
};
|
|
|
|
|
|
|
|
Ok(Template::render("form", &form_context))
|
|
|
|
}
|
|
|
|
|
|
|
|
#[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
|
|
|
|
+ &uri!(index(Some(name), Some(email), Some(telefon), Some(message))).to_string(),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
match mailer.send(&name, &email, &telefon, &message) {
|
|
|
|
Ok(_) => (),
|
|
|
|
Err(_) => {
|
|
|
|
return Redirect::to(
|
|
|
|
path_prefix
|
|
|
|
+ &uri!(index(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")]
|
|
|
|
pub fn success() -> &'static str {
|
|
|
|
"Ihre Anfrage wurde übermittelt! Wir melden uns bald bei Ihnen zurück."
|
2022-10-26 00:23:55 +00:00
|
|
|
}
|