]> Untitled Git - lemmy.git/blobdiff - crates/api_common/src/request.rs
Embed Peertube videos (#2261)
[lemmy.git] / crates / api_common / src / request.rs
index 7e3b2152cbbdbf4618b2007522cd42f298b775d1..8578996093ed5f512ca254d0c260b78938a93fe1 100644 (file)
@@ -1,6 +1,7 @@
 use crate::post::SiteMetadata;
 use encoding::{all::encodings, DecoderTrap};
-use lemmy_utils::{settings::structs::Settings, version::VERSION, LemmyError, REQWEST_TIMEOUT};
+use lemmy_db_schema::newtypes::DbUrl;
+use lemmy_utils::{error::LemmyError, settings::structs::Settings, version::VERSION};
 use percent_encoding::{utf8_percent_encode, NON_ALPHANUMERIC};
 use reqwest_middleware::ClientWithMiddleware;
 use serde::Deserialize;
@@ -15,11 +16,7 @@ pub async fn fetch_site_metadata(
   url: &Url,
 ) -> Result<SiteMetadata, LemmyError> {
   info!("Fetching site metadata for url: {}", url);
-  let response = client
-    .get(url.as_str())
-    .timeout(REQWEST_TIMEOUT)
-    .send()
-    .await?;
+  let response = client.get(url.as_str()).send().await?;
 
   // Can't use .text() here, because it only checks the content header, not the actual bytes
   // https://github.com/LemmyNet/lemmy/issues/1964
@@ -81,16 +78,17 @@ fn html_to_site_metadata(html_bytes: &[u8]) -> Result<SiteMetadata, LemmyError>
     .images
     .get(0)
     .and_then(|ogo| Url::parse(&ogo.url).ok());
-
-  let title = og_title.or(page_title);
-  let description = og_description.or(page_description);
-  let image = og_image;
+  let og_embed_url = page
+    .opengraph
+    .videos
+    .first()
+    .and_then(|v| Url::parse(&v.url).ok());
 
   Ok(SiteMetadata {
-    title,
-    description,
-    image,
-    html: None,
+    title: og_title.or(page_title),
+    description: og_description.or(page_description),
+    image: og_image.map(Into::into),
+    embed_video_url: og_embed_url.map(Into::into),
   })
 }
 
@@ -122,11 +120,7 @@ pub(crate) async fn fetch_pictrs(
       utf8_percent_encode(image_url.as_str(), NON_ALPHANUMERIC) // TODO this might not be needed
     );
 
-    let response = client
-      .get(&fetch_url)
-      .timeout(REQWEST_TIMEOUT)
-      .send()
-      .await?;
+    let response = client.get(&fetch_url).send().await?;
 
     let response: PictrsResponse = response.json().await.map_err(LemmyError::from)?;
 
@@ -147,7 +141,7 @@ pub async fn fetch_site_data(
   client: &ClientWithMiddleware,
   settings: &Settings,
   url: Option<&Url>,
-) -> (Option<SiteMetadata>, Option<Url>) {
+) -> (Option<SiteMetadata>, Option<DbUrl>) {
   match &url {
     Some(url) => {
       // Fetch metadata
@@ -187,7 +181,7 @@ pub async fn fetch_site_data(
         .ok()
         .flatten();
 
-      (metadata_option, pictrs_thumbnail)
+      (metadata_option, pictrs_thumbnail.map(Into::into))
     }
     None => (None, None),
   }
@@ -195,11 +189,7 @@ pub async fn fetch_site_data(
 
 #[tracing::instrument(skip_all)]
 async fn is_image_content_type(client: &ClientWithMiddleware, url: &Url) -> Result<(), LemmyError> {
-  let response = client
-    .get(url.as_str())
-    .timeout(REQWEST_TIMEOUT)
-    .send()
-    .await?;
+  let response = client.get(url.as_str()).send().await?;
   if response
     .headers()
     .get("Content-Type")
@@ -247,21 +237,12 @@ mod tests {
         image: Some(
           Url::parse("https://gitlab.com/uploads/-/system/project/avatar/4877469/iod_logo.png")
             .unwrap()
+            .into()
         ),
-        html: None,
+        embed_video_url: None,
       },
       sample_res
     );
-
-    let youtube_url = Url::parse("https://www.youtube.com/watch?v=IquO_TcMZIQ").unwrap();
-    let youtube_res = fetch_site_metadata(&client, &youtube_url).await.unwrap();
-    assert_eq!(
-      SiteMetadata {
-        title: Some("A Hard Look at Rent and Rent Seeking with Michael Hudson & Pepe Escobar".to_string()),
-        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()),
-        image: Some(Url::parse("https://i.ytimg.com/vi/IquO_TcMZIQ/maxresdefault.jpg").unwrap()),
-        html: None,
-      }, youtube_res);
   }
 
   // #[test]