feat: Implement /join command with room change messages and update startup notice

This commit introduces the following enhancements:
- Renamed the  command to  for changing chat rooms.
- Added leaving messages to the old room and joining messages to the new room when a user changes rooms using .
- Updated the startup notice to inform users about the  command and the current room.
This commit is contained in:
2025-07-14 15:34:25 -04:00
parent e5a5e5133f
commit 53fdd0a0f2

View File

@@ -21,17 +21,21 @@ struct App {
messages: Vec<(String, String)>, messages: Vec<(String, String)>,
input: String, input: String,
username: String, username: String,
current_room: String,
} }
impl App { impl App {
fn new(username: String) -> App { fn new(username: String) -> App {
let mut messages = Vec::new(); let mut messages = Vec::new();
let default_room = "default".to_string();
messages.push(("System".to_string(), format!("Welcome to the chat, {}!", username))); messages.push(("System".to_string(), format!("Welcome to the chat, {}!", username)));
messages.push(("System".to_string(), "To change your username, type /nick <new_username>".to_string())); messages.push(("System".to_string(), "To change your username, type /nick <new_username>".to_string()));
messages.push(("System".to_string(), format!("You are currently in room: {}. To change room, type /join <new_room_name>", default_room)));
App { App {
messages, messages,
input: String::new(), input: String::new(),
username, username,
current_room: default_room,
} }
} }
} }
@@ -77,7 +81,7 @@ async fn run_app<B: Backend>(terminal: &mut Terminal<B>, mut app: App) -> io::Re
mqttoptions.set_keep_alive(Duration::from_secs(5)); mqttoptions.set_keep_alive(Duration::from_secs(5));
let (client, mut eventloop) = AsyncClient::new(mqttoptions, 10); let (client, mut eventloop) = AsyncClient::new(mqttoptions, 10);
client.subscribe("chat/msg", QoS::AtMostOnce).await.unwrap(); client.subscribe(&format!("chat/{}", app.current_room), QoS::AtMostOnce).await.unwrap();
// Publish connect message // Publish connect message
let connect_message = ChatMessage { let connect_message = ChatMessage {
@@ -85,7 +89,7 @@ async fn run_app<B: Backend>(terminal: &mut Terminal<B>, mut app: App) -> io::Re
message: "has connected".to_string(), message: "has connected".to_string(),
}; };
let connect_payload = serde_json::to_string(&connect_message).unwrap(); let connect_payload = serde_json::to_string(&connect_message).unwrap();
client.publish("chat/msg", QoS::AtMostOnce, false, connect_payload.as_bytes()).await.unwrap(); client.publish(&format!("chat/{}", app.current_room), QoS::AtMostOnce, false, connect_payload.as_bytes()).await.unwrap();
let client_clone = client.clone(); let client_clone = client.clone();
@@ -133,9 +137,36 @@ async fn run_app<B: Backend>(terminal: &mut Terminal<B>, mut app: App) -> io::Re
}; };
let payload = serde_json::to_string(&chat_message).unwrap(); let payload = serde_json::to_string(&chat_message).unwrap();
client_clone client_clone
.publish("chat/msg", QoS::AtMostOnce, false, payload.as_bytes()) .publish(&format!("chat/{}", app.current_room), QoS::AtMostOnce, false, payload.as_bytes())
.await .await
.unwrap(); .unwrap();
} else if message_text.starts_with("/join ") {
let old_room = app.current_room.clone();
let new_room = message_text.split_whitespace().nth(1).unwrap_or(&app.current_room).to_string();
if old_room != new_room {
// Send leaving message to old room
let leaving_message = ChatMessage {
username: app.username.clone(),
message: format!("is leaving for room {}", new_room),
};
let leaving_payload = serde_json::to_string(&leaving_message).unwrap();
client_clone.publish(&format!("chat/{}", old_room), QoS::AtMostOnce, false, leaving_payload.as_bytes()).await.unwrap();
client_clone.unsubscribe(&format!("chat/{}", old_room)).await.unwrap();
client_clone.subscribe(&format!("chat/{}", new_room), QoS::AtMostOnce).await.unwrap();
app.current_room = new_room.clone();
app.messages.push(("System".to_string(), format!("Changed room to: {}", new_room)));
// Send joining message to new room
let joining_message = ChatMessage {
username: app.username.clone(),
message: "has joined the room".to_string(),
};
let joining_payload = serde_json::to_string(&joining_message).unwrap();
client_clone.publish(&format!("chat/{}", new_room), QoS::AtMostOnce, false, joining_payload.as_bytes()).await.unwrap();
} else {
app.messages.push(("System".to_string(), format!("Already in room: {}", new_room)));
}
} else { } else {
let chat_message = ChatMessage { let chat_message = ChatMessage {
username: app.username.clone(), username: app.username.clone(),
@@ -143,7 +174,7 @@ async fn run_app<B: Backend>(terminal: &mut Terminal<B>, mut app: App) -> io::Re
}; };
let payload = serde_json::to_string(&chat_message).unwrap(); let payload = serde_json::to_string(&chat_message).unwrap();
client_clone client_clone
.publish("chat/msg", QoS::AtMostOnce, false, payload.as_bytes()) .publish(&format!("chat/{}", app.current_room), QoS::AtMostOnce, false, payload.as_bytes())
.await .await
.unwrap(); .unwrap();
} }