summary refs log tree commit diff
path: root/crates/messenger_server/src/app.rs
diff options
context:
space:
mode:
authorSophie Forrest <git@sophieforrest.com>2024-08-30 23:13:20 +1200
committerSophie Forrest <git@sophieforrest.com>2024-08-30 23:13:44 +1200
commite3cb82a3b33bd2a2e49c58ce18d1258fb505869e (patch)
tree2375279182fb4f90f5c28560a08cda90591f608b /crates/messenger_server/src/app.rs
chore: initial commit (codeberg upload) HEAD main
Diffstat (limited to '')
-rw-r--r--crates/messenger_server/src/app.rs34
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);
+	}
+}