Add create room command and room management enhancements in yourchat2
Implemented the `create_room` command to allow users to create new chat rooms with customizable settings such as privacy, age restrictions, and ownership. Enhanced room management by introducing functions to mark rooms as occupied or possibly empty, and added cleanup logic for stale temporary rooms. Updated the `RoomMeta` structure to include new fields for room creation timestamps and temporary status, ensuring better room lifecycle management.
This commit is contained in:
88
src/state.rs
88
src/state.rs
@@ -1,6 +1,7 @@
|
||||
use crate::types::{ChatState, ClientId, Command, RoomInfo};
|
||||
use serde_json::{json, Value};
|
||||
use std::sync::Arc;
|
||||
use std::time::{SystemTime, UNIX_EPOCH};
|
||||
use tokio::sync::RwLock;
|
||||
|
||||
pub async fn authorize(client_id: ClientId, command: &Command, state: Arc<RwLock<ChatState>>) -> bool {
|
||||
@@ -31,12 +32,19 @@ pub async fn authorize(client_id: ClientId, command: &Command, state: Arc<RwLock
|
||||
pub async fn send_room_list(client_id: ClientId, state: Arc<RwLock<ChatState>>) {
|
||||
let list = {
|
||||
let guard = state.read().await;
|
||||
let mut rooms: Vec<RoomInfo> = guard
|
||||
.rooms
|
||||
.iter()
|
||||
.map(|(name, members)| RoomInfo {
|
||||
name: name.clone(),
|
||||
users: members.len(),
|
||||
let mut names = guard.room_meta.keys().cloned().collect::<Vec<_>>();
|
||||
for name in guard.rooms.keys() {
|
||||
if !guard.room_meta.contains_key(name) {
|
||||
names.push(name.clone());
|
||||
}
|
||||
}
|
||||
names.sort();
|
||||
names.dedup();
|
||||
let mut rooms: Vec<RoomInfo> = names
|
||||
.into_iter()
|
||||
.map(|name| RoomInfo {
|
||||
users: guard.rooms.get(&name).map(|members| members.len()).unwrap_or(0),
|
||||
name,
|
||||
})
|
||||
.collect();
|
||||
rooms.sort_by(|a, b| a.name.cmp(&b.name));
|
||||
@@ -126,7 +134,20 @@ pub async fn disconnect_client(client_id: ClientId, state: Arc<RwLock<ChatState>
|
||||
if let Some(members) = guard.rooms.get_mut(&client.room) {
|
||||
members.remove(&client_id);
|
||||
if members.is_empty() {
|
||||
guard.rooms.remove(&client.room);
|
||||
let is_temporary = guard
|
||||
.room_meta
|
||||
.get(&client.room)
|
||||
.map(|meta| meta.is_temporary)
|
||||
.unwrap_or(false);
|
||||
if is_temporary {
|
||||
if let Some(meta) = guard.room_meta.get_mut(&client.room) {
|
||||
if meta.empty_since_unix.is_none() {
|
||||
meta.empty_since_unix = Some(now_unix());
|
||||
}
|
||||
}
|
||||
} else {
|
||||
guard.rooms.remove(&client.room);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -152,3 +173,56 @@ pub async fn disconnect_client(client_id: ClientId, state: Arc<RwLock<ChatState>
|
||||
.await;
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn mark_room_occupied(room_name: &str, state: Arc<RwLock<ChatState>>) {
|
||||
let mut guard = state.write().await;
|
||||
if let Some(meta) = guard.room_meta.get_mut(room_name) {
|
||||
meta.empty_since_unix = None;
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn mark_room_possibly_empty(room_name: &str, state: Arc<RwLock<ChatState>>) {
|
||||
let mut guard = state.write().await;
|
||||
let is_empty = guard.rooms.get(room_name).map(|members| members.is_empty()).unwrap_or(true);
|
||||
if !is_empty {
|
||||
return;
|
||||
}
|
||||
if let Some(meta) = guard.room_meta.get_mut(room_name) {
|
||||
if meta.is_temporary && meta.empty_since_unix.is_none() {
|
||||
meta.empty_since_unix = Some(now_unix());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn cleanup_stale_temporary_rooms(state: Arc<RwLock<ChatState>>, max_empty_seconds: i64) -> usize {
|
||||
let mut guard = state.write().await;
|
||||
let now = now_unix();
|
||||
let to_remove = guard
|
||||
.room_meta
|
||||
.iter()
|
||||
.filter_map(|(name, meta)| {
|
||||
if !meta.is_temporary {
|
||||
return None;
|
||||
}
|
||||
let empty_since = meta.empty_since_unix?;
|
||||
if now - empty_since >= max_empty_seconds {
|
||||
Some(name.clone())
|
||||
} else {
|
||||
None
|
||||
}
|
||||
})
|
||||
.collect::<Vec<_>>();
|
||||
for room in &to_remove {
|
||||
guard.room_meta.remove(room);
|
||||
guard.rooms.remove(room);
|
||||
guard.dice_games.remove(room);
|
||||
}
|
||||
to_remove.len()
|
||||
}
|
||||
|
||||
fn now_unix() -> i64 {
|
||||
SystemTime::now()
|
||||
.duration_since(UNIX_EPOCH)
|
||||
.map(|d| d.as_secs() as i64)
|
||||
.unwrap_or(0)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user