From e0a9f60336938c8d222bca6e78ccdae5040e08d3 Mon Sep 17 00:00:00 2001 From: vicrodh <103399283+vicrodh@users.noreply.github.com> Date: Fri, 28 Aug 2026 19:54:47 -0600 Subject: [PATCH] fix(scrobble): stamp started_at and time the threshold by audible position The delayed scrobble used unix_now() when it fired (up to 240 s late on a long track) and slept on wall-clock, so a track paused two minutes in still scrobbled. Capture started_at on the track edge and wait on the player's own position while it is playing: pause holds the scrobble, a stop drops it, a newer track edge still self-cancels via the generation. Same rule qbzd's scrobble_engine already applies. cc --- crates/qbz-qt/src/integrations_qt.rs | 54 ++++++++++++++++++++++------ 1 file changed, 44 insertions(+), 10 deletions(-) diff --git a/crates/qbz-qt/src/integrations_qt.rs b/crates/qbz-qt/src/integrations_qt.rs index d4eba9cb4..0eca55d46 100644 --- a/crates/qbz-qt/src/integrations_qt.rs +++ b/crates/qbz-qt/src/integrations_qt.rs @@ -769,9 +769,18 @@ pub fn meta_from_queue_track(track: &QueueTrack) -> ScrobbleMeta { /// Monotonic generation, bumped on every track change so a delayed scrobble /// timer that fires after the user skipped is dropped (the Svelte -/// `clearTimeout` equivalent). Like Tauri, pause/stop do NOT cancel it. +/// `clearTimeout` equivalent). +/// +/// The wait itself is NOT wall-clock any more: it is timed by the player's +/// own position while it is playing, so a pause holds the scrobble and a +/// stop drops it (`qbzd/src/scrobble_engine.rs` — `Playing{started_at,..}` +/// + `PositionUpdated` — is the reference; the old `sleep(wait)` scrobbled a +/// track the user had paused two minutes in). static SCROBBLE_GEN: AtomicU64 = AtomicU64::new(0); +/// How often the delayed-scrobble task samples the player while waiting. +const SCROBBLE_TICK: Duration = Duration::from_secs(1); + /// Track-change entry point. Fires now-playing immediately for each enabled + /// authed service, then arms a delayed scrobble at `min(50% of duration, /// 240s)`. No-op when no service is active (opt-in gate). @@ -785,6 +794,10 @@ pub fn on_track_changed(meta: ScrobbleMeta) { if !cfg.lastfm_active() && !cfg.listenbrainz_active() { return; } + // Last.fm wants the time the track STARTED, captured here on the edge — + // never the time the threshold fired (a 240 s wait on a long track put + // every scrobble four minutes late). + let started_at = unix_now(); crate::spawn(async move { // Now-playing is never queued. It may use the independent network in // manual-offline immediate mode or an opted-in logged-out session. @@ -801,14 +814,32 @@ pub fn on_track_changed(meta: ScrobbleMeta) { ); return; }; - if wait > 0 { - tokio::time::sleep(Duration::from_secs(wait)).await; + // Wait for the PLAYER to reach the threshold, not the clock: sample + // its position once a second and only count it while playing. A + // pause simply keeps waiting; a stop (no track loaded) ends the wait + // without a scrobble; a newer track edge self-cancels via the + // generation, as before. Seeks move the position and therefore the + // moment this fires — the same rule the daemon applies. + let rt = crate::app(); + let mut ticker = tokio::time::interval(SCROBBLE_TICK); + loop { + ticker.tick().await; + if SCROBBLE_GEN.load(Ordering::SeqCst) != my_gen { + return; + } + let ev = rt.core().player().get_playback_event(); + if ev.track_id == 0 { + log::debug!( + "[qbz-qt] scrobble: stopped before the threshold, dropping '{}'", + meta.track + ); + return; + } + if ev.is_playing && ev.position >= wait { + break; + } } - // Self-cancel if a newer track change superseded us. - if SCROBBLE_GEN.load(Ordering::SeqCst) != my_gen { - return; - } - send_scrobble(&meta).await; + send_scrobble(&meta, started_at).await; }); } @@ -909,10 +940,13 @@ async fn send_now_playing(meta: &ScrobbleMeta, cfg: &ScrobblerSettings) { /// Fire, retain or drop the actual scrobble according to the restored offline /// policy plus the logged-out opt-out. Settings are re-read in case the user /// changed a policy or disconnected while the timer waited. -async fn send_scrobble(meta: &ScrobbleMeta) { +/// +/// `started_at` is the unix time the track STARTED (captured on the track +/// edge), which is what both services define as the listen's timestamp. +async fn send_scrobble(meta: &ScrobbleMeta, started_at: i64) { let cfg = scrobble_settings(); let album = meta.album.as_deref(); - let timestamp = unix_now(); + let timestamp = started_at; let policy = current_scrobble_policy(&cfg).await; if cfg.lastfm_active() {