A few additions for markdown-link-rule.

This commit is contained in:
Dessalines 2024-01-05 12:38:05 -05:00
parent ab5cd3d196
commit 5f257d5442
5 changed files with 33 additions and 44 deletions

View file

@ -259,15 +259,7 @@ async fn generate_pictrs_thumbnail(
let pictrs_config = context.settings().pictrs_config()?; let pictrs_config = context.settings().pictrs_config()?;
if !pictrs_config.cache_external_link_previews { if !pictrs_config.cache_external_link_previews {
return Ok( return Ok(proxy_image_link(image_url.clone(), context).await?.into());
proxy_image_link(
image_url.clone(),
context.settings().pictrs_config()?.image_proxy,
context,
)
.await?
.into(),
);
} }
// fetch remote non-pictrs images for persistent thumbnail link // fetch remote non-pictrs images for persistent thumbnail link

View file

@ -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 /// A wrapper for `proxy_image_link` for use in tests.
/// if image_proxy setting is enabled.
/// ///
/// 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. /// 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, link: Url,
image_proxy: bool, force_image_proxy: bool,
context: &LemmyContext, context: &LemmyContext,
) -> LemmyResult<DbUrl> { ) -> LemmyResult<DbUrl> {
// Dont rewrite links pointing to local domain. // Dont rewrite links pointing to local domain.
if link.domain() == Some(&context.settings().hostname) || !image_proxy { if link.domain() == Some(&context.settings().hostname) || !force_image_proxy {
return Ok(link.into()); 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!( /// Rewrite a link to go through `/api/v3/image_proxy` endpoint. This is only for remote urls and
"{}/api/v3/image_proxy?url={}", /// if image_proxy setting is enabled.
context.settings().get_protocol_and_hostname(), pub(crate) async fn proxy_image_link(link: Url, context: &LemmyContext) -> LemmyResult<DbUrl> {
encode(link.as_str()) proxy_image_link_wrapper(link, false, context).await
);
RemoteImage::create(&mut context.pool(), vec![link]).await?;
Ok(Url::parse(&proxied)?.into())
} }
pub async fn proxy_image_link_opt_api( pub async fn proxy_image_link_opt_api(
@ -910,13 +915,7 @@ pub async fn proxy_image_link_api(
None => None, None => None,
}; };
if let Some(l) = link { if let Some(l) = link {
proxy_image_link( proxy_image_link(l.into(), context).await.map(Some)
l.into(),
context.settings().pictrs_config()?.image_proxy,
context,
)
.await
.map(Some)
} else { } else {
Ok(link) Ok(link)
} }
@ -927,9 +926,7 @@ pub async fn proxy_image_link_opt_apub(
context: &LemmyContext, context: &LemmyContext,
) -> LemmyResult<Option<DbUrl>> { ) -> LemmyResult<Option<DbUrl>> {
if let Some(l) = link { if let Some(l) = link {
proxy_image_link(l, context.settings().pictrs_config()?.image_proxy, context) proxy_image_link(l, context).await.map(Some)
.await
.map(Some)
} else { } else {
Ok(None) Ok(None)
} }
@ -991,14 +988,14 @@ mod tests {
// image from local domain is unchanged // image from local domain is unchanged
let local_url = Url::parse("http://lemmy-alpha/image.png").unwrap(); 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 .await
.unwrap(); .unwrap();
assert_eq!(&local_url, proxied.inner()); assert_eq!(&local_url, proxied.inner());
// image from remote domain is proxied // image from remote domain is proxied
let remote_image = Url::parse("http://lemmy-beta/image.png").unwrap(); 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 .await
.unwrap(); .unwrap();
assert_eq!( assert_eq!(

View file

@ -116,9 +116,9 @@ pub async fn create_post(
.community_id(data.community_id) .community_id(data.community_id)
.creator_id(local_user_view.person.id) .creator_id(local_user_view.person.id)
.nsfw(data.nsfw) .nsfw(data.nsfw)
.embed_title(metadata.title) .embed_title(metadata.opengraph_data.title)
.embed_description(metadata.description) .embed_description(metadata.opengraph_data.description)
.embed_video_url(metadata.embed_video_url) .embed_video_url(metadata.opengraph_data.embed_video_url)
.language_id(language_id) .language_id(language_id)
.thumbnail_url(metadata.thumbnail) .thumbnail_url(metadata.thumbnail)
.build(); .build();

View file

@ -75,9 +75,9 @@ pub async fn update_post(
Some(url) => { Some(url) => {
let metadata = fetch_link_metadata(url, true, &context).await?; let metadata = fetch_link_metadata(url, true, &context).await?;
( (
Some(metadata.title), Some(metadata.opengraph_data.title),
Some(metadata.description), Some(metadata.opengraph_data.description),
Some(metadata.embed_video_url), Some(metadata.opengraph_data.embed_video_url),
Some(metadata.thumbnail), Some(metadata.thumbnail),
) )
} }

View file

@ -250,9 +250,9 @@ impl Object for ApubPost {
updated: page.updated.map(Into::into), updated: page.updated.map(Into::into),
deleted: Some(false), deleted: Some(false),
nsfw: page.sensitive, nsfw: page.sensitive,
embed_title: metadata.title, embed_title: metadata.opengraph_data.title,
embed_description: metadata.description, embed_description: metadata.opengraph_data.description,
embed_video_url: metadata.embed_video_url, embed_video_url: metadata.opengraph_data.embed_video_url,
thumbnail_url, thumbnail_url,
ap_id: Some(page.id.clone().into()), ap_id: Some(page.id.clone().into()),
local: Some(false), local: Some(false),