From 92bbeeb45e28e17f2b24162b7e390dc517efe922 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Igor=20Gali=C4=87?= Date: Fri, 24 May 2019 14:30:38 +0100 Subject: [PATCH] add Host(String) wrapper type we can use this to handle rocket Requests (from_request) but we still have to figure out how to automatically assign the type to custom_domain: Optional, and public_domain: Host. --- plume-models/src/blogs.rs | 56 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) 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)