feat(reco): log favorite-ADD events (track, album, artist)
Log a reco favorite event at each favorite-add seam, ADD-only and on network success so un-favorites, rollbacks and bulk syncs never enter the signal: - track-row heart (main.rs) + now-playing heart (queue.rs) -> track favorite - album grid-add arm + album-header toggle -> album favorite - artist follow -> artist favorite (Qobuz id parsed to u64) Each logs via spawn_blocking off the async path. The album-toggle and artist seams log from the async block, not the upgrade_in_event_loop UI-thread closure (where spawn_blocking would panic). cc
This commit is contained in:
3 files changed
+91
-1
No files matched your search
@@ -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::<u64>() {
|
||||
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<dyn std::error::Error>> {
|
||||
// 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<dyn std::error::Error>> {
|
||||
"[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::<AlbumState>();
|
||||
let open_now = st.get_id() == album_id.as_str();
|
||||
@@ -8566,6 +8588,13 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
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::<u64>() {
|
||||
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);
|
||||
});
|
||||
|
||||
@@ -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}");
|
||||
|
||||
+55
-1
@@ -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<String>, artist_id: Option<u64>) {
|
||||
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<u64>) {
|
||||
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<u64>,
|
||||
album_id: Option<String>,
|
||||
artist_id: Option<u64>,
|
||||
) {
|
||||
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)
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
Reference in new issue
Block a user