-
-
Notifications
You must be signed in to change notification settings - Fork 882
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Markdown link rule-dess #4356
Merged
Merged
Markdown link rule-dess #4356
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
aa6f0c3
Merge branch 'main' into markdown-link-rule
Nutomic bdc507f
Merge branch 'markdown-link-rule' of https://github.com/LemmyNet/lemm…
dessalines ab5cd3d
Extracting opengraph_data to its own type.
dessalines 5f257d5
A few additions for markdown-link-rule.
dessalines File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,10 @@ | ||
use crate::{context::LemmyContext, post::LinkMetadata, utils::proxy_image_link}; | ||
use crate::{ | ||
context::LemmyContext, | ||
post::{LinkMetadata, OpenGraphData}, | ||
utils::proxy_image_link, | ||
}; | ||
use encoding::{all::encodings, DecoderTrap}; | ||
use lemmy_db_schema::newtypes::DbUrl; | ||
use lemmy_utils::{ | ||
error::{LemmyError, LemmyErrorType}, | ||
settings::structs::Settings, | ||
|
@@ -43,29 +48,28 @@ pub async fn fetch_link_metadata( | |
.get(CONTENT_TYPE) | ||
.and_then(|h| h.to_str().ok()) | ||
.and_then(|h| h.parse().ok()); | ||
let is_image = content_type.as_ref().unwrap_or(&mime::TEXT_PLAIN).type_() == mime::IMAGE; | ||
|
||
// Can't use .text() here, because it only checks the content header, not the actual bytes | ||
// https://github.com/LemmyNet/lemmy/issues/1964 | ||
let html_bytes = response.bytes().await.map_err(LemmyError::from)?.to_vec(); | ||
|
||
let mut metadata = extract_opengraph_data(&html_bytes, url).unwrap_or_default(); | ||
let opengraph_data = extract_opengraph_data(&html_bytes, url).unwrap_or_default(); | ||
let thumbnail = extract_thumbnail_from_opengraph_data( | ||
url, | ||
&opengraph_data, | ||
&content_type, | ||
generate_thumbnail, | ||
context, | ||
) | ||
.await; | ||
|
||
metadata.content_type = content_type.map(|c| c.to_string()); | ||
if generate_thumbnail && is_image { | ||
let image_url = metadata | ||
.image | ||
.as_ref() | ||
.map(lemmy_db_schema::newtypes::DbUrl::inner) | ||
.unwrap_or(url); | ||
metadata.thumbnail = generate_pictrs_thumbnail(image_url, context) | ||
.await | ||
.ok() | ||
.map(Into::into); | ||
} | ||
|
||
Ok(metadata) | ||
Ok(LinkMetadata { | ||
opengraph_data, | ||
content_type: content_type.map(|c| c.to_string()), | ||
thumbnail, | ||
}) | ||
} | ||
|
||
#[tracing::instrument(skip_all)] | ||
pub async fn fetch_link_metadata_opt( | ||
url: Option<&Url>, | ||
|
@@ -81,7 +85,7 @@ pub async fn fetch_link_metadata_opt( | |
} | ||
|
||
/// Extract site metadata from HTML Opengraph attributes. | ||
fn extract_opengraph_data(html_bytes: &[u8], url: &Url) -> Result<LinkMetadata, LemmyError> { | ||
fn extract_opengraph_data(html_bytes: &[u8], url: &Url) -> Result<OpenGraphData, LemmyError> { | ||
let html = String::from_utf8_lossy(html_bytes); | ||
|
||
// Make sure the first line is doctype html | ||
|
@@ -137,16 +141,38 @@ fn extract_opengraph_data(html_bytes: &[u8], url: &Url) -> Result<LinkMetadata, | |
// join also works if the target URL is absolute | ||
.and_then(|v| url.join(&v.url).ok()); | ||
|
||
Ok(LinkMetadata { | ||
Ok(OpenGraphData { | ||
title: og_title.or(page_title), | ||
description: og_description.or(page_description), | ||
image: og_image.map(Into::into), | ||
embed_video_url: og_embed_url.map(Into::into), | ||
content_type: None, | ||
thumbnail: None, | ||
}) | ||
} | ||
|
||
#[tracing::instrument(skip_all)] | ||
pub async fn extract_thumbnail_from_opengraph_data( | ||
url: &Url, | ||
opengraph_data: &OpenGraphData, | ||
content_type: &Option<Mime>, | ||
generate_thumbnail: bool, | ||
context: &LemmyContext, | ||
) -> Option<DbUrl> { | ||
let is_image = content_type.as_ref().unwrap_or(&mime::TEXT_PLAIN).type_() == mime::IMAGE; | ||
if generate_thumbnail && is_image { | ||
let image_url = opengraph_data | ||
.image | ||
.as_ref() | ||
.map(lemmy_db_schema::newtypes::DbUrl::inner) | ||
.unwrap_or(url); | ||
generate_pictrs_thumbnail(image_url, context) | ||
.await | ||
.ok() | ||
.map(Into::into) | ||
} else { | ||
None | ||
} | ||
} | ||
|
||
#[derive(Deserialize, Debug)] | ||
struct PictrsResponse { | ||
files: Vec<PictrsFile>, | ||
|
@@ -233,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()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I commented in the other PR, but this makes it seem like we need to get rid of one of these two pictrs settings bools, since they do the same thing. |
||
} | ||
|
||
// fetch remote non-pictrs images for persistent thumbnail link | ||
|
@@ -314,21 +332,21 @@ mod tests { | |
.unwrap(); | ||
assert_eq!( | ||
Some("FAQ · Wiki · IzzyOnDroid / repo · GitLab".to_string()), | ||
sample_res.title | ||
sample_res.opengraph_data.title | ||
); | ||
assert_eq!( | ||
Some("The F-Droid compatible repo at https://apt.izzysoft.de/fdroid/".to_string()), | ||
sample_res.description | ||
sample_res.opengraph_data.description | ||
); | ||
assert_eq!( | ||
Some( | ||
Url::parse("https://gitlab.com/uploads/-/system/project/avatar/4877469/iod_logo.png") | ||
.unwrap() | ||
.into() | ||
), | ||
sample_res.image | ||
sample_res.opengraph_data.image | ||
); | ||
assert_eq!(None, sample_res.embed_video_url); | ||
assert_eq!(None, sample_res.opengraph_data.embed_video_url); | ||
assert_eq!( | ||
Some(mime::TEXT_HTML_UTF_8.to_string()), | ||
sample_res.content_type | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should work okay without any breaking changes.