forked from Furumi/furumi-web
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:
1 parent
d1be583d1e
commit
6f3ed4b7ee
3 files changed
+238
No files matched your search
@@ -114,6 +114,7 @@ function activePlayer(queueTracks, currentIndex, audioState = {}, options = {})
|
||||
'_skipUnplayableCurrentItem', '_knownPosition', '_currentItemHasLocalAudio',
|
||||
'_showUnplayableQueueItem', '_syncLocalPlaybackState',
|
||||
'_queueItemSourceKind', '_isUnplayableQueueItem', '_playableQueueIndex',
|
||||
'_jamParticipantCommandAllowed', '_dropJamPlaceholders',
|
||||
];
|
||||
const audio = Object.assign({
|
||||
paused: true, currentTime: 0, duration: NaN, src: '',
|
||||
@@ -131,6 +132,11 @@ function activePlayer(queueTracks, currentIndex, audioState = {}, options = {})
|
||||
const index = this.tracks.indexOf(track);
|
||||
if (index >= 0) this.currentIndex = index;
|
||||
},
|
||||
// Simplified local queue edits, as the host applies remote commands.
|
||||
_removeLocal(index) { if (index >= 0 && index < this.tracks.length) this.tracks.splice(index, 1); },
|
||||
_moveTrackLocal(from, to) { this.tracks.splice(to, 0, this.tracks.splice(from, 1)[0]); },
|
||||
_clearLocal() { this.tracks = []; },
|
||||
_addToEndLocal(tracks) { this.tracks.push(...tracks); },
|
||||
};
|
||||
const stores = {queue, devices: {heartbeat() {}}};
|
||||
const context = vm.createContext({audio, Alpine: {store: name => stores[name]}});
|
||||
@@ -299,3 +305,73 @@ test('prev and seek work on a handed-off episode', () => {
|
||||
assert.equal(first.currentTrack, episode);
|
||||
assert.equal(first.currentTime, 1200);
|
||||
});
|
||||
|
||||
test('the Jam host keeps its private items against stale participant commands', () => {
|
||||
const a = song(1);
|
||||
const b = song(2);
|
||||
const p = activePlayer([a, episode, b], 0, {src: '/api/player/stream/1'});
|
||||
// One poll batch: the server checked index 2 against the old order [A, P, B].
|
||||
for (const command of [
|
||||
{command: 'queue_move', payload: {from_index: 1, to_index: 2, jam_participant: true}},
|
||||
{command: 'queue_remove', payload: {index: 2, jam_participant: true}},
|
||||
]) p._executeRemoteCommand(command);
|
||||
assert.deepEqual(p.queue.tracks, [a, b, episode]);
|
||||
p._executeRemoteCommand({command: 'queue_clear', payload: {jam_participant: true}});
|
||||
assert.equal(p.queue.tracks.length, 3);
|
||||
p._executeRemoteCommand({command: 'queue_add_end', payload: {tracks: [privateItem], jam_participant: true}});
|
||||
assert.equal(p.queue.tracks.length, 3);
|
||||
// A full queue from a stale view brings back nothing the host removed.
|
||||
p._executeRemoteCommand({command: 'play_from_index', payload: {
|
||||
tracks: [a, episode, episode, b], index: 0, track: a, jam_participant: true,
|
||||
}});
|
||||
assert.deepEqual(p.queue.tracks, [a, b, episode]);
|
||||
p._executeRemoteCommand({command: 'play_from_index', payload: {
|
||||
tracks: [a, b], index: 0, track: a, jam_participant: true,
|
||||
}});
|
||||
assert.deepEqual(p.queue.tracks, [a, b, episode]);
|
||||
// Music is still theirs to change, and the host's own commands are not limited.
|
||||
p._executeRemoteCommand({command: 'queue_remove', payload: {index: 0, jam_participant: true}});
|
||||
assert.deepEqual(p.queue.tracks, [b, episode]);
|
||||
p._executeRemoteCommand({command: 'play_from_index', payload: {
|
||||
tracks: [episode, b], index: 1, track: b, jam_participant: true,
|
||||
}});
|
||||
assert.deepEqual(p.queue.tracks, [episode, b]);
|
||||
p._executeRemoteCommand({command: 'queue_remove', payload: {index: 0}});
|
||||
assert.deepEqual(p.queue.tracks, [b]);
|
||||
});
|
||||
|
||||
test('leaving a Jam drops the host placeholders from the guest queue', () => {
|
||||
const placeholder = Object.assign({}, privateItem);
|
||||
const p = activePlayer([song(1), placeholder, song(2)], 1);
|
||||
p.currentTrack = placeholder;
|
||||
p._dropJamPlaceholders();
|
||||
assert.deepEqual(Array.from(p.queue.tracks, track => track.id), [1, 2]);
|
||||
// The current placeholder hands over to the next item, paused.
|
||||
assert.equal(p.queue.currentIndex, 1);
|
||||
assert.equal(p.currentTrack.id, 2);
|
||||
assert.equal(p.isPlaying, false);
|
||||
assert.deepEqual(p.audio.played, []);
|
||||
|
||||
const music = activePlayer([song(1), placeholder, song(2)], 2);
|
||||
music._dropJamPlaceholders();
|
||||
assert.equal(music.queue.currentIndex, 1);
|
||||
assert.equal(music.currentTrack.id, 2);
|
||||
|
||||
const only = activePlayer([placeholder], 0);
|
||||
only._dropJamPlaceholders();
|
||||
assert.deepEqual(only.queue.tracks, []);
|
||||
assert.equal(only.currentTrack, null);
|
||||
// The guest's own podcast episodes stay.
|
||||
const own = activePlayer([episode, song(1)], 0);
|
||||
own._dropJamPlaceholders();
|
||||
assert.equal(own.queue.tracks.length, 2);
|
||||
});
|
||||
|
||||
test('the devices store drops placeholders when the guest stops controlling a Jam', () => {
|
||||
const start = template.indexOf('\n _apply(data) {');
|
||||
const apply = template.slice(start, template.indexOf('\n },\n', start));
|
||||
assert.match(apply, /const wasControllingJam = this\.isControllingRemoteJam\(\);/);
|
||||
assert.match(apply, /if \(wasControllingJam && !this\.isControllingRemoteJam\(\)\) \{\s*Alpine\.store\('player'\)\?\._dropJamPlaceholders\(\);/);
|
||||
const clear = template.slice(template.indexOf('\n clearJamSelection() {'));
|
||||
assert.match(clear.slice(0, clear.indexOf('\n },\n')), /if \(wasControllingJam\) Alpine\.store\('player'\)\?\._dropJamPlaceholders\(\);/);
|
||||
});
|
||||
Reference in new issue
Block a user