Addressing PR comments.

This commit is contained in:
Dessalines 2024-03-04 09:55:05 -05:00
parent 7d0640eeb3
commit b5ce51470a
3 changed files with 10 additions and 10 deletions

View file

@ -62,15 +62,13 @@ pub async fn create_post(
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?;
let alt_text = &data.alt_text;
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"
let custom_thumbnail = data.custom_thumbnail.as_ref().map(clean_url_params); let custom_thumbnail = data.custom_thumbnail.as_ref().map(clean_url_params);
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_alt_text_field(alt_text)?; is_valid_alt_text_field(&data.alt_text)?;
check_url_scheme(&url)?; check_url_scheme(&url)?;
check_url_scheme(&custom_thumbnail)?; check_url_scheme(&custom_thumbnail)?;
@ -135,7 +133,7 @@ pub async fn create_post(
.url_content_type(metadata.content_type) .url_content_type(metadata.content_type)
.url(url) .url(url)
.body(body) .body(body)
.alt_text(alt_text.clone()) .alt_text(&data.alt_text.clone())
.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

@ -54,14 +54,13 @@ 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 = &data.alt_text;
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_alt_text_field(alt_text)?; is_valid_alt_text_field(&data.alt_text)?;
check_url_scheme(&url)?; check_url_scheme(&url)?;
check_url_scheme(&custom_thumbnail)?; check_url_scheme(&custom_thumbnail)?;
@ -86,7 +85,7 @@ pub async fn update_post(
Some(url) => { Some(url) => {
// Only generate the thumbnail if there's no custom thumbnail provided, // Only generate the thumbnail if there's no custom thumbnail provided,
// otherwise it will save it in pictrs // otherwise it will save it in pictrs
let generate_thumbnail = custom_thumbnail.is_none(); let generate_thumbnail = custom_thumbnail.is_none() || orig_post.thumbnail_url.is_none();
let metadata = fetch_link_metadata(url, generate_thumbnail, &context).await?; let metadata = fetch_link_metadata(url, generate_thumbnail, &context).await?;
( (
@ -127,7 +126,7 @@ pub async fn update_post(
url, url,
url_content_type: metadata_content_type, url_content_type: metadata_content_type,
body: diesel_option_overwrite(body), body: diesel_option_overwrite(body),
alt_text: diesel_option_overwrite(alt_text.clone()), alt_text: diesel_option_overwrite(&data.alt_text.clone()),
nsfw: data.nsfw, nsfw: data.nsfw,
embed_title, embed_title,
embed_description, embed_description,

View file

@ -92,6 +92,8 @@ pub(crate) struct Document {
#[serde(rename = "type")] #[serde(rename = "type")]
kind: DocumentType, kind: DocumentType,
url: Url, url: Url,
/// Used for alt_text
name: Option<String>,
} }
#[derive(Clone, Debug, Deserialize, Serialize)] #[derive(Clone, Debug, Deserialize, Serialize)]
@ -117,6 +119,7 @@ impl Attachment {
pub(crate) fn alt_text(self) -> Option<String> { pub(crate) fn alt_text(self) -> Option<String> {
match self { match self {
Attachment::Image(i) => i.name, Attachment::Image(i) => i.name,
Attachment::Document(d) => d.name,
_ => None, _ => None,
} }
} }
@ -177,13 +180,13 @@ impl Page {
impl Attachment { impl Attachment {
/// Creates new attachment for a given link and mime type. /// Creates new attachment for a given link and mime type.
pub(crate) fn new(url: Url, media_type: Option<String>, name: Option<String>) -> Attachment { pub(crate) fn new(url: Url, media_type: Option<String>, alt_text: Option<String>) -> Attachment {
let is_image = media_type.clone().unwrap_or_default().starts_with("image"); let is_image = media_type.clone().unwrap_or_default().starts_with("image");
if is_image { if is_image {
Attachment::Image(Image { Attachment::Image(Image {
kind: Default::default(), kind: Default::default(),
url, url,
name, name: alt_text,
}) })
} else { } else {
Attachment::Link(Link { Attachment::Link(Link {