feat(reco): per-user RecoStore wrapper + Qobuz play-event logging
Wire the ported, unit-tested RecoStore engine into the session lifecycle and log plays at the recents chokepoint. - New crates/qbz/src/reco.rs: per-user store behind a process-global Mutex<Option<RecoStore>> (mirrors fav_cache); helpers no-op when no session is open. init_for_user/teardown, home_seeds reader, is_qobuz_source gate, log_play_gated. - Lifecycle: init on login/restore/offline-entry next to fav_cache; teardown on logout. - playback::record_recent logs each play via spawn_blocking (blocking SQLite off the async path), gated Qobuz-catalog only (skip local/plex/ephemeral so unresolvable ids never poison the home seeds; qobuz_download counts). cc
This commit is contained in:
4 files changed
+142
No files matched your search
@@ -110,6 +110,8 @@ where
|
||||
if let Some(dir) = crate::offline_mode::user_data_dir(user_id) {
|
||||
crate::offline_mode::init_for_user(&dir);
|
||||
crate::fav_cache::init_for_user(&dir);
|
||||
// Recommendation event store (shared events.db with Tauri).
|
||||
crate::reco::init_for_user(&dir);
|
||||
crate::discover_prefs::init_for_user(&dir);
|
||||
crate::artist_blacklist::init_for_user(&dir);
|
||||
// Intelligent Search (cache + ranking), seeded from the persisted pref.
|
||||
@@ -226,6 +228,8 @@ where
|
||||
if let Some(dir) = crate::offline_mode::user_data_dir(user_id) {
|
||||
crate::offline_mode::init_for_user(&dir);
|
||||
crate::fav_cache::init_for_user(&dir);
|
||||
// Recommendation event store (shared events.db with Tauri).
|
||||
crate::reco::init_for_user(&dir);
|
||||
crate::discover_prefs::init_for_user(&dir);
|
||||
crate::artist_blacklist::init_for_user(&dir);
|
||||
// Intelligent Search (cache + ranking), seeded from the pref.
|
||||
@@ -279,6 +283,7 @@ where
|
||||
crate::offline::deactivate().await;
|
||||
crate::offline_mode::teardown();
|
||||
crate::fav_cache::teardown();
|
||||
crate::reco::teardown();
|
||||
crate::discover_prefs::teardown();
|
||||
crate::artist_blacklist::teardown();
|
||||
crate::search_service::teardown();
|
||||
|
||||
@@ -100,6 +100,7 @@ mod plex_auth;
|
||||
mod plex_settings;
|
||||
mod playlist_picker;
|
||||
mod quality;
|
||||
mod reco;
|
||||
mod recently;
|
||||
mod scrobble;
|
||||
mod scrobbler_settings;
|
||||
@@ -516,6 +517,8 @@ async fn enter_shell_offline(
|
||||
if let Some(dir) = crate::offline_mode::user_data_dir(user_id) {
|
||||
crate::offline_mode::init_for_user(&dir);
|
||||
crate::fav_cache::init_for_user(&dir);
|
||||
// Recommendation event store (shared events.db with Tauri).
|
||||
crate::reco::init_for_user(&dir);
|
||||
crate::discover_prefs::init_for_user(&dir);
|
||||
// D-FIX-a: bind the blacklist offline too — Tauri never initialized it
|
||||
// in offline mode, so blacklisted artists leaked into offline surfaces.
|
||||
|
||||
@@ -1760,6 +1760,19 @@ async fn record_recent(runtime: &Runtime) {
|
||||
if let Some(artist_id) = track.artist_id {
|
||||
crate::play_history::record_play(artist_id, &track.artist);
|
||||
}
|
||||
// reco: log this play for taste scoring. The helper gates to Qobuz-catalog
|
||||
// sources only (local/plex/ephemeral ids don't resolve against the Qobuz
|
||||
// catalog and would poison the home seeds). SQLite is blocking, so it runs
|
||||
// on the blocking pool, off the async record_recent path.
|
||||
let (rid, ralb, rart, rsrc) = (
|
||||
track.id,
|
||||
track.album_id.clone(),
|
||||
track.artist_id,
|
||||
track.source.clone(),
|
||||
);
|
||||
tokio::task::spawn_blocking(move || {
|
||||
crate::reco::log_play_gated(rid, ralb, rart, rsrc.as_deref());
|
||||
});
|
||||
}
|
||||
|
||||
/// THE single queue-drop predicate for an already-built `QueueTrack` (Task 7).
|
||||
|
||||
@@ -0,0 +1,121 @@
|
||||
//! Per-user `RecoStore` runtime wrapper (mirrors `fav_cache.rs`).
|
||||
//!
|
||||
//! Holds the ported, headless `qbz_app` [`RecoStore`] behind a process-global
|
||||
//! `Mutex<Option<…>>`; all access goes through typed helpers so the source
|
||||
//! gating and the `spawn_blocking` discipline live in ONE place. Every helper
|
||||
//! degrades to a no-op when no session is active (the store is `None`), so
|
||||
//! callers never branch on "is reco enabled" — reco simply contributes
|
||||
//! nothing until a session opens it.
|
||||
//!
|
||||
//! Lifecycle mirrors `fav_cache`: [`init_for_user`] on every session
|
||||
//! activation (login, restore, offline entry), [`teardown`] on logout. The DB
|
||||
//! file is shared with Tauri (`<base_dir>/reco/events.db`), so a user's
|
||||
//! existing recommendation history carries across frontends.
|
||||
|
||||
use std::path::Path;
|
||||
use std::sync::Mutex;
|
||||
|
||||
use qbz_app::settings::reco_store::{HomeSeedLimits, HomeSeeds, RecoStore};
|
||||
|
||||
/// Per-user reco event store. `None` until a session (online or offline) is
|
||||
/// activated; every helper is a no-op in that window.
|
||||
static RECO: Mutex<Option<RecoStore>> = Mutex::new(None);
|
||||
|
||||
/// Open the per-user reco store (`<base_dir>/reco/events.db`). Best-effort: a
|
||||
/// failure logs and leaves reco disabled (every helper then degrades to
|
||||
/// no-op). Called on every session activation — login, restore, offline
|
||||
/// entry — next to `fav_cache::init_for_user`.
|
||||
pub fn init_for_user(base_dir: &Path) {
|
||||
match RecoStore::new_at(base_dir) {
|
||||
Ok(store) => {
|
||||
log::info!("[reco] event store opened for session");
|
||||
if let Ok(mut guard) = RECO.lock() {
|
||||
*guard = Some(store);
|
||||
}
|
||||
}
|
||||
Err(e) => log::warn!("[reco] init failed, reco disabled: {e}"),
|
||||
}
|
||||
}
|
||||
|
||||
/// Drop the per-user store on logout (mirrors `fav_cache::teardown`).
|
||||
pub fn teardown() {
|
||||
if let Ok(mut guard) = RECO.lock() {
|
||||
*guard = None;
|
||||
}
|
||||
}
|
||||
|
||||
/// Read the home/Discover ID seeds. `None` when reco is disabled (no session)
|
||||
/// or the read fails — callers fall back to their existing local source so a
|
||||
/// cold reco store never empties a surface.
|
||||
#[allow(dead_code)] // wired by W6/W9 (mix seeds + For You / Discover Home re-source)
|
||||
pub fn home_seeds(limits: HomeSeedLimits) -> Option<HomeSeeds> {
|
||||
let guard = RECO.lock().ok()?;
|
||||
let store = guard.as_ref()?;
|
||||
store.get_home_seeds(limits).ok()
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Play events (W2)
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
/// CRITICAL source gate: only Qobuz-catalog plays may enter reco. `None`
|
||||
/// defaults to `"qobuz"` (the queue's own normalization in
|
||||
/// `playback::record_recent`); only `local` / `plex` / `ephemeral` carry
|
||||
/// non-catalog ids that don't resolve against Qobuz and would poison the home
|
||||
/// seeds. A `qobuz_download` (a purchased Qobuz track) keeps a resolvable
|
||||
/// Qobuz id, so it counts. Same exclusion the mix seeder uses (`mix.rs`).
|
||||
pub fn is_qobuz_source(source: Option<&str>) -> bool {
|
||||
!matches!(source.unwrap_or("qobuz"), "local" | "plex" | "ephemeral")
|
||||
}
|
||||
|
||||
/// Log a Qobuz play event. Blocking SQLite — call from `spawn_blocking`.
|
||||
/// Returns whether it was logged (`false` = gated out as non-Qobuz, or reco
|
||||
/// disabled). `genre_id` is `None`: a `QueueTrack` carries no genre, exactly
|
||||
/// as in Tauri (genre is supplied later via the album-meta write-back).
|
||||
pub fn log_play_gated(
|
||||
track_id: u64,
|
||||
album_id: Option<String>,
|
||||
artist_id: Option<u64>,
|
||||
source: Option<&str>,
|
||||
) -> bool {
|
||||
if !is_qobuz_source(source) {
|
||||
return false;
|
||||
}
|
||||
if let Ok(guard) = RECO.lock() {
|
||||
if let Some(store) = guard.as_ref() {
|
||||
if let Err(e) = store.log_play_event(track_id, album_id, artist_id, None) {
|
||||
log::warn!("[reco] log_play failed: {e}");
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
false
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Tests
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn helpers_are_noop_when_uninitialized() {
|
||||
teardown(); // ensure no store is open
|
||||
// Logging with no open store must not panic and must report "not logged".
|
||||
assert!(!log_play_gated(123, None, None, Some("qobuz")));
|
||||
// Reading seeds with no open store yields None (caller falls back to local).
|
||||
assert!(home_seeds(HomeSeedLimits::default()).is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn qobuz_source_gate_excludes_local_plex_ephemeral() {
|
||||
assert!(is_qobuz_source(None)); // queue default = "qobuz"
|
||||
assert!(is_qobuz_source(Some("qobuz")));
|
||||
assert!(is_qobuz_source(Some("qobuz_download"))); // purchased Qobuz track, resolvable id
|
||||
assert!(!is_qobuz_source(Some("local")));
|
||||
assert!(!is_qobuz_source(Some("plex")));
|
||||
assert!(!is_qobuz_source(Some("ephemeral")));
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user