Add a function to find the ActivityPub representation of an actor with WebFinger

This commit is contained in:
Bat 2018-04-30 20:37:19 +01:00
parent 59652e8655
commit bf54a7c4ef
2 changed files with 27 additions and 1 deletions

View File

@ -1,4 +1,7 @@
use diesel::PgConnection; use diesel::PgConnection;
use reqwest::Client;
use reqwest::header::{Accept, qitem};
use reqwest::mime::Mime;
use serde_json; use serde_json;
pub trait Webfinger { pub trait Webfinger {
@ -20,3 +23,26 @@ pub trait Webfinger {
}).to_string() }).to_string()
} }
} }
pub fn resolve(acct: String) -> Result<String, String> {
let instance = acct.split("@").next().unwrap();
let url = format!("https://{}/.well-known/webfinger?resource=acct:{}", instance, acct);
Client::new()
.get(&url[..])
.header(Accept(vec![qitem("application/jrd+json".parse::<Mime>().unwrap())]))
.send()
.map(|mut r| {
let res = r.text().unwrap();
let json: serde_json::Value = serde_json::from_str(&res[..]).unwrap();
json["links"].as_array().unwrap()
.into_iter()
.find_map(|link| {
if link["rel"].as_str().unwrap() == "self" && link["href"].as_str().unwrap() == "application/activity+json" {
Some(String::from(link["href"].as_str().unwrap()))
} else {
None
}
}).unwrap()
})
.map_err(|_| String::from("Error while fetchin WebFinger resource"))
}

View File

@ -1,4 +1,4 @@
#![feature(plugin, custom_derive)] #![feature(plugin, custom_derive, iterator_find_map)]
#![plugin(rocket_codegen)] #![plugin(rocket_codegen)]
extern crate base64; extern crate base64;