Files
qbz/crates/qbz-slint/src/discovery_dismiss.rs
T
vicrodh 165674d8a8 feat(slint): you-may-also-like discovery section with sticky dismiss
Closes the network sidebar with the fifth section — tag-based "You may
also like" discovery, with a per-tag dismiss store so a thumbs-down
sticks across artist pages that share the same primary genre tag.

Backend (`qbz-integrations` + `qbz-core`):
  - DiscoveryArtist and DiscoveryResponse DTOs added to
    qbz-integrations::musicbrainz::models.
  - QbzCore::musicbrainz_discover_artists ports
    v2_get_discovery_artists end to end (sans the RecoState local
    "play count > 2" filter, which has no equivalent in the Slint
    shell yet): primary-tag search, exclude seed + similar +
    dismissed, deterministic shuffle keyed on seed_mbid, validate
    each candidate on Qobuz, fall back to the secondary tag if we
    have fewer than 5 matches.
  - normalize_artist_name re-exported at the qbz-core crate root so
    the dismiss handler in qbz-slint can use the same key shape that
    the discovery pipeline filters on.
  - Deterministic shuffle implemented with a simple xorshift PRNG
    seeded from a stable hash of seed_mbid — keeps qbz-core free of
    the rand dep.

Slint controller (`crates/qbz-slint/src/`):
  - discovery_dismiss.rs — small JSON store at
    `<data-dir>/qbz/discovery_dismiss.json` keyed by lowercased tag
    -> list of normalized artist names. dismissed_for_tag is passed
    into the discovery call as the filter source; dismiss() is
    invoked by the thumbs-down handler.
  - artist::load_mb_discovery / apply_mb_discovery /
    remove_discovery_artist plumb the pipeline result through the
    standard load-off-thread / apply-on-UI split. Discovery state
    includes primary_tag so the dismiss handler can persist under
    the right key.
  - main.rs chains discovery after relationships in the same MB
    enrichment task. NetworkSidebarActions.discovery-dismissed (now
    carrying mbid + name) normalizes the name, persists the
    dismiss, and removes the row from the visible list.

Slint UI (`ui/state.slint`, `ui/artist/ArtistPageView.slint`):
  - discovery-tag property added to NetworkSidebarState.
  - YOU MAY ALSO LIKE section after Relationships — one row per
    candidate (SidebarLink + a 24x24 thumbs-down button on the
    right). Clicking the link navigates via the artist-clicked
    callback when a Qobuz id is present.
  - thumbs-down.svg icon added to ui/assets/icons (Lucide
    ThumbsDown).

cc
2026-05-20 10:39:53 -06:00

89 lines
2.7 KiB
Rust

//! Per-tag discovery dismiss store.
//!
//! The "You may also like" sidebar section lets the user thumbs-down a
//! candidate. The rejection is sticky for that genre tag, so the same
//! tag never re-suggests the artist on future runs. A single JSON file
//! at the shared QBZ data path holds the dismissals — small enough for
//! a full read on each discovery call, no SQLite needed at this layer.
//!
//! Names are stored normalized (trim + lowercase + collapse whitespace)
//! so casing/whitespace differences from MB and Qobuz collide on the
//! same key.
use std::collections::{HashMap, HashSet};
use std::path::PathBuf;
use serde::{Deserialize, Serialize};
/// Map of normalized-lowercased tag -> list of normalized artist names
/// the user has dismissed for that tag.
#[derive(Default, Serialize, Deserialize)]
struct DismissStore {
#[serde(flatten)]
by_tag: HashMap<String, Vec<String>>,
}
fn store_path() -> Option<PathBuf> {
Some(
dirs::data_dir()?
.join("qbz")
.join("discovery_dismiss.json"),
)
}
fn load_store() -> DismissStore {
let Some(path) = store_path() else {
return DismissStore::default();
};
match std::fs::read(&path) {
Ok(bytes) => serde_json::from_slice(&bytes).unwrap_or_default(),
Err(_) => DismissStore::default(),
}
}
fn write_store(store: &DismissStore) {
let Some(path) = store_path() else {
return;
};
if let Some(parent) = path.parent() {
if let Err(e) = std::fs::create_dir_all(parent) {
log::warn!("[qbz-slint] discovery-dismiss dir failed: {e}");
return;
}
}
match serde_json::to_vec_pretty(store) {
Ok(json) => {
if let Err(e) = std::fs::write(&path, json) {
log::warn!("[qbz-slint] discovery-dismiss write failed: {e}");
}
}
Err(e) => log::warn!("[qbz-slint] discovery-dismiss serialize failed: {e}"),
}
}
/// Return the set of dismissed normalized names for `tag` (which is
/// expected lowercase). Used as a filter input by the discovery
/// pipeline.
pub fn dismissed_for_tag(tag: &str) -> HashSet<String> {
let store = load_store();
store
.by_tag
.get(tag)
.cloned()
.unwrap_or_default()
.into_iter()
.collect()
}
/// Record a dismissal. The caller is expected to pass the tag already
/// lowercased and the name already normalized via
/// `qbz_core::normalize_artist_name`.
pub fn dismiss(tag: &str, normalized_name: &str) {
let mut store = load_store();
let entry = store.by_tag.entry(tag.to_string()).or_default();
if !entry.iter().any(|n| n == normalized_name) {
entry.push(normalized_name.to_string());
}
write_store(&store);
}