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()?;
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

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
/// 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<DbUrl> {
// 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<DbUrl> {
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<Option<DbUrl>> {
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!(

View file

@ -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();

View file

@ -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),
)
}

View file

@ -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),