From cc0df4ecb2ca6766d69260177feb758e32868e81 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Igor=20Gali=C4=87?= Date: Fri, 14 Jun 2019 19:28:31 +0200 Subject: [PATCH] redirect blog urls to custom_domain if it exists --- plume-models/src/blogs.rs | 2 +- src/routes/blogs.rs | 36 +++++++++++++++++++++++++++++++----- src/routes/mod.rs | 9 ++++++++- 3 files changed, 40 insertions(+), 7 deletions(-) diff --git a/plume-models/src/blogs.rs b/plume-models/src/blogs.rs index a81b88f9..a52dad57 100644 --- a/plume-models/src/blogs.rs +++ b/plume-models/src/blogs.rs @@ -32,7 +32,7 @@ use {Connection, Error, PlumeRocket, Result}; pub type CustomGroup = CustomObject; -#[derive(Clone, Debug, DieselNewType, Shrinkwrap)] +#[derive(Clone, Debug, PartialEq, DieselNewType, Shrinkwrap)] pub struct Host(String); impl Host { diff --git a/src/routes/blogs.rs b/src/routes/blogs.rs index 60c57783..bdcb1c55 100644 --- a/src/routes/blogs.rs +++ b/src/routes/blogs.rs @@ -19,7 +19,11 @@ use plume_models::{ use routes::{errors::ErrorPage, Page, RespondOrRedirect}; use template_utils::{IntoContext, Ructe}; -fn detail_guts(blog: Blog, page: Option, rockets: PlumeRocket) -> Result { +fn detail_guts( + blog: Blog, + page: Option, + rockets: PlumeRocket, +) -> Result { let page = page.unwrap_or_default(); let conn = &*rockets.conn; let posts = Post::blog_page(conn, &blog, page.limits())?; @@ -33,7 +37,8 @@ fn detail_guts(blog: Blog, page: Option, rockets: PlumeRocket) -> Result?", rank = 2)] @@ -41,15 +46,36 @@ pub fn custom_details( custom_domain: String, page: Option, rockets: PlumeRocket, -) -> Result { +) -> 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, page: Option, rockets: PlumeRocket) -> Result { +pub fn details( + name: String, + page: Option, + rockets: PlumeRocket, +) -> Result { let blog = Blog::find_by_fqn(&rockets, &name)?; - detail_guts(blog, page, rockets) + + // 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, page, rockets); + } + + match (blog.custom_domain, page) { + (Some(ref custom_domain), Some(ref page)) => { + Ok(Redirect::to(format!("https://{}/?{}", custom_domain, page)).into()) + } + (Some(ref custom_domain), _) => { + 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!"), + } } pub fn activity_detail_guts( diff --git a/src/routes/mod.rs b/src/routes/mod.rs index c29ab10f..b3438a7f 100644 --- a/src/routes/mod.rs +++ b/src/routes/mod.rs @@ -10,6 +10,7 @@ use rocket::{ response::{Flash, NamedFile, Redirect}, Outcome, }; +use std::fmt; use std::path::{Path, PathBuf}; use template_utils::Ructe; @@ -52,9 +53,15 @@ impl From> for RespondOrRedirect { } } -#[derive(Shrinkwrap, Copy, Clone, UriDisplayQuery)] +#[derive(Debug, Shrinkwrap, Copy, Clone, UriDisplayQuery)] pub struct Page(i32); +impl fmt::Display for Page { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + write!(f, "{}", self.0) + } +} + impl<'v> FromFormValue<'v> for Page { type Error = &'v RawStr; fn from_form_value(form_value: &'v RawStr) -> Result {