From 468e6633440188307dbb89812f5b842199ba9489 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Igor=20Gali=C4=87?= Date: Mon, 27 May 2019 17:55:09 +0200 Subject: [PATCH] Add custom_details and custom_activity_details as first routes how this works: we use find_by_host() to find the Host in question, the defer to the existing function! Caveat: Currently, we, in that function, we do another lookup DB lookup for the Blog, even thou we already know it. It might be wise, to have both of those another wrapper here?! --- plume-models/src/blogs.rs | 17 +++++++++++++++++ po/plume/plume.pot | 18 +++++++++--------- src/main.rs | 7 +++++++ src/routes/blogs.rs | 20 ++++++++++++++++++++ 4 files changed, 53 insertions(+), 9 deletions(-) diff --git a/plume-models/src/blogs.rs b/plume-models/src/blogs.rs index dab6bbcc..f2ea8235 100644 --- a/plume-models/src/blogs.rs +++ b/plume-models/src/blogs.rs @@ -195,6 +195,23 @@ impl Blog { } } + pub fn find_by_host(c: &PlumeRocket, host: Host) -> Result { + let from_db = blogs::table + .filter(blogs::custom_domain.eq(host)) + .limit(1) + .load::(&*c.conn)? + .into_iter() + .next(); + if let Some(from_db) = from_db { + Ok(from_db) + } else { + // we should never get here, because + // a) load::()? should return early if it fails + // b) this function is only called after a Request::guard::() + Err(Error::NotFound) + } + } + fn fetch_from_webfinger(c: &PlumeRocket, acct: &str) -> Result { resolve_with_prefix(Prefix::Group, acct.to_owned(), true)? .links diff --git a/po/plume/plume.pot b/po/plume/plume.pot index 8e55c82e..751273b7 100644 --- a/po/plume/plume.pot +++ b/po/plume/plume.pot @@ -36,39 +36,39 @@ msgstr "" msgid "{0}'s avatar" msgstr "" -# src/routes/blogs.rs:64 +# src/routes/blogs.rs:84 msgid "To create a new blog, you need to be logged in" msgstr "" -# src/routes/blogs.rs:106 +# src/routes/blogs.rs:126 msgid "A blog with the same name already exists." msgstr "" -# src/routes/blogs.rs:141 +# src/routes/blogs.rs:161 msgid "Your blog was successfully created!" msgstr "" -# src/routes/blogs.rs:163 +# src/routes/blogs.rs:183 msgid "Your blog was deleted." msgstr "" -# src/routes/blogs.rs:170 +# src/routes/blogs.rs:190 msgid "You are not allowed to delete this blog." msgstr "" -# src/routes/blogs.rs:218 +# src/routes/blogs.rs:238 msgid "You are not allowed to edit this blog." msgstr "" -# src/routes/blogs.rs:263 +# src/routes/blogs.rs:283 msgid "You can't use this media as a blog icon." msgstr "" -# src/routes/blogs.rs:281 +# src/routes/blogs.rs:301 msgid "You can't use this media as a blog banner." msgstr "" -# src/routes/blogs.rs:314 +# src/routes/blogs.rs:334 msgid "Your blog information have been updated." msgstr "" diff --git a/src/main.rs b/src/main.rs index f44ed66e..c0fd2577 100644 --- a/src/main.rs +++ b/src/main.rs @@ -197,6 +197,13 @@ Then try to restart Plume }); let rocket = rocket::custom(CONFIG.rocket.clone().unwrap()) + .mount( + "/custom_domains/", + routes![ + routes::blogs::custom_details, + routes::blogs::custom_activity_details, + ], + ) .mount( "/", routes![ diff --git a/src/routes/blogs.rs b/src/routes/blogs.rs index 7ab9e2b3..86f387ab 100644 --- a/src/routes/blogs.rs +++ b/src/routes/blogs.rs @@ -19,6 +19,16 @@ use plume_models::{ use routes::{errors::ErrorPage, Page, RespondOrRedirect}; use template_utils::{IntoContext, Ructe}; +#[get("/?", rank = 2)] +pub fn custom_details( + custom_domain: String, + page: Option, + rockets: PlumeRocket, +) -> Result { + let blog = Blog::find_by_host(&rockets, Host::new(custom_domain))?; + details(blog.fqn, page, rockets) +} + #[get("/~/?", rank = 2)] pub fn details(name: String, page: Option, rockets: PlumeRocket) -> Result { let page = page.unwrap_or_default(); @@ -38,6 +48,16 @@ pub fn details(name: String, page: Option, rockets: PlumeRocket) -> Result ))) } +#[get("/", rank = 1)] +pub fn custom_activity_details( + custom_domain: String, + rockets: PlumeRocket, + _ap: ApRequest, +) -> Option> { + let blog = Blog::find_by_host(&rockets, Host::new(custom_domain)).ok()?; + activity_details(blog.fqn, rockets, _ap) +} + #[get("/~/", rank = 1)] pub fn activity_details( name: String,