feat(handler): Perform basic input validation on new thread view

This commit is contained in:
Vincent Ambo 2018-04-12 01:07:25 +02:00
parent f46f6f3c42
commit e7a54a5aff
4 changed files with 39 additions and 11 deletions

View file

@ -141,7 +141,15 @@ impl Handler<ThreadPage> for Renderer {
}
/// Message used to render new thread page.
pub struct NewThreadPage;
///
/// It can optionally contain a vector of warnings to display to the
/// user in alert boxes, such as input validation errors.
#[derive(Default)]
pub struct NewThreadPage {
pub alerts: Vec<&'static str>,
pub title: Option<String>,
pub body: Option<String>,
}
impl Message for NewThreadPage {
type Result = Result<String>;
@ -150,7 +158,11 @@ impl Message for NewThreadPage {
impl Handler<NewThreadPage> for Renderer {
type Result = Result<String>;
fn handle(&mut self, _: NewThreadPage, _: &mut Self::Context) -> Self::Result {
Ok(self.tera.render("new-thread.html", &Context::new())?)
fn handle(&mut self, msg: NewThreadPage, _: &mut Self::Context) -> Self::Result {
let mut ctx = Context::new();
ctx.add("alerts", &msg.alerts);
ctx.add("title", &msg.title.map(|s| escape_html(&s)));
ctx.add("body", &msg.body.map(|s| escape_html(&s)));
Ok(self.tera.render("new-thread.html", &ctx)?)
}
}