diff --git a/src/main.rs b/src/main.rs index e09f833..61113be 100644 --- a/src/main.rs +++ b/src/main.rs @@ -27,6 +27,7 @@ impl App { fn new(username: String) -> App { let mut messages = Vec::new(); messages.push(("System".to_string(), format!("Welcome to the chat, {}!", username))); + messages.push(("System".to_string(), "To change your username, type /nick ".to_string())); App { messages, input: String::new(), @@ -121,9 +122,20 @@ async fn run_app(terminal: &mut Terminal, mut app: App) -> io::Re KeyCode::Enter => { let message_text = app.input.drain(..).collect::(); if message_text.starts_with("/nick ") { + let old_username = app.username.clone(); let new_username = message_text.split_whitespace().nth(1).unwrap_or(&app.username).to_string(); - app.username = new_username; + app.username = new_username.clone(); app.messages.push(("System".to_string(), format!("Username changed to: {}", app.username))); + + let chat_message = ChatMessage { + username: old_username, + message: format!("has changed their name to {}", new_username), + }; + let payload = serde_json::to_string(&chat_message).unwrap(); + client_clone + .publish("chat/msg", QoS::AtMostOnce, false, payload.as_bytes()) + .await + .unwrap(); } else { let chat_message = ChatMessage { username: app.username.clone(),