From bf54a7c4efef7faa8acf6110b733553d51856404 Mon Sep 17 00:00:00 2001 From: Bat Date: Mon, 30 Apr 2018 20:37:19 +0100 Subject: [PATCH] Add a function to find the ActivityPub representation of an actor with WebFinger --- src/activity_pub/webfinger.rs | 26 ++++++++++++++++++++++++++ src/main.rs | 2 +- 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/src/activity_pub/webfinger.rs b/src/activity_pub/webfinger.rs index 00754dbd..2149f2e3 100644 --- a/src/activity_pub/webfinger.rs +++ b/src/activity_pub/webfinger.rs @@ -1,4 +1,7 @@ use diesel::PgConnection; +use reqwest::Client; +use reqwest::header::{Accept, qitem}; +use reqwest::mime::Mime; use serde_json; pub trait Webfinger { @@ -20,3 +23,26 @@ pub trait Webfinger { }).to_string() } } + +pub fn resolve(acct: String) -> Result { + 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::().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")) +} diff --git a/src/main.rs b/src/main.rs index a72b2621..82586157 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,4 +1,4 @@ -#![feature(plugin, custom_derive)] +#![feature(plugin, custom_derive, iterator_find_map)] #![plugin(rocket_codegen)] extern crate base64;