diff --git a/plume-models/src/blogs.rs b/plume-models/src/blogs.rs index 7fd91aa3..20c63f18 100644 --- a/plume-models/src/blogs.rs +++ b/plume-models/src/blogs.rs @@ -7,6 +7,10 @@ use openssl::{ rsa::Rsa, sign::{Signer, Verifier}, }; +use rocket::{ + outcome::IntoOutcome, + request::{self, FromRequest, Request}, +}; use serde_json; use url::Url; use webfinger::*; @@ -21,11 +25,45 @@ use posts::Post; use safe_string::SafeString; use schema::blogs; use search::Searcher; +use std::fmt; use users::User; use {Connection, Error, PlumeRocket, Result}; pub type CustomGroup = CustomObject; +#[derive(Queryable, Clone)] +pub struct Host(String); + +impl Host { + pub fn new>(host: T) -> Host { + Host(host.into()) + } +} + +impl Into for Host { + fn into(self) -> String { + self.0.clone() + } +} + +impl From for Host { + fn from(s: String) -> Host { + Host::new(s) + } +} + +impl AsRef for Host { + fn as_ref(&self) -> &str { + &self.0 + } +} + +impl std::fmt::Display for Host { + fn fmt(&self, f: &mut std::fmt::Formatter) -> fmt::Result { + write!(f, "{}", self.0) + } +} + #[derive(Queryable, Identifiable, Clone, AsChangeset)] #[changeset_options(treat_none_as_null = "true")] pub struct Blog { @@ -294,6 +332,24 @@ impl Blog { } } +impl<'a, 'r> FromRequest<'a, 'r> for Host { + type Error = (); + + fn from_request(request: &'a Request<'r>) -> request::Outcome { + request + .headers() + .get_one("Host") + .and_then(|x| { + if x != Instance::get_local().ok()?.public_domain { + Some(Host(x.to_string())) + } else { + None + } + }) + .or_forward(()) + } +} + impl IntoId for Blog { fn into_id(self) -> Id { Id::new(self.ap_url)