summary refs log tree commit diff
path: root/crates/messenger_server/src/app.rs
blob: 2d91919e09ca74bb65e9c75c79576bf7c2159ffd (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
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);
	}
}