From 8b9f719e770efe69cc53cc1b4a7ea88c3502980d Mon Sep 17 00:00:00 2001 From: Jackson <54308792+jacksongoode@users.noreply.github.com> Date: Sat, 28 Dec 2024 17:54:09 -0800 Subject: [PATCH] Santize home description (#575) This was reverted in the past commit --- psst-gui/src/data/playlist.rs | 6 ++---- psst-gui/src/data/utils.rs | 7 +++++++ psst-gui/src/webapi/client.rs | 21 +++++++++++---------- 3 files changed, 20 insertions(+), 14 deletions(-) diff --git a/psst-gui/src/data/playlist.rs b/psst-gui/src/data/playlist.rs index 3e004634..45528322 100644 --- a/psst-gui/src/data/playlist.rs +++ b/psst-gui/src/data/playlist.rs @@ -1,10 +1,9 @@ use std::sync::Arc; use druid::{im::Vector, Data, Lens}; -use sanitize_html::rules::predefined::DEFAULT; -use sanitize_html::sanitize_str; use serde::{Deserialize, Deserializer, Serialize}; +use crate::data::utils::sanitize_html_string; use crate::data::{user::PublicUser, Image, Promise, Track, TrackId}; #[derive(Clone, Debug, Data, Lens)] @@ -98,6 +97,5 @@ where D: Deserializer<'de>, { let description: String = String::deserialize(deserializer)?; - let sanitized = sanitize_str(&DEFAULT, &description).unwrap_or_default(); - Ok(Arc::from(sanitized.replace("&", "&"))) + Ok(sanitize_html_string(&description)) } diff --git a/psst-gui/src/data/utils.rs b/psst-gui/src/data/utils.rs index 7f09287b..77c7adb5 100644 --- a/psst-gui/src/data/utils.rs +++ b/psst-gui/src/data/utils.rs @@ -6,6 +6,8 @@ use std::{ }; use druid::{im::Vector, Data, Lens}; +use sanitize_html::rules::predefined::DEFAULT; +use sanitize_html::sanitize_str; use serde::{Deserialize, Deserializer, Serialize}; use time::{Date, Month}; @@ -172,3 +174,8 @@ where let opt = Option::deserialize(deserializer)?; Ok(opt.unwrap_or_else(default_str)) } + +pub fn sanitize_html_string(text: &str) -> Arc { + let sanitized = sanitize_str(&DEFAULT, text).unwrap_or_default(); + Arc::from(sanitized.replace("&", "&")) +} diff --git a/psst-gui/src/webapi/client.rs b/psst-gui/src/webapi/client.rs index 36379b66..a7c78b2e 100644 --- a/psst-gui/src/webapi/client.rs +++ b/psst-gui/src/webapi/client.rs @@ -26,6 +26,7 @@ use psst_core::{ }; use crate::{ + data::utils::sanitize_html_string, data::{ self, Album, AlbumType, Artist, ArtistAlbums, ArtistInfo, ArtistLink, ArtistStats, AudioAnalysis, Cached, Episode, EpisodeId, EpisodeLink, Image, MixedView, Nav, Page, @@ -486,21 +487,21 @@ impl WebApi { }, )), description: { - let desc = item - .content - .data - .description - .as_deref() - .unwrap_or_default() - .to_string(); + let desc = sanitize_html_string( + item.content + .data + .description + .as_deref() + .unwrap_or_default(), + ); + // This is roughly 3 lines of description, truncated if too long if desc.chars().count() > 55 { - desc.chars().take(52).collect::() + "..." + Arc::from(desc.chars().take(52).collect::() + "...") } else { desc } - } - .into(), + }, track_count: item.content.data.attributes.as_ref().and_then( |attrs| { attrs