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<Host>, and public_domain: Host.
This commit is contained in:
Igor Galić 2019-05-24 14:30:38 +01:00 committed by Igor Galić
parent 6bcc4f0ab0
commit 92bbeeb45e
No known key found for this signature in database
GPG Key ID: ACFEFF7F6A123A86

View File

@ -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<ApSignature, Group>;
#[derive(Queryable, Clone)]
pub struct Host(String);
impl Host {
pub fn new<T: Into<String>>(host: T) -> Host {
Host(host.into())
}
}
impl Into<String> for Host {
fn into(self) -> String {
self.0.clone()
}
}
impl From<String> for Host {
fn from(s: String) -> Host {
Host::new(s)
}
}
impl AsRef<str> 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<Host, ()> {
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)