octo-tui/src/playlists.rs
src/playlists.rsRaw
1use crate::model::Song;
2use serde_json::Value;
3
4#[derive(Clone, Debug, Default)]
5pub struct PlaylistChoice {
6 pub id: String,
7 pub name: String,
8 pub readonly: bool,
9 pub upkeep: bool,
10 pub imported: bool,
11 pub upkeep_kind: Option<String>,
12}
13
14impl PlaylistChoice {
15 /// Upkeep action lists can alter files; the playlist picker only adds to music or request lists.
16 pub fn addition_error(&self) -> Option<&'static str> {
17 if self.readonly || self.imported {
18 Some(
19 "The server marks this playlist read-only. Choose another or create a new playlist.",
20 )
21 } else if self.upkeep && self.upkeep_kind.as_deref() != Some("request") {
22 Some(
23 "Library upkeep action and notice lists cannot be used by the add-to-playlist dialog.",
24 )
25 } else {
26 None
27 }
28 }
29 pub fn suffix(&self) -> &'static str {
30 if self.readonly || self.imported {
31 " [read-only]"
32 } else if self.upkeep {
33 " [library upkeep]"
34 } else {
35 ""
36 }
37 }
38}
39fn wire_bool(value: &Value) -> bool {
40 match value {
41 Value::Bool(value) => *value,
42 Value::String(value) => value.eq_ignore_ascii_case("true") || value == "1",
43 Value::Number(value) => value.as_u64() == Some(1),
44 _ => false,
45 }
46}
47
48/// Share parsing between the browse section and picker; optional metadata must not hide a list.
49pub fn parse_playlist_choices(value: &Value) -> Vec<PlaylistChoice> {
50 let Some(rows) = value.as_array() else {
51 return vec![];
52 };
53 rows.iter()
54 .filter_map(|row| {
55 let id = match &row["id"] {
56 Value::String(id) => id.clone(),
57 Value::Number(id) if id.is_i64() || id.is_u64() => id.to_string(),
58 _ => return None,
59 };
60 if id.trim().is_empty() {
61 return None;
62 }
63 let name = row["name"]
64 .as_str()
65 .filter(|name| !name.trim().is_empty())
66 .unwrap_or(&id)
67 .to_owned();
68 let readonly = match row.get("readonly").or_else(|| row.get("readOnly")) {
69 Some(Value::Bool(value)) => *value,
70 Some(Value::String(value)) => value.eq_ignore_ascii_case("true") || value == "1",
71 Some(Value::Number(value)) => value.as_u64() == Some(1),
72 _ => false,
73 };
74 Some(PlaylistChoice {
75 id,
76 name,
77 readonly,
78 upkeep: wire_bool(&row["octoUpkeep"]),
79 imported: wire_bool(&row["octoImport"]),
80 upkeep_kind: row["octoUpkeepKind"].as_str().map(str::to_owned),
81 })
82 })
83 .collect()
84}
85
86#[derive(Clone, Debug)]
87pub enum PlaylistTarget {
88 Existing { id: String, name: String },
89 New { name: String },
90}
91impl PlaylistTarget {
92 pub fn name(&self) -> &str {
93 match self {
94 Self::Existing { name, .. } | Self::New { name } => name,
95 }
96 }
97}
98
99pub struct PlaylistDialog {
100 pub id: u64,
101 pub song: Song,
102 pub choices: Vec<PlaylistChoice>,
103 pub selected: usize,
104 pub loading: bool,
105 pub saving: bool,
106 pub new_name: Option<String>,
107 pub error: Option<String>,
108 pub pending_target: Option<PlaylistTarget>,
109}
110impl PlaylistDialog {
111 pub fn new(id: u64, song: Song) -> Self {
112 Self {
113 id,
114 song,
115 choices: vec![],
116 selected: 0,
117 loading: true,
118 saving: false,
119 new_name: None,
120 error: None,
121 pending_target: None,
122 }
123 }
124}
125
126#[cfg(test)]
127mod tests {
128 use super::*;
129 #[test]
130 fn upkeep_actions_and_imports_cannot_be_used_as_normal_playlists() {
131 let choices = parse_playlist_choices(&serde_json::json!([
132 {"id":"normal"},
133 {"id":"action","octoUpkeep":true,"octoUpkeepKind":"action"},
134 {"id":"notice","octoUpkeep":true,"octoUpkeepKind":"notice"},
135 {"id":"old-action","octoUpkeep":true},
136 {"id":"request","octoUpkeep":true,"octoUpkeepKind":"request"},
137 {"id":"import","octoImport":true}
138 ]));
139 assert!(choices[0].addition_error().is_none());
140 for i in [1, 2, 3, 5] {
141 assert!(choices[i].addition_error().is_some());
142 }
143 assert!(choices[4].addition_error().is_none());
144 }
145 #[test]
146 fn optional_metadata_and_one_bad_row_do_not_hide_playlists() {
147 let rows = serde_json::json!([
148 {"id":"mine","name":"My songs","readonly":null,"owner":null},
149 {"id":42,"name":"Numeric ID","readonly":"false"},
150 {"id":"generated","name":"Station","readonly":true},
151 {"id":"other","name":"Chart","readOnly":"true"},
152 {"id":null,"name":"Invalid"},
153 {"id":"unnamed","name":null}
154 ]);
155 let choices = parse_playlist_choices(&rows);
156 assert_eq!(choices.len(), 5);
157 assert!(!choices[0].readonly);
158 assert_eq!(choices[1].id, "42");
159 assert!(choices[2].readonly);
160 assert!(choices[3].readonly);
161 assert_eq!(choices[4].name, "unnamed");
162 }
163}