custom_domainify posts::details

This commit is contained in:
Igor Galić 2019-07-10 17:34:45 +02:00 committed by Igor Galić
parent fe110b5d8a
commit f73fba583a
No known key found for this signature in database
GPG Key ID: ACFEFF7F6A123A86
2 changed files with 80 additions and 21 deletions

View File

@ -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,
],

View File

@ -31,28 +31,14 @@ use routes::{
};
use template_utils::{IntoContext, Ructe};
#[get("/~/<blog>/<slug>?<responding_to>", rank = 4)]
pub fn details(
blog: String,
slug: String,
fn detail_guts(
blog: &Blog,
post: &Post,
responding_to: Option<i32>,
rockets: PlumeRocket,
) -> Result<Ructe, ErrorPage> {
rockets: &PlumeRocket,
) -> Result<RespondOrRedirect, ErrorPage> {
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/<custom_domain>/<slug>?<responding_to>", rank = 4)]
pub fn custom_details(
custom_domain: String,
slug: String,
responding_to: Option<i32>,
rockets: PlumeRocket,
) -> Result<RespondOrRedirect, ErrorPage> {
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("/~/<blog>/<slug>?<responding_to>", rank = 4)]
pub fn details(
blog: String,
slug: String,
responding_to: Option<i32>,
rockets: PlumeRocket,
) -> Result<RespondOrRedirect, ErrorPage> {
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("/~/<blog>/<slug>", rank = 3)]