From 16d71a8c1ce7300a84bbd1417debdc954ca00089 Mon Sep 17 00:00:00 2001 From: "Torsten Schulz (local)" Date: Fri, 27 Mar 2026 13:42:52 +0100 Subject: [PATCH] Enhance room initialization and joining commands with detailed debug logging Refactored the handling of room names in `handle_init_command` and `handle_join_command` to utilize new functions for extracting requested room names. Added comprehensive debug logging to track client interactions, including requested and resolved room names, and password lengths when debugging is enabled. This update improves troubleshooting capabilities and provides clearer insights into room access processes. --- src/commands.rs | 79 +++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 77 insertions(+), 2 deletions(-) diff --git a/src/commands.rs b/src/commands.rs index 2ef2f90..996d79b 100644 --- a/src/commands.rs +++ b/src/commands.rs @@ -156,8 +156,18 @@ async fn handle_init_command( return; } }; - let room_name = normalize_room_name(command.room.as_deref().unwrap_or("lobby")); + let requested_room_raw = init_requested_room(command); + let room_name = normalize_room_name(&requested_room_raw); let room_password = command.password.clone().unwrap_or_default(); + if room_debug_enabled() { + eprintln!( + "[yourchat2][room-debug][init] client_id={client_id} user='{}' requested_room_raw='{}' normalized_room='{}' password_len={}", + requested_user_name, + requested_room_raw, + room_name, + room_password.len() + ); + } let (resolved_room_name, room_meta) = { let guard = state.read().await; let Some(resolved_room_name) = resolve_room_name(&guard, &room_name) else { @@ -179,6 +189,12 @@ async fn handle_init_command( send_error(client_id, Arc::clone(&state), "room_not_found").await; return; }; + if room_debug_enabled() { + eprintln!( + "[yourchat2][room-debug][init] client_id={client_id} requested_room='{}' resolved_room='{}'", + room_name, resolved_room_name + ); + } (resolved_room_name, room_meta) }; if let Err(access_error) = room_access_allowed( @@ -305,6 +321,12 @@ async fn handle_init_command( state::mark_room_possibly_empty(&old_room_name, Arc::clone(&state)).await; } state::mark_room_occupied(&actual_room_name, Arc::clone(&state)).await; + if room_debug_enabled() { + eprintln!( + "[yourchat2][room-debug][init] client_id={client_id} user='{}' joined_room='{}'", + user_name, actual_room_name + ); + } state::send_to_client( client_id, @@ -337,8 +359,17 @@ async fn handle_join_command( if !state::authorize(client_id, command, Arc::clone(&state)).await { return; } - let room = normalize_room_name(command.room.as_deref().or(command.name.as_deref()).unwrap_or("lobby")); + let requested_room_raw = join_requested_room(command); + let room = normalize_room_name(&requested_room_raw); let password = command.password.clone().unwrap_or_default(); + if room_debug_enabled() { + eprintln!( + "[yourchat2][room-debug][join] client_id={client_id} requested_room_raw='{}' normalized_room='{}' password_len={}", + requested_room_raw, + room, + password.len() + ); + } let (resolved_room, room_meta, falukant_user_id, gender_id, age, right_type_ids, rights) = { let guard = state.read().await; let Some(resolved_room) = resolve_room_name(&guard, &room) else { @@ -360,6 +391,12 @@ async fn handle_join_command( send_error(client_id, Arc::clone(&state), "room_not_found").await; return; }; + if room_debug_enabled() { + eprintln!( + "[yourchat2][room-debug][join] client_id={client_id} requested_room='{}' resolved_room='{}'", + room, resolved_room + ); + } let Some(client) = guard.clients.get(&client_id) else { return; }; @@ -432,6 +469,12 @@ async fn handle_join_command( state::mark_room_possibly_empty(&from_room, Arc::clone(&state)).await; } state::mark_room_occupied(&actual_room_name, Arc::clone(&state)).await; + if room_debug_enabled() { + eprintln!( + "[yourchat2][room-debug][join] client_id={client_id} user='{}' from='{}' to='{}'", + user_name, from_room, actual_room_name + ); + } if !from_room.is_empty() && from_room != room { state::broadcast_room( &from_room, @@ -1422,6 +1465,38 @@ fn normalize_room_name(input: &str) -> String { trimmed.to_string() } +fn init_requested_room(command: &Command) -> String { + command + .room + .clone() + .or_else(|| extract_room_from_value(command.value.as_ref())) + .unwrap_or_else(|| "lobby".to_string()) +} + +fn join_requested_room(command: &Command) -> String { + command + .room + .clone() + .or_else(|| extract_room_from_value(command.value.as_ref())) + .or(command.name.clone()) + .unwrap_or_else(|| "lobby".to_string()) +} + +fn extract_room_from_value(value: Option<&Value>) -> Option { + let Value::Object(map) = value? else { + return None; + }; + for key in ["room", "roomName", "targetRoom", "selectedRoom", "to"] { + if let Some(candidate) = map.get(key).and_then(value_as_string) { + let trimmed = candidate.trim(); + if !trimmed.is_empty() { + return Some(trimmed.to_string()); + } + } + } + None +} + fn normalize_color(input: &str) -> String { let mut value = input.trim().to_string(); if value.is_empty() {