Actually validate forms
This commit is contained in:
+23
-8
@@ -4,7 +4,7 @@ use rocket::request::LenientForm;
|
||||
use rocket::response::{Redirect, Flash};
|
||||
use rocket_contrib::Template;
|
||||
use serde_json;
|
||||
use validator::{Validate, ValidationError};
|
||||
use validator::{Validate, ValidationError, ValidationErrors};
|
||||
|
||||
use plume_common::activity_pub::{broadcast, ActivityStream};
|
||||
use plume_common::utils;
|
||||
@@ -94,22 +94,32 @@ fn valid_slug(title: &str) -> Result<(), ValidationError> {
|
||||
let slug = title.to_string().to_kebab_case();
|
||||
if slug.len() == 0 {
|
||||
Err(ValidationError::new("empty_slug"))
|
||||
} else if slug == "new" {
|
||||
Err(ValidationError::new("invalid_slug"))
|
||||
} else {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
#[post("/~/<blog_name>/new", data = "<data>")]
|
||||
fn create(blog_name: String, data: LenientForm<NewPostForm>, user: User, conn: DbConn) -> Redirect {
|
||||
fn create(blog_name: String, data: LenientForm<NewPostForm>, user: User, conn: DbConn) -> Result<Redirect, Template> {
|
||||
let blog = Blog::find_by_fqn(&*conn, blog_name.to_string()).unwrap();
|
||||
let form = data.get();
|
||||
let slug = form.title.to_string().to_kebab_case();
|
||||
let slug_taken_err = Blog::find_local(&*conn, slug.clone()).ok_or(ValidationError::new("existing_slug"));
|
||||
|
||||
let mut errors = match form.validate() {
|
||||
Ok(_) => ValidationErrors::new(),
|
||||
Err(e) => e
|
||||
};
|
||||
if let Err(e) = slug_taken_err {
|
||||
errors.add("title", e)
|
||||
}
|
||||
|
||||
if !user.is_author_in(&*conn, blog.clone()) {
|
||||
Redirect::to(uri!(super::blogs::details: name = blog_name))
|
||||
} else {
|
||||
if slug == "new" || Post::find_by_slug(&*conn, slug.clone(), blog.id).is_some() {
|
||||
Redirect::to(uri!(new: blog = blog_name))
|
||||
if errors.is_empty() {
|
||||
if !user.is_author_in(&*conn, blog.clone()) {
|
||||
// actually it's not "Ok"…
|
||||
Ok(Redirect::to(uri!(super::blogs::details: name = blog_name)))
|
||||
} else {
|
||||
let (content, mentions) = utils::md_to_html(form.content.to_string().as_ref());
|
||||
|
||||
@@ -135,7 +145,12 @@ fn create(blog_name: String, data: LenientForm<NewPostForm>, user: User, conn: D
|
||||
let act = post.create_activity(&*conn);
|
||||
broadcast(&user, act, user.get_followers(&*conn));
|
||||
|
||||
Redirect::to(uri!(details: blog = blog_name, slug = slug))
|
||||
Ok(Redirect::to(uri!(details: blog = blog_name, slug = slug)))
|
||||
}
|
||||
} else {
|
||||
Err(Template::render("posts/new", json!({
|
||||
"account": user,
|
||||
"errors": errors.inner()
|
||||
})))
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user