From 4d0b352258685848050940f4d95e4bdf53f055d2 Mon Sep 17 00:00:00 2001 From: Sanapach Date: Thu, 19 Nov 2026 18:35:49 +0200 Subject: [PATCH] Keep the episode position and skip unplayable items safely in the player Co-Authored-By: Claude Opus 5.5 --- src/federation/devices/gating_tests.rs | 4 + templates/player/scripts.html | 59 ++++++++++++--- tests/player_queue_sources.test.cjs | 100 +++++++++++++++++++++++++ 3 files changed, 152 insertions(+), 11 deletions(-) diff --git a/src/federation/devices/gating_tests.rs b/src/federation/devices/gating_tests.rs index bc24b3a..cbe8402 100644 --- a/src/federation/devices/gating_tests.rs +++ b/src/federation/devices/gating_tests.rs @@ -356,12 +356,14 @@ async fn podcast_queue_survives_server_browser_server_round_trip() { let mut incoming = state(queue.clone()); incoming.queue_pos = 3; incoming.paused = true; + incoming.position_secs = 1200.0; // peer -> server -> browser let payload = web_playback_payload(&pool, &incoming).await.unwrap(); let tracks = payload["tracks"].as_array().unwrap(); assert_eq!(tracks.len(), 4, "no item is dropped"); assert_eq!(payload["index"], 3); + assert_eq!(payload["position_seconds"], 1200.0); assert_eq!(tracks[0]["source"]["kind"], "podcast_episode"); assert_eq!(tracks[0]["unavailable"], true); assert_eq!(tracks[1]["source"]["kind"], "private"); @@ -385,6 +387,8 @@ async fn podcast_queue_survives_server_browser_server_round_trip() { assert_eq!(outgoing.queue[..4], queue[..]); assert_eq!(outgoing.queue_pos, 3); assert!(outgoing.paused); + // The episode position goes back to the peer (handoff web -> TUI). + assert_eq!(outgoing.position_secs, 1200.0); let library = &outgoing.queue[4]; assert_eq!(library.id, 77); assert_eq!(library.content_id.as_deref(), Some(CONTENT_ID)); diff --git a/templates/player/scripts.html b/templates/player/scripts.html index ad54e8d..ed68b5d 100644 --- a/templates/player/scripts.html +++ b/templates/player/scripts.html @@ -774,6 +774,9 @@ document.addEventListener('alpine:init', () => { }); audio.addEventListener('timeupdate', () => { + // Unloading the audio for an unplayable item fires a late + // timeupdate at 0; keep that item's handed-off position. + if (!this._currentItemHasLocalAudio()) return; this.currentTime = audio.currentTime; this.duration = audio.duration || 0; this.progress = this.duration > 0 ? (this.currentTime / this.duration) * 100 : 0; @@ -887,7 +890,8 @@ document.addEventListener('alpine:init', () => { // Podcast episodes stay in the queue; this player skips them. const playable = this._playableQueueIndex(queue.tracks, idx, idx >= previousIndex ? 1 : -1); if (playable < 0) { - this._showUnplayableQueueItem(track); + // Staying on the current item keeps its position. + this._showUnplayableQueueItem(track, track === this.currentTrack ? this.currentTime : 0); return; } queue.currentIndex = playable; @@ -916,7 +920,7 @@ document.addEventListener('alpine:init', () => { _playLocal(track, options = {}) { if (this._isUnplayableQueueItem(track)) { this._recordHistory('replaced'); - this._showUnplayableQueueItem(track); + this._showUnplayableQueueItem(track, options.position_seconds); return; } this._recordHistory('replaced'); @@ -977,12 +981,25 @@ document.addEventListener('alpine:init', () => { return; } if (this._isUnplayableQueueItem(this.currentTrack)) { - this.next(); + // Not next(): with repeat "one" it would resume this item again. + this._skipUnplayableCurrentItem(); return; } audio.play().catch(() => {}); }, + _skipUnplayableCurrentItem() { + const queue = Alpine.store('queue'); + const tracks = queue?.tracks || []; + let idx = this._playableQueueIndex(tracks, Number(queue?.currentIndex ?? -1) + 1, 1); + if (idx < 0 && this.repeatMode === 'all') idx = this._playableQueueIndex(tracks, 0, 1); + if (idx < 0) { + this._showUnplayableQueueItem(this.currentTrack, this.currentTime); + return; + } + this.playQueueIndex(idx); + }, + toggle() { if (!this.currentTrack) return; if (this.isPlaying) { this.pause(); } @@ -1060,7 +1077,8 @@ document.addEventListener('alpine:init', () => { this._recordHistory('skipped'); let nextIdx; - if (this.repeatMode === 'one') { + // Repeat "one" cannot repeat an item this player cannot play. + if (this.repeatMode === 'one' && !this._isUnplayableQueueItem(this.currentTrack)) { this.seek(0); this._historyRecorded = false; this._resetPlaybackTracking(); @@ -1234,8 +1252,12 @@ document.addEventListener('alpine:init', () => { const queue = Alpine.store('queue'); const track = this.currentTrack || queue?.tracks?.[queue.currentIndex] || null; if (!track && (!queue || queue.tracks.length === 0)) return null; + //