Use the webfinger crate for fetching resources too

This commit is contained in:
Bat 2018-06-18 22:54:13 +01:00
parent 5415b70854
commit 78be09c47c
3 changed files with 3 additions and 57 deletions

View File

@ -17,7 +17,6 @@ pub mod inbox;
pub mod object;
pub mod request;
pub mod sign;
pub mod webfinger;
pub type ActivityPub = Content<Json<serde_json::Value>>;

View File

@ -1,52 +0,0 @@
use diesel::PgConnection;
use reqwest::Client;
use reqwest::{
header::{Accept, qitem},
mime::Mime
};
use serde_json;
use activity_pub::ap_url;
pub trait Webfinger {
fn webfinger_subject(&self, conn: &PgConnection) -> String;
fn webfinger_aliases(&self, conn: &PgConnection) -> Vec<String>;
fn webfinger_links(&self, conn: &PgConnection) -> Vec<Vec<(String, String)>>;
fn webfinger(&self, conn: &PgConnection) -> String {
json!({
"subject": self.webfinger_subject(conn),
"aliases": self.webfinger_aliases(conn),
"links": self.webfinger_links(conn).into_iter().map(|link| {
let mut link_obj = serde_json::Map::new();
for (k, v) in link {
link_obj.insert(k, serde_json::Value::String(v));
}
serde_json::Value::Object(link_obj)
}).collect::<Vec<serde_json::Value>>()
}).to_string()
}
}
pub fn resolve(acct: String) -> Result<String, String> {
let instance = acct.split("@").last().unwrap();
let url = ap_url(format!("{}/.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["type"].as_str().unwrap() == "application/activity+json" {
Some(String::from(link["href"].as_str().unwrap()))
} else {
None
}
}).unwrap()
})
.map_err(|e| format!("Error while fetchin WebFinger resource ({})", e))
}

View File

@ -31,8 +31,7 @@ use activity_pub::{
ap_url, ActivityStream, Id, IntoId,
actor::{ActorType, Actor as APActor},
inbox::{Inbox, WithInbox},
sign::{Signer, gen_keypair},
webfinger::{resolve}
sign::{Signer, gen_keypair}
};
use db_conn::DbConn;
use models::{
@ -140,9 +139,9 @@ impl User {
fn fetch_from_webfinger(conn: &PgConnection, acct: String) -> Option<User> {
match resolve(acct.clone()) {
Ok(url) => User::fetch_from_url(conn, url),
Ok(wf) => wf.links.into_iter().find(|l| l.mime_type == Some(String::from("application/activity+json"))).and_then(|l| User::fetch_from_url(conn, l.href)),
Err(details) => {
println!("{}", details);
println!("{:?}", details);
None
}
}