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:
Torsten Schulz (local)
2026-03-04 22:44:00 +01:00
parent 3eaf31d64f
commit d620b8f8ae
5 changed files with 342 additions and 9 deletions

View File

@@ -9,6 +9,7 @@ use std::sync::atomic::{AtomicU64, Ordering};
use tokio::io::{AsyncBufReadExt, AsyncRead, AsyncWrite, AsyncWriteExt, BufReader};
use tokio::net::{TcpListener, UnixListener};
use tokio::sync::{mpsc, watch, RwLock};
use tokio::time::{Duration, interval};
use tokio_rustls::TlsAcceptor;
use tokio_rustls::rustls::pki_types::{CertificateDer, PrivateKeyDer};
use tokio_rustls::rustls::ServerConfig as RustlsServerConfig;
@@ -58,6 +59,26 @@ async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
}
let next_client_id = Arc::new(AtomicU64::new(1));
let (shutdown_tx, shutdown_rx) = watch::channel(false);
let cleanup_state = Arc::clone(&state);
let mut cleanup_shutdown_rx = shutdown_rx.clone();
let cleanup_task = tokio::spawn(async move {
let mut ticker = interval(Duration::from_secs(60));
loop {
tokio::select! {
changed = cleanup_shutdown_rx.changed() => {
if changed.is_ok() && *cleanup_shutdown_rx.borrow() {
break;
}
}
_ = ticker.tick() => {
let removed = state::cleanup_stale_temporary_rooms(Arc::clone(&cleanup_state), 15 * 60).await;
if removed > 0 {
println!("[yourchat2] removed {removed} stale temporary room(s)");
}
}
}
}
});
let ws_listener = TcpListener::bind(&ws_addr).await?;
if ws_tls {
@@ -205,6 +226,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
let _ = ws_task.await;
let _ = tcp_task.await;
let _ = cleanup_task.await;
if let Some(task) = unix_task {
let _ = task.await;
if let Some(path) = unix_socket {