asyncify reqwest calls (again?)

This commit is contained in:
Igor Galić 2020-02-14 19:29:30 +01:00
parent 82088596a8
commit a010025074
No known key found for this signature in database
GPG Key ID: ACFEFF7F6A123A86

View File

@ -221,8 +221,8 @@ impl User {
.ok_or(Error::Webfinger) .ok_or(Error::Webfinger)
} }
fn fetch(url: &str) -> Result<CustomPerson> { async fn fetch(url: &str) -> Result<CustomPerson> {
let mut res = ClientBuilder::new() let res = ClientBuilder::new()
.connect_timeout(std::time::Duration::from_secs(5)) .connect_timeout(std::time::Duration::from_secs(5))
.build()? .build()?
.get(url) .get(url)
@ -235,8 +235,8 @@ impl User {
.join(", "), .join(", "),
)?, )?,
) )
.send()?; .send().await?;
let text = &res.text()?; let text = &res.text().await?;
// 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)?;
let mut json = serde_json::from_str::<CustomPerson>(text)?; let mut json = serde_json::from_str::<CustomPerson>(text)?;
@ -244,12 +244,13 @@ impl User {
Ok(json) Ok(json)
} }
pub fn fetch_from_url(c: &PlumeRocket, url: &str) -> Result<User> { pub async fn fetch_from_url(c: &PlumeRocket, url: &str) -> Result<User> {
User::fetch(url).and_then(|json| User::from_activity(c, json)) let json = User::fetch(url).await?;
User::from_activity(c, json)
} }
pub fn refetch(&self, conn: &Connection) -> Result<()> { pub async fn refetch(&self, conn: &Connection) -> Result<()> {
User::fetch(&self.ap_url.clone()).and_then(|json| { let json = User::fetch(&self.ap_url.clone()).await?;
let avatar = Media::save_remote( let avatar = Media::save_remote(
conn, conn,
json.object json.object
@ -285,7 +286,6 @@ impl User {
.execute(conn) .execute(conn)
.map(|_| ()) .map(|_| ())
.map_err(Error::from) .map_err(Error::from)
})
} }
pub fn hash_pass(pass: &str) -> Result<String> { pub fn hash_pass(pass: &str) -> Result<String> {
@ -356,8 +356,9 @@ impl User {
.set_part_of_link(Id::new(&self.outbox_url))?; .set_part_of_link(Id::new(&self.outbox_url))?;
Ok(ActivityStream::new(coll)) Ok(ActivityStream::new(coll))
} }
fn fetch_outbox_page<T: Activity>(&self, url: &str) -> Result<(Vec<T>, Option<String>)> {
let mut res = ClientBuilder::new() async fn fetch_outbox_page<T: Activity>(&self, url: &str) -> Result<(Vec<T>, Option<String>)> {
let res = ClientBuilder::new()
.connect_timeout(std::time::Duration::from_secs(5)) .connect_timeout(std::time::Duration::from_secs(5))
.build()? .build()?
.get(url) .get(url)
@ -370,8 +371,8 @@ impl User {
.join(", "), .join(", "),
)?, )?,
) )
.send()?; .send().await?;
let text = &res.text()?; let text = &res.text().await?;
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"]
.as_array() .as_array()
@ -386,8 +387,8 @@ impl User {
}; };
Ok((items, next)) Ok((items, next))
} }
pub fn fetch_outbox<T: Activity>(&self) -> Result<Vec<T>> { pub async fn fetch_outbox<T: Activity>(&self) -> Result<Vec<T>> {
let mut res = ClientBuilder::new() let res = ClientBuilder::new()
.connect_timeout(std::time::Duration::from_secs(5)) .connect_timeout(std::time::Duration::from_secs(5))
.build()? .build()?
.get(&self.outbox_url[..]) .get(&self.outbox_url[..])
@ -400,13 +401,13 @@ impl User {
.join(", "), .join(", "),
)?, )?,
) )
.send()?; .send().await?;
let text = &res.text()?; let text = &res.text().await?;
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") {
let mut items: Vec<T> = Vec::new(); let mut items: Vec<T> = Vec::new();
let mut next = first.as_str().unwrap().to_owned(); let mut next = first.as_str().unwrap().to_owned();
while let Ok((mut page, nxt)) = self.fetch_outbox_page(&next) { while let Ok((mut page, nxt)) = self.fetch_outbox_page(&next).await {
if page.is_empty() { if page.is_empty() {
break; break;
} }
@ -431,8 +432,8 @@ impl User {
} }
} }
pub fn fetch_followers_ids(&self) -> Result<Vec<String>> { pub async fn fetch_followers_ids(&self) -> Result<Vec<String>> {
let mut res = ClientBuilder::new() let res = ClientBuilder::new()
.connect_timeout(std::time::Duration::from_secs(5)) .connect_timeout(std::time::Duration::from_secs(5))
.build()? .build()?
.get(&self.followers_endpoint[..]) .get(&self.followers_endpoint[..])
@ -445,8 +446,8 @@ impl User {
.join(", "), .join(", "),
)?, )?,
) )
.send()?; .send().await?;
let text = &res.text()?; let text = &res.text().await?;
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"]
.as_array() .as_array()