move custom_ route functions into a custom namespace

this way, we can actually use the url! macro
This commit is contained in:
Igor Galić 2019-07-14 17:14:08 +02:00 committed by Igor Galić
parent 7139119b8f
commit f635dcf6c3
No known key found for this signature in database
GPG Key ID: ACFEFF7F6A123A86
4 changed files with 77 additions and 59 deletions

View File

@ -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(

View File

@ -41,16 +41,6 @@ fn detail_guts(
.into())
}
#[get("/<custom_domain>?<page>", rank = 2)]
pub fn custom_details(
custom_domain: String,
page: Option<Page>,
rockets: PlumeRocket,
) -> Result<RespondOrRedirect, ErrorPage> {
let blog = Blog::find_by_host(&rockets, Host::new(custom_domain))?;
detail_guts(blog, page, rockets)
}
#[get("/~/<name>?<page>", 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("/<custom_domain>", rank = 1)]
pub fn custom_activity_details(
custom_domain: String,
rockets: PlumeRocket,
_ap: ApRequest,
) -> Option<ActivityStream<CustomGroup>> {
let blog = Blog::find_by_host(&rockets, Host::new(custom_domain)).ok()?;
activity_detail_guts(blog, rockets, _ap)
}
#[get("/~/<name>", 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("/<custom_domain>?<page>", rank = 2)]
pub fn details(
custom_domain: String,
page: Option<Page>,
rockets: PlumeRocket,
) -> Result<RespondOrRedirect, ErrorPage> {
let blog = Blog::find_by_host(&rockets, Host::new(custom_domain))?;
super::detail_guts(blog, page, rockets)
}
#[get("/<custom_domain>", rank = 1)]
pub fn activity_details(
custom_domain: String,
rockets: PlumeRocket,
_ap: ApRequest,
) -> Option<ActivityStream<CustomGroup>> {
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<Redirect> {
utils::requires_login(

View File

@ -83,33 +83,6 @@ fn detail_guts(
)).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,
@ -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/<custom_domain>/<slug>?<responding_to>", rank = 4)]
pub fn 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());
}
super::detail_guts(&blog, &post, responding_to, &rockets)
}
}
#[get("/~/<blog>/<slug>", rank = 3)]
pub fn activity_details(
blog: String,

View File

@ -88,11 +88,17 @@ pub fn search(query: Option<Form<SearchQuery>>, rockets: PlumeRocket) -> Ructe {
search_guts(query, rockets)
}
#[get("/<_custom_domain>/search?<query..>")]
pub fn custom_search(
_custom_domain: String,
query: Option<Form<SearchQuery>>,
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?<query..>")]
pub fn search(
_custom_domain: String,
query: Option<Form<super::SearchQuery>>,
rockets: PlumeRocket,
) -> Ructe {
super::search_guts(query, rockets)
}
}