2018-04-24 11:38:43 +00:00
|
|
|
use rocket::http::ContentType;
|
|
|
|
use rocket::response::Content;
|
2018-06-18 21:50:40 +00:00
|
|
|
use serde_json;
|
|
|
|
use webfinger::*;
|
2018-04-24 11:38:43 +00:00
|
|
|
|
2018-05-02 11:53:42 +00:00
|
|
|
use BASE_URL;
|
2018-06-18 21:50:40 +00:00
|
|
|
use activity_pub::ap_url;
|
2018-04-24 08:35:45 +00:00
|
|
|
use db_conn::DbConn;
|
2018-05-19 07:39:59 +00:00
|
|
|
use models::{blogs::Blog, users::User};
|
2018-04-24 08:35:45 +00:00
|
|
|
|
2018-06-10 19:33:42 +00:00
|
|
|
#[get("/.well-known/nodeinfo")]
|
|
|
|
fn nodeinfo() -> Content<String> {
|
|
|
|
Content(ContentType::new("application", "jrd+json"), json!({
|
|
|
|
"links": [
|
|
|
|
{
|
|
|
|
"rel": "http://nodeinfo.diaspora.software/ns/schema/2.0",
|
|
|
|
"href": ap_url(format!("{domain}/nodeinfo", domain = BASE_URL.as_str()))
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}).to_string())
|
|
|
|
}
|
|
|
|
|
2018-04-24 08:35:45 +00:00
|
|
|
#[get("/.well-known/host-meta", format = "application/xml")]
|
2018-05-02 11:53:42 +00:00
|
|
|
fn host_meta() -> String {
|
2018-04-24 08:35:45 +00:00
|
|
|
format!(r#"
|
|
|
|
<?xml version="1.0"?>
|
|
|
|
<XRD xmlns="http://docs.oasis-open.org/ns/xri/xrd-1.0">
|
2018-05-02 12:47:46 +00:00
|
|
|
<Link rel="lrdd" type="application/xrd+xml" template="{url}"/>
|
2018-04-24 08:35:45 +00:00
|
|
|
</XRD>
|
2018-05-02 12:47:46 +00:00
|
|
|
"#, url = ap_url(format!("{domain}/.well-known/webfinger?resource={{uri}}", domain = BASE_URL.as_str())))
|
2018-04-24 08:35:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(FromForm)]
|
|
|
|
struct WebfingerQuery {
|
|
|
|
resource: String
|
|
|
|
}
|
|
|
|
|
2018-06-18 21:50:40 +00:00
|
|
|
struct WebfingerResolver;
|
|
|
|
|
|
|
|
impl Resolver<DbConn> for WebfingerResolver {
|
|
|
|
fn instance_domain<'a>() -> &'a str {
|
|
|
|
BASE_URL.as_str()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn find(acct: String, conn: DbConn) -> Result<Webfinger, ResolverError> {
|
|
|
|
match User::find_local(&*conn, acct.clone()) {
|
|
|
|
Some(usr) => Ok(usr.webfinger(&*conn)),
|
|
|
|
None => match Blog::find_local(&*conn, acct) {
|
|
|
|
Some(blog) => Ok(blog.webfinger(&*conn)),
|
|
|
|
None => Err(ResolverError::NotFound)
|
|
|
|
}
|
2018-04-24 08:35:45 +00:00
|
|
|
}
|
2018-06-18 21:50:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[get("/.well-known/webfinger?<query>")]
|
|
|
|
fn webfinger(query: WebfingerQuery, conn: DbConn) -> Content<String> {
|
|
|
|
match WebfingerResolver::endpoint(query.resource, conn).and_then(|wf| serde_json::to_string(&wf).map_err(|_| ResolverError::NotFound)) {
|
|
|
|
Ok(wf) => Content(ContentType::new("application", "jrd+json"), wf),
|
|
|
|
Err(err) => Content(ContentType::new("text", "plain"), String::from(match err {
|
|
|
|
ResolverError::InvalidResource => "Invalid resource. Make sure to request an acct: URI",
|
|
|
|
ResolverError::NotFound => "Requested resource was not found",
|
|
|
|
ResolverError::WrongInstance => "This is not the instance of the requested resource"
|
|
|
|
}))
|
2018-04-24 08:35:45 +00:00
|
|
|
}
|
|
|
|
}
|