redirect blog urls to custom_domain if it exists

This commit is contained in:
Igor Galić 2019-06-14 19:28:31 +02:00 committed by Igor Galić
parent 9cee38ae6a
commit cc0df4ecb2
No known key found for this signature in database
GPG Key ID: ACFEFF7F6A123A86
3 changed files with 40 additions and 7 deletions

View File

@ -32,7 +32,7 @@ use {Connection, Error, PlumeRocket, Result};
pub type CustomGroup = CustomObject<ApSignature, Group>; pub type CustomGroup = CustomObject<ApSignature, Group>;
#[derive(Clone, Debug, DieselNewType, Shrinkwrap)] #[derive(Clone, Debug, PartialEq, DieselNewType, Shrinkwrap)]
pub struct Host(String); pub struct Host(String);
impl Host { impl Host {

View File

@ -19,7 +19,11 @@ use plume_models::{
use routes::{errors::ErrorPage, Page, RespondOrRedirect}; use routes::{errors::ErrorPage, Page, RespondOrRedirect};
use template_utils::{IntoContext, Ructe}; use template_utils::{IntoContext, Ructe};
fn detail_guts(blog: Blog, page: Option<Page>, rockets: PlumeRocket) -> Result<Ructe, ErrorPage> { fn detail_guts(
blog: Blog,
page: Option<Page>,
rockets: PlumeRocket,
) -> Result<RespondOrRedirect, ErrorPage> {
let page = page.unwrap_or_default(); let page = page.unwrap_or_default();
let conn = &*rockets.conn; let conn = &*rockets.conn;
let posts = Post::blog_page(conn, &blog, page.limits())?; let posts = Post::blog_page(conn, &blog, page.limits())?;
@ -33,7 +37,8 @@ fn detail_guts(blog: Blog, page: Option<Page>, rockets: PlumeRocket) -> Result<R
page.0, page.0,
Page::total(articles_count as i32), Page::total(articles_count as i32),
posts posts
))) ))
.into())
} }
#[get("/<custom_domain>?<page>", rank = 2)] #[get("/<custom_domain>?<page>", rank = 2)]
@ -41,15 +46,36 @@ pub fn custom_details(
custom_domain: String, custom_domain: String,
page: Option<Page>, page: Option<Page>,
rockets: PlumeRocket, rockets: PlumeRocket,
) -> Result<Ructe, ErrorPage> { ) -> Result<RespondOrRedirect, ErrorPage> {
let blog = Blog::find_by_host(&rockets, Host::new(custom_domain))?; let blog = Blog::find_by_host(&rockets, Host::new(custom_domain))?;
detail_guts(blog, page, rockets) detail_guts(blog, page, rockets)
} }
#[get("/~/<name>?<page>", rank = 2)] #[get("/~/<name>?<page>", rank = 2)]
pub fn details(name: String, page: Option<Page>, rockets: PlumeRocket) -> Result<Ructe, ErrorPage> { pub fn details(
name: String,
page: Option<Page>,
rockets: PlumeRocket,
) -> Result<RespondOrRedirect, ErrorPage> {
let blog = Blog::find_by_fqn(&rockets, &name)?; 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( pub fn activity_detail_guts(

View File

@ -10,6 +10,7 @@ use rocket::{
response::{Flash, NamedFile, Redirect}, response::{Flash, NamedFile, Redirect},
Outcome, Outcome,
}; };
use std::fmt;
use std::path::{Path, PathBuf}; use std::path::{Path, PathBuf};
use template_utils::Ructe; use template_utils::Ructe;
@ -52,9 +53,15 @@ impl From<Flash<Redirect>> for RespondOrRedirect {
} }
} }
#[derive(Shrinkwrap, Copy, Clone, UriDisplayQuery)] #[derive(Debug, Shrinkwrap, Copy, Clone, UriDisplayQuery)]
pub struct Page(i32); 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 { impl<'v> FromFormValue<'v> for Page {
type Error = &'v RawStr; type Error = &'v RawStr;
fn from_form_value(form_value: &'v RawStr) -> Result<Page, &'v RawStr> { fn from_form_value(form_value: &'v RawStr) -> Result<Page, &'v RawStr> {