]> Untitled Git - lemmy.git/commitdiff
Embed Peertube videos (#2261)
authorNutomic <me@nutomic.com>
Thu, 2 Jun 2022 21:44:47 +0000 (21:44 +0000)
committerGitHub <noreply@github.com>
Thu, 2 Jun 2022 21:44:47 +0000 (21:44 +0000)
* Use og:video attribute for embeds, change Post.embed_html to embed_url

* fix clippy

12 files changed:
crates/api_common/src/post.rs
crates/api_common/src/request.rs
crates/api_crud/src/post/create.rs
crates/api_crud/src/post/update.rs
crates/apub/src/objects/post.rs
crates/db_schema/src/impls/post.rs
crates/db_schema/src/schema.rs
crates/db_schema/src/source/post.rs
crates/db_views/src/comment_view.rs
crates/db_views/src/post_view.rs
migrations/2022-05-20-135341_embed-url/down.sql [new file with mode: 0644]
migrations/2022-05-20-135341_embed-url/up.sql [new file with mode: 0644]

index 38aceed9ed9b118d4361dcfcf38a7b48e13470e8..8c05790e92c21f0ba6bc568fe1420af5fbcab1a0 100644 (file)
@@ -1,6 +1,6 @@
 use crate::sensitive::Sensitive;
 use lemmy_db_schema::{
-  newtypes::{CommunityId, PostId, PostReportId},
+  newtypes::{CommunityId, DbUrl, PostId, PostReportId},
   ListingType,
   SortType,
 };
@@ -166,6 +166,6 @@ pub struct GetSiteMetadataResponse {
 pub struct SiteMetadata {
   pub title: Option<String>,
   pub description: Option<String>,
-  pub(crate) image: Option<Url>,
-  pub html: Option<String>,
+  pub(crate) image: Option<DbUrl>,
+  pub embed_video_url: Option<DbUrl>,
 }
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
     );
index 6ef706d0a2513d66ae1ac8c6b1f655a719375480..9151e3ba1c636fd4fc87e903288b40100028b0c1 100644 (file)
@@ -86,11 +86,11 @@ impl PerformCrud for CreatePost {
 
     // Fetch post links and pictrs cached image
     let data_url = data.url.as_ref();
-    let (metadata_res, pictrs_thumbnail) =
+    let (metadata_res, thumbnail_url) =
       fetch_site_data(context.client(), &context.settings(), data_url).await;
-    let (embed_title, embed_description, embed_html) = metadata_res
-      .map(|u| (u.title, u.description, u.html))
-      .unwrap_or((None, None, None));
+    let (embed_title, embed_description, embed_video_url) = metadata_res
+      .map(|u| (u.title, u.description, u.embed_video_url))
+      .unwrap_or_default();
 
     let post_form = PostForm {
       name: data.name.trim().to_owned(),
@@ -101,8 +101,8 @@ impl PerformCrud for CreatePost {
       nsfw: data.nsfw,
       embed_title,
       embed_description,
-      embed_html,
-      thumbnail_url: pictrs_thumbnail.map(|u| u.into()),
+      embed_video_url,
+      thumbnail_url,
       ..PostForm::default()
     };
 
index 285a08b55277cfc178912f4cc5ff71d3bc48ff20..3f3e0c2a0537fe97a60ec95c4a65c043bbbf977e 100644 (file)
@@ -69,11 +69,11 @@ impl PerformCrud for EditPost {
 
     // Fetch post links and Pictrs cached image
     let data_url = data.url.as_ref();
-    let (metadata_res, pictrs_thumbnail) =
+    let (metadata_res, thumbnail_url) =
       fetch_site_data(context.client(), &context.settings(), data_url).await;
-    let (embed_title, embed_description, embed_html) = metadata_res
-      .map(|u| (u.title, u.description, u.html))
-      .unwrap_or((None, None, None));
+    let (embed_title, embed_description, embed_video_url) = metadata_res
+      .map(|u| (u.title, u.description, u.embed_video_url))
+      .unwrap_or_default();
 
     let post_form = PostForm {
       creator_id: orig_post.creator_id.to_owned(),
@@ -85,8 +85,8 @@ impl PerformCrud for EditPost {
       updated: Some(naive_now()),
       embed_title,
       embed_description,
-      embed_html,
-      thumbnail_url: pictrs_thumbnail.map(|u| u.into()),
+      embed_video_url,
+      thumbnail_url,
       ..PostForm::default()
     };
 
index bdefe2802cee957f587a92679d8324697fd889bc..65bcbda68ae93751baeec6d2b13cde003c7d3ba5 100644 (file)
@@ -167,15 +167,14 @@ impl ApubObject for ApubPost {
         // url sent by lemmy (old)
         page.url
       };
-      let thumbnail_url: Option<Url> = page.image.map(|i| i.url);
-      let (metadata_res, pictrs_thumbnail) = if let Some(url) = &url {
+      let (metadata_res, thumbnail_url) = if let Some(url) = &url {
         fetch_site_data(context.client(), &context.settings(), Some(url)).await
       } else {
-        (None, thumbnail_url)
+        (None, page.image.map(|i| i.url.into()))
       };
-      let (embed_title, embed_description, embed_html) = metadata_res
-        .map(|u| (u.title, u.description, u.html))
-        .unwrap_or((None, None, None));
+      let (embed_title, embed_description, embed_video_url) = metadata_res
+        .map(|u| (u.title, u.description, u.embed_video_url))
+        .unwrap_or_default();
       let body_slurs_removed =
         read_from_string_or_source_opt(&page.content, &page.media_type, &page.source)
           .map(|s| remove_slurs(&s, &context.settings().slur_regex()));
@@ -195,8 +194,8 @@ impl ApubObject for ApubPost {
         stickied: page.stickied,
         embed_title,
         embed_description,
-        embed_html,
-        thumbnail_url: pictrs_thumbnail.map(|u| u.into()),
+        embed_video_url,
+        thumbnail_url,
         ap_id: Some(page.id.clone().into()),
         local: Some(false),
       }
index c878931a1ea2a470fdcc3572000063d9130e32ef..03a4b7192ba939fe351725d5245992c84ee669f9 100644 (file)
@@ -251,7 +251,7 @@ impl DeleteableOrRemoveable for Post {
     self.body = None;
     self.embed_title = None;
     self.embed_description = None;
-    self.embed_html = None;
+    self.embed_video_url = None;
     self.thumbnail_url = None;
 
     self
@@ -316,7 +316,7 @@ mod tests {
       updated: None,
       embed_title: None,
       embed_description: None,
-      embed_html: None,
+      embed_video_url: None,
       thumbnail_url: None,
       ap_id: inserted_post.ap_id.to_owned(),
       local: true,
index 666986a691cb9e5a476c98a0cf7fcc0132dd5c9b..6146b9e3056d764d4b3f53886c08b45e41359e26 100644 (file)
@@ -357,7 +357,7 @@ table! {
         stickied -> Bool,
         embed_title -> Nullable<Text>,
         embed_description -> Nullable<Text>,
-        embed_html -> Nullable<Text>,
+        embed_video_url -> Nullable<Text>,
         thumbnail_url -> Nullable<Text>,
         ap_id -> Varchar,
         local -> Bool,
index a3054e4080650755181c3387c6469b573976ef50..f8e7dc36c36ce0992817c90d3e9f186724e75b97 100644 (file)
@@ -23,7 +23,7 @@ pub struct Post {
   pub stickied: bool,
   pub embed_title: Option<String>,
   pub embed_description: Option<String>,
-  pub embed_html: Option<String>,
+  pub embed_video_url: Option<DbUrl>,
   pub thumbnail_url: Option<DbUrl>,
   pub ap_id: DbUrl,
   pub local: bool,
@@ -47,7 +47,7 @@ pub struct PostForm {
   pub stickied: Option<bool>,
   pub embed_title: Option<String>,
   pub embed_description: Option<String>,
-  pub embed_html: Option<String>,
+  pub embed_video_url: Option<DbUrl>,
   pub thumbnail_url: Option<DbUrl>,
   pub ap_id: Option<DbUrl>,
   pub local: Option<bool>,
index 87ec3c07cd1e73be9499421e5a15fd2ac94a462f..53aac9519e88bedac25c04e6dfac928d1ad0311d 100644 (file)
@@ -673,7 +673,7 @@ mod tests {
         nsfw: false,
         embed_title: None,
         embed_description: None,
-        embed_html: None,
+        embed_video_url: None,
         thumbnail_url: None,
         ap_id: inserted_post.ap_id.to_owned(),
         local: true,
index 2fe6d216f8a3cba086b89ca217acb65c65426a5a..809b9986014057deead56c40070bfea81891506d 100644 (file)
@@ -646,7 +646,7 @@ mod tests {
         nsfw: false,
         embed_title: None,
         embed_description: None,
-        embed_html: None,
+        embed_video_url: None,
         thumbnail_url: None,
         ap_id: inserted_post.ap_id.to_owned(),
         local: true,
diff --git a/migrations/2022-05-20-135341_embed-url/down.sql b/migrations/2022-05-20-135341_embed-url/down.sql
new file mode 100644 (file)
index 0000000..1c4ff1f
--- /dev/null
@@ -0,0 +1,2 @@
+alter table post drop column embed_url;
+alter table post add column embed_video_url text;
\ No newline at end of file
diff --git a/migrations/2022-05-20-135341_embed-url/up.sql b/migrations/2022-05-20-135341_embed-url/up.sql
new file mode 100644 (file)
index 0000000..47d0df6
--- /dev/null
@@ -0,0 +1,2 @@
+alter table post drop column embed_html;
+alter table post add column embed_video_url text;
\ No newline at end of file