diff --git a/plume-common/src/activity_pub/inbox.rs b/plume-common/src/activity_pub/inbox.rs index 9483a039..48ba6c5f 100644 --- a/plume-common/src/activity_pub/inbox.rs +++ b/plume-common/src/activity_pub/inbox.rs @@ -1,7 +1,4 @@ -use reqwest::{ - header::{HeaderValue, HOST}, - Url, -}; +use reqwest; use std::fmt::Debug; use super::{request, sign::Signer}; @@ -367,43 +364,16 @@ pub trait FromId: Sized { id: &str, proxy: Option, ) -> Result, Self::Error)> { - let mut headers = request::headers(); - let url = Url::parse(id).map_err(|_| (None, InboxError::DerefError.into()))?; - if !url.has_host() { - return Err((None, InboxError::DerefError.into())); - } - let host_header_value = HeaderValue::from_str(url.host_str().expect("Unreachable")) - .map_err(|_| (None, InboxError::DerefError.into()))?; - headers.insert(HOST, host_header_value); - if let Some(proxy) = proxy { - reqwest::ClientBuilder::new().proxy(proxy) - } else { - reqwest::ClientBuilder::new() - } - .connect_timeout(Some(std::time::Duration::from_secs(5))) - .build() - .map_err(|_| (None, InboxError::DerefError.into()))? - .get(id) - .headers(headers.clone()) - .header( - "Signature", - request::signature( - Self::get_sender(), - &headers, - ("get", url.path(), url.query()), - ) - .map_err(|_| (None, InboxError::DerefError.into()))?, - ) - .send() - .map_err(|_| (None, InboxError::DerefError)) - .and_then(|mut r| { - let json: serde_json::Value = r - .json() - .map_err(|_| (None, InboxError::InvalidObject(None)))?; - serde_json::from_value(json.clone()) - .map_err(|_| (Some(json), InboxError::InvalidObject(None))) - }) - .map_err(|(json, e)| (json, e.into())) + request::get(id, Self::get_sender(), proxy) + .map_err(|_| (None, InboxError::DerefError)) + .and_then(|mut r| { + let json: serde_json::Value = r + .json() + .map_err(|_| (None, InboxError::InvalidObject(None)))?; + serde_json::from_value(json.clone()) + .map_err(|_| (Some(json), InboxError::InvalidObject(None))) + }) + .map_err(|(json, e)| (json, e.into())) } /// Builds a `Self` from its ActivityPub representation diff --git a/plume-models/src/lib.rs b/plume-models/src/lib.rs index 33a5ec2b..16d3a159 100755 --- a/plume-models/src/lib.rs +++ b/plume-models/src/lib.rs @@ -17,7 +17,7 @@ extern crate serde_json; extern crate tantivy; use once_cell::sync::Lazy; -use plume_common::activity_pub::{inbox::InboxError, sign}; +use plume_common::activity_pub::{inbox::InboxError, request, sign}; use posts::PostEvent; use riker::actors::{channel, ActorSystem, ChannelRef, SystemBuilder}; use users::UserEvent; @@ -157,6 +157,12 @@ impl From> for Error { } } +impl From for Error { + fn from(_err: request::Error) -> Error { + Error::Request + } +} + pub type Result = std::result::Result; /// Adds a function to a model, that returns the first diff --git a/plume-models/src/medias.rs b/plume-models/src/medias.rs index 5be9bc70..68cb52c4 100644 --- a/plume-models/src/medias.rs +++ b/plume-models/src/medias.rs @@ -7,7 +7,7 @@ use askama_escape::escape; use diesel::{self, ExpressionMethods, QueryDsl, RunQueryDsl}; use guid_create::GUID; use plume_common::{ - activity_pub::{inbox::FromId, Id}, + activity_pub::{inbox::FromId, request, Id}, utils::MediaProcessor, }; use std::{ @@ -220,13 +220,11 @@ impl Media { let mut dest = fs::File::create(path.clone())?; // TODO: conditional GET - if let Some(proxy) = CONFIG.proxy() { - reqwest::ClientBuilder::new().proxy(proxy.clone()).build()? - } else { - reqwest::Client::new() - } - .get(remote_url.as_str()) - .send()? + request::get( + remote_url.as_str(), + User::get_sender(), + CONFIG.proxy().cloned(), + )? .copy_to(&mut dest)?; Media::find_by_file_path(conn, path.to_str().ok_or(Error::InvalidValue)?) diff --git a/plume-models/src/users.rs b/plume-models/src/users.rs index b8b6b476..14ea4e64 100644 --- a/plume-models/src/users.rs +++ b/plume-models/src/users.rs @@ -22,17 +22,13 @@ use openssl::{ }; use plume_common::{ activity_pub::{ - ap_accept_header, inbox::{AsActor, AsObject, FromId}, + request::get, sign::{gen_keypair, Error as SignError, Result as SignResult, Signer}, ActivityStream, ApSignature, Id, IntoId, PublicKey, PUBLIC_VISIBILITY, }, utils, }; -use reqwest::{ - header::{HeaderValue, ACCEPT}, - ClientBuilder, -}; use riker::actors::{Publish, Tell}; use rocket::{ outcome::IntoOutcome, @@ -231,20 +227,7 @@ impl User { } fn fetch(url: &str) -> Result { - let mut res = ClientBuilder::new() - .connect_timeout(Some(std::time::Duration::from_secs(5))) - .build()? - .get(url) - .header( - ACCEPT, - HeaderValue::from_str( - &ap_accept_header() - .into_iter() - .collect::>() - .join(", "), - )?, - ) - .send()?; + let mut res = get(url, Self::get_sender(), CONFIG.proxy().cloned())?; let text = &res.text()?; // without this workaround, publicKey is not correctly deserialized let ap_sign = serde_json::from_str::(text)?; @@ -293,7 +276,10 @@ impl User { )) .execute(conn) .map(|_| ()) - .map_err(Error::from) + .map_err(|err| { + tracing::error!("{:?}", err); + Error::from(err) + }) }) } @@ -471,20 +457,7 @@ impl User { Ok(ActivityStream::new(coll)) } fn fetch_outbox_page(&self, url: &str) -> Result<(Vec, Option)> { - let mut res = ClientBuilder::new() - .connect_timeout(Some(std::time::Duration::from_secs(5))) - .build()? - .get(url) - .header( - ACCEPT, - HeaderValue::from_str( - &ap_accept_header() - .into_iter() - .collect::>() - .join(", "), - )?, - ) - .send()?; + let mut res = get(url, Self::get_sender(), CONFIG.proxy().cloned())?; let text = &res.text()?; let json: serde_json::Value = serde_json::from_str(text)?; let items = json["items"] @@ -498,20 +471,11 @@ impl User { Ok((items, next)) } pub fn fetch_outbox(&self) -> Result> { - let mut res = ClientBuilder::new() - .connect_timeout(Some(std::time::Duration::from_secs(5))) - .build()? - .get(&self.outbox_url[..]) - .header( - ACCEPT, - HeaderValue::from_str( - &ap_accept_header() - .into_iter() - .collect::>() - .join(", "), - )?, - ) - .send()?; + let mut res = get( + &self.outbox_url[..], + Self::get_sender(), + CONFIG.proxy().cloned(), + )?; let text = &res.text()?; let json: serde_json::Value = serde_json::from_str(text)?; if let Some(first) = json.get("first") { @@ -543,20 +507,11 @@ impl User { } pub fn fetch_followers_ids(&self) -> Result> { - let mut res = ClientBuilder::new() - .connect_timeout(Some(std::time::Duration::from_secs(5))) - .build()? - .get(&self.followers_endpoint[..]) - .header( - ACCEPT, - HeaderValue::from_str( - &ap_accept_header() - .into_iter() - .collect::>() - .join(", "), - )?, - ) - .send()?; + let mut res = get( + &self.followers_endpoint[..], + Self::get_sender(), + CONFIG.proxy().cloned(), + )?; let text = &res.text()?; let json: serde_json::Value = serde_json::from_str(text)?; Ok(json["items"]