From a01002507453ef7902a0ed09221c700223656f92 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Igor=20Gali=C4=87?= Date: Fri, 14 Feb 2020 19:29:30 +0100 Subject: [PATCH] asyncify reqwest calls (again?) --- plume-models/src/users.rs | 113 +++++++++++++++++++------------------- 1 file changed, 57 insertions(+), 56 deletions(-) diff --git a/plume-models/src/users.rs b/plume-models/src/users.rs index b6309acc..2ad04139 100644 --- a/plume-models/src/users.rs +++ b/plume-models/src/users.rs @@ -221,8 +221,8 @@ impl User { .ok_or(Error::Webfinger) } - fn fetch(url: &str) -> Result { - let mut res = ClientBuilder::new() + async fn fetch(url: &str) -> Result { + let res = ClientBuilder::new() .connect_timeout(std::time::Duration::from_secs(5)) .build()? .get(url) @@ -235,8 +235,8 @@ impl User { .join(", "), )?, ) - .send()?; - let text = &res.text()?; + .send().await?; + let text = &res.text().await?; // without this workaround, publicKey is not correctly deserialized let ap_sign = serde_json::from_str::(text)?; let mut json = serde_json::from_str::(text)?; @@ -244,48 +244,48 @@ impl User { Ok(json) } - pub fn fetch_from_url(c: &PlumeRocket, url: &str) -> Result { - User::fetch(url).and_then(|json| User::from_activity(c, json)) + pub async fn fetch_from_url(c: &PlumeRocket, url: &str) -> Result { + let json = User::fetch(url).await?; + User::from_activity(c, json) } - pub fn refetch(&self, conn: &Connection) -> Result<()> { - User::fetch(&self.ap_url.clone()).and_then(|json| { - let avatar = Media::save_remote( - conn, - json.object - .object_props - .icon_image()? - .object_props - .url_string()?, - &self, - ) - .ok(); + pub async fn refetch(&self, conn: &Connection) -> Result<()> { + let json = User::fetch(&self.ap_url.clone()).await?; + let avatar = Media::save_remote( + conn, + json.object + .object_props + .icon_image()? + .object_props + .url_string()?, + &self, + ) + .ok(); - diesel::update(self) - .set(( - users::username.eq(json.object.ap_actor_props.preferred_username_string()?), - users::display_name.eq(json.object.object_props.name_string()?), - users::outbox_url.eq(json.object.ap_actor_props.outbox_string()?), - users::inbox_url.eq(json.object.ap_actor_props.inbox_string()?), - users::summary.eq(SafeString::new( - &json - .object - .object_props - .summary_string() - .unwrap_or_default(), - )), - users::followers_endpoint.eq(json.object.ap_actor_props.followers_string()?), - users::avatar_id.eq(avatar.map(|a| a.id)), - users::last_fetched_date.eq(Utc::now().naive_utc()), - users::public_key.eq(json - .custom_props - .public_key_publickey()? - .public_key_pem_string()?), - )) - .execute(conn) - .map(|_| ()) - .map_err(Error::from) - }) + diesel::update(self) + .set(( + users::username.eq(json.object.ap_actor_props.preferred_username_string()?), + users::display_name.eq(json.object.object_props.name_string()?), + users::outbox_url.eq(json.object.ap_actor_props.outbox_string()?), + users::inbox_url.eq(json.object.ap_actor_props.inbox_string()?), + users::summary.eq(SafeString::new( + &json + .object + .object_props + .summary_string() + .unwrap_or_default(), + )), + users::followers_endpoint.eq(json.object.ap_actor_props.followers_string()?), + users::avatar_id.eq(avatar.map(|a| a.id)), + users::last_fetched_date.eq(Utc::now().naive_utc()), + users::public_key.eq(json + .custom_props + .public_key_publickey()? + .public_key_pem_string()?), + )) + .execute(conn) + .map(|_| ()) + .map_err(Error::from) } pub fn hash_pass(pass: &str) -> Result { @@ -356,8 +356,9 @@ impl User { .set_part_of_link(Id::new(&self.outbox_url))?; Ok(ActivityStream::new(coll)) } - fn fetch_outbox_page(&self, url: &str) -> Result<(Vec, Option)> { - let mut res = ClientBuilder::new() + + async fn fetch_outbox_page(&self, url: &str) -> Result<(Vec, Option)> { + let res = ClientBuilder::new() .connect_timeout(std::time::Duration::from_secs(5)) .build()? .get(url) @@ -370,8 +371,8 @@ impl User { .join(", "), )?, ) - .send()?; - let text = &res.text()?; + .send().await?; + let text = &res.text().await?; let json: serde_json::Value = serde_json::from_str(text)?; let items = json["items"] .as_array() @@ -386,8 +387,8 @@ impl User { }; Ok((items, next)) } - pub fn fetch_outbox(&self) -> Result> { - let mut res = ClientBuilder::new() + pub async fn fetch_outbox(&self) -> Result> { + let res = ClientBuilder::new() .connect_timeout(std::time::Duration::from_secs(5)) .build()? .get(&self.outbox_url[..]) @@ -400,13 +401,13 @@ impl User { .join(", "), )?, ) - .send()?; - let text = &res.text()?; + .send().await?; + let text = &res.text().await?; let json: serde_json::Value = serde_json::from_str(text)?; if let Some(first) = json.get("first") { let mut items: Vec = Vec::new(); 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() { break; } @@ -431,8 +432,8 @@ impl User { } } - pub fn fetch_followers_ids(&self) -> Result> { - let mut res = ClientBuilder::new() + pub async fn fetch_followers_ids(&self) -> Result> { + let res = ClientBuilder::new() .connect_timeout(std::time::Duration::from_secs(5)) .build()? .get(&self.followers_endpoint[..]) @@ -445,8 +446,8 @@ impl User { .join(", "), )?, ) - .send()?; - let text = &res.text()?; + .send().await?; + let text = &res.text().await?; let json: serde_json::Value = serde_json::from_str(text)?; Ok(json["items"] .as_array()