Use request::get() instead of ClientBuilder
This commit is contained in:
parent
48fab8ad2c
commit
25fe2ad802
@ -1,7 +1,4 @@
|
|||||||
use reqwest::{
|
use reqwest;
|
||||||
header::{HeaderValue, HOST},
|
|
||||||
Url,
|
|
||||||
};
|
|
||||||
use std::fmt::Debug;
|
use std::fmt::Debug;
|
||||||
|
|
||||||
use super::{request, sign::Signer};
|
use super::{request, sign::Signer};
|
||||||
@ -367,34 +364,7 @@ pub trait FromId<C>: Sized {
|
|||||||
id: &str,
|
id: &str,
|
||||||
proxy: Option<reqwest::Proxy>,
|
proxy: Option<reqwest::Proxy>,
|
||||||
) -> Result<Self::Object, (Option<serde_json::Value>, Self::Error)> {
|
) -> Result<Self::Object, (Option<serde_json::Value>, Self::Error)> {
|
||||||
let mut headers = request::headers();
|
request::get(id, Self::get_sender(), proxy)
|
||||||
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))
|
.map_err(|_| (None, InboxError::DerefError))
|
||||||
.and_then(|mut r| {
|
.and_then(|mut r| {
|
||||||
let json: serde_json::Value = r
|
let json: serde_json::Value = r
|
||||||
|
@ -17,7 +17,7 @@ extern crate serde_json;
|
|||||||
extern crate tantivy;
|
extern crate tantivy;
|
||||||
|
|
||||||
use once_cell::sync::Lazy;
|
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 posts::PostEvent;
|
||||||
use riker::actors::{channel, ActorSystem, ChannelRef, SystemBuilder};
|
use riker::actors::{channel, ActorSystem, ChannelRef, SystemBuilder};
|
||||||
use users::UserEvent;
|
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>;
|
pub type Result<T> = std::result::Result<T, Error>;
|
||||||
|
|
||||||
/// Adds a function to a model, that returns the first
|
/// 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 diesel::{self, ExpressionMethods, QueryDsl, RunQueryDsl};
|
||||||
use guid_create::GUID;
|
use guid_create::GUID;
|
||||||
use plume_common::{
|
use plume_common::{
|
||||||
activity_pub::{inbox::FromId, Id},
|
activity_pub::{inbox::FromId, request, Id},
|
||||||
utils::MediaProcessor,
|
utils::MediaProcessor,
|
||||||
};
|
};
|
||||||
use std::{
|
use std::{
|
||||||
@ -220,13 +220,11 @@ impl Media {
|
|||||||
|
|
||||||
let mut dest = fs::File::create(path.clone())?;
|
let mut dest = fs::File::create(path.clone())?;
|
||||||
// TODO: conditional GET
|
// TODO: conditional GET
|
||||||
if let Some(proxy) = CONFIG.proxy() {
|
request::get(
|
||||||
reqwest::ClientBuilder::new().proxy(proxy.clone()).build()?
|
remote_url.as_str(),
|
||||||
} else {
|
User::get_sender(),
|
||||||
reqwest::Client::new()
|
CONFIG.proxy().cloned(),
|
||||||
}
|
)?
|
||||||
.get(remote_url.as_str())
|
|
||||||
.send()?
|
|
||||||
.copy_to(&mut dest)?;
|
.copy_to(&mut dest)?;
|
||||||
|
|
||||||
Media::find_by_file_path(conn, path.to_str().ok_or(Error::InvalidValue)?)
|
Media::find_by_file_path(conn, path.to_str().ok_or(Error::InvalidValue)?)
|
||||||
|
@ -22,17 +22,13 @@ use openssl::{
|
|||||||
};
|
};
|
||||||
use plume_common::{
|
use plume_common::{
|
||||||
activity_pub::{
|
activity_pub::{
|
||||||
ap_accept_header,
|
|
||||||
inbox::{AsActor, AsObject, FromId},
|
inbox::{AsActor, AsObject, FromId},
|
||||||
|
request::get,
|
||||||
sign::{gen_keypair, Error as SignError, Result as SignResult, Signer},
|
sign::{gen_keypair, Error as SignError, Result as SignResult, Signer},
|
||||||
ActivityStream, ApSignature, Id, IntoId, PublicKey, PUBLIC_VISIBILITY,
|
ActivityStream, ApSignature, Id, IntoId, PublicKey, PUBLIC_VISIBILITY,
|
||||||
},
|
},
|
||||||
utils,
|
utils,
|
||||||
};
|
};
|
||||||
use reqwest::{
|
|
||||||
header::{HeaderValue, ACCEPT},
|
|
||||||
ClientBuilder,
|
|
||||||
};
|
|
||||||
use riker::actors::{Publish, Tell};
|
use riker::actors::{Publish, Tell};
|
||||||
use rocket::{
|
use rocket::{
|
||||||
outcome::IntoOutcome,
|
outcome::IntoOutcome,
|
||||||
@ -231,20 +227,7 @@ impl User {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn fetch(url: &str) -> Result<CustomPerson> {
|
fn fetch(url: &str) -> Result<CustomPerson> {
|
||||||
let mut res = ClientBuilder::new()
|
let mut res = get(url, Self::get_sender(), CONFIG.proxy().cloned())?;
|
||||||
.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 text = &res.text()?;
|
let text = &res.text()?;
|
||||||
// without this workaround, publicKey is not correctly deserialized
|
// without this workaround, publicKey is not correctly deserialized
|
||||||
let ap_sign = serde_json::from_str::<ApSignature>(text)?;
|
let ap_sign = serde_json::from_str::<ApSignature>(text)?;
|
||||||
@ -293,7 +276,10 @@ impl User {
|
|||||||
))
|
))
|
||||||
.execute(conn)
|
.execute(conn)
|
||||||
.map(|_| ())
|
.map(|_| ())
|
||||||
.map_err(Error::from)
|
.map_err(|err| {
|
||||||
|
tracing::error!("{:?}", err);
|
||||||
|
Error::from(err)
|
||||||
|
})
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -471,20 +457,7 @@ impl User {
|
|||||||
Ok(ActivityStream::new(coll))
|
Ok(ActivityStream::new(coll))
|
||||||
}
|
}
|
||||||
fn fetch_outbox_page<T: Activity>(&self, url: &str) -> Result<(Vec<T>, Option<String>)> {
|
fn fetch_outbox_page<T: Activity>(&self, url: &str) -> Result<(Vec<T>, Option<String>)> {
|
||||||
let mut res = ClientBuilder::new()
|
let mut res = get(url, Self::get_sender(), CONFIG.proxy().cloned())?;
|
||||||
.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 text = &res.text()?;
|
let text = &res.text()?;
|
||||||
let json: serde_json::Value = serde_json::from_str(text)?;
|
let json: serde_json::Value = serde_json::from_str(text)?;
|
||||||
let items = json["items"]
|
let items = json["items"]
|
||||||
@ -498,20 +471,11 @@ impl User {
|
|||||||
Ok((items, next))
|
Ok((items, next))
|
||||||
}
|
}
|
||||||
pub fn fetch_outbox<T: Activity>(&self) -> Result<Vec<T>> {
|
pub fn fetch_outbox<T: Activity>(&self) -> Result<Vec<T>> {
|
||||||
let mut res = ClientBuilder::new()
|
let mut res = get(
|
||||||
.connect_timeout(Some(std::time::Duration::from_secs(5)))
|
&self.outbox_url[..],
|
||||||
.build()?
|
Self::get_sender(),
|
||||||
.get(&self.outbox_url[..])
|
CONFIG.proxy().cloned(),
|
||||||
.header(
|
)?;
|
||||||
ACCEPT,
|
|
||||||
HeaderValue::from_str(
|
|
||||||
&ap_accept_header()
|
|
||||||
.into_iter()
|
|
||||||
.collect::<Vec<_>>()
|
|
||||||
.join(", "),
|
|
||||||
)?,
|
|
||||||
)
|
|
||||||
.send()?;
|
|
||||||
let text = &res.text()?;
|
let text = &res.text()?;
|
||||||
let json: serde_json::Value = serde_json::from_str(text)?;
|
let json: serde_json::Value = serde_json::from_str(text)?;
|
||||||
if let Some(first) = json.get("first") {
|
if let Some(first) = json.get("first") {
|
||||||
@ -543,20 +507,11 @@ impl User {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn fetch_followers_ids(&self) -> Result<Vec<String>> {
|
pub fn fetch_followers_ids(&self) -> Result<Vec<String>> {
|
||||||
let mut res = ClientBuilder::new()
|
let mut res = get(
|
||||||
.connect_timeout(Some(std::time::Duration::from_secs(5)))
|
&self.followers_endpoint[..],
|
||||||
.build()?
|
Self::get_sender(),
|
||||||
.get(&self.followers_endpoint[..])
|
CONFIG.proxy().cloned(),
|
||||||
.header(
|
)?;
|
||||||
ACCEPT,
|
|
||||||
HeaderValue::from_str(
|
|
||||||
&ap_accept_header()
|
|
||||||
.into_iter()
|
|
||||||
.collect::<Vec<_>>()
|
|
||||||
.join(", "),
|
|
||||||
)?,
|
|
||||||
)
|
|
||||||
.send()?;
|
|
||||||
let text = &res.text()?;
|
let text = &res.text()?;
|
||||||
let json: serde_json::Value = serde_json::from_str(text)?;
|
let json: serde_json::Value = serde_json::from_str(text)?;
|
||||||
Ok(json["items"]
|
Ok(json["items"]
|
||||||
|
Loading…
Reference in New Issue
Block a user