]> Untitled Git - lemmy.git/blob - crates/apub/src/fetcher/fetch.rs
Rewrite fetcher (#1792)
[lemmy.git] / crates / apub / src / fetcher / fetch.rs
1 use crate::{check_is_apub_id_valid, APUB_JSON_CONTENT_TYPE};
2 use anyhow::anyhow;
3 use lemmy_utils::{request::retry, LemmyError};
4 use log::info;
5 use reqwest::Client;
6 use serde::Deserialize;
7 use std::time::Duration;
8 use url::Url;
9
10 /// Maximum number of HTTP requests allowed to handle a single incoming activity (or a single object
11 /// fetch through the search).
12 ///
13 /// A community fetch will load the outbox with up to 20 items, and fetch the creator for each item.
14 /// So we are looking at a maximum of 22 requests (rounded up just to be safe).
15 static MAX_REQUEST_NUMBER: i32 = 25;
16
17 /// Fetch any type of ActivityPub object, handling things like HTTP headers, deserialisation,
18 /// timeouts etc.
19 pub(in crate::fetcher) async fn fetch_remote_object<Response>(
20   client: &Client,
21   url: &Url,
22   recursion_counter: &mut i32,
23 ) -> Result<Response, LemmyError>
24 where
25   Response: for<'de> Deserialize<'de> + std::fmt::Debug,
26 {
27   *recursion_counter += 1;
28   if *recursion_counter > MAX_REQUEST_NUMBER {
29     return Err(anyhow!("Maximum recursion depth reached").into());
30   }
31   check_is_apub_id_valid(url, false)?;
32
33   let timeout = Duration::from_secs(60);
34
35   let res = retry(|| {
36     client
37       .get(url.as_str())
38       .header("Accept", APUB_JSON_CONTENT_TYPE)
39       .timeout(timeout)
40       .send()
41   })
42   .await?;
43
44   let object = res.json().await?;
45   info!("Fetched remote object {}", url);
46   Ok(object)
47 }