2018-04-24 13:38:43 +02:00
|
|
|
use rocket::http::ContentType;
|
|
|
|
use rocket::response::Content;
|
|
|
|
|
2018-05-02 13:53:42 +02:00
|
|
|
use BASE_URL;
|
2018-05-02 14:47:46 +02:00
|
|
|
use activity_pub::ap_url;
|
2018-04-24 11:21:39 +02:00
|
|
|
use activity_pub::webfinger::Webfinger;
|
2018-04-24 10:35:45 +02:00
|
|
|
use db_conn::DbConn;
|
|
|
|
use models::blogs::Blog;
|
2018-04-24 11:21:39 +02:00
|
|
|
use models::users::User;
|
2018-04-24 10:35:45 +02:00
|
|
|
|
|
|
|
#[get("/.well-known/host-meta", format = "application/xml")]
|
2018-05-02 13:53:42 +02:00
|
|
|
fn host_meta() -> String {
|
2018-04-24 10:35:45 +02:00
|
|
|
format!(r#"
|
|
|
|
<?xml version="1.0"?>
|
|
|
|
<XRD xmlns="http://docs.oasis-open.org/ns/xri/xrd-1.0">
|
2018-05-02 14:47:46 +02:00
|
|
|
<Link rel="lrdd" type="application/xrd+xml" template="{url}"/>
|
2018-04-24 10:35:45 +02:00
|
|
|
</XRD>
|
2018-05-02 14:47:46 +02:00
|
|
|
"#, url = ap_url(format!("{domain}/.well-known/webfinger?resource={{uri}}", domain = BASE_URL.as_str())))
|
2018-04-24 10:35:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(FromForm)]
|
|
|
|
struct WebfingerQuery {
|
|
|
|
resource: String
|
|
|
|
}
|
|
|
|
|
2018-04-24 13:38:43 +02:00
|
|
|
#[get("/.well-known/webfinger?<query>")]
|
|
|
|
fn webfinger(query: WebfingerQuery, conn: DbConn) -> Content<Result<String, &'static str>> {
|
2018-05-02 15:05:26 +02:00
|
|
|
let mut parsed_query = query.resource.splitn(2, ":");
|
2018-04-24 10:35:45 +02:00
|
|
|
let res_type = parsed_query.next().unwrap();
|
|
|
|
let res = parsed_query.next().unwrap();
|
|
|
|
if res_type == "acct" {
|
|
|
|
let mut parsed_res = res.split("@");
|
|
|
|
let user = parsed_res.next().unwrap();
|
|
|
|
let res_dom = parsed_res.next().unwrap();
|
|
|
|
|
2018-05-02 13:53:42 +02:00
|
|
|
if res_dom == BASE_URL.as_str() {
|
2018-05-01 13:48:19 +02:00
|
|
|
let res = match User::find_local(&*conn, String::from(user)) {
|
2018-04-24 13:38:43 +02:00
|
|
|
Some(usr) => Ok(usr.webfinger(&*conn)),
|
2018-04-24 10:35:45 +02:00
|
|
|
None => match Blog::find_by_actor_id(&*conn, String::from(user)) {
|
2018-04-24 13:38:43 +02:00
|
|
|
Some(blog) => Ok(blog.webfinger(&*conn)),
|
2018-04-24 10:35:45 +02:00
|
|
|
None => Err("Requested actor not found")
|
|
|
|
}
|
2018-04-24 13:38:43 +02:00
|
|
|
};
|
|
|
|
Content(ContentType::new("application", "jrd+json"), res)
|
2018-04-24 10:35:45 +02:00
|
|
|
} else {
|
2018-04-24 13:38:43 +02:00
|
|
|
Content(ContentType::new("text", "plain"), Err("Invalid instance"))
|
2018-04-24 10:35:45 +02:00
|
|
|
}
|
|
|
|
} else {
|
2018-04-24 13:38:43 +02:00
|
|
|
Content(ContentType::new("text", "plain"), Err("Invalid resource type. Only acct is supported"))
|
2018-04-24 10:35:45 +02:00
|
|
|
}
|
|
|
|
}
|