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,48 +244,48 @@ 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
.object_props .object_props
.icon_image()? .icon_image()?
.object_props .object_props
.url_string()?, .url_string()?,
&self, &self,
) )
.ok(); .ok();
diesel::update(self) diesel::update(self)
.set(( .set((
users::username.eq(json.object.ap_actor_props.preferred_username_string()?), users::username.eq(json.object.ap_actor_props.preferred_username_string()?),
users::display_name.eq(json.object.object_props.name_string()?), users::display_name.eq(json.object.object_props.name_string()?),
users::outbox_url.eq(json.object.ap_actor_props.outbox_string()?), users::outbox_url.eq(json.object.ap_actor_props.outbox_string()?),
users::inbox_url.eq(json.object.ap_actor_props.inbox_string()?), users::inbox_url.eq(json.object.ap_actor_props.inbox_string()?),
users::summary.eq(SafeString::new( users::summary.eq(SafeString::new(
&json &json
.object .object
.object_props .object_props
.summary_string() .summary_string()
.unwrap_or_default(), .unwrap_or_default(),
)), )),
users::followers_endpoint.eq(json.object.ap_actor_props.followers_string()?), users::followers_endpoint.eq(json.object.ap_actor_props.followers_string()?),
users::avatar_id.eq(avatar.map(|a| a.id)), users::avatar_id.eq(avatar.map(|a| a.id)),
users::last_fetched_date.eq(Utc::now().naive_utc()), users::last_fetched_date.eq(Utc::now().naive_utc()),
users::public_key.eq(json users::public_key.eq(json
.custom_props .custom_props
.public_key_publickey()? .public_key_publickey()?
.public_key_pem_string()?), .public_key_pem_string()?),
)) ))
.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()