Check Jam participant commands against the host queue

The server checked a participant's queue_remove/queue_clear against the
host state of the last report, but the host browser runs its pending
commands in one batch: after a move, a checked index can name a private
item. Participant commands are now stamped `jam_participant`, and the
host browser refuses to remove, clear, add or bring back its private
items for them, judged on the queue it is about to change.

A guest that stops controlling a Jam drops the host's private
placeholders from its own queue, so they are not saved or relayed as
the guest's queue.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
sanapachandClaude Opus 5.5 committed 2026-11-19 19:21:51 +02:00
1 parent d1be583d1e
commit 6f3ed4b7ee
3 files changed
+238

No files matched your search

+79
View File
@@ -1398,6 +1398,9 @@ document.addEventListener('alpine:init', () => {
if (!command || !command.command) return;
const payload = command.payload || {};
const queue = Alpine.store('queue');
// The server stamps Jam participant commands. It checks them against
// the host state it saw last; this queue is the one they change.
if (payload.jam_participant === true && !this._jamParticipantCommandAllowed(command.command, payload, queue)) return;
this._remoteExecuting = true;
try {
if (typeof payload.shuffle === 'boolean') this.shuffle = payload.shuffle;
@@ -1523,6 +1526,76 @@ document.addEventListener('alpine:init', () => {
Alpine.store('queue')?.syncCurrentIndexToTrack(track);
},
_jamParticipantCommandAllowed(command, payload, queue) {
// Jam participants cannot remove, clear away, add or bring back the
// host's private items (podcast episodes). Commands run in a batch,
// so an index the server checked can name another item by now.
const tracks = queue?.tracks || [];
const isPrivate = track => this._isUnplayableQueueItem(track);
if (command === 'queue_remove') {
const index = Number(payload.index);
if (!Number.isInteger(index)) return !tracks.some(isPrivate);
return !isPrivate(tracks[index]);
}
if (command === 'queue_clear') return !tracks.some(isPrivate);
if (command === 'queue_add_end' || command === 'queue_add_next') {
return !(Array.isArray(payload.tracks) ? payload.tracks : []).some(isPrivate);
}
if (command === 'play_track' || command === 'play_from_index' || command === 'transfer_state') {
const incoming = Array.isArray(payload.tracks) ? payload.tracks.filter(Boolean) : [];
if (incoming.length === 0) return !isPrivate(payload.track);
// A full queue replaces this one: it must hold exactly the private
// items this queue holds now (none dropped, repeated or added).
const key = track => {
const source = track?.source || track?.wire_track?.source || {};
return `${source.kind || ''}:${source.episode_id || ''}`;
};
const counts = list => {
const result = new Map();
list.filter(isPrivate).forEach(track => result.set(key(track), (result.get(key(track)) || 0) + 1));
return result;
};
const mine = counts(tracks);
const theirs = counts(incoming);
return mine.size === theirs.size && [...theirs].every(([item, count]) => mine.get(item) === count);
}
return true;
},
_dropJamPlaceholders() {
// A Jam guest's queue shows the host's private items as placeholders.
// They stand for someone else's items and never become this user's
// own queue (persisted, relayed to their devices) after the Jam.
const queue = Alpine.store('queue');
if (!queue) return;
const isPlaceholder = track => this._queueItemSourceKind(track) === 'private';
const tracks = Array.isArray(queue.tracks) ? queue.tracks : [];
if (!tracks.some(isPlaceholder) && !isPlaceholder(this.currentTrack)) return;
const currentIndex = Math.max(0, Number(queue.currentIndex || 0));
const kept = tracks.filter(track => !isPlaceholder(track));
let index = kept.indexOf(tracks[currentIndex]);
// A removed current item is followed by the next item that stays.
if (index < 0) index = tracks.slice(0, currentIndex).filter(track => !isPlaceholder(track)).length;
queue.tracks = kept;
queue.currentIndex = Math.max(0, Math.min(index, kept.length - 1));
if (!isPlaceholder(this.currentTrack)) return;
const next = kept[queue.currentIndex] || null;
if (next) {
this._playLocal(next, { paused: true });
return;
}
this._pauseLocal();
audio.removeAttribute('src');
audio.load();
this.currentTrack = null;
this._historyTrack = null;
this._localSourceTrackId = null;
this._positionUnknown = false;
this.currentTime = 0;
this.duration = 0;
this.progress = 0;
},
_queueItemsPayload(tracks) {
// Library tracks by id; everything else verbatim with its source.
return (tracks || []).map(track => {
@@ -1919,6 +1992,7 @@ document.addEventListener('alpine:init', () => {
_apply(data) {
const previousJamId = this.currentJamId;
const wasControllingJam = this.isControllingRemoteJam();
this.activeDeviceId = data.active_device_id || null;
this.devices = Array.isArray(data.devices) ? data.devices : [];
this.jams = Array.isArray(data.jams) ? data.jams : [];
@@ -1936,6 +2010,9 @@ document.addEventListener('alpine:init', () => {
if (!this.isActive() && !this.shouldPlayJamLocally()) {
Alpine.store('player')?._pauseLocal();
}
if (wasControllingJam && !this.isControllingRemoteJam()) {
Alpine.store('player')?._dropJamPlaceholders();
}
this._maybeShowRemoteHint();
},
@@ -2436,11 +2513,13 @@ document.addEventListener('alpine:init', () => {
},
clearJamSelection() {
const wasControllingJam = this.isControllingRemoteJam();
this.currentJamId = null;
this.jamPanelOpen = false;
this.jamPanelJamId = null;
this._setJamLocalPlayback(false, { pauseLocal: true });
sessionStorage.removeItem('furu_player_jam_id');
if (wasControllingJam) Alpine.store('player')?._dropJamPlaceholders();
},
});