Adding ability to specify a custom post thumbnail.

- Context: #4204
custom_thumbnails
Dessalines 2024-02-04 15:40:51 -05:00
parent 3647a46e86
commit d9184acb17
3 changed files with 8 additions and 3 deletions

View File

@ -28,6 +28,8 @@ pub struct CreatePost {
pub honeypot: Option<String>, pub honeypot: Option<String>,
pub nsfw: Option<bool>, pub nsfw: Option<bool>,
pub language_id: Option<LanguageId>, pub language_id: Option<LanguageId>,
/// Instead of fetching a thumbnail, use a custom one.
pub custom_thumbnail: Option<Url>,
} }
#[derive(Debug, Serialize, Deserialize, Clone)] #[derive(Debug, Serialize, Deserialize, Clone)]
@ -114,6 +116,8 @@ pub struct EditPost {
pub body: Option<String>, pub body: Option<String>,
pub nsfw: Option<bool>, pub nsfw: Option<bool>,
pub language_id: Option<LanguageId>, pub language_id: Option<LanguageId>,
/// Instead of fetching a thumbnail, use a custom one.
pub custom_thumbnail: Option<Url>,
} }
#[derive(Debug, Serialize, Deserialize, Clone, Default)] #[derive(Debug, Serialize, Deserialize, Clone, Default)]

View File

@ -87,6 +87,7 @@ pub async fn create_post(
// Fetch post links and pictrs cached image // Fetch post links and pictrs cached image
let metadata = fetch_link_metadata_opt(url.as_ref(), true, &context).await; let metadata = fetch_link_metadata_opt(url.as_ref(), true, &context).await;
let url = proxy_image_link_opt_apub(url, &context).await?; let url = proxy_image_link_opt_apub(url, &context).await?;
let thumbnail_url = data.custom_thumbnail.or(metadata.thumbnail);
// Only need to check if language is allowed in case user set it explicitly. When using default // Only need to check if language is allowed in case user set it explicitly. When using default
// language, it already only returns allowed languages. // language, it already only returns allowed languages.
@ -121,7 +122,7 @@ pub async fn create_post(
.embed_description(metadata.opengraph_data.description) .embed_description(metadata.opengraph_data.description)
.embed_video_url(metadata.opengraph_data.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(thumbnail_url)
.build(); .build();
let inserted_post = Post::create(&mut context.pool(), &post_form) let inserted_post = Post::create(&mut context.pool(), &post_form)

View File

@ -70,7 +70,7 @@ pub async fn update_post(
Err(LemmyErrorType::NoPostEditAllowed)? Err(LemmyErrorType::NoPostEditAllowed)?
} }
// Fetch post links and Pictrs cached image if url was updated // Fetch post links and thumbnail if url was updated
let (embed_title, embed_description, embed_video_url, thumbnail_url) = match &url { let (embed_title, embed_description, embed_video_url, thumbnail_url) = match &url {
Some(url) => { Some(url) => {
let metadata = fetch_link_metadata(url, true, &context).await?; let metadata = fetch_link_metadata(url, true, &context).await?;
@ -78,7 +78,7 @@ pub async fn update_post(
Some(metadata.opengraph_data.title), Some(metadata.opengraph_data.title),
Some(metadata.opengraph_data.description), Some(metadata.opengraph_data.description),
Some(metadata.opengraph_data.embed_video_url), Some(metadata.opengraph_data.embed_video_url),
Some(metadata.thumbnail), Some(data.custom_thumbnail.or(metadata.thumbnail)),
) )
} }
_ => Default::default(), _ => Default::default(),