From c5a98c5a3ff5c666e5a44fcba76a2d72d7c30fae Mon Sep 17 00:00:00 2001 From: Dessalines Date: Tue, 27 Feb 2024 16:14:21 -0500 Subject: [PATCH] Add alt_text for posts. Fixes #1086 --- crates/api_common/src/post.rs | 4 ++++ crates/api_crud/src/post/create.rs | 7 ++++++- crates/api_crud/src/post/update.rs | 3 +++ crates/apub/src/objects/post.rs | 3 +++ crates/apub/src/protocol/objects/page.rs | 1 + crates/db_schema/src/impls/post.rs | 1 + crates/db_schema/src/schema.rs | 1 + crates/db_schema/src/source/post.rs | 4 ++++ crates/db_views/src/comment_view.rs | 1 + crates/db_views/src/post_view.rs | 1 + migrations/2024-02-27-204628_add_post_alt_text/down.sql | 3 +++ migrations/2024-02-27-204628_add_post_alt_text/up.sql | 3 +++ 12 files changed, 31 insertions(+), 1 deletion(-) create mode 100644 migrations/2024-02-27-204628_add_post_alt_text/down.sql create mode 100644 migrations/2024-02-27-204628_add_post_alt_text/up.sql diff --git a/crates/api_common/src/post.rs b/crates/api_common/src/post.rs index 1db07e451..f953ea319 100644 --- a/crates/api_common/src/post.rs +++ b/crates/api_common/src/post.rs @@ -24,6 +24,8 @@ pub struct CreatePost { pub url: Option, /// An optional body for the post in markdown. pub body: Option, + /// An optional alt_text, usable for image posts. + pub alt_text: Option, /// A honeypot to catch bots. Should be None. pub honeypot: Option, pub nsfw: Option, @@ -115,6 +117,8 @@ pub struct EditPost { pub url: Option, /// An optional body for the post in markdown. pub body: Option, + /// An optional alt_text, usable for image posts. + pub alt_text: Option, pub nsfw: Option, pub language_id: Option, #[cfg_attr(feature = "full", ts(type = "string"))] diff --git a/crates/api_crud/src/post/create.rs b/crates/api_crud/src/post/create.rs index a5a9c013f..684e98645 100644 --- a/crates/api_crud/src/post/create.rs +++ b/crates/api_crud/src/post/create.rs @@ -50,10 +50,13 @@ pub async fn create_post( ) -> Result, LemmyError> { let local_site = LocalSite::read(&mut context.pool()).await?; + honeypot_check(&data.honeypot)?; + let slur_regex = local_site_to_slur_regex(&local_site); check_slurs(&data.name, &slur_regex)?; + 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 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_body_field(&body, true)?; + is_valid_body_field(&alt_text, false)?; check_url_scheme(&url)?; check_url_scheme(&custom_thumbnail)?; @@ -124,6 +128,7 @@ pub async fn create_post( .name(data.name.trim().to_string()) .url(url) .body(body) + .alt_text(alt_text) .community_id(data.community_id) .creator_id(local_user_view.person.id) .nsfw(data.nsfw) diff --git a/crates/api_crud/src/post/update.rs b/crates/api_crud/src/post/update.rs index e858d9b30..fd1e43312 100644 --- a/crates/api_crud/src/post/update.rs +++ b/crates/api_crud/src/post/update.rs @@ -48,12 +48,14 @@ pub async fn update_post( let slur_regex = local_site_to_slur_regex(&local_site); check_slurs_opt(&data.name, &slur_regex)?; 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 { is_valid_post_title(name)?; } is_valid_body_field(&body, true)?; + is_valid_body_field(&alt_text, false)?; check_url_scheme(&url)?; check_url_scheme(&custom_thumbnail)?; @@ -116,6 +118,7 @@ pub async fn update_post( name: data.name.clone(), url, body: diesel_option_overwrite(body), + alt_text: diesel_option_overwrite(alt_text), nsfw: data.nsfw, embed_title, embed_description, diff --git a/crates/apub/src/objects/post.rs b/crates/apub/src/objects/post.rs index 681fef2c4..47cb87b57 100644 --- a/crates/apub/src/objects/post.rs +++ b/crates/apub/src/objects/post.rs @@ -127,6 +127,7 @@ impl Object for ApubPost { cc: vec![], name: Some(self.name.clone()), 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), source: self.body.clone().map(Source::new), 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 = process_markdown_opt(&body, slur_regex, context).await?; + let alt_text = process_markdown_opt(&page.alt_text, slur_regex, context).await?; let language_id = LanguageTag::to_language_id_single(page.language, &mut context.pool()).await?; @@ -241,6 +243,7 @@ impl Object for ApubPost { name, url: url.map(Into::into), body, + alt_text, creator_id: creator.id, community_id: community.id, removed: None, diff --git a/crates/apub/src/protocol/objects/page.rs b/crates/apub/src/protocol/objects/page.rs index fbcb98254..096d16091 100644 --- a/crates/apub/src/protocol/objects/page.rs +++ b/crates/apub/src/protocol/objects/page.rs @@ -52,6 +52,7 @@ pub struct Page { #[serde(deserialize_with = "deserialize_one_or_many", default)] pub(crate) cc: Vec, pub(crate) content: Option, + pub(crate) alt_text: Option, pub(crate) media_type: Option, #[serde(deserialize_with = "deserialize_skip_error", default)] pub(crate) source: Option, diff --git a/crates/db_schema/src/impls/post.rs b/crates/db_schema/src/impls/post.rs index f49af6226..2f01ba02a 100644 --- a/crates/db_schema/src/impls/post.rs +++ b/crates/db_schema/src/impls/post.rs @@ -417,6 +417,7 @@ mod tests { name: "A test post".into(), url: None, body: None, + alt_text: None, creator_id: inserted_person.id, community_id: inserted_community.id, published: inserted_post.published, diff --git a/crates/db_schema/src/schema.rs b/crates/db_schema/src/schema.rs index 19f9183a5..812dbad1b 100644 --- a/crates/db_schema/src/schema.rs +++ b/crates/db_schema/src/schema.rs @@ -705,6 +705,7 @@ diesel::table! { featured_community -> Bool, featured_local -> Bool, url_content_type -> Nullable, + alt_text -> Nullable, } } diff --git a/crates/db_schema/src/source/post.rs b/crates/db_schema/src/source/post.rs index 4ac3e2a65..15d6f3f20 100644 --- a/crates/db_schema/src/source/post.rs +++ b/crates/db_schema/src/source/post.rs @@ -56,6 +56,8 @@ pub struct Post { /// Whether the post is featured to its site. pub featured_local: bool, pub url_content_type: Option, + /// An optional alt_text, usable for image posts. + pub alt_text: Option, } #[derive(Debug, Clone, TypedBuilder)] @@ -87,6 +89,7 @@ pub struct PostInsertForm { pub featured_community: Option, pub featured_local: Option, pub url_content_type: Option, + pub alt_text: Option, } #[derive(Debug, Clone, Default)] @@ -112,6 +115,7 @@ pub struct PostUpdateForm { pub featured_community: Option, pub featured_local: Option, pub url_content_type: Option, + pub alt_text: Option>, } #[derive(PartialEq, Eq, Debug)] diff --git a/crates/db_views/src/comment_view.rs b/crates/db_views/src/comment_view.rs index ada83b93f..880af01bd 100644 --- a/crates/db_views/src/comment_view.rs +++ b/crates/db_views/src/comment_view.rs @@ -1015,6 +1015,7 @@ mod tests { creator_id: data.timmy_local_user_view.person.id, url: None, body: None, + alt_text: None, published: data.inserted_post.published, updated: None, community_id: data.inserted_community.id, diff --git a/crates/db_views/src/post_view.rs b/crates/db_views/src/post_view.rs index 6e15d1678..37947c2fb 100644 --- a/crates/db_views/src/post_view.rs +++ b/crates/db_views/src/post_view.rs @@ -1490,6 +1490,7 @@ mod tests { creator_id: inserted_person.id, url: None, body: None, + alt_text: None, published: inserted_post.published, updated: None, community_id: inserted_community.id, diff --git a/migrations/2024-02-27-204628_add_post_alt_text/down.sql b/migrations/2024-02-27-204628_add_post_alt_text/down.sql new file mode 100644 index 000000000..28a91cd0e --- /dev/null +++ b/migrations/2024-02-27-204628_add_post_alt_text/down.sql @@ -0,0 +1,3 @@ +ALTER TABLE post + DROP COLUMN alt_text; + diff --git a/migrations/2024-02-27-204628_add_post_alt_text/up.sql b/migrations/2024-02-27-204628_add_post_alt_text/up.sql new file mode 100644 index 000000000..4345d8cfc --- /dev/null +++ b/migrations/2024-02-27-204628_add_post_alt_text/up.sql @@ -0,0 +1,3 @@ +ALTER TABLE post + ADD COLUMN alt_text text; +