1 use crate::{settings::structs::Settings, LemmyError};
4 use percent_encoding::{utf8_percent_encode, NON_ALPHANUMERIC};
6 use serde::{Deserialize, Serialize};
7 use std::future::Future;
12 #[derive(Clone, Debug, Error)]
13 #[error("Error sending request, {0}")]
14 struct SendError(pub String);
16 #[derive(Clone, Debug, Error)]
17 #[error("Error receiving response, {0}")]
18 pub struct RecvError(pub String);
20 pub async fn retry<F, Fut, T>(f: F) -> Result<T, reqwest::Error>
23 Fut: Future<Output = Result<T, reqwest::Error>>,
25 retry_custom(|| async { Ok((f)().await) }).await
28 async fn retry_custom<F, Fut, T>(f: F) -> Result<T, reqwest::Error>
31 Fut: Future<Output = Result<Result<T, reqwest::Error>, reqwest::Error>>,
33 let mut response: Option<Result<T, reqwest::Error>> = None;
37 Ok(t) => return Ok(t),
40 response = Some(Err(e));
48 response.expect("retry http request")
51 #[derive(Deserialize, Serialize, Debug, PartialEq, Clone)]
52 pub struct SiteMetadata {
53 pub title: Option<String>,
54 pub description: Option<String>,
56 pub html: Option<String>,
59 /// Fetches the post link html tags (like title, description, image, etc)
60 pub async fn fetch_site_metadata(client: &Client, url: &Url) -> Result<SiteMetadata, LemmyError> {
61 let response = retry(|| client.get(url.as_str()).send()).await?;
66 .map_err(|e| RecvError(e.to_string()))?;
68 let tags = html_to_site_metadata(&html)?;
73 fn html_to_site_metadata(html: &str) -> Result<SiteMetadata, LemmyError> {
74 let page = HTML::from_string(html.to_string(), None)?;
76 let page_title = page.title;
77 let page_description = page.description;
79 let og_description = page
83 .map(|t| t.to_string());
88 .map(|t| t.to_string());
93 .map(|ogo| Url::parse(&ogo.url).ok())
96 let title = og_title.or(page_title);
97 let description = og_description.or(page_description);
108 #[derive(Deserialize, Debug, Clone)]
109 pub(crate) struct PictrsResponse {
110 files: Vec<PictrsFile>,
114 #[derive(Deserialize, Debug, Clone)]
115 pub(crate) struct PictrsFile {
117 delete_token: String,
120 pub(crate) async fn fetch_pictrs(
123 ) -> Result<PictrsResponse, LemmyError> {
124 if let Some(pictrs_url) = Settings::get().pictrs_url {
125 is_image_content_type(client, image_url).await?;
127 let fetch_url = format!(
128 "{}/image/download?url={}",
130 utf8_percent_encode(image_url.as_str(), NON_ALPHANUMERIC) // TODO this might not be needed
133 let response = retry(|| client.get(&fetch_url).send()).await?;
135 let response: PictrsResponse = response
138 .map_err(|e| RecvError(e.to_string()))?;
140 if response.msg == "ok" {
143 Err(anyhow!("{}", &response.msg).into())
146 Err(anyhow!("pictrs_url not set up in config").into())
150 /// Both are options, since the URL might be either an html page, or an image
151 /// Returns the SiteMetadata, and a Pictrs URL, if there is a picture associated
152 pub async fn fetch_site_data(
155 ) -> (Option<SiteMetadata>, Option<Url>) {
159 // Ignore errors, since it may be an image, or not have the data.
160 // Warning, this may ignore SSL errors
161 let metadata_option = fetch_site_metadata(client, url).await.ok();
163 // Fetch pictrs thumbnail
164 let pictrs_hash = match &metadata_option {
165 Some(metadata_res) => match &metadata_res.image {
166 // Metadata, with image
167 // Try to generate a small thumbnail if there's a full sized one from post-links
168 Some(metadata_image) => fetch_pictrs(client, metadata_image)
170 .map(|r| r.files[0].file.to_owned()),
171 // Metadata, but no image
172 None => fetch_pictrs(client, url)
174 .map(|r| r.files[0].file.to_owned()),
176 // No metadata, try to fetch the URL as an image
177 None => fetch_pictrs(client, url)
179 .map(|r| r.files[0].file.to_owned()),
182 // The full urls are necessary for federation
183 let pictrs_thumbnail = pictrs_hash
186 "{}/pictrs/image/{}",
187 Settings::get().get_protocol_and_hostname(),
195 (metadata_option, pictrs_thumbnail)
197 None => (None, None),
201 async fn is_image_content_type(client: &Client, test: &Url) -> Result<(), LemmyError> {
202 let response = retry(|| client.get(test.to_owned()).send()).await?;
206 .ok_or_else(|| anyhow!("No Content-Type header"))?
208 .starts_with("image/")
212 Err(anyhow!("Not an image type.").into())
218 use crate::request::fetch_site_metadata;
221 use super::SiteMetadata;
223 // These helped with testing
225 async fn test_site_metadata() {
226 let client = reqwest::Client::default();
227 let sample_url = Url::parse("https://www.redspark.nu/en/peoples-war/district-leader-of-chand-led-cpn-arrested-in-bhojpur/").unwrap();
228 let sample_res = fetch_site_metadata(&client, &sample_url).await.unwrap();
231 title: Some("District Leader Of Chand Led CPN Arrested In Bhojpur - Redspark".to_string()),
232 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()),
233 image: Some(Url::parse("https://www.redspark.nu/wp-content/uploads/2020/03/netra-bikram-chand-attends-program-1272019033653-1000x0-845x653-1.jpg").unwrap()),
237 let youtube_url = Url::parse("https://www.youtube.com/watch?v=IquO_TcMZIQ").unwrap();
238 let youtube_res = fetch_site_metadata(&client, &youtube_url).await.unwrap();
241 title: Some("A Hard Look at Rent and Rent Seeking with Michael Hudson & Pepe Escobar".to_string()),
242 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()),
243 image: Some(Url::parse("https://i.ytimg.com/vi/IquO_TcMZIQ/maxresdefault.jpg").unwrap()),
249 // fn test_pictshare() {
250 // let res = fetch_pictshare("https://upload.wikimedia.org/wikipedia/en/2/27/The_Mandalorian_logo.jpg");
251 // assert!(res.is_ok());
252 // let res_other = fetch_pictshare("https://upload.wikimedia.org/wikipedia/en/2/27/The_Mandalorian_logo.jpgaoeu");
253 // assert!(res_other.is_err());