Relay device-sync v4 and keep podcast queue items
Use the shared music_dht::device_sync types instead of private copies (byte-identical for v3 content, checked against the 0.12.x types and a golden fixture) and announce protocol 4, because every v4 element is kept: - podcast operations stay in the operation log and are relayed only to peers that reported v4; their registers and the podcast snapshot sections are merged with the shared rules into an opaque relay table and returned in this server's snapshots (m_0055); - the browser bridge carries PlaybackTrack.source: items that are not library tracks keep the verbatim wire item, so podcast episodes and Jam placeholders survive the round trip; the player labels and skips them instead of dropping them; - the typed queue is persisted in queue_items_json next to queue_json, which is still written for rollback (m_0054). Peer versions come from self-reports (profile_protocol_version); relayed profiles only raise them and DeviceProfileSet no longer stores this server's constant. Invites carry the inviter's version and a DeviceProfileSet announces v4 once. Handoffs of a podcast queue to a device below v4 are refused with an update message, and its playback commands are ignored while the queue has podcast items. Jam guests see podcast items as private placeholders, which are mapped back on their commands. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
75c67026ac
commit
b558cf381e
20 files changed
+3587
-653
No files matched your search
@@ -64,6 +64,8 @@ const T = {
|
||||
lastfmDisconnectFailed: "{{ t.player_lastfm_disconnect_failed }}",
|
||||
download: "{{ t.track_download }}",
|
||||
shareUnavailable: "{{ t.share_unavailable }}",
|
||||
queuePodcastUnplayable: "{{ t.player_queue_podcast_unplayable }}",
|
||||
queuePrivateItem: "{{ t.player_queue_private_item }}",
|
||||
searchSimilarTo: "{{ t.player_search_similar_to }}",
|
||||
findSimilar: "{{ t.player_find_similar }}",
|
||||
similarityFailed: "{{ t.player_similarity_failed }}",
|
||||
@@ -857,6 +859,7 @@ document.addEventListener('alpine:init', () => {
|
||||
playQueueIndex(idx) {
|
||||
const queue = Alpine.store('queue');
|
||||
if (!queue || idx < 0 || idx >= queue.tracks.length) return;
|
||||
const previousIndex = queue.currentIndex;
|
||||
queue.currentIndex = idx;
|
||||
const track = queue.tracks[idx];
|
||||
if (track?.federation_pending && track?._federationTrack) {
|
||||
@@ -880,6 +883,17 @@ document.addEventListener('alpine:init', () => {
|
||||
}));
|
||||
return;
|
||||
}
|
||||
if (this._isUnplayableQueueItem(track)) {
|
||||
// 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);
|
||||
return;
|
||||
}
|
||||
queue.currentIndex = playable;
|
||||
this._playLocal(queue.tracks[playable]);
|
||||
return;
|
||||
}
|
||||
this._playLocal(track);
|
||||
},
|
||||
|
||||
@@ -900,6 +914,11 @@ document.addEventListener('alpine:init', () => {
|
||||
},
|
||||
|
||||
_playLocal(track, options = {}) {
|
||||
if (this._isUnplayableQueueItem(track)) {
|
||||
this._recordHistory('replaced');
|
||||
this._showUnplayableQueueItem(track);
|
||||
return;
|
||||
}
|
||||
this._recordHistory('replaced');
|
||||
this.currentTrack = track;
|
||||
this._historyTrack = track;
|
||||
@@ -957,6 +976,10 @@ document.addEventListener('alpine:init', () => {
|
||||
this._sendRemote('resume');
|
||||
return;
|
||||
}
|
||||
if (this._isUnplayableQueueItem(this.currentTrack)) {
|
||||
this.next();
|
||||
return;
|
||||
}
|
||||
audio.play().catch(() => {});
|
||||
},
|
||||
|
||||
@@ -1276,6 +1299,11 @@ document.addEventListener('alpine:init', () => {
|
||||
this._remoteStateBaseTime = desiredTime;
|
||||
this._remoteStateReceivedAt = Date.now();
|
||||
|
||||
if (this._isUnplayableQueueItem(track)) {
|
||||
this._showUnplayableQueueItem(track);
|
||||
this._updateMediaSession();
|
||||
return;
|
||||
}
|
||||
if (this._localSourceTrackId !== track.id) {
|
||||
this._playLocal(track, {
|
||||
position_seconds: desiredTime,
|
||||
@@ -1386,13 +1414,83 @@ document.addEventListener('alpine:init', () => {
|
||||
navigator.mediaSession.setActionHandler('seekto', (d) => { if (d.seekTime != null) this.seek(d.seekTime); });
|
||||
},
|
||||
|
||||
_queueItemSourceKind(track) {
|
||||
return track?.source?.kind || track?.wire_track?.source?.kind || 'music';
|
||||
},
|
||||
|
||||
_isUnplayableQueueItem(track) {
|
||||
const kind = this._queueItemSourceKind(track);
|
||||
return kind === 'podcast_episode' || kind === 'private';
|
||||
},
|
||||
|
||||
_unplayableQueueLabel(track) {
|
||||
const kind = this._queueItemSourceKind(track);
|
||||
if (kind === 'podcast_episode') return T.queuePodcastUnplayable;
|
||||
if (kind === 'private') return T.queuePrivateItem;
|
||||
return '';
|
||||
},
|
||||
|
||||
_playableQueueIndex(tracks, start, step = 1) {
|
||||
const count = Array.isArray(tracks) ? tracks.length : 0;
|
||||
for (let index = start; index >= 0 && index < count; index += step) {
|
||||
if (!this._isUnplayableQueueItem(tracks[index])) return index;
|
||||
}
|
||||
return -1;
|
||||
},
|
||||
|
||||
_showUnplayableQueueItem(track) {
|
||||
// Unload the previous source so a later play() cannot resume it.
|
||||
audio.pause();
|
||||
audio.removeAttribute('src');
|
||||
audio.load();
|
||||
this.isPlaying = false;
|
||||
this.currentTrack = track;
|
||||
this._historyTrack = null;
|
||||
this._localSourceTrackId = track?.id ?? null;
|
||||
this.currentTime = 0;
|
||||
this.progress = 0;
|
||||
this.duration = Number(track?.duration_seconds || 0);
|
||||
Alpine.store('queue')?.syncCurrentIndexToTrack(track);
|
||||
},
|
||||
|
||||
_queueItemsPayload(tracks) {
|
||||
// Library tracks by id; everything else verbatim with its source.
|
||||
return (tracks || []).map(track => {
|
||||
if (track?.wire_track) return { kind: 'external', track: track.wire_track };
|
||||
const id = Number(track?.id);
|
||||
if (Number.isInteger(id) && id > 0 && !track?.federation_pending) {
|
||||
return { kind: 'library', track_id: id };
|
||||
}
|
||||
return null;
|
||||
});
|
||||
},
|
||||
|
||||
_restoredQueueTracks(items, libraryTracks) {
|
||||
const byId = new Map((libraryTracks || []).map(track => [track.id, track]));
|
||||
const tracks = [];
|
||||
const positions = [];
|
||||
(items || []).forEach((item, index) => {
|
||||
const track = item?.kind === 'external' ? item.track : byId.get(item?.track_id);
|
||||
if (!track) return;
|
||||
positions[index] = tracks.length;
|
||||
tracks.push(track);
|
||||
});
|
||||
return { tracks, positions };
|
||||
},
|
||||
|
||||
_buildStatePayload() {
|
||||
const queue = Alpine.store('queue');
|
||||
const localQueue = queue.tracks.filter(track => Number.isInteger(Number(track.id)));
|
||||
const currentLocalId = this.currentTrack && Number.isInteger(Number(this.currentTrack.id))
|
||||
? Number(this.currentTrack.id)
|
||||
: null;
|
||||
const typedItems = this._queueItemsPayload(queue.tracks);
|
||||
const currentItemIndex = Math.max(0, Math.min(Number(queue.currentIndex || 0), queue.tracks.length - 1));
|
||||
const queueItems = typedItems.filter(Boolean);
|
||||
const queueItemsPosition = typedItems.slice(0, currentItemIndex).filter(Boolean).length;
|
||||
return {
|
||||
queue_items: queueItems,
|
||||
queue_items_position: queueItemsPosition,
|
||||
current_track_id: currentLocalId,
|
||||
position_ms: Math.floor(this.currentTime * 1000),
|
||||
queue: localQueue.map(track => Number(track.id)),
|
||||
@@ -1436,6 +1534,54 @@ document.addEventListener('alpine:init', () => {
|
||||
this.repeatMode = state.repeat_mode || 'off';
|
||||
this._setVolumeLocal(typeof state.volume === 'number' ? state.volume : 0.7);
|
||||
|
||||
// Typed queue: library tracks by id plus external items
|
||||
// (podcast episodes and other items kept with their source).
|
||||
if (Array.isArray(state.queue_items) && state.queue_items.length > 0) {
|
||||
try {
|
||||
const ids = state.queue_items.filter(item => item?.kind === 'library').map(item => item.track_id);
|
||||
let libraryTracks = [];
|
||||
if (ids.length > 0) {
|
||||
const tracksRes = await fetch('/api/player/tracks-by-ids', {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ ids }),
|
||||
});
|
||||
if (tracksRes.ok) libraryTracks = await tracksRes.json();
|
||||
}
|
||||
const restored = this._restoredQueueTracks(state.queue_items, libraryTracks);
|
||||
if (restored.tracks.length > 0) {
|
||||
const queue = Alpine.store('queue');
|
||||
queue.tracks = restored.tracks;
|
||||
const wanted = Number(state.queue_items_position || 0);
|
||||
let idx = restored.positions[wanted];
|
||||
if (idx == null) idx = restored.positions.slice(wanted).find(value => value != null);
|
||||
if (idx == null) idx = restored.tracks.length - 1;
|
||||
queue.currentIndex = idx;
|
||||
const currentTrack = restored.tracks[idx];
|
||||
if (this._isUnplayableQueueItem(currentTrack)) {
|
||||
this._showUnplayableQueueItem(currentTrack);
|
||||
} else if (currentTrack) {
|
||||
this.currentTrack = currentTrack;
|
||||
this._historyTrack = currentTrack;
|
||||
this._localSourceTrackId = currentTrack.id;
|
||||
this._historyRecorded = false;
|
||||
this._resetPlaybackTracking();
|
||||
audio.src = currentTrack.stream_url;
|
||||
const seekMs = state.position_ms || 0;
|
||||
if (seekMs > 0) {
|
||||
const onLoaded = () => {
|
||||
audio.currentTime = seekMs / 1000;
|
||||
audio.removeEventListener('loadedmetadata', onLoaded);
|
||||
};
|
||||
audio.addEventListener('loadedmetadata', onLoaded);
|
||||
}
|
||||
this._updateMediaSession();
|
||||
}
|
||||
return;
|
||||
}
|
||||
} catch {}
|
||||
}
|
||||
|
||||
// Restore queue if there are track IDs
|
||||
if (state.queue && state.queue.length > 0) {
|
||||
try {
|
||||
@@ -1605,6 +1751,7 @@ document.addEventListener('alpine:init', () => {
|
||||
fedInviteInput: '',
|
||||
fedBusy: false,
|
||||
fedError: '',
|
||||
selectError: '',
|
||||
remoteHintVisible: false,
|
||||
remoteHintDeviceName: '',
|
||||
_remoteHintShown: false,
|
||||
@@ -2008,7 +2155,12 @@ document.addEventListener('alpine:init', () => {
|
||||
current_device_id: this.id,
|
||||
}),
|
||||
});
|
||||
if (!res.ok) return;
|
||||
if (!res.ok) {
|
||||
// For example: the target device needs an update to receive podcast episodes.
|
||||
this.selectError = res.status === 409 ? await this._errorText(res) : '';
|
||||
return;
|
||||
}
|
||||
this.selectError = '';
|
||||
const data = await res.json();
|
||||
this._apply(data);
|
||||
this.open = false;
|
||||
@@ -2330,7 +2482,10 @@ document.addEventListener('alpine:init', () => {
|
||||
|
||||
playFromIndex(idx) {
|
||||
if (idx < 0 || idx >= this.tracks.length) return;
|
||||
Alpine.store('player').playQueueIndex(idx);
|
||||
const player = Alpine.store('player');
|
||||
// A device playing the audio elsewhere may support the item.
|
||||
if (player._isUnplayableQueueItem(this.tracks[idx]) && !player._shouldSendRemote()) return;
|
||||
player.playQueueIndex(idx);
|
||||
},
|
||||
|
||||
remove(idx) {
|
||||
|
||||
Reference in new issue
Block a user