Use request::get() instead of ClientBuilder
This commit is contained in:
		
							parent
							
								
									48fab8ad2c
								
							
						
					
					
						commit
						25fe2ad802
					
				| @ -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<C>: Sized { | ||||
|         id: &str, | ||||
|         proxy: Option<reqwest::Proxy>, | ||||
|     ) -> Result<Self::Object, (Option<serde_json::Value>, 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
 | ||||
|  | ||||
| @ -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<InboxError<Error>> for Error { | ||||
|     } | ||||
| } | ||||
| 
 | ||||
| impl From<request::Error> for Error { | ||||
|     fn from(_err: request::Error) -> Error { | ||||
|         Error::Request | ||||
|     } | ||||
| } | ||||
| 
 | ||||
| pub type Result<T> = std::result::Result<T, Error>; | ||||
| 
 | ||||
| /// Adds a function to a model, that returns the first
 | ||||
|  | ||||
| @ -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)?) | ||||
|  | ||||
| @ -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<CustomPerson> { | ||||
|         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::<Vec<_>>() | ||||
|                         .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::<ApSignature>(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<T: Activity>(&self, url: &str) -> Result<(Vec<T>, Option<String>)> { | ||||
|         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::<Vec<_>>() | ||||
|                         .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<T: Activity>(&self) -> Result<Vec<T>> { | ||||
|         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::<Vec<_>>() | ||||
|                         .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<Vec<String>> { | ||||
|         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::<Vec<_>>() | ||||
|                         .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"] | ||||
|  | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user