From 06257f9e0dd640d45e54c7b4a1fc64fea2a85828 Mon Sep 17 00:00:00 2001 From: Felix Ableitner Date: Thu, 21 Dec 2023 13:25:12 +0100 Subject: [PATCH] fix test --- crates/api_common/src/request.rs | 10 +++++- crates/api_common/src/utils.rs | 48 ++++++++++++++++--------- crates/api_crud/src/community/create.rs | 25 +++---------- 3 files changed, 45 insertions(+), 38 deletions(-) diff --git a/crates/api_common/src/request.rs b/crates/api_common/src/request.rs index 8cd46b8bd..77e2f697f 100644 --- a/crates/api_common/src/request.rs +++ b/crates/api_common/src/request.rs @@ -234,7 +234,15 @@ 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).await?.into()); + return Ok( + proxy_image_link( + image_url.clone(), + context.settings().pictrs_config()?.image_proxy, + 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 87faf5698..aee25f7a5 100644 --- a/crates/api_common/src/utils.rs +++ b/crates/api_common/src/utils.rs @@ -862,11 +862,13 @@ pub async fn process_markdown_opt( } } -pub async fn proxy_image_link(link: Url, context: &LemmyContext) -> LemmyResult { +pub(crate) async fn proxy_image_link( + link: Url, + image_proxy: bool, + context: &LemmyContext, +) -> LemmyResult { // Dont rewrite links pointing to local domain. - if link.domain() == Some(&context.settings().hostname) - || !context.settings().pictrs_config()?.image_proxy - { + if link.domain() == Some(&context.settings().hostname) || !image_proxy { return Ok(link.into()); } @@ -883,19 +885,29 @@ pub async fn proxy_image_link_opt_api( link: &Option, context: &LemmyContext, ) -> LemmyResult>> { - let link: Option> = match link.as_ref().map(String::as_str) { + proxy_image_link_api(link, context).await.map(Some) +} + +pub async fn proxy_image_link_api( + link: &Option, + context: &LemmyContext, +) -> LemmyResult> { + let link: Option = match link.as_ref().map(String::as_str) { // An empty string is an erase - Some("") => Some(None), + Some("") => None, Some(str_url) => Url::parse(str_url) - .map(|u| Some(Some(u.into()))) + .map(|u| Some(u.into())) .with_lemmy_type(LemmyErrorType::InvalidUrl)?, None => None, }; - if let Some(Some(l)) = link { - proxy_image_link(l.into(), context) - .await - .map(Some) - .map(Some) + if let Some(l) = link { + proxy_image_link( + l.into(), + context.settings().pictrs_config()?.image_proxy, + context, + ) + .await + .map(Some) } else { Ok(link) } @@ -906,7 +918,9 @@ pub async fn proxy_image_link_opt_apub( context: &LemmyContext, ) -> LemmyResult> { if let Some(l) = link { - proxy_image_link(l, context).await.map(Some) + proxy_image_link(l, context.settings().pictrs_config()?.image_proxy, context) + .await + .map(Some) } else { Ok(None) } @@ -967,12 +981,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(), &context).await.unwrap(); + let proxied = proxy_image_link(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(), &context) + let proxied = proxy_image_link(remote_image.clone(), true, &context) .await .unwrap(); assert_eq!( @@ -992,7 +1008,7 @@ mod tests { let context = LemmyContext::init_test_context().await; assert!(matches!( - proxy_image_link_opt_api(&None, &context).await, + proxy_image_link_api(&None, &context).await, Ok(None) )); assert!(matches!( diff --git a/crates/api_crud/src/community/create.rs b/crates/api_crud/src/community/create.rs index f062a3ec6..435a0e8b7 100644 --- a/crates/api_crud/src/community/create.rs +++ b/crates/api_crud/src/community/create.rs @@ -12,12 +12,12 @@ use lemmy_api_common::{ is_admin, local_site_to_slur_regex, process_markdown_opt, - proxy_image_link, + proxy_image_link_api, + proxy_image_link_opt_api, EndpointType, }, }; use lemmy_db_schema::{ - newtypes::DbUrl, source::{ actor_language::{CommunityLanguage, SiteLanguage}, community::{ @@ -39,7 +39,6 @@ use lemmy_utils::{ validation::{is_valid_actor_name, is_valid_body_field}, }, }; -use url::Url; #[tracing::instrument(skip(context))] pub async fn create_community( @@ -58,8 +57,8 @@ pub async fn create_community( check_slurs(&data.name, &slur_regex)?; check_slurs(&data.title, &slur_regex)?; let description = process_markdown_opt(&data.description, &slur_regex, &context).await?; - let icon = proxy_image_link_create(&data.icon, &context).await?; - let banner = proxy_image_link_create(&data.banner, &context).await?; + let icon = proxy_image_link_api(&data.icon, &context).await?; + let banner = proxy_image_link_api(&data.banner, &context).await?; is_valid_actor_name(&data.name, local_site.actor_name_max_length as usize)?; is_valid_body_field(&data.description, false)?; @@ -136,19 +135,3 @@ pub async fn create_community( build_community_response(&context, local_user_view, community_id).await } - -async fn proxy_image_link_create( - opt: &Option, - context: &LemmyContext, -) -> Result, LemmyError> { - match opt.as_ref().map(String::as_str) { - // An empty string is nothing - Some("") => Ok(None), - Some(str_url) => { - let url = Url::parse(str_url).with_lemmy_type(LemmyErrorType::InvalidUrl)?; - let url = proxy_image_link(url, context).await?; - Ok(Some(url)) - } - None => Ok(None), - } -}