Add alt_text for posts. Fixes #1086

This commit is contained in:
Dessalines 2024-02-27 16:14:21 -05:00
parent 328a48c9f5
commit c5a98c5a3f
12 changed files with 31 additions and 1 deletions

View file

@ -24,6 +24,8 @@ pub struct CreatePost {
pub url: Option<Url>, pub url: Option<Url>,
/// An optional body for the post in markdown. /// An optional body for the post in markdown.
pub body: Option<String>, pub body: Option<String>,
/// An optional alt_text, usable for image posts.
pub alt_text: Option<String>,
/// A honeypot to catch bots. Should be None. /// A honeypot to catch bots. Should be None.
pub honeypot: Option<String>, pub honeypot: Option<String>,
pub nsfw: Option<bool>, pub nsfw: Option<bool>,
@ -115,6 +117,8 @@ pub struct EditPost {
pub url: Option<Url>, pub url: Option<Url>,
/// An optional body for the post in markdown. /// An optional body for the post in markdown.
pub body: Option<String>, pub body: Option<String>,
/// An optional alt_text, usable for image posts.
pub alt_text: Option<String>,
pub nsfw: Option<bool>, pub nsfw: Option<bool>,
pub language_id: Option<LanguageId>, pub language_id: Option<LanguageId>,
#[cfg_attr(feature = "full", ts(type = "string"))] #[cfg_attr(feature = "full", ts(type = "string"))]

View file

@ -50,10 +50,13 @@ pub async fn create_post(
) -> Result<Json<PostResponse>, LemmyError> { ) -> Result<Json<PostResponse>, LemmyError> {
let local_site = LocalSite::read(&mut context.pool()).await?; let local_site = LocalSite::read(&mut context.pool()).await?;
honeypot_check(&data.honeypot)?;
let slur_regex = local_site_to_slur_regex(&local_site); let slur_regex = local_site_to_slur_regex(&local_site);
check_slurs(&data.name, &slur_regex)?; check_slurs(&data.name, &slur_regex)?;
let body = process_markdown_opt(&data.body, &slur_regex, &context).await?; let body = process_markdown_opt(&data.body, &slur_regex, &context).await?;
honeypot_check(&data.honeypot)?; let alt_text = process_markdown_opt(&data.alt_text, &slur_regex, &context).await?;
let data_url = data.url.as_ref(); let data_url = data.url.as_ref();
let url = data_url.map(clean_url_params); // TODO no good way to handle a "clear" let url = data_url.map(clean_url_params); // TODO no good way to handle a "clear"
@ -61,6 +64,7 @@ pub async fn create_post(
is_valid_post_title(&data.name)?; is_valid_post_title(&data.name)?;
is_valid_body_field(&body, true)?; is_valid_body_field(&body, true)?;
is_valid_body_field(&alt_text, false)?;
check_url_scheme(&url)?; check_url_scheme(&url)?;
check_url_scheme(&custom_thumbnail)?; check_url_scheme(&custom_thumbnail)?;
@ -124,6 +128,7 @@ pub async fn create_post(
.name(data.name.trim().to_string()) .name(data.name.trim().to_string())
.url(url) .url(url)
.body(body) .body(body)
.alt_text(alt_text)
.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)

View file

@ -48,12 +48,14 @@ pub async fn update_post(
let slur_regex = local_site_to_slur_regex(&local_site); let slur_regex = local_site_to_slur_regex(&local_site);
check_slurs_opt(&data.name, &slur_regex)?; check_slurs_opt(&data.name, &slur_regex)?;
let body = process_markdown_opt(&data.body, &slur_regex, &context).await?; let body = process_markdown_opt(&data.body, &slur_regex, &context).await?;
let alt_text = process_markdown_opt(&data.alt_text, &slur_regex, &context).await?;
if let Some(name) = &data.name { if let Some(name) = &data.name {
is_valid_post_title(name)?; is_valid_post_title(name)?;
} }
is_valid_body_field(&body, true)?; is_valid_body_field(&body, true)?;
is_valid_body_field(&alt_text, false)?;
check_url_scheme(&url)?; check_url_scheme(&url)?;
check_url_scheme(&custom_thumbnail)?; check_url_scheme(&custom_thumbnail)?;
@ -116,6 +118,7 @@ pub async fn update_post(
name: data.name.clone(), name: data.name.clone(),
url, url,
body: diesel_option_overwrite(body), body: diesel_option_overwrite(body),
alt_text: diesel_option_overwrite(alt_text),
nsfw: data.nsfw, nsfw: data.nsfw,
embed_title, embed_title,
embed_description, embed_description,

View file

@ -127,6 +127,7 @@ impl Object for ApubPost {
cc: vec![], cc: vec![],
name: Some(self.name.clone()), name: Some(self.name.clone()),
content: self.body.as_ref().map(|b| markdown_to_html(b)), content: self.body.as_ref().map(|b| markdown_to_html(b)),
alt_text: self.alt_text.as_ref().map(|b| markdown_to_html(b)),
media_type: Some(MediaTypeMarkdownOrHtml::Html), media_type: Some(MediaTypeMarkdownOrHtml::Html),
source: self.body.clone().map(Source::new), source: self.body.clone().map(Source::new),
attachment, attachment,
@ -234,6 +235,7 @@ impl Object for ApubPost {
let body = read_from_string_or_source_opt(&page.content, &page.media_type, &page.source); let body = read_from_string_or_source_opt(&page.content, &page.media_type, &page.source);
let body = process_markdown_opt(&body, slur_regex, context).await?; let body = process_markdown_opt(&body, slur_regex, context).await?;
let alt_text = process_markdown_opt(&page.alt_text, slur_regex, context).await?;
let language_id = let language_id =
LanguageTag::to_language_id_single(page.language, &mut context.pool()).await?; LanguageTag::to_language_id_single(page.language, &mut context.pool()).await?;
@ -241,6 +243,7 @@ impl Object for ApubPost {
name, name,
url: url.map(Into::into), url: url.map(Into::into),
body, body,
alt_text,
creator_id: creator.id, creator_id: creator.id,
community_id: community.id, community_id: community.id,
removed: None, removed: None,

View file

@ -52,6 +52,7 @@ pub struct Page {
#[serde(deserialize_with = "deserialize_one_or_many", default)] #[serde(deserialize_with = "deserialize_one_or_many", default)]
pub(crate) cc: Vec<Url>, pub(crate) cc: Vec<Url>,
pub(crate) content: Option<String>, pub(crate) content: Option<String>,
pub(crate) alt_text: Option<String>,
pub(crate) media_type: Option<MediaTypeMarkdownOrHtml>, pub(crate) media_type: Option<MediaTypeMarkdownOrHtml>,
#[serde(deserialize_with = "deserialize_skip_error", default)] #[serde(deserialize_with = "deserialize_skip_error", default)]
pub(crate) source: Option<Source>, pub(crate) source: Option<Source>,

View file

@ -417,6 +417,7 @@ mod tests {
name: "A test post".into(), name: "A test post".into(),
url: None, url: None,
body: None, body: None,
alt_text: None,
creator_id: inserted_person.id, creator_id: inserted_person.id,
community_id: inserted_community.id, community_id: inserted_community.id,
published: inserted_post.published, published: inserted_post.published,

View file

@ -705,6 +705,7 @@ diesel::table! {
featured_community -> Bool, featured_community -> Bool,
featured_local -> Bool, featured_local -> Bool,
url_content_type -> Nullable<Text>, url_content_type -> Nullable<Text>,
alt_text -> Nullable<Text>,
} }
} }

View file

@ -56,6 +56,8 @@ pub struct Post {
/// Whether the post is featured to its site. /// Whether the post is featured to its site.
pub featured_local: bool, pub featured_local: bool,
pub url_content_type: Option<String>, pub url_content_type: Option<String>,
/// An optional alt_text, usable for image posts.
pub alt_text: Option<String>,
} }
#[derive(Debug, Clone, TypedBuilder)] #[derive(Debug, Clone, TypedBuilder)]
@ -87,6 +89,7 @@ pub struct PostInsertForm {
pub featured_community: Option<bool>, pub featured_community: Option<bool>,
pub featured_local: Option<bool>, pub featured_local: Option<bool>,
pub url_content_type: Option<String>, pub url_content_type: Option<String>,
pub alt_text: Option<String>,
} }
#[derive(Debug, Clone, Default)] #[derive(Debug, Clone, Default)]
@ -112,6 +115,7 @@ pub struct PostUpdateForm {
pub featured_community: Option<bool>, pub featured_community: Option<bool>,
pub featured_local: Option<bool>, pub featured_local: Option<bool>,
pub url_content_type: Option<String>, pub url_content_type: Option<String>,
pub alt_text: Option<Option<String>>,
} }
#[derive(PartialEq, Eq, Debug)] #[derive(PartialEq, Eq, Debug)]

View file

@ -1015,6 +1015,7 @@ mod tests {
creator_id: data.timmy_local_user_view.person.id, creator_id: data.timmy_local_user_view.person.id,
url: None, url: None,
body: None, body: None,
alt_text: None,
published: data.inserted_post.published, published: data.inserted_post.published,
updated: None, updated: None,
community_id: data.inserted_community.id, community_id: data.inserted_community.id,

View file

@ -1490,6 +1490,7 @@ mod tests {
creator_id: inserted_person.id, creator_id: inserted_person.id,
url: None, url: None,
body: None, body: None,
alt_text: None,
published: inserted_post.published, published: inserted_post.published,
updated: None, updated: None,
community_id: inserted_community.id, community_id: inserted_community.id,

View file

@ -0,0 +1,3 @@
ALTER TABLE post
DROP COLUMN alt_text;

View file

@ -0,0 +1,3 @@
ALTER TABLE post
ADD COLUMN alt_text text;