From e3cb82a3b33bd2a2e49c58ce18d1258fb505869e Mon Sep 17 00:00:00 2001 From: Sophie Forrest Date: Fri, 30 Aug 2024 23:13:20 +1200 Subject: chore: initial commit (codeberg upload) --- crates/messenger_server/src/app.rs | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 crates/messenger_server/src/app.rs (limited to 'crates/messenger_server/src/app.rs') 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>, + + /// Channel used to send messages to all connected clients. + pub tx: broadcast::Sender, + + /// We require unique usernames. This tracks which usernames have been + /// taken. + pub user_set: Mutex>, +} + +/// 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); + } +} -- cgit 1.4.1