The release-section "Load more" fetched a page and appended it to a structure
no view ever re-read. The handler pushed IN PLACE into the object behind
`readonly property var artist: JSON.parse(QbzArtist.artistJson)`, and the
releaseSections projection returned that same array on the unfiltered arm, so
both Repeaters were re-handed the identical reference and nothing was dirty.
The tree's own rule, written in cards/PlaylistCollage.qml: rebind requires a
NEW object reference. A second bug sat on the same path - the artist document
republishes progressively (stories, then each MusicBrainz section as it
resolves) and every republish re-runs the parse, wiping anything appended, so
fixing only the first would have left a bug that looks random. Both
pre-existing; the in-flight placeholder added earlier is what made an
always-silent no-op visible.
Appended pages now live in an overlay keyed by release type. It survives a
republish, emits new arrays so the Repeaters rebuild, dedupes by album id,
carries the reference's 4-page cap per bucket (artist.rs:718-720, which hands
off to the discography page beyond it), and is guarded against a page
requested on one artist landing on another's grid - releaseSectionReady
carries no artist id and every artist has an "album" bucket.
"See discography" was a pointer-cursor MouseArea with no onClicked under a
POC-NOTE saying the page was out of scope. ArtistReleasesView.qml +
artist_releases_qt.rs port ArtistReleasesView.slint: infinite scroll rather
than a button, plus the loading, error-with-retry, empty and offline arms.
The album page's "More from <artist>" view-all is a second door to the same
page. Its paging offset counts what the SERVER returned rather than the
rendered row count the reference uses: blacklist filtering and id dedupe make
the rendered count smaller, so the reference re-requests seen rows and skips
the tail whenever the blacklist is non-empty.
The per-section sort was a dimmed Rectangle with no MouseArea, under a
comment asserting no seam existed. It is now a real QbzSelect with the
reference's five options and index mapping, persisted per release type in
artist_prefs. Choosing "Default" after a sort does not restore the server
order until the page reloads - that is the reference's behaviour
(album_map.rs:262 is a bare no-op arm and resort sorts the live model), kept
deliberately rather than diverged.
Also, two visual fixes from the same round that share these files: Popular
Tracks drew its cover at radius 8 where the shared rows/TrackRow.qml uses 4,
and the album page's rail mounts said `width: parent.width` inside a Column
with 32px padding - QML padding shifts children without narrowing
parent.width, so the rails hung 32px off the right and the right page chevron
landed outside the window.
Carries scripts/qt-diag.sh, which has been sitting untracked since the
rendering round: it answers "which backend is this actually running on" from
a real graphical session, refusing ssh and refusing offscreen because either
would answer the wrong question. That refusal is the point - the doctrine it
disproved ("shader effects render nothing on this port's software path") came
from an offscreen observation, and offscreen forces software by definition.
cc
1529 lines
57 KiB
Rust
1529 lines
57 KiB
Rust
//! Artist detail data layer — Slint-free port of `crates/qbz/src/artist.rs`
|
||
//! (`load_artist`/`map_artist`, `load_release_page`, the release-section
|
||
//! bucketing in the official order). Publishes ONE JSON document.
|
||
//!
|
||
//! The page publishes PROGRESSIVELY: `load_artist_view` returns the Qobuz
|
||
//! document immediately (main.rs sets it on the bridge), then a background
|
||
//! task re-publishes the SAME document enriched with the Magazine stories and
|
||
//! the MusicBrainz network-sidebar sections as each one resolves. The stashed
|
||
//! copy is generation-guarded so a late reply for a previous artist is
|
||
//! dropped instead of overwriting the current page.
|
||
//!
|
||
//! MusicBrainz is OPT-IN-RESPECTING: `network.mbAvailable` is seeded from
|
||
//! `core.musicbrainz_is_enabled()` BEFORE anything is fetched — when the user
|
||
//! has MB off, no resolve/metadata/relationship/discovery request is made at
|
||
//! all and the Origin / Relationships / You-may-also-like sections are simply
|
||
//! absent from the sidebar (never an error, never an empty frame).
|
||
//!
|
||
//! POC-NOTEs:
|
||
//! - Blacklist banner/filter, "In library" PLAYLISTS sub-lists, jump-tab
|
||
//! scroll-tracking: out of scope.
|
||
//! - `is_following` seeds from `library_qt::is_favorite` — the favourite-id
|
||
//! cache first (the reference's `fav_cache`, now ported as `fav_cache_qt`),
|
||
//! the phase-5 library feed second. It used to read the feed ALONE, which is
|
||
//! empty until the Library view is opened: the heart drew un-followed on
|
||
//! artists the user follows, and the toggle then re-ADDED the follow.
|
||
//! - Discovery runs with EMPTY `dismissed_per_tag` / `known_artists`
|
||
//! callbacks: the Slint app feeds those from `discovery_dismiss` +
|
||
//! `play_history` + `reco`, none of which this POC brings up. That is the
|
||
//! same default a first-run Slint profile lands on (no exclusion applied),
|
||
//! not a fabricated one.
|
||
//! - MB Origin location is rendered as plain text: the clickable form opens
|
||
//! ArtistsByLocationView, which this POC has no port of, so the affordance
|
||
//! is left out rather than dead.
|
||
|
||
use std::collections::{BTreeMap, HashMap, HashSet};
|
||
use std::sync::atomic::{AtomicU64, Ordering};
|
||
use std::sync::{Arc, Mutex};
|
||
use std::time::Instant;
|
||
|
||
use cxx_qt_lib::QString;
|
||
use qbz_app::shell::AppRuntime;
|
||
use qbz_core::LoggingAdapter;
|
||
use qbz_models::{
|
||
ArtistStoryItem, PageArtistRelease, PageArtistResponse, PageArtistTrack, QueueTrack,
|
||
};
|
||
use serde::Serialize;
|
||
use serde_json::json;
|
||
|
||
use crate::album_qt::{truncate_words, AlbumCardData, TrackRow};
|
||
use crate::home_qt;
|
||
|
||
pub const RELEASE_PAGE_SIZE: u32 = 20;
|
||
|
||
/// Official on-screen order of release buckets (artist.rs
|
||
/// RELEASE_SECTION_ORDER; titles are the msgids translated at build time).
|
||
const RELEASE_SECTION_ORDER: &[(&str, &str)] = &[
|
||
("album", "Albums"),
|
||
("epSingle", "EPs & Singles"),
|
||
("ep", "EPs & Singles"),
|
||
("single", "EPs & Singles"),
|
||
("live", "Live"),
|
||
("compilation", "Compilations"),
|
||
("download", "Purchase Only"),
|
||
("composer", "Composer"),
|
||
("other", "Other"),
|
||
("awardedRelease", "Critics' Picks"),
|
||
("next", "Upcoming"),
|
||
];
|
||
|
||
/// Title-case a raw release_type key — the fallback for a bucket the table
|
||
/// above does not know (`qbz/src/artist.rs:117-123` `title_case`).
|
||
fn title_case(key: &str) -> String {
|
||
let mut chars = key.chars();
|
||
match chars.next() {
|
||
Some(first) => first.to_uppercase().collect::<String>() + chars.as_str(),
|
||
None => String::new(),
|
||
}
|
||
}
|
||
|
||
/// Display title for one release_type — the discography page's header
|
||
/// (`qbz/src/artist.rs:107-114` `release_type_title`).
|
||
///
|
||
/// Same table, same fallback, as the per-section headers `map_artist` builds
|
||
/// below: the page the user lands on after "See discography" must be titled
|
||
/// exactly like the section they clicked from.
|
||
///
|
||
/// Translated HERE rather than in QML because it travels inside a JSON
|
||
/// document — which is also why `artist_releases_qt::republish` exists.
|
||
pub(crate) fn release_type_title(release_type: &str) -> String {
|
||
RELEASE_SECTION_ORDER
|
||
.iter()
|
||
.find(|(rt, _)| *rt == release_type)
|
||
.map(|(_, title)| qbz_i18n::t(title))
|
||
.unwrap_or_else(|| title_case(release_type))
|
||
}
|
||
|
||
#[derive(Clone, Serialize)]
|
||
pub struct ArtistReleaseSection {
|
||
#[serde(rename = "releaseType")]
|
||
pub release_type: String,
|
||
pub title: String,
|
||
#[serde(rename = "hasMore")]
|
||
pub has_more: bool,
|
||
pub cards: Vec<AlbumCardData>,
|
||
/// The bucket's persisted sort (`default` | `newest` | `oldest` |
|
||
/// `title-asc` | `title-desc`) — the Slint's `ArtistReleaseSection.sort-by`.
|
||
///
|
||
/// It travels WITH the data because that is what makes the picker come
|
||
/// back showing the user's choice: `ReleaseGrid.slint:34-39` maps this
|
||
/// string back to a `current-index` for its QbzSelect, and the QML port
|
||
/// does the same (`ArtistView.qml`, the ReleaseSection header). The value
|
||
/// is stamped here at page build from `artist_prefs::read_all()`, so the
|
||
/// FIRST paint is already in the right order — the cards below are sorted
|
||
/// before this struct exists (`artist.rs:786-812`: "Apply the persisted
|
||
/// per-bucket sort up front so the first paint already honors the user's
|
||
/// choice").
|
||
#[serde(rename = "sortBy")]
|
||
pub sort_by: String,
|
||
}
|
||
|
||
#[derive(Clone, Serialize)]
|
||
pub struct ArtistLabel {
|
||
pub id: String,
|
||
pub name: String,
|
||
}
|
||
|
||
#[derive(Clone, Serialize)]
|
||
pub struct ArtistSimilar {
|
||
pub id: String,
|
||
pub name: String,
|
||
}
|
||
|
||
#[derive(Clone, Serialize)]
|
||
pub struct ArtistPlaylist {
|
||
pub id: String,
|
||
pub title: String,
|
||
pub subtitle: String,
|
||
#[serde(rename = "artUrl")]
|
||
pub art_url: String,
|
||
}
|
||
|
||
/// Magazine story teaser (Qobuz editorial — `/artist/getStory`). Not an
|
||
/// integration: it is the same Qobuz session the rest of the page uses.
|
||
#[derive(Clone, Default, Serialize)]
|
||
pub struct StoryRow {
|
||
pub title: String,
|
||
pub author: String,
|
||
pub excerpt: String,
|
||
pub url: String,
|
||
#[serde(rename = "artUrl")]
|
||
pub art_url: String,
|
||
}
|
||
|
||
/// MusicBrainz Origin block (artist.rs `MbOrigin`). `hasData` is the gate the
|
||
/// sidebar uses so an MB-matched artist with no life-span/location renders
|
||
/// nothing at all instead of an empty heading.
|
||
#[derive(Clone, Default, Serialize)]
|
||
pub struct MbOriginJson {
|
||
#[serde(rename = "isPerson")]
|
||
pub is_person: bool,
|
||
#[serde(rename = "beginDate")]
|
||
pub begin_date: String,
|
||
#[serde(rename = "endDate")]
|
||
pub end_date: String,
|
||
#[serde(rename = "locationDisplay")]
|
||
pub location_display: String,
|
||
#[serde(rename = "hasData")]
|
||
pub has_data: bool,
|
||
}
|
||
|
||
/// One Members / Member-Of / Collaborators row (artist.rs `MbRelationshipRow`).
|
||
#[derive(Clone, Default, Serialize)]
|
||
pub struct MbRelationshipJson {
|
||
pub mbid: String,
|
||
pub name: String,
|
||
pub role: String,
|
||
pub tooltip: String,
|
||
}
|
||
|
||
#[derive(Clone, Default, Serialize)]
|
||
pub struct MbRelationshipsJson {
|
||
pub members: Vec<MbRelationshipJson>,
|
||
pub groups: Vec<MbRelationshipJson>,
|
||
pub collaborators: Vec<MbRelationshipJson>,
|
||
#[serde(rename = "hasData")]
|
||
pub has_data: bool,
|
||
}
|
||
|
||
/// One "You may also like" candidate. `qobuzId` is "" when the MB candidate
|
||
/// never validated against Qobuz — those rows are informational only.
|
||
#[derive(Clone, Default, Serialize)]
|
||
pub struct MbDiscoveryJson {
|
||
pub mbid: String,
|
||
pub name: String,
|
||
#[serde(rename = "qobuzId")]
|
||
pub qobuz_id: String,
|
||
}
|
||
|
||
/// Everything the Network sidebar renders beyond LABELS / SIMILAR ARTISTS
|
||
/// (which ride the Qobuz artist page). Mirrors Slint's `NetworkSidebarState`.
|
||
#[derive(Clone, Default, Serialize)]
|
||
pub struct ArtistNetwork {
|
||
/// MusicBrainz is enabled AND this artist resolved to an MBID. False =
|
||
/// every MB-driven section is ABSENT (opt-in rule: no error, no frame).
|
||
#[serde(rename = "mbAvailable")]
|
||
pub mb_available: bool,
|
||
pub mbid: String,
|
||
#[serde(rename = "originLoading")]
|
||
pub origin_loading: bool,
|
||
pub origin: MbOriginJson,
|
||
#[serde(rename = "relationshipsLoading")]
|
||
pub relationships_loading: bool,
|
||
pub relationships: MbRelationshipsJson,
|
||
#[serde(rename = "discoveryLoading")]
|
||
pub discovery_loading: bool,
|
||
#[serde(rename = "discoveryTag")]
|
||
pub discovery_tag: String,
|
||
pub discovery: Vec<MbDiscoveryJson>,
|
||
}
|
||
|
||
#[derive(Default, Serialize)]
|
||
pub struct ArtistViewData {
|
||
pub id: String,
|
||
pub name: String,
|
||
pub bio: String,
|
||
#[serde(rename = "bioShort")]
|
||
pub bio_short: String,
|
||
#[serde(rename = "bioTruncated")]
|
||
pub bio_truncated: bool,
|
||
/// Biography attribution ("TiVo" etc); "" when Qobuz sends none.
|
||
#[serde(rename = "bioSource")]
|
||
pub bio_source: String,
|
||
#[serde(rename = "artUrl")]
|
||
pub artwork_url: String,
|
||
#[serde(rename = "isFollowing")]
|
||
pub is_following: bool,
|
||
#[serde(rename = "isPinned")]
|
||
pub is_pinned: bool,
|
||
/// Blacklisted state at build time, seeded from the per-user artist
|
||
/// blacklist (1:1 with `qbz/src/main.rs:2653-2659`, which seeds it beside
|
||
/// the pin state before the view flips). `ArtistView.qml` folds it through
|
||
/// `toggleState("artistBlacklist", ...)` to choose between "Blacklist
|
||
/// artist" and "Show artist" and to show the hidden banner. Reads false
|
||
/// when the feature is disabled — the reference accepts that here too.
|
||
#[serde(rename = "isBlacklisted")]
|
||
pub is_blacklisted: bool,
|
||
#[serde(rename = "libraryCount")]
|
||
pub library_count: i64,
|
||
#[serde(rename = "topTracks")]
|
||
pub top_tracks: Vec<TrackRow>,
|
||
#[serde(rename = "appearsOn")]
|
||
pub appears_on: Vec<TrackRow>,
|
||
#[serde(rename = "lastRelease")]
|
||
pub last_release: Option<AlbumCardData>,
|
||
#[serde(rename = "releaseSections")]
|
||
pub release_sections: Vec<ArtistReleaseSection>,
|
||
pub labels: Vec<ArtistLabel>,
|
||
#[serde(rename = "similarArtists")]
|
||
pub similar_artists: Vec<ArtistSimilar>,
|
||
pub playlists: Vec<ArtistPlaylist>,
|
||
/// Magazine tab. Empty + `storiesLoading` false = "No stories".
|
||
pub stories: Vec<StoryRow>,
|
||
#[serde(rename = "storiesLoading")]
|
||
pub stories_loading: bool,
|
||
/// MusicBrainz-driven Network sidebar sections (filled progressively).
|
||
pub network: ArtistNetwork,
|
||
// ---- Header atmosphere (controls/HeaderGradient.qml) ------------------
|
||
/// Artwork-derived header tint, "#rrggbb" ("" until the portrait
|
||
/// resolves). Same field, same meaning, same producer as the album page's
|
||
/// (`album_qt::header_tint_hex`) — the Slint derives both from the SAME
|
||
/// `artwork::header_tint`, so there is one colour pipeline, not two.
|
||
#[serde(rename = "headerColor")]
|
||
pub header_color: String,
|
||
/// `album_header_gradient` as of page load; the view prefers the live
|
||
/// settings snapshot and falls back to this (see album_qt.rs).
|
||
#[serde(rename = "headerGradient")]
|
||
pub header_gradient: bool,
|
||
}
|
||
|
||
fn mmss(secs: u32) -> String {
|
||
format!("{}:{:02}", secs / 60, secs % 60)
|
||
}
|
||
|
||
/// artist.rs `map_release`.
|
||
pub(crate) fn map_release(release: &PageArtistRelease) -> AlbumCardData {
|
||
let artist = release
|
||
.artist
|
||
.as_ref()
|
||
.map(|a| a.name.display.clone())
|
||
.or_else(|| {
|
||
release
|
||
.artists
|
||
.as_ref()
|
||
.and_then(|list| list.first().map(|a| a.name.clone()))
|
||
})
|
||
.unwrap_or_default();
|
||
let artist_id = release
|
||
.artist
|
||
.as_ref()
|
||
.map(|a| a.id.to_string())
|
||
.unwrap_or_default();
|
||
let bit_depth = release.audio_info.as_ref().and_then(|a| a.maximum_bit_depth);
|
||
let sample_rate = release
|
||
.audio_info
|
||
.as_ref()
|
||
.and_then(|a| a.maximum_sampling_rate);
|
||
AlbumCardData {
|
||
is_pinned: crate::sidebar_qt::is_pinned("album", &release.id),
|
||
// Heart from the favourite-id cache — the artist page's Releases grid
|
||
// mounts the same AlbumCard as Home (see `AlbumCardData::is_favorite`).
|
||
is_favorite: crate::fav_cache_qt::is_album_favorite(&release.id),
|
||
id: release.id.clone(),
|
||
title: release.title.clone(),
|
||
artist,
|
||
artist_id,
|
||
genre: release
|
||
.genre
|
||
.as_ref()
|
||
.map(|g| g.name.clone())
|
||
.unwrap_or_default(),
|
||
year: release
|
||
.dates
|
||
.as_ref()
|
||
.and_then(|d| d.original.as_deref())
|
||
.and_then(|s| s.get(..4).map(|y| y.to_string()))
|
||
.unwrap_or_default(),
|
||
quality_tier: home_qt::quality_tier_from_depth(bit_depth).to_string(),
|
||
quality_detail: home_qt::quality_detail_from_parts(bit_depth, sample_rate),
|
||
art_url: release
|
||
.image
|
||
.as_ref()
|
||
.and_then(|img| img.best().cloned())
|
||
.unwrap_or_default(),
|
||
}
|
||
}
|
||
|
||
/// `album_map::sort_album_items` (crates/qbz/src/album_map.rs:244-264) for the
|
||
/// artist page's release buckets — the SAME function `label_qt::sort_cards`
|
||
/// already ports for `HomeCard`, here for `AlbumCardData`.
|
||
///
|
||
/// `year` on these cards is the PLAIN 4-digit year (`map_release` slices
|
||
/// `dates.original[..4]`), so the lexicographic compare IS a chronological one.
|
||
/// `title`/`artist` are case-insensitive, per the reference's doc comment.
|
||
///
|
||
/// "default" — and any key this function does not know — falls through
|
||
/// UNSORTED (`album_map.rs:262` is a bare `_ => {}`). That is not an oversight:
|
||
/// "Default" in the picker means "the order Qobuz sent", which is exactly what
|
||
/// leaving the vector alone produces, and it is why `artist_prefs::set_sort`
|
||
/// DELETES the entry for it instead of storing a no-op.
|
||
///
|
||
/// `artist-asc` / `artist-desc` are unreachable from the artist page's picker
|
||
/// (its five options stop at title-desc) but are carried anyway so this stays
|
||
/// 1:1 with `sort_album_items`.
|
||
///
|
||
/// The discography page has its OWN copy of these arms
|
||
/// (`artist_releases_qt::sort_cards`) rather than reusing this one: its rows
|
||
/// are `home_qt::HomeCard` (they must carry `artPath` for `AlbumCollection`),
|
||
/// not `AlbumCardData`, so the two differ in their item type and nothing else.
|
||
/// `label_qt::sort_cards` is the third copy, for the same reason.
|
||
pub(crate) fn sort_release_cards(items: &mut [AlbumCardData], sort: &str) {
|
||
match sort {
|
||
"oldest" | "year-asc" => items.sort_by(|a, b| a.year.cmp(&b.year)),
|
||
"newest" | "year-desc" => items.sort_by(|a, b| b.year.cmp(&a.year)),
|
||
"title-asc" => items.sort_by(|a, b| a.title.to_lowercase().cmp(&b.title.to_lowercase())),
|
||
"title-desc" => items.sort_by(|a, b| b.title.to_lowercase().cmp(&a.title.to_lowercase())),
|
||
"artist-asc" => items.sort_by(|a, b| a.artist.to_lowercase().cmp(&b.artist.to_lowercase())),
|
||
"artist-desc" => {
|
||
items.sort_by(|a, b| b.artist.to_lowercase().cmp(&a.artist.to_lowercase()))
|
||
}
|
||
_ => {}
|
||
}
|
||
}
|
||
|
||
/// `sort_release_cards` over the STASHED document's `cards` array, which is raw
|
||
/// JSON rather than typed rows (the stash is a `serde_json::Value` — see
|
||
/// `ARTIST_DOC`).
|
||
///
|
||
/// The round-trip through `AlbumCardData` is deliberate: it keeps ONE copy of
|
||
/// the sort arms above instead of a second comparator written against
|
||
/// `Value`, and these arrays are at most a few dozen rows. A row that fails to
|
||
/// deserialize leaves the whole bucket untouched rather than half-sorted — the
|
||
/// document is what the view paints, and a partial re-order would be worse
|
||
/// than none.
|
||
fn sort_release_values(items: &mut [serde_json::Value], sort: &str) {
|
||
if sort == crate::artist_prefs::DEFAULT_SORT {
|
||
return;
|
||
}
|
||
let parsed: Result<Vec<AlbumCardData>, _> = items
|
||
.iter()
|
||
.map(|v| serde_json::from_value::<AlbumCardData>(v.clone()))
|
||
.collect();
|
||
let mut cards = match parsed {
|
||
Ok(cards) => cards,
|
||
Err(e) => {
|
||
log::warn!("[qbz-qt] release bucket not sortable ({e}) — leaving its order alone");
|
||
return;
|
||
}
|
||
};
|
||
sort_release_cards(&mut cards, sort);
|
||
for (slot, card) in items.iter_mut().zip(cards.iter()) {
|
||
match serde_json::to_value(card) {
|
||
Ok(value) => *slot = value,
|
||
Err(e) => log::warn!("[qbz-qt] release card re-serialize failed: {e}"),
|
||
}
|
||
}
|
||
}
|
||
|
||
/// artist.rs `map_track` (Popular Tracks / Appears On rows — album title +
|
||
/// cover ride the nested album object).
|
||
fn map_track(index: usize, track: PageArtistTrack) -> TrackRow {
|
||
let mut title = track.title;
|
||
if let Some(version) = track.version.as_ref().filter(|v| !v.is_empty()) {
|
||
title = format!("{title} ({version})");
|
||
}
|
||
let (artist, artist_id) = track
|
||
.artist
|
||
.map(|a| (a.name.display, a.id.to_string()))
|
||
.unwrap_or_default();
|
||
let (album_id, album, artwork_url) = track
|
||
.album
|
||
.map(|a| {
|
||
let url = a.image.and_then(|img| img.smallest().cloned()).unwrap_or_default();
|
||
(a.id, a.title, url)
|
||
})
|
||
.unwrap_or_default();
|
||
let bit_depth = track.audio_info.as_ref().and_then(|a| a.maximum_bit_depth);
|
||
let sample_rate = track.audio_info.as_ref().and_then(|a| a.maximum_sampling_rate);
|
||
TrackRow {
|
||
is_favorite: crate::fav_cache_qt::contains_track(track.id),
|
||
id: track.id.to_string(),
|
||
number: (index + 1).to_string(),
|
||
title,
|
||
artist,
|
||
artist_id,
|
||
album,
|
||
album_id,
|
||
duration: mmss(track.duration.unwrap_or(0)),
|
||
quality_tier: home_qt::quality_tier_from_depth(bit_depth).to_string(),
|
||
quality_detail: home_qt::quality_detail_from_parts(bit_depth, sample_rate),
|
||
// The RAW numbers ride with the row (see `TrackRow::bit_depth`) so
|
||
// `track_row_to_queue` can fill the queue entry the way the reference's
|
||
// `make_top_track_queue` (playback.rs:3188) fills it off `audio_info`.
|
||
bit_depth,
|
||
sample_rate,
|
||
explicit: track.parental_warning.unwrap_or(false),
|
||
disc: 1,
|
||
work_header: String::new(),
|
||
work_composer_name: String::new(),
|
||
work_composer_id: String::new(),
|
||
artwork_url,
|
||
}
|
||
}
|
||
|
||
/// Fetch and map an artist page by id (artist.rs `load_artist`/`map_artist`).
|
||
pub async fn load_artist(
|
||
runtime: &Arc<AppRuntime<LoggingAdapter>>,
|
||
artist_id: &str,
|
||
) -> Result<ArtistViewData, String> {
|
||
let id: u64 = artist_id
|
||
.parse()
|
||
.map_err(|_| format!("invalid artist id: {artist_id}"))?;
|
||
let page = runtime
|
||
.core()
|
||
.get_artist_page(id, None)
|
||
.await
|
||
.map_err(|e| e.to_string())?;
|
||
Ok(map_artist(page))
|
||
}
|
||
|
||
fn map_artist(page: PageArtistResponse) -> ArtistViewData {
|
||
let name = page.name.display;
|
||
// Biography: content (HTML-stripped) + the attribution name. Qobuz sends
|
||
// `biography.source` as a raw JSON value (sometimes a string, sometimes an
|
||
// object) — only the string form carries an attribution.
|
||
let (bio, bio_source) = match page.biography {
|
||
Some(biography) => {
|
||
let content = biography
|
||
.content
|
||
.map(|c| qbz_text_utils::strip_html::strip_html(&c))
|
||
.unwrap_or_default();
|
||
let source = biography
|
||
.source
|
||
.and_then(|v| {
|
||
v.as_str()
|
||
.map(qbz_text_utils::strip_html::decode_html_entities)
|
||
})
|
||
.unwrap_or_default();
|
||
(content, source)
|
||
}
|
||
None => (String::new(), String::new()),
|
||
};
|
||
let bio_short = truncate_words(&bio, 360);
|
||
let bio_truncated = bio_short != bio;
|
||
let artwork_url = page
|
||
.images
|
||
.and_then(|images| images.portrait)
|
||
.map(|portrait| {
|
||
format!(
|
||
"https://static.qobuz.com/images/artists/covers/large/{}.{}",
|
||
portrait.hash, portrait.format
|
||
)
|
||
})
|
||
.unwrap_or_default();
|
||
|
||
let top_tracks = page
|
||
.top_tracks
|
||
.unwrap_or_default()
|
||
.into_iter()
|
||
.enumerate()
|
||
.map(|(index, track)| map_track(index, track))
|
||
.collect();
|
||
|
||
// Server-driven bucketing (webplayer source of truth): every non-empty
|
||
// bucket in the official order; ids deduped across groups; labels from
|
||
// the artist's own "album" group.
|
||
let mut bucket_cards: HashMap<String, Vec<AlbumCardData>> = HashMap::new();
|
||
let mut bucket_has_more: HashMap<String, bool> = HashMap::new();
|
||
let mut seen_release_ids: HashSet<String> = HashSet::new();
|
||
let mut labels_by_id: BTreeMap<u64, String> = BTreeMap::new();
|
||
|
||
for group in page.releases.into_iter().flatten() {
|
||
let release_type = group.release_type.clone();
|
||
let is_album_group = release_type == "album";
|
||
*bucket_has_more.entry(release_type.clone()).or_insert(false) |= group.has_more;
|
||
for release in group.items.into_iter() {
|
||
if seen_release_ids.contains(&release.id) {
|
||
continue;
|
||
}
|
||
seen_release_ids.insert(release.id.clone());
|
||
if is_album_group {
|
||
if let Some(label) = release.label.as_ref() {
|
||
labels_by_id
|
||
.entry(label.id)
|
||
.or_insert_with(|| label.name.clone());
|
||
}
|
||
}
|
||
bucket_cards
|
||
.entry(release_type.clone())
|
||
.or_default()
|
||
.push(map_release(&release));
|
||
}
|
||
}
|
||
|
||
let mut labels: Vec<ArtistLabel> = labels_by_id
|
||
.into_iter()
|
||
.map(|(id, name)| ArtistLabel {
|
||
id: id.to_string(),
|
||
name,
|
||
})
|
||
.collect();
|
||
labels.sort_by(|a, b| a.name.to_lowercase().cmp(&b.name.to_lowercase()));
|
||
|
||
let similar_artists: Vec<ArtistSimilar> = page
|
||
.similar_artists
|
||
.map(|s| {
|
||
s.items
|
||
.into_iter()
|
||
.map(|item| ArtistSimilar {
|
||
id: item.id.to_string(),
|
||
name: item.name.display,
|
||
})
|
||
.collect()
|
||
})
|
||
.unwrap_or_default();
|
||
|
||
let playlists: Vec<ArtistPlaylist> = page
|
||
.playlists
|
||
.map(|p| {
|
||
p.items
|
||
.into_iter()
|
||
.map(|pl| {
|
||
let owner = pl
|
||
.owner
|
||
.and_then(|o| o.name)
|
||
.filter(|s| !s.is_empty())
|
||
.unwrap_or_else(|| "Qobuz".to_string());
|
||
let track_count = pl.tracks_count.unwrap_or(0);
|
||
let art_url = pl
|
||
.images
|
||
.and_then(|imgs| imgs.rectangle)
|
||
.and_then(|rects| rects.into_iter().find(|s| !s.is_empty()))
|
||
.unwrap_or_default();
|
||
ArtistPlaylist {
|
||
id: pl.id.to_string(),
|
||
title: pl.title.unwrap_or_default(),
|
||
subtitle: format!("{owner} · {track_count}"),
|
||
art_url,
|
||
}
|
||
})
|
||
.collect()
|
||
})
|
||
.unwrap_or_default();
|
||
|
||
let last_release = page.last_release.as_ref().map(map_release);
|
||
let appears_on: Vec<TrackRow> = page
|
||
.tracks_appears_on
|
||
.into_iter()
|
||
.flatten()
|
||
.enumerate()
|
||
.map(|(index, track)| map_track(index, track))
|
||
.collect();
|
||
|
||
// The persisted per-bucket sorts, read ONCE for the whole page (the Slint
|
||
// calls `artist_prefs::get_sort` per bucket off a thread_local cache;
|
||
// this port has no cache by design — see artist_prefs.rs's header).
|
||
let saved_sorts = crate::artist_prefs::read_all();
|
||
let sort_for = |rt: &str| {
|
||
saved_sorts
|
||
.get(rt)
|
||
.cloned()
|
||
.unwrap_or_else(|| crate::artist_prefs::DEFAULT_SORT.to_string())
|
||
};
|
||
|
||
// One section per non-empty bucket in the official order; "download"
|
||
// drains but never renders (Purchase Only is hidden upstream too).
|
||
let mut release_sections: Vec<ArtistReleaseSection> = Vec::new();
|
||
for &(rt, title) in RELEASE_SECTION_ORDER {
|
||
if rt == "download" {
|
||
bucket_cards.remove(rt);
|
||
continue;
|
||
}
|
||
if let Some(mut cards) = bucket_cards.remove(rt) {
|
||
if cards.is_empty() {
|
||
continue;
|
||
}
|
||
// artist.rs:786-812 — the sort is applied at page BUILD, not on
|
||
// first click, "so the first paint already honors the user's
|
||
// choice", and `sort_by` is stamped so the picker seats itself on
|
||
// that choice instead of snapping back to Default on a revisit.
|
||
let sort = sort_for(rt);
|
||
sort_release_cards(&mut cards, &sort);
|
||
release_sections.push(ArtistReleaseSection {
|
||
release_type: rt.to_string(),
|
||
title: qbz_i18n::t(title),
|
||
has_more: bucket_has_more.get(rt).copied().unwrap_or(false),
|
||
cards,
|
||
sort_by: sort,
|
||
});
|
||
}
|
||
}
|
||
// Leftovers: server buckets unknown to the table, appended (title-cased).
|
||
// These render a full ReleaseSection header, sort picker included, so they
|
||
// must round-trip the pref exactly like the known buckets.
|
||
for (rt, mut cards) in bucket_cards {
|
||
if cards.is_empty() {
|
||
continue;
|
||
}
|
||
// Factored into `title_case` (above) when the discography page needed
|
||
// the same fallback — one copy, so a leftover bucket's section header
|
||
// and its discography page header cannot drift apart.
|
||
let title = title_case(&rt);
|
||
let sort = sort_for(&rt);
|
||
sort_release_cards(&mut cards, &sort);
|
||
release_sections.push(ArtistReleaseSection {
|
||
has_more: false,
|
||
release_type: rt,
|
||
title,
|
||
cards,
|
||
sort_by: sort,
|
||
});
|
||
}
|
||
|
||
// The library COUNT is feed-only by nature (it counts owned items), but the
|
||
// follow state must not be: the feed is empty until the Library view is
|
||
// opened, so this drew an un-followed heart on artists the user follows —
|
||
// and `toggle_favorite` then read the same false and re-added the follow.
|
||
// `library_qt::is_favorite` checks the favourite-id cache first.
|
||
let library_count = crate::library_qt::with_library(|d| {
|
||
d.feed
|
||
.iter()
|
||
.filter(|i| {
|
||
(i.kind == "album" || i.kind == "track") && i.artist_id == page.id.to_string()
|
||
})
|
||
.count() as i64
|
||
})
|
||
.unwrap_or(0);
|
||
let is_following = crate::library_qt::is_favorite("artist", &page.id.to_string());
|
||
|
||
ArtistViewData {
|
||
id: page.id.to_string(),
|
||
name,
|
||
bio,
|
||
bio_short,
|
||
bio_truncated,
|
||
bio_source,
|
||
artwork_url,
|
||
is_following,
|
||
is_pinned: crate::sidebar_qt::is_pinned("artist", &page.id.to_string()),
|
||
is_blacklisted: crate::artist_blacklist::is_blacklisted(page.id),
|
||
library_count,
|
||
top_tracks,
|
||
appears_on,
|
||
last_release,
|
||
release_sections,
|
||
labels,
|
||
similar_artists,
|
||
playlists,
|
||
// Filled by the background enrichment pass (see load_artist_view).
|
||
stories: Vec::new(),
|
||
stories_loading: false,
|
||
network: ArtistNetwork::default(),
|
||
// Header atmosphere: the tint resolves from this page's artwork on a
|
||
// background pass (spawn_header_color), so the first publish carries
|
||
// the empty value and the gradient fades in with it.
|
||
header_color: String::new(),
|
||
header_gradient: false,
|
||
}
|
||
}
|
||
|
||
/// The last loaded artist's Popular Tracks as a playable queue (row play
|
||
/// + Play-all + Shuffle-all + Play-next/queue on rows).
|
||
static TOP_QUEUE: std::sync::Mutex<Vec<QueueTrack>> = std::sync::Mutex::new(Vec::new());
|
||
|
||
fn track_row_to_queue(row: &TrackRow) -> QueueTrack {
|
||
let duration_secs = {
|
||
let mut parts = row.duration.split(':');
|
||
parts.next().and_then(|m| m.parse::<u64>().ok()).unwrap_or(0) * 60
|
||
+ parts.next().and_then(|s| s.parse::<u64>().ok()).unwrap_or(0)
|
||
};
|
||
QueueTrack {
|
||
id: row.id.parse().unwrap_or(0),
|
||
title: row.title.clone(),
|
||
version: None,
|
||
artist: row.artist.clone(),
|
||
album: row.album.clone(),
|
||
album_version: None,
|
||
duration_secs,
|
||
artwork_url: if row.artwork_url.is_empty() {
|
||
None
|
||
} else {
|
||
Some(row.artwork_url.clone())
|
||
},
|
||
// playback.rs `make_top_track_queue` (:3188): the CATALOG max travels
|
||
// with the queue track. `None` here zeroed `quality_state`'s
|
||
// `TRACK_MAX_*` seed, so the NPB AudioStamp drew a bare tier with no
|
||
// "24-bit / 96 kHz" line on every artist Popular-Tracks play.
|
||
hires: row.quality_tier == "hires",
|
||
bit_depth: row.bit_depth,
|
||
sample_rate: row.sample_rate,
|
||
is_local: false,
|
||
album_id: if row.album_id.is_empty() {
|
||
None
|
||
} else {
|
||
Some(row.album_id.clone())
|
||
},
|
||
artist_id: row.artist_id.parse::<u64>().ok(),
|
||
streamable: true,
|
||
source: Some("qobuz".to_string()),
|
||
parental_warning: row.explicit,
|
||
source_item_id_hint: None,
|
||
context_kind: None,
|
||
context_id: None,
|
||
}
|
||
}
|
||
|
||
/// Stash the queue at publish time (called from load_artist_view).
|
||
///
|
||
/// The queue is born STAMPED with the PAGE's artist — playback.rs stamps
|
||
/// ("artist", artist_id) at every artist play path (:2848 Popular, :3108
|
||
/// shuffled, :3170 from-row) and never derives it. Deriving is wrong here in
|
||
/// two ways: `derive_context` returns the shared ALBUM before it ever tests the
|
||
/// artist (so an artist whose Popular Tracks come from one album lands on that
|
||
/// album), and `TrackRow.artist_id` is the track's OWN credited performer
|
||
/// (`map_track`), so a single featured collaboration makes the queue share no
|
||
/// artist and lose its origin entirely. `data.id` is the page id — the thing
|
||
/// the user actually launched.
|
||
fn stash_top_queue(data: &ArtistViewData) {
|
||
let mut queue: Vec<QueueTrack> = data.top_tracks.iter().map(track_row_to_queue).collect();
|
||
if !data.id.is_empty() {
|
||
for track in queue.iter_mut() {
|
||
track.context_kind = Some("artist".to_string());
|
||
track.context_id = Some(data.id.clone());
|
||
}
|
||
}
|
||
*TOP_QUEUE.lock().unwrap() = queue;
|
||
}
|
||
|
||
/// The stashed queue (cloned) + the start index for `track_id` (0 when
|
||
/// unknown — e.g. Play-all).
|
||
pub fn top_queue(track_id: Option<u64>) -> (Vec<QueueTrack>, usize) {
|
||
let queue = TOP_QUEUE.lock().unwrap().clone();
|
||
let start = track_id
|
||
.and_then(|id| queue.iter().position(|t| t.id == id))
|
||
.unwrap_or(0);
|
||
(queue, start)
|
||
}
|
||
|
||
/// One more page of a releases bucket (artist.rs `load_release_page`).
|
||
pub async fn load_release_page(
|
||
runtime: &Arc<AppRuntime<LoggingAdapter>>,
|
||
artist_id: &str,
|
||
release_type: &str,
|
||
offset: u32,
|
||
) -> Result<(Vec<AlbumCardData>, bool), String> {
|
||
let id: u64 = artist_id
|
||
.parse()
|
||
.map_err(|_| format!("invalid artist id: {artist_id}"))?;
|
||
let resp = runtime
|
||
.core()
|
||
.get_releases_grid(id, release_type, RELEASE_PAGE_SIZE, offset, Some("release_date"))
|
||
.await
|
||
.map_err(|e| e.to_string())?;
|
||
let has_more = resp.has_more;
|
||
let cards: Vec<AlbumCardData> = resp.items.iter().map(map_release).collect();
|
||
// Fold the page into the stashed document too. The view appends it to its
|
||
// own parsed copy when the signal lands, but an enrichment pass that
|
||
// republishes the document afterwards would otherwise reset the section
|
||
// back to page 1. Ids are deduped on both sides.
|
||
merge_release_page(artist_id, release_type, &cards, has_more);
|
||
Ok((cards, has_more))
|
||
}
|
||
|
||
/// Append `cards` to `release_type`'s bucket inside the stashed document
|
||
/// (no republish — the caller's `releaseSectionReady` signal already paints
|
||
/// them; this only keeps a LATER republish from losing them). Ignored when
|
||
/// the stashed document is for a different artist (the user navigated away
|
||
/// while the page was in flight).
|
||
fn merge_release_page(
|
||
artist_id: &str,
|
||
release_type: &str,
|
||
cards: &[AlbumCardData],
|
||
has_more: bool,
|
||
) {
|
||
let Ok(mut guard) = ARTIST_DOC.lock() else {
|
||
return;
|
||
};
|
||
let Some((_, doc)) = guard.as_mut() else {
|
||
return;
|
||
};
|
||
if doc.get("id").and_then(|v| v.as_str()) != Some(artist_id) {
|
||
return;
|
||
}
|
||
let Some(sections) = doc
|
||
.get_mut("releaseSections")
|
||
.and_then(|v| v.as_array_mut())
|
||
else {
|
||
return;
|
||
};
|
||
let Some(section) = sections.iter_mut().find(|s| {
|
||
s.get("releaseType").and_then(|v| v.as_str()) == Some(release_type)
|
||
}) else {
|
||
return;
|
||
};
|
||
section["hasMore"] = json!(has_more);
|
||
// The bucket's live sort, read off the row itself — artist.rs:1245-1258
|
||
// `append_release_page` does exactly this (`let sort = row.sort_by...`)
|
||
// before re-sorting, because under a custom sort a page appended at the
|
||
// TAIL is simply in the wrong place.
|
||
let sort = section
|
||
.get("sortBy")
|
||
.and_then(|v| v.as_str())
|
||
.unwrap_or(crate::artist_prefs::DEFAULT_SORT)
|
||
.to_string();
|
||
let Some(existing) = section.get_mut("cards").and_then(|v| v.as_array_mut()) else {
|
||
return;
|
||
};
|
||
let seen: HashSet<String> = existing
|
||
.iter()
|
||
.filter_map(|c| c.get("id").and_then(|v| v.as_str()).map(str::to_string))
|
||
.collect();
|
||
for card in cards {
|
||
if seen.contains(&card.id) {
|
||
continue;
|
||
}
|
||
if let Ok(value) = serde_json::to_value(card) {
|
||
existing.push(value);
|
||
}
|
||
}
|
||
// No-op under "default" (the whole point of that key), so the common path
|
||
// pays nothing.
|
||
sort_release_values(existing, &sort);
|
||
}
|
||
|
||
/// Whether the stashed artist document still belongs to `artist_id` — the
|
||
/// SAME test `merge_release_page` opens with, exported so main.rs's
|
||
/// `load_release_section` can apply it to the OTHER half of a landed page.
|
||
///
|
||
/// The page travels on two legs: `merge_release_page` folds it into the stash
|
||
/// (guarded — a stash for a different artist drops it), and the
|
||
/// `releaseSectionReady` signal hands it to the view. The signal carries NO
|
||
/// artist id, and ArtistView.qml folds whatever arrives into
|
||
/// `root.releaseOverlay` keyed by release_type alone — keys every artist
|
||
/// shares ("album" exists on all of them). So a page requested on artist A
|
||
/// that lands after artist B's document has been published would graft A's
|
||
/// albums onto B's album grid, permanently (the overlay is only reset when the
|
||
/// artist ID changes again). Emitting only while the stash still names the
|
||
/// requesting artist closes that leak at its source; the view cannot, because
|
||
/// the signal does not tell it whose page this is.
|
||
pub(crate) fn stash_is_for(artist_id: &str) -> bool {
|
||
match ARTIST_DOC.lock() {
|
||
Ok(guard) => guard
|
||
.as_ref()
|
||
.map(|(_, doc)| doc.get("id").and_then(|v| v.as_str()) == Some(artist_id))
|
||
.unwrap_or(false),
|
||
Err(_) => false,
|
||
}
|
||
}
|
||
|
||
/// Artist page per-section sort — the port of `crates/qbz/src/artist.rs:1178-1210`
|
||
/// `resort_section` ("Re-sort one release bucket in place […] and persist the
|
||
/// choice"), reached from `QbzArtist.setSectionSort` (artist_bridge.rs).
|
||
///
|
||
/// THE SORT NEVER LEAVES THIS PROCESS. `main.rs:14997-15007` — the whole Slint
|
||
/// handler — calls `artist::resort_section` and nothing else: no refetch, no
|
||
/// query param. Every artist-page `get_releases_grid` call in BOTH trees passes
|
||
/// the constant `Some("release_date")` (here: `load_release_page`), and the five
|
||
/// picker keys have no server equivalent; `album_map::sort_album_items` applies
|
||
/// them locally over the cards already loaded. So `load_release_page` is
|
||
/// deliberately left untouched by this feature.
|
||
///
|
||
/// Persist FIRST (so the choice survives even if the page is being torn down),
|
||
/// then patch the stashed document and republish it through the existing
|
||
/// generation-guarded `publish_patch` — the port's ONE transport for the artist
|
||
/// view. Re-serializing the whole document is also what makes QML notice: a JS
|
||
/// array handed back by the same reference re-triggers nothing
|
||
/// (cards/PlaylistCollage.qml's rule), whereas a fresh `artistJson` string
|
||
/// re-runs the parse.
|
||
///
|
||
/// Reading the generation off the stash rather than off `ARTIST_GEN` is what
|
||
/// keeps a click aimed at the page ON SCREEN: if the user has already navigated
|
||
/// away, the stash belongs to another artist and `publish_patch` drops the
|
||
/// edit.
|
||
///
|
||
/// 1:1 note — picking "Default" after "A–Z" does NOT restore the server order
|
||
/// until the page is reloaded, because "default" is a no-op sort over the
|
||
/// vector as it stands. The reference behaves identically (`sort_album_items`
|
||
/// falls through on that key); restoring it would need an untouched copy of the
|
||
/// server order that neither tree keeps.
|
||
pub(crate) fn resort_section(release_type: &str, sort: &str) {
|
||
crate::artist_prefs::set_sort(release_type, sort);
|
||
// The guard is dropped at the end of this statement — `publish_patch`
|
||
// takes the same non-reentrant lock.
|
||
let generation = ARTIST_DOC
|
||
.lock()
|
||
.ok()
|
||
.and_then(|guard| guard.as_ref().map(|(gen, _)| *gen));
|
||
let Some(generation) = generation else {
|
||
return;
|
||
};
|
||
let release_type = release_type.to_string();
|
||
let sort = sort.to_string();
|
||
publish_patch(generation, move |doc| {
|
||
let Some(sections) = doc.get_mut("releaseSections").and_then(|v| v.as_array_mut()) else {
|
||
return;
|
||
};
|
||
let Some(section) = sections.iter_mut().find(|s| {
|
||
s.get("releaseType").and_then(|v| v.as_str()) == Some(release_type.as_str())
|
||
}) else {
|
||
return;
|
||
};
|
||
// Stamp the key BEFORE the cards: `sortBy` is what seats the picker
|
||
// (and what `merge_release_page` reads for the next page), so it has to
|
||
// be written even when the sort itself is a no-op.
|
||
section["sortBy"] = json!(sort);
|
||
if let Some(cards) = section.get_mut("cards").and_then(|v| v.as_array_mut()) {
|
||
sort_release_values(cards, &sort);
|
||
}
|
||
});
|
||
}
|
||
|
||
/// Fetch + publish (perf-marked like phase 5), then kick the progressive
|
||
/// enrichment (Magazine stories + the MusicBrainz Network sidebar) off in the
|
||
/// background. The returned JSON is what main.rs writes onto the bridge; every
|
||
/// later pass re-publishes the SAME document through `publish_patch`.
|
||
pub async fn load_artist_view(
|
||
runtime: &Arc<AppRuntime<LoggingAdapter>>,
|
||
artist_id: &str,
|
||
) -> Result<String, String> {
|
||
let t = Instant::now();
|
||
let mut data = load_artist(runtime, artist_id).await?;
|
||
let sections: usize = data.release_sections.iter().map(|s| s.cards.len()).sum();
|
||
stash_top_queue(&data);
|
||
|
||
// Seed the sidebar's own loading flags BEFORE serializing so the sections
|
||
// paint their "Loading…" state with the first frame. `mb_available` is the
|
||
// user's MusicBrainz opt-in read straight from the core: false here means
|
||
// the enrichment task never even starts, so no MB request is issued.
|
||
let mb_on = runtime.core().musicbrainz_is_enabled().await;
|
||
data.network.mb_available = mb_on;
|
||
data.network.origin_loading = mb_on;
|
||
data.network.relationships_loading = mb_on;
|
||
data.network.discovery_loading = mb_on;
|
||
data.stories_loading = true;
|
||
// Header atmosphere — see the ArtistViewData field comments.
|
||
data.header_gradient = crate::settings_qt::pref_bool("album_header_gradient", true);
|
||
let header_art = data.artwork_url.clone();
|
||
|
||
let json = serde_json::to_string(&data).map_err(|e| e.to_string())?;
|
||
log::info!(
|
||
"[qbz-qt][perf] artist load: {:?} ({} top tracks, {} releases in {} sections)",
|
||
t.elapsed(),
|
||
data.top_tracks.len(),
|
||
sections,
|
||
data.release_sections.len(),
|
||
);
|
||
|
||
// Stash the document for the progressive passes, under a fresh generation.
|
||
let generation = ARTIST_GEN.fetch_add(1, Ordering::SeqCst) + 1;
|
||
match serde_json::from_str::<serde_json::Value>(&json) {
|
||
Ok(doc) => {
|
||
if let Ok(mut guard) = ARTIST_DOC.lock() {
|
||
*guard = Some((generation, doc));
|
||
}
|
||
}
|
||
Err(e) => log::warn!("[qbz-qt] artist doc stash failed: {e}"),
|
||
}
|
||
|
||
spawn_header_color(generation, header_art);
|
||
|
||
let similar_names: Vec<String> =
|
||
data.similar_artists.iter().map(|s| s.name.clone()).collect();
|
||
spawn_enrichment(
|
||
runtime.clone(),
|
||
generation,
|
||
artist_id.to_string(),
|
||
data.name.clone(),
|
||
similar_names,
|
||
mb_on,
|
||
);
|
||
|
||
Ok(json)
|
||
}
|
||
|
||
// ==================== Progressive enrichment (stories + MusicBrainz) =======
|
||
|
||
/// Monotonic id for the artist page currently on screen. Every background
|
||
/// pass carries the generation it was started for and is dropped when the
|
||
/// user has navigated on — a slow MB reply can never repaint a later artist.
|
||
static ARTIST_GEN: AtomicU64 = AtomicU64::new(0);
|
||
/// The last published artist document, kept so a partial update can be merged
|
||
/// into it and the WHOLE document re-published (the bridge carries exactly one
|
||
/// JSON property, phase-23 pattern).
|
||
static ARTIST_DOC: Mutex<Option<(u64, serde_json::Value)>> = Mutex::new(None);
|
||
|
||
/// Merge `f`'s edits into the stashed document and re-publish it, but only
|
||
/// while `generation` is still the page on screen.
|
||
fn publish_patch(generation: u64, f: impl FnOnce(&mut serde_json::Value)) {
|
||
let json = {
|
||
let Ok(mut guard) = ARTIST_DOC.lock() else {
|
||
return;
|
||
};
|
||
let Some((current, doc)) = guard.as_mut() else {
|
||
return;
|
||
};
|
||
if *current != generation {
|
||
return;
|
||
}
|
||
f(doc);
|
||
match serde_json::to_string(&*doc) {
|
||
Ok(s) => s,
|
||
Err(e) => {
|
||
log::warn!("[qbz-qt] artist doc republish failed: {e}");
|
||
return;
|
||
}
|
||
}
|
||
};
|
||
crate::artist_bridge::ui(move |mut b| {
|
||
b.as_mut().set_artist_json(QString::from(json.as_str()));
|
||
});
|
||
}
|
||
|
||
/// Resolve the artist portrait (downloading it if needed), reduce it to the
|
||
/// header tint (`album_qt::header_tint_hex` — ONE colour pipeline for both
|
||
/// detail pages, exactly as the Slint shares `artwork::header_tint`) and
|
||
/// patch it in. Generation-guarded: a slow portrait for artist A can never
|
||
/// tint artist B's header. Same benign publish-ordering note as
|
||
/// `album_qt::spawn_header_color` — the stash is written first, so a
|
||
/// clobbered publish is recovered by the next enrichment republish (stories
|
||
/// always run).
|
||
fn spawn_header_color(generation: u64, artwork_url: String) {
|
||
if artwork_url.is_empty() {
|
||
return;
|
||
}
|
||
crate::spawn(async move {
|
||
if crate::artwork_qt::cached_path(&artwork_url).is_empty() {
|
||
crate::artwork_qt::download_missing(vec![artwork_url.clone()]).await;
|
||
}
|
||
let path = crate::artwork_qt::cached_path(&artwork_url);
|
||
if path.is_empty() {
|
||
return;
|
||
}
|
||
let path = path.trim_start_matches("file://").to_string();
|
||
// One decode, two products: the flat tint (Slint's FALLBACK arm) and
|
||
// the blurred atmosphere it actually renders behind the header.
|
||
let p2 = path.clone();
|
||
let hex = tokio::task::spawn_blocking(move || crate::album_qt::header_tint_hex(&path))
|
||
.await
|
||
.ok()
|
||
.flatten();
|
||
let atmo = tokio::task::spawn_blocking(move || {
|
||
crate::atmosphere_qt::for_cover_blocking(&p2)
|
||
})
|
||
.await
|
||
.ok()
|
||
.flatten();
|
||
if hex.is_some() || atmo.is_some() {
|
||
publish_patch(generation, move |doc| {
|
||
if let Some(hex) = hex {
|
||
doc["headerColor"] = serde_json::json!(hex);
|
||
}
|
||
if let Some(atmo) = atmo {
|
||
doc["headerAtmosphere"] = serde_json::json!(atmo);
|
||
}
|
||
});
|
||
}
|
||
});
|
||
}
|
||
|
||
/// Patch fields inside the `network` object and re-publish.
|
||
fn publish_network(generation: u64, fields: Vec<(&'static str, serde_json::Value)>) {
|
||
publish_patch(generation, move |doc| {
|
||
if let Some(net) = doc.get_mut("network").and_then(|v| v.as_object_mut()) {
|
||
for (key, value) in fields {
|
||
net.insert(key.to_string(), value);
|
||
}
|
||
}
|
||
});
|
||
}
|
||
|
||
/// Every MB section off at once — MB disabled, no confident match, or the
|
||
/// resolve failed. The sidebar renders NOTHING for them (opt-in rule).
|
||
fn publish_mb_unavailable(generation: u64) {
|
||
publish_network(
|
||
generation,
|
||
vec![
|
||
("mbAvailable", json!(false)),
|
||
("originLoading", json!(false)),
|
||
("relationshipsLoading", json!(false)),
|
||
("discoveryLoading", json!(false)),
|
||
],
|
||
);
|
||
}
|
||
|
||
fn spawn_enrichment(
|
||
runtime: Arc<AppRuntime<LoggingAdapter>>,
|
||
generation: u64,
|
||
artist_id: String,
|
||
artist_name: String,
|
||
similar_names: Vec<String>,
|
||
mb_on: bool,
|
||
) {
|
||
// --- Magazine stories (Qobuz editorial, MB-independent) ---------------
|
||
{
|
||
let runtime = runtime.clone();
|
||
crate::spawn(async move {
|
||
let stories = load_stories(&runtime, &artist_id).await;
|
||
publish_patch(generation, move |doc| {
|
||
doc["stories"] =
|
||
serde_json::to_value(&stories).unwrap_or_else(|_| json!([]));
|
||
doc["storiesLoading"] = json!(false);
|
||
});
|
||
});
|
||
}
|
||
|
||
// --- MusicBrainz network sidebar --------------------------------------
|
||
// Hard gate: with MB off nothing below runs, so nothing leaves the process.
|
||
if !mb_on {
|
||
return;
|
||
}
|
||
crate::spawn(async move {
|
||
let meta = match load_mb_metadata(&runtime, &artist_name).await {
|
||
Ok(Some(meta)) => meta,
|
||
Ok(None) => {
|
||
publish_mb_unavailable(generation);
|
||
return;
|
||
}
|
||
Err(e) => {
|
||
log::warn!("[qbz-qt] MB metadata load failed: {e}");
|
||
publish_mb_unavailable(generation);
|
||
return;
|
||
}
|
||
};
|
||
let mbid = meta.mbid.clone();
|
||
publish_network(
|
||
generation,
|
||
vec![
|
||
("mbid", json!(mbid.clone())),
|
||
(
|
||
"origin",
|
||
serde_json::to_value(&meta.origin).unwrap_or_else(|_| json!({})),
|
||
),
|
||
("originLoading", json!(false)),
|
||
],
|
||
);
|
||
|
||
match load_mb_relationships(&runtime, &mbid).await {
|
||
Ok(rel) => publish_network(
|
||
generation,
|
||
vec![
|
||
(
|
||
"relationships",
|
||
serde_json::to_value(&rel).unwrap_or_else(|_| json!({})),
|
||
),
|
||
("relationshipsLoading", json!(false)),
|
||
],
|
||
),
|
||
Err(e) => {
|
||
log::warn!("[qbz-qt] MB relationships failed: {e}");
|
||
publish_network(generation, vec![("relationshipsLoading", json!(false))]);
|
||
}
|
||
}
|
||
|
||
match load_mb_discovery(&runtime, &mbid, &artist_name, similar_names).await {
|
||
Ok((tag, rows)) => publish_network(
|
||
generation,
|
||
vec![
|
||
("discoveryTag", json!(tag)),
|
||
(
|
||
"discovery",
|
||
serde_json::to_value(&rows).unwrap_or_else(|_| json!([])),
|
||
),
|
||
("discoveryLoading", json!(false)),
|
||
],
|
||
),
|
||
Err(e) => {
|
||
log::warn!("[qbz-qt] MB discovery failed: {e}");
|
||
publish_network(generation, vec![("discoveryLoading", json!(false))]);
|
||
}
|
||
}
|
||
});
|
||
}
|
||
|
||
// ----- Magazine stories ---------------------------------------------------
|
||
|
||
fn map_story(item: ArtistStoryItem) -> StoryRow {
|
||
let author = item
|
||
.authors
|
||
.and_then(|list| list.into_iter().next())
|
||
.map(|a| a.name)
|
||
.unwrap_or_default();
|
||
// `image` is a ready-to-use arc-cdn URL; fall back to the first `images[]`.
|
||
let art_url = item
|
||
.image
|
||
.or_else(|| {
|
||
item.images
|
||
.and_then(|list| list.into_iter().next())
|
||
.map(|img| img.url)
|
||
})
|
||
.unwrap_or_default();
|
||
StoryRow {
|
||
url: format!("https://play.qobuz.com/magazine/story/{}", item.id),
|
||
// Magazine content comes from a CMS: titles carry entities (& …),
|
||
// excerpts may additionally carry markup.
|
||
title: qbz_text_utils::strip_html::decode_html_entities(&item.title),
|
||
author,
|
||
excerpt: item
|
||
.description_short
|
||
.as_deref()
|
||
.map(qbz_text_utils::strip_html::strip_html)
|
||
.unwrap_or_default(),
|
||
art_url,
|
||
}
|
||
}
|
||
|
||
/// The artist's Magazine stories (limit 2, like the official client). Any
|
||
/// failure yields an empty list and the tab shows "No stories".
|
||
async fn load_stories(
|
||
runtime: &Arc<AppRuntime<LoggingAdapter>>,
|
||
artist_id: &str,
|
||
) -> Vec<StoryRow> {
|
||
let Ok(id) = artist_id.parse::<u64>() else {
|
||
return Vec::new();
|
||
};
|
||
match runtime.core().get_artist_story(id, 0, 2).await {
|
||
Ok(resp) => resp.items.into_iter().map(map_story).collect(),
|
||
Err(e) => {
|
||
log::warn!("[qbz-qt] artist story load failed: {e}");
|
||
Vec::new()
|
||
}
|
||
}
|
||
}
|
||
|
||
// ----- MusicBrainz: Origin ------------------------------------------------
|
||
|
||
struct MbMetadata {
|
||
mbid: String,
|
||
origin: MbOriginJson,
|
||
}
|
||
|
||
/// Resolve the artist name to an MBID, then fetch its metadata. `Ok(None)` =
|
||
/// MB disabled or no confident match — the caller hides every MB section.
|
||
async fn load_mb_metadata(
|
||
runtime: &Arc<AppRuntime<LoggingAdapter>>,
|
||
artist_name: &str,
|
||
) -> Result<Option<MbMetadata>, String> {
|
||
if !runtime.core().musicbrainz_is_enabled().await {
|
||
return Ok(None);
|
||
}
|
||
let resolved = runtime
|
||
.core()
|
||
.musicbrainz_resolve_artist(artist_name)
|
||
.await
|
||
.map_err(|e| e.to_string())?;
|
||
let Some(resolved) = resolved else {
|
||
return Ok(None);
|
||
};
|
||
let mbid = resolved.mbid;
|
||
if mbid.is_empty() {
|
||
return Ok(None);
|
||
}
|
||
let meta = runtime
|
||
.core()
|
||
.musicbrainz_get_artist_metadata(&mbid)
|
||
.await
|
||
.map_err(|e| e.to_string())?;
|
||
Ok(Some(MbMetadata {
|
||
origin: map_origin(&meta),
|
||
mbid,
|
||
}))
|
||
}
|
||
|
||
fn map_origin(meta: &qbz_integrations::musicbrainz::ArtistMetadata) -> MbOriginJson {
|
||
use qbz_integrations::musicbrainz::ArtistType;
|
||
|
||
let is_person = matches!(meta.artist_type, ArtistType::Person);
|
||
let begin_date = meta
|
||
.life_span
|
||
.as_ref()
|
||
.and_then(|ls| ls.begin.as_deref().map(format_mb_date_short))
|
||
.unwrap_or_default();
|
||
let end_date = meta
|
||
.life_span
|
||
.as_ref()
|
||
.and_then(|ls| ls.end.as_deref().map(format_mb_date_short))
|
||
.unwrap_or_default();
|
||
let location_display = meta
|
||
.location
|
||
.as_ref()
|
||
.map(|loc| loc.display_name.clone())
|
||
.unwrap_or_default();
|
||
let has_data =
|
||
!begin_date.is_empty() || !end_date.is_empty() || !location_display.is_empty();
|
||
MbOriginJson {
|
||
is_person,
|
||
begin_date,
|
||
end_date,
|
||
location_display,
|
||
has_data,
|
||
}
|
||
}
|
||
|
||
/// A MusicBrainz partial date as "1990" / "May 1990" / "May 14, 1990"
|
||
/// (artist.rs `format_mb_date_short`; month names go through the catalog).
|
||
fn format_mb_date_short(date: &str) -> String {
|
||
let parts: Vec<&str> = date.split('-').collect();
|
||
let month = |m: &str| -> Option<&'static str> {
|
||
Some(match m {
|
||
"01" => qbz_i18n::mark("January"),
|
||
"02" => qbz_i18n::mark("February"),
|
||
"03" => qbz_i18n::mark("March"),
|
||
"04" => qbz_i18n::mark("April"),
|
||
"05" => qbz_i18n::mark("May"),
|
||
"06" => qbz_i18n::mark("June"),
|
||
"07" => qbz_i18n::mark("July"),
|
||
"08" => qbz_i18n::mark("August"),
|
||
"09" => qbz_i18n::mark("September"),
|
||
"10" => qbz_i18n::mark("October"),
|
||
"11" => qbz_i18n::mark("November"),
|
||
"12" => qbz_i18n::mark("December"),
|
||
_ => return None,
|
||
})
|
||
};
|
||
match parts.as_slice() {
|
||
[y] => (*y).to_string(),
|
||
[y, m] => match month(m) {
|
||
Some(name) => {
|
||
let name_tr = qbz_i18n::t(name);
|
||
qbz_i18n::t_args("{} {}", &[name_tr.as_str(), *y])
|
||
}
|
||
None => date.to_string(),
|
||
},
|
||
[y, m, d] => match month(m) {
|
||
Some(name) => {
|
||
let day = d.trim_start_matches('0');
|
||
let name_tr = qbz_i18n::t(name);
|
||
qbz_i18n::t_args("{} {}, {}", &[name_tr.as_str(), day, *y])
|
||
}
|
||
None => date.to_string(),
|
||
},
|
||
_ => date.to_string(),
|
||
}
|
||
}
|
||
|
||
// ----- MusicBrainz: Relationships -----------------------------------------
|
||
|
||
async fn load_mb_relationships(
|
||
runtime: &Arc<AppRuntime<LoggingAdapter>>,
|
||
mbid: &str,
|
||
) -> Result<MbRelationshipsJson, String> {
|
||
let relations = runtime
|
||
.core()
|
||
.musicbrainz_get_artist_relationships(mbid)
|
||
.await
|
||
.map_err(|e| e.to_string())?;
|
||
Ok(map_relationships(relations))
|
||
}
|
||
|
||
fn map_relationships(
|
||
rels: qbz_integrations::musicbrainz::ArtistRelationships,
|
||
) -> MbRelationshipsJson {
|
||
let members = group_relations(rels.members, "Band Member");
|
||
let groups = group_relations(rels.groups, "Band");
|
||
let collaborators = group_relations(rels.collaborators, "Collaborator");
|
||
let has_data = !members.is_empty() || !groups.is_empty() || !collaborators.is_empty();
|
||
MbRelationshipsJson {
|
||
members,
|
||
groups,
|
||
collaborators,
|
||
has_data,
|
||
}
|
||
}
|
||
|
||
/// Group repeated relations by mbid, combining their roles (artist.rs
|
||
/// `group_relations` / Tauri's `groupMembersByMbid`).
|
||
fn group_relations(
|
||
rels: Vec<qbz_integrations::musicbrainz::RelatedArtist>,
|
||
default_role: &str,
|
||
) -> Vec<MbRelationshipJson> {
|
||
struct Pending {
|
||
name: String,
|
||
roles: Vec<String>,
|
||
begin: Option<String>,
|
||
end: Option<String>,
|
||
}
|
||
let mut by_mbid: HashMap<String, Pending> = HashMap::new();
|
||
let mut order: Vec<String> = Vec::new();
|
||
for r in rels {
|
||
let begin = r.period.as_ref().and_then(|p| p.begin.clone());
|
||
let end = r.period.as_ref().and_then(|p| p.end.clone());
|
||
match by_mbid.get_mut(&r.mbid) {
|
||
Some(existing) => {
|
||
if let Some(role) = r.role.clone() {
|
||
if !existing.roles.iter().any(|rr| rr == &role) {
|
||
existing.roles.push(role);
|
||
}
|
||
}
|
||
}
|
||
None => {
|
||
order.push(r.mbid.clone());
|
||
let mut roles = Vec::new();
|
||
if let Some(role) = r.role.clone() {
|
||
roles.push(role);
|
||
}
|
||
by_mbid.insert(
|
||
r.mbid.clone(),
|
||
Pending {
|
||
name: r.name,
|
||
roles,
|
||
begin,
|
||
end,
|
||
},
|
||
);
|
||
}
|
||
}
|
||
}
|
||
order
|
||
.into_iter()
|
||
.filter_map(|mbid| by_mbid.remove(&mbid).map(|p| (mbid, p)))
|
||
.map(|(mbid, p)| {
|
||
let period = format_period(p.begin.as_deref(), p.end.as_deref());
|
||
let tooltip = if !p.roles.is_empty() {
|
||
let roles_joined = p.roles.join(", ");
|
||
if period.is_empty() {
|
||
roles_joined
|
||
} else {
|
||
format!("{roles_joined} ({period})")
|
||
}
|
||
} else if !period.is_empty() {
|
||
period.clone()
|
||
} else {
|
||
p.name.clone()
|
||
};
|
||
let role = p
|
||
.roles
|
||
.first()
|
||
.cloned()
|
||
.unwrap_or_else(|| default_role.to_string());
|
||
MbRelationshipJson {
|
||
mbid,
|
||
name: p.name,
|
||
role,
|
||
tooltip,
|
||
}
|
||
})
|
||
.collect()
|
||
}
|
||
|
||
fn format_period(begin: Option<&str>, end: Option<&str>) -> String {
|
||
if begin.is_some() || end.is_some() {
|
||
format!("{} - {}", begin.unwrap_or("?"), end.unwrap_or("present"))
|
||
} else {
|
||
String::new()
|
||
}
|
||
}
|
||
|
||
// ----- MusicBrainz: Discovery ("You may also like") -----------------------
|
||
|
||
/// Tag-seeded discovery candidates, validated against Qobuz by the core.
|
||
/// Returns `(primary_tag, rows)`.
|
||
///
|
||
/// The `dismissed_per_tag` / `known_artists` callbacks are EMPTY here: the
|
||
/// Slint app feeds them from its `discovery_dismiss`, `play_history` and
|
||
/// `reco` stores, none of which this POC opens. Empty = no exclusion, which
|
||
/// is exactly what a first-run Slint profile does.
|
||
async fn load_mb_discovery(
|
||
runtime: &Arc<AppRuntime<LoggingAdapter>>,
|
||
seed_mbid: &str,
|
||
seed_name: &str,
|
||
similar_names: Vec<String>,
|
||
) -> Result<(String, Vec<MbDiscoveryJson>), String> {
|
||
let dismissed = |_tag: &str| HashSet::<String>::new();
|
||
let known = || (HashSet::<u64>::new(), HashSet::<String>::new());
|
||
let response = runtime
|
||
.core()
|
||
.musicbrainz_discover_artists(seed_mbid, seed_name, &similar_names, &dismissed, &known)
|
||
.await
|
||
.map_err(|e| e.to_string())?;
|
||
let rows = response
|
||
.artists
|
||
.into_iter()
|
||
.map(|a| MbDiscoveryJson {
|
||
mbid: a.mbid,
|
||
name: a.name,
|
||
qobuz_id: a.qobuz_id.map(|id| id.to_string()).unwrap_or_default(),
|
||
})
|
||
.collect();
|
||
Ok((response.primary_tag, rows))
|
||
}
|