]> Untitled Git - lemmy.git/blob - crates/utils/src/request.rs
Consolidate reqwest clients, use reqwest-middleware for tracing
[lemmy.git] / crates / utils / src / request.rs
1 use crate::{settings::structs::Settings, version::VERSION, LemmyError};
2 use anyhow::anyhow;
3 use percent_encoding::{utf8_percent_encode, NON_ALPHANUMERIC};
4 use reqwest_middleware::ClientWithMiddleware;
5 use serde::{Deserialize, Serialize};
6 use std::future::Future;
7 use thiserror::Error;
8 use tracing::error;
9 use url::Url;
10 use webpage::HTML;
11
12 #[derive(Clone, Debug, Error)]
13 #[error("Error sending request, {0}")]
14 struct SendError(pub String);
15
16 #[derive(Clone, Debug, Error)]
17 #[error("Error receiving response, {0}")]
18 pub struct RecvError(pub String);
19
20 pub async fn retry<F, Fut, T>(f: F) -> Result<T, reqwest_middleware::Error>
21 where
22   F: Fn() -> Fut,
23   Fut: Future<Output = Result<T, reqwest_middleware::Error>>,
24 {
25   retry_custom(|| async { Ok((f)().await) }).await
26 }
27
28 async fn retry_custom<F, Fut, T>(f: F) -> Result<T, reqwest_middleware::Error>
29 where
30   F: Fn() -> Fut,
31   Fut: Future<Output = Result<Result<T, reqwest_middleware::Error>, reqwest_middleware::Error>>,
32 {
33   let mut response: Option<Result<T, reqwest_middleware::Error>> = None;
34
35   for _ in 0u8..3 {
36     match (f)().await? {
37       Ok(t) => return Ok(t),
38       Err(reqwest_middleware::Error::Reqwest(e)) => {
39         if e.is_timeout() {
40           response = Some(Err(reqwest_middleware::Error::Reqwest(e)));
41           continue;
42         }
43         return Err(reqwest_middleware::Error::Reqwest(e));
44       }
45       Err(otherwise) => {
46         return Err(otherwise);
47       }
48     }
49   }
50
51   response.expect("retry http request")
52 }
53
54 #[derive(Deserialize, Serialize, Debug, PartialEq, Clone)]
55 pub struct SiteMetadata {
56   pub title: Option<String>,
57   pub description: Option<String>,
58   image: Option<Url>,
59   pub html: Option<String>,
60 }
61
62 /// Fetches the post link html tags (like title, description, image, etc)
63 pub async fn fetch_site_metadata(
64   client: &ClientWithMiddleware,
65   url: &Url,
66 ) -> Result<SiteMetadata, LemmyError> {
67   let response = client.get(url.as_str()).send().await?;
68
69   let html = response
70     .text()
71     .await
72     .map_err(|e| RecvError(e.to_string()))?;
73
74   let tags = html_to_site_metadata(&html)?;
75
76   Ok(tags)
77 }
78
79 fn html_to_site_metadata(html: &str) -> Result<SiteMetadata, LemmyError> {
80   let page = HTML::from_string(html.to_string(), None)?;
81
82   let page_title = page.title;
83   let page_description = page.description;
84
85   let og_description = page
86     .opengraph
87     .properties
88     .get("description")
89     .map(|t| t.to_string());
90   let og_title = page
91     .opengraph
92     .properties
93     .get("title")
94     .map(|t| t.to_string());
95   let og_image = page
96     .opengraph
97     .images
98     .get(0)
99     .map(|ogo| Url::parse(&ogo.url).ok())
100     .flatten();
101
102   let title = og_title.or(page_title);
103   let description = og_description.or(page_description);
104   let image = og_image;
105
106   Ok(SiteMetadata {
107     title,
108     description,
109     image,
110     html: None,
111   })
112 }
113
114 #[derive(Deserialize, Debug, Clone)]
115 pub(crate) struct PictrsResponse {
116   files: Vec<PictrsFile>,
117   msg: String,
118 }
119
120 #[derive(Deserialize, Debug, Clone)]
121 pub(crate) struct PictrsFile {
122   file: String,
123   #[allow(dead_code)]
124   delete_token: String,
125 }
126
127 pub(crate) async fn fetch_pictrs(
128   client: &ClientWithMiddleware,
129   settings: &Settings,
130   image_url: &Url,
131 ) -> Result<PictrsResponse, LemmyError> {
132   if let Some(pictrs_url) = settings.pictrs_url.to_owned() {
133     is_image_content_type(client, image_url).await?;
134
135     let fetch_url = format!(
136       "{}/image/download?url={}",
137       pictrs_url,
138       utf8_percent_encode(image_url.as_str(), NON_ALPHANUMERIC) // TODO this might not be needed
139     );
140
141     let response = client.get(&fetch_url).send().await?;
142
143     let response: PictrsResponse = response
144       .json()
145       .await
146       .map_err(|e| RecvError(e.to_string()))?;
147
148     if response.msg == "ok" {
149       Ok(response)
150     } else {
151       Err(anyhow!("{}", &response.msg).into())
152     }
153   } else {
154     Err(anyhow!("pictrs_url not set up in config").into())
155   }
156 }
157
158 /// Both are options, since the URL might be either an html page, or an image
159 /// Returns the SiteMetadata, and a Pictrs URL, if there is a picture associated
160 pub async fn fetch_site_data(
161   client: &ClientWithMiddleware,
162   settings: &Settings,
163   url: Option<&Url>,
164 ) -> (Option<SiteMetadata>, Option<Url>) {
165   match &url {
166     Some(url) => {
167       // Fetch metadata
168       // Ignore errors, since it may be an image, or not have the data.
169       // Warning, this may ignore SSL errors
170       let metadata_option = fetch_site_metadata(client, url).await.ok();
171
172       // Fetch pictrs thumbnail
173       let pictrs_hash = match &metadata_option {
174         Some(metadata_res) => match &metadata_res.image {
175           // Metadata, with image
176           // Try to generate a small thumbnail if there's a full sized one from post-links
177           Some(metadata_image) => fetch_pictrs(client, settings, metadata_image)
178             .await
179             .map(|r| r.files[0].file.to_owned()),
180           // Metadata, but no image
181           None => fetch_pictrs(client, settings, url)
182             .await
183             .map(|r| r.files[0].file.to_owned()),
184         },
185         // No metadata, try to fetch the URL as an image
186         None => fetch_pictrs(client, settings, url)
187           .await
188           .map(|r| r.files[0].file.to_owned()),
189       };
190
191       // The full urls are necessary for federation
192       let pictrs_thumbnail = pictrs_hash
193         .map(|p| {
194           Url::parse(&format!(
195             "{}/pictrs/image/{}",
196             settings.get_protocol_and_hostname(),
197             p
198           ))
199           .ok()
200         })
201         .ok()
202         .flatten();
203
204       (metadata_option, pictrs_thumbnail)
205     }
206     None => (None, None),
207   }
208 }
209
210 async fn is_image_content_type(client: &ClientWithMiddleware, url: &Url) -> Result<(), LemmyError> {
211   let response = client.get(url.as_str()).send().await?;
212   if response
213     .headers()
214     .get("Content-Type")
215     .ok_or_else(|| anyhow!("No Content-Type header"))?
216     .to_str()?
217     .starts_with("image/")
218   {
219     Ok(())
220   } else {
221     Err(anyhow!("Not an image type.").into())
222   }
223 }
224
225 pub fn build_user_agent(settings: &Settings) -> String {
226   format!(
227     "Lemmy/{}; +{}",
228     VERSION,
229     settings.get_protocol_and_hostname()
230   )
231 }
232
233 #[cfg(test)]
234 mod tests {
235   use crate::request::{build_user_agent, fetch_site_metadata};
236   use url::Url;
237
238   use super::SiteMetadata;
239   use crate::settings::structs::Settings;
240
241   // These helped with testing
242   #[actix_rt::test]
243   async fn test_site_metadata() {
244     let settings = Settings::init().unwrap();
245     let client = reqwest::Client::builder()
246       .user_agent(build_user_agent(&settings))
247       .build()
248       .unwrap()
249       .into();
250     let sample_url = Url::parse("https://www.redspark.nu/en/peoples-war/district-leader-of-chand-led-cpn-arrested-in-bhojpur/").unwrap();
251     let sample_res = fetch_site_metadata(&client, &sample_url).await.unwrap();
252     assert_eq!(
253       SiteMetadata {
254         title: Some("District Leader Of Chand Led CPN Arrested In Bhojpur - Redspark".to_string()),
255         description: Some("BHOJPUR: A district leader of the outlawed Netra Bikram Chand alias Biplav-led outfit has been arrested. According to District Police".to_string()),
256         image: Some(Url::parse("https://www.redspark.nu/wp-content/uploads/2020/03/netra-bikram-chand-attends-program-1272019033653-1000x0-845x653-1.jpg").unwrap()),
257         html: None,
258       }, sample_res);
259
260     let youtube_url = Url::parse("https://www.youtube.com/watch?v=IquO_TcMZIQ").unwrap();
261     let youtube_res = fetch_site_metadata(&client, &youtube_url).await.unwrap();
262     assert_eq!(
263       SiteMetadata {
264         title: Some("A Hard Look at Rent and Rent Seeking with Michael Hudson & Pepe Escobar".to_string()),
265         description: Some("An interactive discussion on wealth inequality and the “Great Game” on the control of natural resources.In this webinar organized jointly by the Henry George...".to_string()),
266         image: Some(Url::parse("https://i.ytimg.com/vi/IquO_TcMZIQ/maxresdefault.jpg").unwrap()),
267         html: None,
268       }, youtube_res);
269   }
270
271   // #[test]
272   // fn test_pictshare() {
273   //   let res = fetch_pictshare("https://upload.wikimedia.org/wikipedia/en/2/27/The_Mandalorian_logo.jpg");
274   //   assert!(res.is_ok());
275   //   let res_other = fetch_pictshare("https://upload.wikimedia.org/wikipedia/en/2/27/The_Mandalorian_logo.jpgaoeu");
276   //   assert!(res_other.is_err());
277   // }
278 }