]> Untitled Git - lemmy.git/blobdiff - crates/api_common/src/request.rs
Embed Peertube videos (#2261)
[lemmy.git] / crates / api_common / src / request.rs
index c5f750115a7ca9495ae4327ccac24cfc326574b0..8578996093ed5f512ca254d0c260b78938a93fe1 100644 (file)
@@ -1,5 +1,6 @@
 use crate::post::SiteMetadata;
 use encoding::{all::encodings, DecoderTrap};
+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;
@@ -77,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),
   })
 }
 
@@ -139,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
@@ -179,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),
   }
@@ -235,8 +237,9 @@ 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
     );