diff options
Diffstat (limited to '')
| -rw-r--r-- | crates/messenger_server/src/app.rs | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/crates/messenger_server/src/app.rs b/crates/messenger_server/src/app.rs new file mode 100644 index 0000000..2d91919 --- /dev/null +++ b/crates/messenger_server/src/app.rs @@ -0,0 +1,34 @@ +//! Contains functions and structures useful to the general web server + +use std::collections::{HashSet, VecDeque}; + +use tokio::sync::{broadcast, Mutex}; + +/// Our shared state +#[derive(Debug)] +pub struct State { + /// Contains the history of the last 100 messages sent + pub message_history: Mutex<VecDeque<String>>, + + /// Channel used to send messages to all connected clients. + pub tx: broadcast::Sender<String>, + + /// We require unique usernames. This tracks which usernames have been + /// taken. + pub user_set: Mutex<HashSet<String>>, +} + +/// Doc +pub async fn check_username(state: &State, string: &mut String, name: &str) { + let mut user_set = state.user_set.lock().await; + + let name = name.trim(); + + if !name.is_empty() && !user_set.contains(name) { + user_set.insert(name.to_owned()); + + drop(user_set); + + string.push_str(name); + } +} |