diff --git a/crates/qbz/src/main.rs b/crates/qbz/src/main.rs index e353d19cf..a1c4ed215 100644 --- a/crates/qbz/src/main.rs +++ b/crates/qbz/src/main.rs @@ -1177,6 +1177,15 @@ fn toggle_track_favorite( } else { runtime.core().remove_favorite("track", &id).await }; + // reco: log a favorite ADD on success (skip removes/failures) for taste + // scoring; blocking SQLite off the async path. + if make_fav && res.is_ok() { + if let Ok(tid) = id.parse::() { + tokio::task::spawn_blocking(move || { + crate::reco::log_favorite_track(tid, None, None) + }); + } + } if let Err(e) = res { log::error!("[qbz-slint] toggle track favorite failed: {e}"); // Roll the optimistic change back on failure. @@ -7961,6 +7970,11 @@ fn main() -> Result<(), Box> { // album-header heart reflects a grid favorite. crate::fav_cache::set_album(&album_id, true); crate::toast::success_weak(&weak, "Added to favorites"); + // reco: log the album favorite (add-only arm). + let aid = album_id.clone(); + tokio::task::spawn_blocking(move || { + crate::reco::log_favorite_album(aid, None) + }); } Err(e) => { log::error!("[qbz-slint] favorite album failed: {e}"); @@ -8000,6 +8014,14 @@ fn main() -> Result<(), Box> { "[qbz-slint] toggle favorite album {album_id} failed: {e}" ); } + // reco: log the album favorite ADD on success (skip the + // un-favorite). Blocking SQLite off the async path. + if ok && new_state { + let aid = album_id.clone(); + tokio::task::spawn_blocking(move || { + crate::reco::log_favorite_album(aid, None) + }); + } let _ = weak.upgrade_in_event_loop(move |w| { let st = w.global::(); let open_now = st.get_id() == album_id.as_str(); @@ -8566,6 +8588,13 @@ fn main() -> Result<(), Box> { handle.spawn(async move { match runtime.core().add_favorite("artist", &artist_id).await { Ok(()) => { + // reco: log the artist favorite (add-only follow + // arm). Qobuz artist id -> u64. + if let Ok(aid) = artist_id.parse::() { + tokio::task::spawn_blocking(move || { + crate::reco::log_favorite_artist(aid) + }); + } let _ = weak.upgrade_in_event_loop(move |w| { search::mark_artist_followed(&w, &artist_id, true); }); diff --git a/crates/qbz/src/queue.rs b/crates/qbz/src/queue.rs index 2979f6594..5a9b7a95e 100644 --- a/crates/qbz/src/queue.rs +++ b/crates/qbz/src/queue.rs @@ -750,6 +750,13 @@ impl QueueController { // Keep the shared cache (memory + disk) in sync so every // other heart surface reflects the change immediately. crate::fav_cache::set(track.id, make_favorite); + // reco: log a favorite ADD (skip un-favorite) for scoring. + if make_favorite { + let tid = track.id; + tokio::task::spawn_blocking(move || { + crate::reco::log_favorite_track(tid, None, None) + }); + } } Err(e) => { log::error!("[qbz-slint] queue: toggle favorite failed: {e}"); diff --git a/crates/qbz/src/reco.rs b/crates/qbz/src/reco.rs index 5afbb5aa5..18244e845 100644 --- a/crates/qbz/src/reco.rs +++ b/crates/qbz/src/reco.rs @@ -15,7 +15,9 @@ use std::path::Path; use std::sync::Mutex; -use qbz_app::settings::reco_store::{HomeSeedLimits, HomeSeeds, RecoStore, TrainParams}; +use qbz_app::settings::reco_store::{ + HomeSeedLimits, HomeSeeds, RecoEventInput, RecoEventType, RecoItemType, RecoStore, TrainParams, +}; /// Per-user reco event store. `None` until a session (online or offline) is /// activated; every helper is a no-op in that window. @@ -92,6 +94,58 @@ pub fn log_play_gated( false } +// --------------------------------------------------------------------------- +// Favorite events (W3) — log ONLY a successful ADD (make==true && network ok); +// the caller applies that gate. Logging an un-favorite or a failed add would +// corrupt the taste signal. Favorited items are inherently Qobuz catalog (the +// add_favorite API only succeeds for Qobuz ids), so no extra source gate. +// --------------------------------------------------------------------------- + +/// Log a favorite of a Qobuz track. +pub fn log_favorite_track(track_id: u64, album_id: Option, artist_id: Option) { + if let Ok(guard) = RECO.lock() { + if let Some(store) = guard.as_ref() { + if let Err(e) = store.log_favorite_event(track_id, album_id, artist_id, None) { + log::warn!("[reco] log_favorite_track failed: {e}"); + } + } + } +} + +/// Log a favorite of a Qobuz album. +pub fn log_favorite_album(album_id: String, artist_id: Option) { + insert_favorite(RecoItemType::Album, None, Some(album_id), artist_id); +} + +/// Log a favorite of a Qobuz artist. +pub fn log_favorite_artist(artist_id: u64) { + insert_favorite(RecoItemType::Artist, None, None, Some(artist_id)); +} + +fn insert_favorite( + item_type: RecoItemType, + track_id: Option, + album_id: Option, + artist_id: Option, +) { + if let Ok(guard) = RECO.lock() { + if let Some(store) = guard.as_ref() { + let ev = RecoEventInput { + event_type: RecoEventType::Favorite, + item_type, + track_id, + album_id, + artist_id, + playlist_id: None, + genre_id: None, + }; + if let Err(e) = store.insert_event(&ev) { + log::warn!("[reco] log_favorite failed: {e}"); + } + } + } +} + // --------------------------------------------------------------------------- // Training (W5) // ---------------------------------------------------------------------------