Carry the newest progress generation in outgoing playback states

A browser queue item keeps the handoff hint it arrived with, which is
older than the checkpoints this account has written since. Every
podcast episode in a playback state the web sends to a trusted device
now carries max(hint, register generation), so the device taking over
writes its explicit action above everything the web has seen. Which
state reads the resume position from the register is decided on the
converted queue, so the bridge itself stays testable without a
database.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
sanapachandClaude Opus 5.5 committed 2026-11-20 15:12:38 +02:00
1 parent 1778942714
commit 2c551c5b71
4 files changed
+182 -24

No files matched your search

+52 -8
View File
@@ -31,7 +31,7 @@ use music_dht::device_sync::{
random_hex, relayed_protocol_version, supports_listen_history, supports_podcasts,
ticket_endpoint_id,
};
use music_dht::podcast::ProgressRecord;
use music_dht::podcast::{EpisodeId, ProgressRecord};
use music_dht::{ByteStream, MusicDhtService, NetworkId, PeerTicket, SecretKey, StreamAcceptor};
use serde::Serialize;
use sqlx::Row;
@@ -3730,20 +3730,64 @@ async fn playback_state_from_browser_json(
) -> Result<PlaybackStateWire> {
let unknown = browser_bool(&state, queue_items::POSITION_UNKNOWN_KEY) == Some(true);
let mut wire = browser_json_to_wire(pool, state).await?;
if unknown
&& let Some(episode) = wire
.queue
.get(wire.queue_pos)
.and_then(PlaybackTrack::podcast_episode_ref)
{
if let Some(episode_id) = episode_needing_resume_position(unknown, &wire) {
let record = PodcastStore::new(pool.clone())
.progress(user_id, &episode.episode_id)
.progress(user_id, &episode_id)
.await?;
apply_resume_position(&mut wire, record.as_ref());
}
let episodes: Vec<EpisodeId> = wire
.queue
.iter()
.filter_map(PlaybackTrack::podcast_episode_ref)
.map(|episode| episode.episode_id.clone())
.collect();
if !episodes.is_empty() {
let generations = PodcastStore::new(pool.clone())
.progress_generations(user_id, &episodes)
.await?;
apply_progress_generations(&mut wire, &generations);
}
Ok(wire)
}
/// Raises `PodcastEpisodeRef::progress_generation` of every episode in the
/// queue to the generation of its progress register here.
///
/// The browser keeps the hint its queue item came with, which can be older
/// than what this account has seen since (its own checkpoints, operations
/// from other devices). The device taking over writes its explicit action
/// above this value (frid `PODCASTS.md`, "Handoff").
fn apply_progress_generations(
state: &mut PlaybackStateWire,
generations: &std::collections::HashMap<EpisodeId, u64>,
) {
for track in &mut state.queue {
if let Some(PlaybackSource::PodcastEpisode(episode)) = track.source.as_mut()
&& let Some(generation) = generations.get(&episode.episode_id)
{
episode.progress_generation = Some(
episode
.progress_generation
.map_or(*generation, |seen| seen.max(*generation)),
);
}
}
}
/// The current episode whose position comes from its progress register:
/// only when the browser said it does not know the position. Known
/// positions (handoffs, seeks, playing) and music are never replaced.
fn episode_needing_resume_position(unknown: bool, wire: &PlaybackStateWire) -> Option<EpisodeId> {
if !unknown {
return None;
}
wire.queue
.get(wire.queue_pos)
.and_then(PlaybackTrack::podcast_episode_ref)
.map(|episode| episode.episode_id.clone())
}
/// Sets the position of the current podcast episode to where ordinary Play
/// resumes it (`ProgressRecord::resume_position_ms`, 0 without a register),
/// and records the register's generation as seen for the handoff.