From f73fba583aadab15d6719dd73c009e5b728237a3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Igor=20Gali=C4=87?= Date: Wed, 10 Jul 2019 17:34:45 +0200 Subject: [PATCH] custom_domainify posts::details --- src/main.rs | 1 + src/routes/posts.rs | 100 ++++++++++++++++++++++++++++++++++---------- 2 files changed, 80 insertions(+), 21 deletions(-) diff --git a/src/main.rs b/src/main.rs index 9c30c7b8..d99a4e2f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -201,6 +201,7 @@ Then try to restart Plume "/custom_domains/", routes![ routes::blogs::custom_details, + routes::posts::custom_details, routes::blogs::custom_activity_details, routes::search::custom_search, ], diff --git a/src/routes/posts.rs b/src/routes/posts.rs index c78fe43c..45420785 100644 --- a/src/routes/posts.rs +++ b/src/routes/posts.rs @@ -31,28 +31,14 @@ use routes::{ }; use template_utils::{IntoContext, Ructe}; -#[get("/~//?", rank = 4)] -pub fn details( - blog: String, - slug: String, +fn detail_guts( + blog: &Blog, + post: &Post, responding_to: Option, - rockets: PlumeRocket, -) -> Result { + rockets: &PlumeRocket, +) -> Result { let conn = &*rockets.conn; let user = rockets.user.clone(); - let blog = Blog::find_by_fqn(&rockets, &blog)?; - 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.") - ))); - } let comments = CommentTree::from_post(&*conn, &post, user.as_ref())?; @@ -61,7 +47,7 @@ pub fn details( Ok(render!(posts::details( &rockets.to_context(), post.clone(), - blog, + blog.clone(), &NewCommentForm { warning: previous.clone().map(|p| p.spoiler_text).unwrap_or_default(), content: previous.clone().and_then(|p| Some(format!( @@ -94,7 +80,79 @@ pub fn details( user.clone().and_then(|u| u.has_reshared(&*conn, &post).ok()).unwrap_or(false), user.and_then(|u| u.is_following(&*conn, post.get_authors(&*conn).ok()?[0].id).ok()).unwrap_or(false), post.get_authors(&*conn)?[0].clone() - ))) + )).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, + slug: String, + responding_to: Option, + rockets: PlumeRocket, +) -> Result { + let conn = &*rockets.conn; + let user = rockets.user.clone(); + let blog = Blog::find_by_fqn(&rockets, &blog)?; + 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()); + } + + // check this first, and return early + // doing this prevents partially moving `blog` into the `match (tuple)`, + // which makes it impossible to reuse then. + if blog.custom_domain == None { + return detail_guts(&blog, &post, responding_to, &rockets); + } + + match (blog.custom_domain, responding_to) { + (Some(ref custom_domain), Some(ref responding_to)) => Ok(Redirect::to(format!( + "https://{}/?responding_to={}", + custom_domain, responding_to + )) + .into()), + (Some(ref custom_domain), _) => { + Ok(Redirect::to(format!("https://{}/", custom_domain)).into()) + } + (None, _) => panic!("This code path should have already been handled!"), + } } #[get("/~//", rank = 3)]