From f635dcf6c3dc7847e967a117d5f12a42bcb5c7fa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Igor=20Gali=C4=87?= Date: Sun, 14 Jul 2019 17:14:08 +0200 Subject: [PATCH] move custom_ route functions into a custom namespace this way, we can actually use the url! macro --- src/main.rs | 8 +++--- src/routes/blogs.rs | 48 +++++++++++++++++++---------------- src/routes/posts.rs | 60 ++++++++++++++++++++++++-------------------- src/routes/search.rs | 20 +++++++++------ 4 files changed, 77 insertions(+), 59 deletions(-) diff --git a/src/main.rs b/src/main.rs index d99a4e2f..f1c903dd 100644 --- a/src/main.rs +++ b/src/main.rs @@ -200,10 +200,10 @@ Then try to restart Plume .mount( "/custom_domains/", routes![ - routes::blogs::custom_details, - routes::posts::custom_details, - routes::blogs::custom_activity_details, - routes::search::custom_search, + routes::blogs::custom::details, + routes::posts::custom::details, + routes::blogs::custom::activity_details, + routes::search::custom::search, ], ) .mount( diff --git a/src/routes/blogs.rs b/src/routes/blogs.rs index 41251a3c..b5edf6c5 100644 --- a/src/routes/blogs.rs +++ b/src/routes/blogs.rs @@ -41,16 +41,6 @@ fn detail_guts( .into()) } -#[get("/?", rank = 2)] -pub fn custom_details( - custom_domain: String, - page: Option, - rockets: PlumeRocket, -) -> Result { - let blog = Blog::find_by_host(&rockets, Host::new(custom_domain))?; - detail_guts(blog, page, rockets) -} - #[get("/~/?", rank = 2)] pub fn details( name: String, @@ -74,7 +64,7 @@ pub fn details( Ok(Redirect::to(format!("https://{}/", custom_domain)).into()) } // we need this match arm, or the match won't compile - (None, _) => panic!("This code path should have already been handled!"), + (None, _) => unreachable!("This code path should have already been handled!"), } } @@ -86,16 +76,6 @@ pub fn activity_detail_guts( Some(ActivityStream::new(blog.to_activity(&*rockets.conn).ok()?)) } -#[get("/", rank = 1)] -pub fn custom_activity_details( - custom_domain: String, - rockets: PlumeRocket, - _ap: ApRequest, -) -> Option> { - let blog = Blog::find_by_host(&rockets, Host::new(custom_domain)).ok()?; - activity_detail_guts(blog, rockets, _ap) -} - #[get("/~/", rank = 1)] pub fn activity_details( name: String, @@ -115,6 +95,32 @@ pub fn new(rockets: PlumeRocket, _user: User) -> Ructe { )) } +pub mod custom { + use plume_common::activity_pub::{ActivityStream, ApRequest}; + use plume_models::{blogs::Blog, blogs::CustomGroup, blogs::Host, PlumeRocket}; + use routes::{errors::ErrorPage, Page, RespondOrRedirect}; + + #[get("/?", rank = 2)] + pub fn details( + custom_domain: String, + page: Option, + rockets: PlumeRocket, + ) -> Result { + let blog = Blog::find_by_host(&rockets, Host::new(custom_domain))?; + super::detail_guts(blog, page, rockets) + } + + #[get("/", rank = 1)] + pub fn activity_details( + custom_domain: String, + rockets: PlumeRocket, + _ap: ApRequest, + ) -> Option> { + let blog = Blog::find_by_host(&rockets, Host::new(custom_domain)).ok()?; + super::activity_detail_guts(blog, rockets, _ap) + } +} + #[get("/blogs/new", rank = 2)] pub fn new_auth(i18n: I18n) -> Flash { utils::requires_login( diff --git a/src/routes/posts.rs b/src/routes/posts.rs index f8ecc1c2..0c1be74e 100644 --- a/src/routes/posts.rs +++ b/src/routes/posts.rs @@ -83,33 +83,6 @@ fn detail_guts( )).into()) } -#[get("/custom_domains//?", rank = 4)] -pub fn custom_details( - custom_domain: String, - slug: String, - responding_to: Option, - rockets: PlumeRocket, -) -> Result { - let conn = &*rockets.conn; - let user = rockets.user.clone(); - let blog = Blog::find_by_host(&rockets, Host::new(custom_domain))?; - let post = Post::find_by_slug(&*conn, &slug, blog.id)?; - if !(post.published - || post - .get_authors(&*conn)? - .into_iter() - .any(|a| a.id == user.clone().map(|u| u.id).unwrap_or(0))) - { - return Ok(render!(errors::not_authorized( - &rockets.to_context(), - i18n!(rockets.intl.catalog, "This post isn't published yet.") - )) - .into()); - } - - detail_guts(&blog, &post, responding_to, &rockets) -} - #[get("/~//?", rank = 4)] pub fn details( blog: String, @@ -155,6 +128,39 @@ pub fn details( } } +pub mod custom { + use plume_models::{blogs::Blog, blogs::Host, posts::Post, PlumeRocket}; + use routes::{errors::ErrorPage, RespondOrRedirect}; + use template_utils::{IntoContext, Ructe}; + + #[get("/custom_domains//?", rank = 4)] + pub fn details( + custom_domain: String, + slug: String, + responding_to: Option, + rockets: PlumeRocket, + ) -> Result { + let conn = &*rockets.conn; + let user = rockets.user.clone(); + let blog = Blog::find_by_host(&rockets, Host::new(custom_domain))?; + let post = Post::find_by_slug(&*conn, &slug, blog.id)?; + if !(post.published + || post + .get_authors(&*conn)? + .into_iter() + .any(|a| a.id == user.clone().map(|u| u.id).unwrap_or(0))) + { + return Ok(render!(errors::not_authorized( + &rockets.to_context(), + i18n!(rockets.intl.catalog, "This post isn't published yet.") + )) + .into()); + } + + super::detail_guts(&blog, &post, responding_to, &rockets) + } +} + #[get("/~//", rank = 3)] pub fn activity_details( blog: String, diff --git a/src/routes/search.rs b/src/routes/search.rs index 11d921d7..4df7ac77 100644 --- a/src/routes/search.rs +++ b/src/routes/search.rs @@ -88,11 +88,17 @@ pub fn search(query: Option>, rockets: PlumeRocket) -> Ructe { search_guts(query, rockets) } -#[get("/<_custom_domain>/search?")] -pub fn custom_search( - _custom_domain: String, - query: Option>, - rockets: PlumeRocket, -) -> Ructe { - search_guts(query, rockets) +pub mod custom { + use plume_models::PlumeRocket; + use rocket::request::Form; + use template_utils::Ructe; + + #[get("/<_custom_domain>/search?")] + pub fn search( + _custom_domain: String, + query: Option>, + rockets: PlumeRocket, + ) -> Ructe { + super::search_guts(query, rockets) + } }