Skip to content

Commit

Permalink
Deserialize instead
Browse files Browse the repository at this point in the history
  • Loading branch information
jacksongoode committed Dec 28, 2024
1 parent 695d60c commit 1f9bdda
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 11 deletions.
12 changes: 12 additions & 0 deletions psst-gui/src/data/playlist.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ use std::sync::Arc;

use druid::{im::Vector, Data, Lens};
use serde::{Deserialize, Deserializer, Serialize};
use sanitize_html::rules::predefined::DEFAULT;
use sanitize_html::sanitize_str;

use crate::data::{user::PublicUser, Image, Promise, Track, TrackId};

Expand Down Expand Up @@ -29,6 +31,7 @@ pub struct Playlist {
pub name: Arc<str>,
#[serde(skip_serializing_if = "Option::is_none")]
pub images: Option<Vector<Image>>,
#[serde(deserialize_with = "deserialize_sanitized_description")]
pub description: Arc<str>,
#[serde(rename = "tracks")]
#[serde(deserialize_with = "deserialize_track_count")]
Expand Down Expand Up @@ -89,3 +92,12 @@ where

Ok(PlaylistTracksRef::deserialize(deserializer)?.total)
}

fn deserialize_sanitized_description<'de, D>(deserializer: D) -> Result<Arc<str>, D::Error>
where
D: Deserializer<'de>,
{
let description: String = String::deserialize(deserializer)?;
let sanitized = sanitize_str(&DEFAULT, &description).unwrap_or_default();
Ok(Arc::from(sanitized.replace("&amp;", "&")))
}
12 changes: 1 addition & 11 deletions psst-gui/src/webapi/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1204,15 +1204,6 @@ impl WebApi {
pub fn get_playlists(&self) -> Result<Vector<Playlist>, Error> {
let request = self.get("v1/me/playlists", None)?;
let result: Vector<Playlist> = self.load_all_pages(request)?;

let result = result
.into_iter()
.map(|mut playlist| {
playlist.description = Self::sanitize_html(&playlist.description).into();
playlist
})
.collect();

Ok(result)
}

Expand All @@ -1231,8 +1222,7 @@ impl WebApi {
// https://developer.spotify.com/documentation/web-api/reference/get-playlist
pub fn get_playlist(&self, id: &str) -> Result<Playlist, Error> {
let request = self.get(format!("v1/playlists/{}", id), None)?;
let mut result: Playlist = self.load(request)?;
result.description = Self::sanitize_html(&result.description).into();
let result: Playlist = self.load(request)?;
Ok(result)
}

Expand Down

0 comments on commit 1f9bdda

Please sign in to comment.