From ecd8e3b11b5292bad73d48c2fbf11db00bc432c2 Mon Sep 17 00:00:00 2001 From: Felix Ableitner Date: Mon, 6 Nov 2023 12:28:05 +0100 Subject: [PATCH] federate post url as image depending on mime type --- crates/api_common/src/request.rs | 2 +- crates/apub/src/objects/post.rs | 2 +- crates/apub/src/protocol/objects/page.rs | 22 +++++++++++++++------- crates/utils/src/settings/structs.rs | 3 ++- crates/utils/translations | 2 +- 5 files changed, 20 insertions(+), 11 deletions(-) diff --git a/crates/api_common/src/request.rs b/crates/api_common/src/request.rs index 3e2311f8f..aeb92b7d8 100644 --- a/crates/api_common/src/request.rs +++ b/crates/api_common/src/request.rs @@ -234,7 +234,7 @@ async fn generate_pictrs_thumbnail( let pictrs_config = context.settings().pictrs_config()?; if pictrs_config.disable_external_link_previews { - return Ok(proxy_image_link(image_url.clone(), context)?); + return Ok(proxy_image_link(image_url.clone(), context).await?.into()); } // fetch remote non-pictrs images for persistent thumbnail link diff --git a/crates/apub/src/objects/post.rs b/crates/apub/src/objects/post.rs index 5a652747f..348d473f3 100644 --- a/crates/apub/src/objects/post.rs +++ b/crates/apub/src/objects/post.rs @@ -116,7 +116,7 @@ impl Object for ApubPost { let attachment = self .url .clone() - .map(|url| Attachment::new(url, self.url_content_type.clone())) + .map(|url| Attachment::new(url.into(), self.url_content_type.clone())) .into_iter() .collect(); diff --git a/crates/apub/src/protocol/objects/page.rs b/crates/apub/src/protocol/objects/page.rs index 85763dad4..149c0c9b7 100644 --- a/crates/apub/src/protocol/objects/page.rs +++ b/crates/apub/src/protocol/objects/page.rs @@ -20,7 +20,6 @@ use activitypub_federation::{ use chrono::{DateTime, Utc}; use itertools::Itertools; use lemmy_api_common::context::LemmyContext; -use lemmy_db_schema::newtypes::DbUrl; use lemmy_utils::error::{LemmyError, LemmyErrorType}; use serde::{de::Error, Deserialize, Deserializer, Serialize}; use serde_with::skip_serializing_none; @@ -168,12 +167,21 @@ impl Page { } impl Attachment { - pub(crate) fn new(url: DbUrl, media_type: Option) -> Attachment { - Attachment::Link(Link { - href: url.into(), - media_type, - r#type: Default::default(), - }) + /// Creates new attachment for a given link and mime type. + pub(crate) fn new(url: Url, media_type: Option) -> Attachment { + let is_image = media_type.clone().unwrap_or_default().starts_with("image"); + if is_image { + Attachment::Image(Image { + kind: Default::default(), + url, + }) + } else { + Attachment::Link(Link { + href: url, + media_type, + r#type: Default::default(), + }) + } } } diff --git a/crates/utils/src/settings/structs.rs b/crates/utils/src/settings/structs.rs index 39153f4a8..a7d60f9ba 100644 --- a/crates/utils/src/settings/structs.rs +++ b/crates/utils/src/settings/structs.rs @@ -91,7 +91,8 @@ pub struct PictrsConfig { /// storage usage. It may also be necessary in certain countries which don't allow copying images /// from newspaper articles, but only allow hotlinking. /// - /// If this setting is enabled, the Opengraph image is directly returned as thumbnail. + /// If this setting is enabled, the Opengraph image is directly returned as thumbnail. Note that + /// it still gets proxied through the local instance if `image_proxy` is enabled. #[default(false)] pub disable_external_link_previews: bool, } diff --git a/crates/utils/translations b/crates/utils/translations index e943f97fe..83540721c 160000 --- a/crates/utils/translations +++ b/crates/utils/translations @@ -1 +1 @@ -Subproject commit e943f97fe481dc425acdebc8872bf1fdcabaf875 +Subproject commit 83540721c6035c701f4ef1daf7d95f78a51fedf7