From 8ab25b1ca22a58a0abf5fdbbe277057dcbe0da48 Mon Sep 17 00:00:00 2001 From: Bat Date: Tue, 19 Jun 2018 22:20:27 +0100 Subject: [PATCH] Use uri! as much as possible instead of directly writing URLs --- src/routes/blogs.rs | 10 ++++------ src/routes/comments.rs | 2 +- src/routes/likes.rs | 4 ++-- src/routes/notifications.rs | 2 +- src/routes/posts.rs | 4 ++-- src/routes/reshares.rs | 4 ++-- src/routes/user.rs | 16 ++++++++-------- src/utils.rs | 4 ++-- 8 files changed, 22 insertions(+), 24 deletions(-) diff --git a/src/routes/blogs.rs b/src/routes/blogs.rs index 880872ad..ce53a647 100644 --- a/src/routes/blogs.rs +++ b/src/routes/blogs.rs @@ -46,7 +46,7 @@ fn new(user: User) -> Template { #[get("/blogs/new", rank = 2)] fn new_auth() -> Flash{ - utils::requires_login("You need to be logged in order to create a new blog", "/blogs/new") + utils::requires_login("You need to be logged in order to create a new blog", uri!(new)) } #[derive(FromForm)] @@ -58,10 +58,8 @@ struct NewBlogForm { fn create(conn: DbConn, data: Form, user: User) -> Redirect { let form = data.get(); let slug = utils::make_actor_id(form.title.to_string()); - if slug.len() == 0 { - return Redirect::to("/blogs/new") - } - if Blog::find_local(&*conn, slug.clone()).is_some() { + + if Blog::find_local(&*conn, slug.clone()).is_some() || slug.len() == 0 { Redirect::to(uri!(new)) } else { let blog = Blog::insert(&*conn, NewBlog::new_local( @@ -78,7 +76,7 @@ fn create(conn: DbConn, data: Form, user: User) -> Redirect { is_owner: true }); - Redirect::to(format!("/~/{}/", slug)) + Redirect::to(uri!(details: name = slug)) } } diff --git a/src/routes/comments.rs b/src/routes/comments.rs index 923795a4..49e678be 100644 --- a/src/routes/comments.rs +++ b/src/routes/comments.rs @@ -30,7 +30,7 @@ fn new(blog: String, slug: String, user: User, conn: DbConn) -> Template { #[get("/~///comment", rank=2)] fn new_auth(blog: String, slug: String) -> Flash{ - utils::requires_login("You need to be logged in order to post a comment", &format!("~/{}/{}/comment", blog, slug)) + utils::requires_login("You need to be logged in order to post a comment", uri!(new: blog = blog, slug = slug)) } #[derive(FromForm)] diff --git a/src/routes/likes.rs b/src/routes/likes.rs index 696210da..a9e4791e 100644 --- a/src/routes/likes.rs +++ b/src/routes/likes.rs @@ -32,10 +32,10 @@ fn create(blog: String, slug: String, user: User, conn: DbConn) -> Redirect { broadcast(&*conn, &user, delete_act, user.get_followers(&*conn)); } - Redirect::to(format!("/~/{}/{}/", blog, slug)) + Redirect::to(uri!(super::posts::details: blog = blog, slug = slug)) } #[get("/~///like", rank = 2)] fn create_auth(blog: String, slug: String) -> Flash{ - utils::requires_login("You need to be logged in order to like a post", &format!("/~/{}/{}/like", blog, slug)) + utils::requires_login("You need to be logged in order to like a post", uri!(create: blog = blog, slug = slug)) } diff --git a/src/routes/notifications.rs b/src/routes/notifications.rs index 6b096148..9548ad74 100644 --- a/src/routes/notifications.rs +++ b/src/routes/notifications.rs @@ -16,5 +16,5 @@ fn notifications(conn: DbConn, user: User) -> Template { #[get("/notifications", rank = 2)] fn notifications_auth() -> Flash{ - utils::requires_login("You need to be logged in order to see your notifications", "/notifications") + utils::requires_login("You need to be logged in order to see your notifications", uri!(notifications)) } diff --git a/src/routes/posts.rs b/src/routes/posts.rs index be59f407..875db015 100644 --- a/src/routes/posts.rs +++ b/src/routes/posts.rs @@ -51,7 +51,7 @@ fn activity_details(blog: String, slug: String, conn: DbConn) -> ActivityPub { #[get("/~//new", rank = 2)] fn new_auth(blog: String) -> Flash { - utils::requires_login("You need to be logged in order to write a new post", &format!("/~/{}/new",blog)) + utils::requires_login("You need to be logged in order to write a new post", uri!(new: blog = blog)) } #[get("/~//new", rank = 1)] @@ -110,6 +110,6 @@ fn create(blog_name: String, data: Form, user: User, conn: DbConn) let act = post.create_activity(&*conn); broadcast(&*conn, &user, act, user.get_followers(&*conn)); - Redirect::to(format!("/~/{}/{}/", blog_name, slug)) + Redirect::to(uri!(details: blog = blog_name, slug = slug)) } } diff --git a/src/routes/reshares.rs b/src/routes/reshares.rs index c243d851..6da07f77 100644 --- a/src/routes/reshares.rs +++ b/src/routes/reshares.rs @@ -32,10 +32,10 @@ fn create(blog: String, slug: String, user: User, conn: DbConn) -> Redirect { broadcast(&*conn, &user, delete_act, user.get_followers(&*conn)); } - Redirect::to(format!("/~/{}/{}/", blog, slug)) + Redirect::to(uri!(super::posts::details: blog = blog, slug = slug)) } #[get("/~///reshare", rank=1)] fn create_auth(blog: String, slug: String) -> Flash { - utils::requires_login("You need to be logged in order to reshare a post", &format!("/~/{}/{}/reshare",blog, slug)) + utils::requires_login("You need to be logged in order to reshare a post", uri!(create: blog = blog, slug = slug)) } diff --git a/src/routes/user.rs b/src/routes/user.rs index 6b925c87..a8d24f80 100644 --- a/src/routes/user.rs +++ b/src/routes/user.rs @@ -27,8 +27,8 @@ use utils; #[get("/me")] fn me(user: Option) -> Result> { match user { - Some(user) => Ok(Redirect::to(format!("/@/{}/", user.username))), - None => Err(utils::requires_login("", "/me")) + Some(user) => Ok(Redirect::to(uri!(details: name = user.username))), + None => Err(utils::requires_login("", uri!(me))) } } @@ -65,7 +65,7 @@ fn dashboard(user: User, conn: DbConn) -> Template { #[get("/dashboard", rank = 2)] fn dashboard_auth() -> Flash { - utils::requires_login("You need to be logged in order to access your dashboard", "/dashboard") + utils::requires_login("You need to be logged in order to access your dashboard", uri!(dashboard)) } #[get("/@//follow")] @@ -82,12 +82,12 @@ fn follow(name: String, conn: DbConn, user: User) -> Redirect { follows::Follow::notify(&*conn, act.clone(), user.clone().into_id()); broadcast(&*conn, &user, act, vec![target]); - Redirect::to(format!("/@/{}/", name)) + Redirect::to(uri!(details: name = name)) } #[get("/@//follow", rank = 2)] fn follow_auth(name: String) -> Flash { - utils::requires_login("You need to be logged in order to follow someone", &format!("/@/{}/follow", name)) + utils::requires_login("You need to be logged in order to follow someone", uri!(follow: name = name)) } #[get("/@//followers", rank = 2)] @@ -134,7 +134,7 @@ fn edit(name: String, user: User) -> Option