use crate::model::Song; use serde_json::Value; #[derive(Clone, Debug, Default)] pub struct PlaylistChoice { pub id: String, pub name: String, pub readonly: bool, pub upkeep: bool, pub imported: bool, pub upkeep_kind: Option, } impl PlaylistChoice { /// Upkeep action lists can alter files; the playlist picker only adds to music or request lists. pub fn addition_error(&self) -> Option<&'static str> { if self.readonly || self.imported { Some( "The server marks this playlist read-only. Choose another or create a new playlist.", ) } else if self.upkeep && self.upkeep_kind.as_deref() != Some("request") { Some( "Library upkeep action and notice lists cannot be used by the add-to-playlist dialog.", ) } else { None } } pub fn suffix(&self) -> &'static str { if self.readonly || self.imported { " [read-only]" } else if self.upkeep { " [library upkeep]" } else { "" } } } fn wire_bool(value: &Value) -> bool { match value { Value::Bool(value) => *value, Value::String(value) => value.eq_ignore_ascii_case("true") || value == "1", Value::Number(value) => value.as_u64() == Some(1), _ => false, } } /// Share parsing between the browse section and picker; optional metadata must not hide a list. pub fn parse_playlist_choices(value: &Value) -> Vec { let Some(rows) = value.as_array() else { return vec![]; }; rows.iter() .filter_map(|row| { let id = match &row["id"] { Value::String(id) => id.clone(), Value::Number(id) if id.is_i64() || id.is_u64() => id.to_string(), _ => return None, }; if id.trim().is_empty() { return None; } let name = row["name"] .as_str() .filter(|name| !name.trim().is_empty()) .unwrap_or(&id) .to_owned(); let readonly = match row.get("readonly").or_else(|| row.get("readOnly")) { Some(Value::Bool(value)) => *value, Some(Value::String(value)) => value.eq_ignore_ascii_case("true") || value == "1", Some(Value::Number(value)) => value.as_u64() == Some(1), _ => false, }; Some(PlaylistChoice { id, name, readonly, upkeep: wire_bool(&row["octoUpkeep"]), imported: wire_bool(&row["octoImport"]), upkeep_kind: row["octoUpkeepKind"].as_str().map(str::to_owned), }) }) .collect() } #[derive(Clone, Debug)] pub enum PlaylistTarget { Existing { id: String, name: String }, New { name: String }, } impl PlaylistTarget { pub fn name(&self) -> &str { match self { Self::Existing { name, .. } | Self::New { name } => name, } } } pub struct PlaylistDialog { pub id: u64, pub song: Song, pub choices: Vec, pub selected: usize, pub loading: bool, pub saving: bool, pub new_name: Option, pub error: Option, pub pending_target: Option, } impl PlaylistDialog { pub fn new(id: u64, song: Song) -> Self { Self { id, song, choices: vec![], selected: 0, loading: true, saving: false, new_name: None, error: None, pending_target: None, } } } #[cfg(test)] mod tests { use super::*; #[test] fn upkeep_actions_and_imports_cannot_be_used_as_normal_playlists() { let choices = parse_playlist_choices(&serde_json::json!([ {"id":"normal"}, {"id":"action","octoUpkeep":true,"octoUpkeepKind":"action"}, {"id":"notice","octoUpkeep":true,"octoUpkeepKind":"notice"}, {"id":"old-action","octoUpkeep":true}, {"id":"request","octoUpkeep":true,"octoUpkeepKind":"request"}, {"id":"import","octoImport":true} ])); assert!(choices[0].addition_error().is_none()); for i in [1, 2, 3, 5] { assert!(choices[i].addition_error().is_some()); } assert!(choices[4].addition_error().is_none()); } #[test] fn optional_metadata_and_one_bad_row_do_not_hide_playlists() { let rows = serde_json::json!([ {"id":"mine","name":"My songs","readonly":null,"owner":null}, {"id":42,"name":"Numeric ID","readonly":"false"}, {"id":"generated","name":"Station","readonly":true}, {"id":"other","name":"Chart","readOnly":"true"}, {"id":null,"name":"Invalid"}, {"id":"unnamed","name":null} ]); let choices = parse_playlist_choices(&rows); assert_eq!(choices.len(), 5); assert!(!choices[0].readonly); assert_eq!(choices[1].id, "42"); assert!(choices[2].readonly); assert!(choices[3].readonly); assert_eq!(choices[4].name, "unnamed"); } }