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
@@ -629,6 +629,7 @@ impl PlayerDeviceHub {
|
||||
.map(|playback| playback.tracks.clone())
|
||||
.unwrap_or_default();
|
||||
restore_jam_command_payload(command, &mut payload, &host_tracks)?;
|
||||
stamp_jam_participant_command(&mut payload);
|
||||
}
|
||||
(jam.host_user_id, target_id)
|
||||
} else {
|
||||
@@ -1793,6 +1794,66 @@ mod jam_command_tests {
|
||||
assert!(restore_jam_command_payload("queue_clear", &mut payload, &[library(1)]).is_ok());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn participant_commands_reach_the_host_stamped() {
|
||||
let hub = PlayerDeviceHub::default();
|
||||
hub.heartbeat(1, "host", None, None, None);
|
||||
let jam = hub
|
||||
.create_jam(1, "Host", "host", vec![(2, "Guest".to_string())])
|
||||
.unwrap();
|
||||
let jam_id = jam.jams[0].id.clone();
|
||||
hub.heartbeat(2, "guest", None, None, None);
|
||||
hub.join_jam(2, "Guest", "guest", &jam_id).unwrap();
|
||||
let tracks = host_tracks();
|
||||
hub.heartbeat(
|
||||
1,
|
||||
"host",
|
||||
None,
|
||||
Some(&jam_id),
|
||||
Some(PlayerDevicePlaybackStateDto {
|
||||
track: Some(tracks[0].clone()),
|
||||
tracks,
|
||||
index: 0,
|
||||
position_seconds: 0.0,
|
||||
duration_seconds: 100.0,
|
||||
paused: false,
|
||||
shuffle: false,
|
||||
repeat_mode: "off".into(),
|
||||
volume: 0.5,
|
||||
updated_at_ms: 0,
|
||||
position_unknown: false,
|
||||
}),
|
||||
);
|
||||
// Move the episode to the end, then remove index 2: the cached host
|
||||
// state still has music there, so only the host can refuse it.
|
||||
for (command, payload) in [
|
||||
(
|
||||
"queue_move",
|
||||
serde_json::json!({"from_index": 1, "to_index": 2}),
|
||||
),
|
||||
("queue_remove", serde_json::json!({"index": 2})),
|
||||
("pause", serde_json::Value::Null),
|
||||
] {
|
||||
hub.enqueue_command(2, None, Some(&jam_id), command, payload)
|
||||
.unwrap();
|
||||
}
|
||||
hub.enqueue_command(
|
||||
1,
|
||||
None,
|
||||
Some(&jam_id),
|
||||
"queue_remove",
|
||||
serde_json::json!({"index": 0}),
|
||||
)
|
||||
.unwrap();
|
||||
let commands = hub.poll(1, "host", None, Some(&jam_id), None).commands;
|
||||
let stamped: Vec<bool> = commands
|
||||
.iter()
|
||||
.map(|command| command.payload[JAM_PARTICIPANT_KEY] == true)
|
||||
.collect();
|
||||
assert_eq!(stamped, [true, true, true, false]);
|
||||
assert_eq!(commands[1].payload["index"], 2);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn redacted_current_episode_has_no_duration() {
|
||||
let tracks = host_tracks();
|
||||
@@ -6323,6 +6384,28 @@ fn restore_jam_command_payload(
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Marks a Jam participant command for the host browser.
|
||||
///
|
||||
/// `restore_jam_command_payload` checks the command against the host state of
|
||||
/// the host's last report. The host browser runs its pending commands in one
|
||||
/// batch, so an earlier command (a move, an insert) can make a checked index
|
||||
/// name a private item there. The host browser applies the same rules to its
|
||||
/// own queue for stamped commands (`_jamParticipantCommandAllowed`).
|
||||
fn stamp_jam_participant_command(payload: &mut serde_json::Value) {
|
||||
if !payload.is_object() {
|
||||
*payload = serde_json::json!({});
|
||||
}
|
||||
if let Some(object) = payload.as_object_mut() {
|
||||
object.insert(
|
||||
JAM_PARTICIPANT_KEY.to_string(),
|
||||
serde_json::Value::Bool(true),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/// Command payload key set by the server on Jam participant commands.
|
||||
const JAM_PARTICIPANT_KEY: &str = "jam_participant";
|
||||
|
||||
fn stamp_jam_queue_tracks(payload: &mut serde_json::Value, user_id: i64, user_name: &str) {
|
||||
let Some(tracks) = payload
|
||||
.get_mut("tracks")
|
||||
|
||||
Reference in new issue
Block a user