From 5f257d54423649256ee2446e31d55720a0cd113e Mon Sep 17 00:00:00 2001 From: Dessalines Date: Fri, 5 Jan 2024 12:38:05 -0500 Subject: [PATCH] A few additions for markdown-link-rule. --- crates/api_common/src/request.rs | 10 +----- crates/api_common/src/utils.rs | 49 ++++++++++++++---------------- crates/api_crud/src/post/create.rs | 6 ++-- crates/api_crud/src/post/update.rs | 6 ++-- crates/apub/src/objects/post.rs | 6 ++-- 5 files changed, 33 insertions(+), 44 deletions(-) diff --git a/crates/api_common/src/request.rs b/crates/api_common/src/request.rs index 753dc724b..10a339f42 100644 --- a/crates/api_common/src/request.rs +++ b/crates/api_common/src/request.rs @@ -259,15 +259,7 @@ async fn generate_pictrs_thumbnail( let pictrs_config = context.settings().pictrs_config()?; if !pictrs_config.cache_external_link_previews { - return Ok( - proxy_image_link( - image_url.clone(), - context.settings().pictrs_config()?.image_proxy, - context, - ) - .await? - .into(), - ); + return Ok(proxy_image_link(image_url.clone(), context).await?.into()); } // fetch remote non-pictrs images for persistent thumbnail link diff --git a/crates/api_common/src/utils.rs b/crates/api_common/src/utils.rs index a6c0951c8..dfe5c8b0d 100644 --- a/crates/api_common/src/utils.rs +++ b/crates/api_common/src/utils.rs @@ -866,28 +866,33 @@ pub async fn process_markdown_opt( } } -/// Rewrite a link to go through `/api/v3/image_proxy` endpoint. This is only for remote urls and -/// if image_proxy setting is enabled. +/// A wrapper for `proxy_image_link` for use in tests. /// -/// The parameter `image_proxy` is the config value of `pictrs.image_proxy`. Its necessary to pass +/// The parameter `force_image_proxy` is the config value of `pictrs.image_proxy`. Its necessary to pass /// as separate parameter so it can be changed in tests. -pub(crate) async fn proxy_image_link( +pub(crate) async fn proxy_image_link_wrapper( link: Url, - image_proxy: bool, + force_image_proxy: bool, context: &LemmyContext, ) -> LemmyResult { // Dont rewrite links pointing to local domain. - if link.domain() == Some(&context.settings().hostname) || !image_proxy { - return Ok(link.into()); + if link.domain() == Some(&context.settings().hostname) || !force_image_proxy { + Ok(link.into()) + } else { + let proxied = format!( + "{}/api/v3/image_proxy?url={}", + context.settings().get_protocol_and_hostname(), + encode(link.as_str()) + ); + RemoteImage::create(&mut context.pool(), vec![link]).await?; + Ok(Url::parse(&proxied)?.into()) } +} - let proxied = format!( - "{}/api/v3/image_proxy?url={}", - context.settings().get_protocol_and_hostname(), - encode(link.as_str()) - ); - RemoteImage::create(&mut context.pool(), vec![link]).await?; - Ok(Url::parse(&proxied)?.into()) +/// Rewrite a link to go through `/api/v3/image_proxy` endpoint. This is only for remote urls and +/// if image_proxy setting is enabled. +pub(crate) async fn proxy_image_link(link: Url, context: &LemmyContext) -> LemmyResult { + proxy_image_link_wrapper(link, false, context).await } pub async fn proxy_image_link_opt_api( @@ -910,13 +915,7 @@ pub async fn proxy_image_link_api( None => None, }; if let Some(l) = link { - proxy_image_link( - l.into(), - context.settings().pictrs_config()?.image_proxy, - context, - ) - .await - .map(Some) + proxy_image_link(l.into(), context).await.map(Some) } else { Ok(link) } @@ -927,9 +926,7 @@ pub async fn proxy_image_link_opt_apub( context: &LemmyContext, ) -> LemmyResult> { if let Some(l) = link { - proxy_image_link(l, context.settings().pictrs_config()?.image_proxy, context) - .await - .map(Some) + proxy_image_link(l, context).await.map(Some) } else { Ok(None) } @@ -991,14 +988,14 @@ mod tests { // image from local domain is unchanged let local_url = Url::parse("http://lemmy-alpha/image.png").unwrap(); - let proxied = proxy_image_link(local_url.clone(), true, &context) + let proxied = proxy_image_link_wrapper(local_url.clone(), true, &context) .await .unwrap(); assert_eq!(&local_url, proxied.inner()); // image from remote domain is proxied let remote_image = Url::parse("http://lemmy-beta/image.png").unwrap(); - let proxied = proxy_image_link(remote_image.clone(), true, &context) + let proxied = proxy_image_link_wrapper(remote_image.clone(), true, &context) .await .unwrap(); assert_eq!( diff --git a/crates/api_crud/src/post/create.rs b/crates/api_crud/src/post/create.rs index 3f42177c3..a3b623638 100644 --- a/crates/api_crud/src/post/create.rs +++ b/crates/api_crud/src/post/create.rs @@ -116,9 +116,9 @@ pub async fn create_post( .community_id(data.community_id) .creator_id(local_user_view.person.id) .nsfw(data.nsfw) - .embed_title(metadata.title) - .embed_description(metadata.description) - .embed_video_url(metadata.embed_video_url) + .embed_title(metadata.opengraph_data.title) + .embed_description(metadata.opengraph_data.description) + .embed_video_url(metadata.opengraph_data.embed_video_url) .language_id(language_id) .thumbnail_url(metadata.thumbnail) .build(); diff --git a/crates/api_crud/src/post/update.rs b/crates/api_crud/src/post/update.rs index c2877ec9d..c36718615 100644 --- a/crates/api_crud/src/post/update.rs +++ b/crates/api_crud/src/post/update.rs @@ -75,9 +75,9 @@ pub async fn update_post( Some(url) => { let metadata = fetch_link_metadata(url, true, &context).await?; ( - Some(metadata.title), - Some(metadata.description), - Some(metadata.embed_video_url), + Some(metadata.opengraph_data.title), + Some(metadata.opengraph_data.description), + Some(metadata.opengraph_data.embed_video_url), Some(metadata.thumbnail), ) } diff --git a/crates/apub/src/objects/post.rs b/crates/apub/src/objects/post.rs index c05ad1214..0aa4507a4 100644 --- a/crates/apub/src/objects/post.rs +++ b/crates/apub/src/objects/post.rs @@ -250,9 +250,9 @@ impl Object for ApubPost { updated: page.updated.map(Into::into), deleted: Some(false), nsfw: page.sensitive, - embed_title: metadata.title, - embed_description: metadata.description, - embed_video_url: metadata.embed_video_url, + embed_title: metadata.opengraph_data.title, + embed_description: metadata.opengraph_data.description, + embed_video_url: metadata.opengraph_data.embed_video_url, thumbnail_url, ap_id: Some(page.id.clone().into()), local: Some(false),