fix(radio): repair /radio/* deserialization and wire every radio entry point

Qobuz /radio/album and /radio/track return a tracks page that omits
`total`; the strict SearchResultsPage<Track> derive rejected it with
"missing field `total`", breaking AlbumView and Discover For You radio.
Port Tauri's lenient_page_flexible into qbz-models and apply it to
RadioResponse.tracks (now non-optional, matching Tauri); derive Default
on Track so the absent-tracks branch resolves to an empty page. Two new
qbz-models tests cover the missing-total and absent-tracks cases.

Wire ArtistView's previously-dead radio dropdown: both items now carry
ArtistState.id and have handler arms -- "QBZ Radio" -> smart pool
builder, "Qobuz Radio" -> the plain /radio/artist endpoint
(play_artist_radio, no longer dead_code). Add a "QBZ track radio"
track-menu item backed by a new play_smart_track_radio that fetches the
track to seed its performer.

cc
This commit is contained in:
vicrodh committed 2026-06-27 13:34:31 -06:00
1 parent 332fa430e5
commit 6520c007ae
6 files changed
+182 -19

No files matched your search

+92
View File
@@ -57,6 +57,62 @@ where
}))
}
/// Deserialize a `SearchResultsPage<T>`, salvaging items field-by-field and
/// defaulting any missing `total`/`offset`/`limit` (the Qobuz `/radio/*` pages
/// OMIT `total`). Unlike [`lenient_page`] — which returns an EMPTY page on any
/// parse failure and would silently drop every radio track — this PRESERVES the
/// items. Ported verbatim from `src-tauri/src/api/models.rs:42-91`.
pub fn lenient_page_flexible<'de, D, T>(deserializer: D) -> Result<SearchResultsPage<T>, D::Error>
where
D: Deserializer<'de>,
T: DeserializeOwned,
{
let value = serde_json::Value::deserialize(deserializer)?;
if value.is_null() {
return Ok(SearchResultsPage {
items: Vec::new(),
total: 0,
offset: 0,
limit: 0,
});
}
let items: Vec<T> = value
.get("items")
.and_then(|v| v.as_array())
.map(|arr| {
arr.iter()
.filter_map(|item| serde_json::from_value::<T>(item.clone()).ok())
.collect()
})
.unwrap_or_default();
let limit = value
.get("limit")
.and_then(|v| v.as_u64())
.map(|v| v as u32)
.unwrap_or(items.len() as u32);
let offset = value
.get("offset")
.and_then(|v| v.as_u64())
.map(|v| v as u32)
.unwrap_or(0);
let total = value
.get("total")
.and_then(|v| v.as_u64())
.map(|v| v as u32)
.unwrap_or(items.len() as u32);
Ok(SearchResultsPage {
items,
total,
offset,
limit,
})
}
/// JSON String OR Number → `String`; anything else → `""`. Used for
/// `PurchaseAlbum.id`.
pub fn deserialize_string_id<'de, D>(deserializer: D) -> Result<String, D::Error>
@@ -241,4 +297,40 @@ mod tests {
// `streamable` defaults TRUE even on the bare track shape.
assert!(track.streamable);
}
#[test]
fn radio_response_tolerates_missing_total() {
use crate::types::RadioResponse;
// Qobuz `/radio/album` + `/radio/track` return a `tracks` page that
// OMITS `total` (and sometimes offset/limit). The strict derive used to
// abort with "missing field `total`"; lenient_page_flexible must default
// it to the item count and PRESERVE the tracks (not drop them).
const RADIO: &str = r#"{
"type": "album",
"title": "Album Radio",
"tracks": {
"items": [
{ "id": 111, "title": "One" },
{ "id": 222, "title": "Two" }
]
}
}"#;
let resp: RadioResponse = serde_json::from_str(RADIO)
.expect("radio response with missing total must deserialize");
assert_eq!(resp.tracks.items.len(), 2);
assert_eq!(resp.tracks.items[0].id, 111);
// `total` defaulted to the item count.
assert_eq!(resp.tracks.total, 2);
}
#[test]
fn radio_response_absent_tracks_is_empty() {
use crate::types::RadioResponse;
// `tracks` key entirely absent → field-level #[serde(default)] → empty
// page (requires `Track: Default`, added alongside this fix).
let resp: RadioResponse = serde_json::from_str(r#"{ "type": "album" }"#)
.expect("radio without tracks deserializes");
assert_eq!(resp.tracks.items.len(), 0);
assert_eq!(resp.tracks.total, 0);
}
}
+3 -3
View File
@@ -297,7 +297,7 @@ impl ImageSet {
// ============ Core Media Types ============
/// Track model
#[derive(Debug, Clone, Serialize, Deserialize)]
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct Track {
#[serde(default)]
pub id: u64,
@@ -928,8 +928,8 @@ pub struct RadioResponse {
pub radio_type: Option<String>,
#[serde(default)]
pub title: Option<String>,
#[serde(default)]
pub tracks: Option<SearchResultsPage<Track>>,
#[serde(default, deserialize_with = "crate::purchase_serde::lenient_page_flexible")]
pub tracks: SearchResultsPage<Track>,
}
/// One entry of the Qobuz `most_popular` block in a combined search.
+4 -4
View File
@@ -403,16 +403,16 @@ export component ArtistPageView inherits Rectangle {
ContextMenu {
ContextMenuItem {
icon: @image-url("../assets/icons/radio.svg");
label: "QBZ Radio";
label: @tr("QBZ Radio");
clicked => {
root.media-action("artist", "", "radio-qbz");
root.media-action("artist", ArtistState.id, "radio-qbz");
}
}
ContextMenuItem {
icon: @image-url("../assets/icons/radio.svg");
label: "Qobuz Radio";
label: @tr("Qobuz Radio");
clicked => {
root.media-action("artist", "", "radio-qobuz");
root.media-action("artist", ArtistState.id, "radio-qobuz");
}
}
}
@@ -62,8 +62,8 @@ export component TrackContextMenu inherits PopupWindow {
width: 224px;
// Nav-only (blacklisted): only Go to album / Go to artist / Track info.
// Otherwise: 3 transient + optional favorite/mixtape/add-to-playlist/remove
// + optional go-to pair + the Qobuz block (3 fixed + 1-or-2 offline
// entries + optional track info).
// + optional go-to pair + the Qobuz block (4 fixed: QBZ radio, Qobuz
// radio, 2 shares; + 1-or-2 offline entries + optional track info).
height: (root.nav-only
? ((root.go-album-action ? 1 : 0)
+ (root.go-artist-action ? 1 : 0)
@@ -76,7 +76,7 @@ export component TrackContextMenu inherits PopupWindow {
+ (root.go-album-action ? 1 : 0)
+ (root.go-artist-action ? 1 : 0)
+ (root.qobuz-actions
? 3 + (root.cache-status == 3 ? 2 : 1) + (root.track-info-action ? 1 : 0)
? 4 + (root.cache-status == 3 ? 2 : 1) + (root.track-info-action ? 1 : 0)
: 0))) * 30px;
ContextMenu {
@@ -103,7 +103,14 @@ export component TrackContextMenu inherits PopupWindow {
}
if root.qobuz-actions && !root.nav-only: ContextMenuItem {
icon: @image-url("../assets/icons/radio.svg");
label: @tr("Create radio");
label: @tr("Create QBZ radio");
clicked => {
root.media-action("track", root.track-id, "create-radio-qbz");
}
}
if root.qobuz-actions && !root.nav-only: ContextMenuItem {
icon: @image-url("../assets/icons/radio.svg");
label: @tr("Create Qobuz radio");
clicked => {
root.media-action("track", root.track-id, "create-radio");
}
+25
View File
@@ -8247,6 +8247,15 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
handle.clone(),
id.clone(),
),
// "QBZ Radio" track variant: the internal smart pool builder
// (fetches the track to seed its performer) vs the plain Qobuz
// /radio/track above.
("track", "create-radio-qbz") => playback::play_smart_track_radio(
runtime.clone(),
weak.clone(),
handle.clone(),
id.clone(),
),
("track", "add-to-playlist") => {
// Open the global picker for this track + load the
// user's playlists. SOURCE-TYPED routing first: this
@@ -8594,6 +8603,22 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
handle.clone(),
id.clone(),
),
// ArtistView's radio dropdown: "QBZ Radio" = the internal
// smart pool builder (richer); "Qobuz Radio" = the plain
// Qobuz /radio/artist endpoint. (Both id-carrying, unlike the
// legacy empty-id placeholders.)
("artist", "radio-qbz") => playback::play_smart_artist_radio(
runtime.clone(),
weak.clone(),
handle.clone(),
id.clone(),
),
("artist", "radio-qobuz") => playback::play_artist_radio(
runtime.clone(),
weak.clone(),
handle.clone(),
id.clone(),
),
("artist", "follow") => {
let runtime = runtime.clone();
let weak = weak.clone();
+47 -8
View File
@@ -2616,11 +2616,10 @@ fn play_radio_response(
play_tracks(runtime, weak, handle, tracks, 0)
}
/// Start a Qobuz artist radio (`/radio/artist`). Kept as the simpler
/// alternative to the smart pool builder; the artist "radio" action
/// uses the smart builder, this remains available for an explicit
/// "Qobuz radio" choice.
#[allow(dead_code)]
/// Start a Qobuz artist radio (`/radio/artist`). The simpler alternative to
/// the smart pool builder: wired to ArtistView's "Qobuz Radio" choice, while
/// the "QBZ Radio" choice and the plain `("artist","radio")` action use the
/// smart builder.
pub fn play_artist_radio(
runtime: Runtime,
weak: slint::Weak<AppWindow>,
@@ -2630,7 +2629,7 @@ pub fn play_artist_radio(
handle.spawn(async move {
match runtime.core().get_radio_artist(&artist_id).await {
Ok(resp) => {
let tracks = resp.tracks.map(|p| p.items).unwrap_or_default();
let tracks = resp.tracks.items;
if !play_radio_response(runtime, weak, tracks) {
log::warn!("[qbz-slint] artist radio {artist_id} returned no tracks");
}
@@ -2674,7 +2673,7 @@ pub fn play_track_radio(
handle.spawn(async move {
match runtime.core().get_radio_track(&track_id).await {
Ok(resp) => {
let tracks = resp.tracks.map(|p| p.items).unwrap_or_default();
let tracks = resp.tracks.items;
if !play_radio_response(runtime, weak, tracks) {
log::warn!("[qbz-slint] track radio {track_id} returned no tracks");
}
@@ -2684,6 +2683,46 @@ pub fn play_track_radio(
});
}
/// Start a smart track radio via the local qbz-radio pool builder (richer than
/// the plain Qobuz `/radio/track`). The track-row menu only carries the track
/// id, so fetch the track first to seed the builder with its performer.
pub fn play_smart_track_radio(
runtime: Runtime,
weak: slint::Weak<AppWindow>,
handle: tokio::runtime::Handle,
track_id: String,
) {
handle.spawn(async move {
let Ok(tid) = track_id.parse::<u64>() else {
log::warn!("[qbz-slint] smart track radio: bad track id {track_id}");
return;
};
let track = match runtime.core().get_track(tid).await {
Ok(t) => t,
Err(e) => {
log::error!("[qbz-slint] smart track radio: get_track {tid} failed: {e}");
return;
}
};
let Some(aid) = track.performer.as_ref().map(|a| a.id) else {
log::warn!("[qbz-slint] smart track radio: track {tid} has no performer");
return;
};
match runtime
.core()
.create_smart_track_radio(tid, aid, track.title.clone())
.await
{
Ok(tracks) => {
if !play_radio_response(runtime, weak, tracks) {
log::warn!("[qbz-slint] smart track radio {tid} returned no tracks");
}
}
Err(e) => log::error!("[qbz-slint] smart track radio {tid} failed: {e}"),
}
});
}
/// Start a Qobuz album radio (`/radio/album`).
pub fn play_album_radio(
runtime: Runtime,
@@ -2694,7 +2733,7 @@ pub fn play_album_radio(
handle.spawn(async move {
match runtime.core().get_radio_album(&album_id).await {
Ok(resp) => {
let tracks = resp.tracks.map(|p| p.items).unwrap_or_default();
let tracks = resp.tracks.items;
if !play_radio_response(runtime, weak, tracks) {
log::warn!("[qbz-slint] album radio {album_id} returned no tracks");
}