Use the webfinger crate for fetching resources too
This commit is contained in:
@@ -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>>;
|
||||
|
||||
|
||||
@@ -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))
|
||||
}
|
||||
Reference in New Issue
Block a user