forked from lavina/lavina
1
0
Fork 0
lavina/crates/lavina-core/src/room.rs

286 lines
10 KiB
Rust
Raw Normal View History

2023-02-04 01:01:49 +00:00
//! Domain of rooms — chats with multiple participants.
use std::collections::HashSet;
2023-09-30 23:12:11 +00:00
use std::{collections::HashMap, hash::Hash, sync::Arc};
2023-02-03 22:43:59 +00:00
use chrono::{DateTime, Utc};
2023-02-12 22:23:52 +00:00
use prometheus::{IntGauge, Registry as MetricRegistry};
2023-02-15 17:10:54 +00:00
use serde::Serialize;
use tokio::sync::RwLock as AsyncRwLock;
2023-02-03 22:43:59 +00:00
2023-09-30 23:12:11 +00:00
use crate::player::{PlayerHandle, PlayerId, Updates};
2023-08-17 13:41:28 +00:00
use crate::prelude::*;
2023-09-30 23:12:11 +00:00
use crate::repo::Storage;
2023-02-03 22:43:59 +00:00
/// Opaque room id
2023-02-15 17:10:54 +00:00
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize)]
2023-04-13 22:38:26 +00:00
pub struct RoomId(Str);
2023-02-14 19:07:07 +00:00
impl RoomId {
2023-04-13 22:38:26 +00:00
pub fn from(str: impl Into<Str>) -> Result<RoomId> {
let bytes = str.into();
2023-02-14 19:07:07 +00:00
if bytes.len() > 32 {
2023-09-30 23:12:11 +00:00
return Err(anyhow::Error::msg("Room name cannot be longer than 32 symbols"));
2023-02-14 19:07:07 +00:00
}
2023-04-13 19:15:48 +00:00
if bytes.contains(' ') {
2023-02-14 19:07:07 +00:00
return Err(anyhow::Error::msg("Room name cannot contain spaces"));
}
Ok(RoomId(bytes))
}
2023-04-13 22:38:26 +00:00
pub fn as_inner(&self) -> &Str {
&self.0
}
2023-04-13 22:38:26 +00:00
pub fn into_inner(self) -> Str {
2023-04-11 16:28:03 +00:00
self.0
}
2023-02-14 19:07:07 +00:00
}
2023-02-03 22:43:59 +00:00
/// Shared data structure for storing metadata about rooms.
2023-02-03 22:43:59 +00:00
#[derive(Clone)]
2023-08-17 13:41:28 +00:00
pub struct RoomRegistry(Arc<AsyncRwLock<RoomRegistryInner>>);
2023-02-03 22:43:59 +00:00
impl RoomRegistry {
2023-08-17 13:41:28 +00:00
pub fn new(metrics: &mut MetricRegistry, storage: Storage) -> Result<RoomRegistry> {
2023-09-30 23:12:11 +00:00
let metric_active_rooms = IntGauge::new("chat_rooms_active", "Number of alive room actors")?;
2023-02-12 22:23:52 +00:00
metrics.register(Box::new(metric_active_rooms.clone()))?;
2023-02-03 22:43:59 +00:00
let inner = RoomRegistryInner {
rooms: HashMap::new(),
2023-02-12 22:23:52 +00:00
metric_active_rooms,
2023-08-17 13:41:28 +00:00
storage,
2023-02-03 22:43:59 +00:00
};
2023-08-17 13:41:28 +00:00
Ok(RoomRegistry(Arc::new(AsyncRwLock::new(inner))))
2023-02-03 22:43:59 +00:00
}
pub fn shutdown(self) -> Result<()> {
let res = match Arc::try_unwrap(self.0) {
Ok(e) => e,
Err(_) => return Err(fail("failed to acquire rooms ownership on shutdown")),
};
let res = res.into_inner();
// TODO drop all rooms
drop(res);
Ok(())
}
#[tracing::instrument(skip(self), name = "RoomRegistry::get_or_create_room")]
2023-08-17 13:41:28 +00:00
pub async fn get_or_create_room(&mut self, room_id: RoomId) -> Result<RoomHandle> {
let mut inner = self.0.write().await;
if let Some(room_handle) = inner.get_or_load_room(&room_id).await? {
2023-08-17 13:41:28 +00:00
Ok(room_handle.clone())
2023-02-13 18:32:52 +00:00
} else {
2023-08-17 13:41:28 +00:00
log::debug!("Creating room {}...", &room_id.0);
let topic = "New room";
2023-08-18 14:45:48 +00:00
let id = inner.storage.create_new_room(&*room_id.0, &*topic).await?;
let room = Room {
2023-08-18 14:45:48 +00:00
storage_id: id,
room_id: room_id.clone(),
subscriptions: HashMap::new(),
members: HashSet::new(),
2023-08-17 13:41:28 +00:00
topic: topic.into(),
message_count: 0,
2023-08-18 14:45:48 +00:00
storage: inner.storage.clone(),
};
let room_handle = RoomHandle(Arc::new(AsyncRwLock::new(room)));
inner.rooms.insert(room_id, room_handle.clone());
2023-02-13 18:32:52 +00:00
inner.metric_active_rooms.inc();
2023-08-17 13:41:28 +00:00
Ok(room_handle)
2023-02-13 18:32:52 +00:00
}
2023-02-03 22:43:59 +00:00
}
#[tracing::instrument(skip(self), name = "RoomRegistry::get_room")]
2023-08-17 13:41:28 +00:00
pub async fn get_room(&self, room_id: &RoomId) -> Option<RoomHandle> {
let mut inner = self.0.write().await;
inner.get_or_load_room(room_id).await.unwrap()
2023-02-03 22:43:59 +00:00
}
2023-02-15 17:10:54 +00:00
#[tracing::instrument(skip(self), name = "RoomRegistry::get_all_rooms")]
2023-02-15 17:10:54 +00:00
pub async fn get_all_rooms(&self) -> Vec<RoomInfo> {
let handles = {
2023-08-17 13:41:28 +00:00
let inner = self.0.read().await;
2023-02-15 17:10:54 +00:00
let handles = inner.rooms.values().cloned().collect::<Vec<_>>();
handles
};
let mut res = vec![];
for i in handles {
res.push(i.get_room_info().await)
}
res
}
2023-02-03 22:43:59 +00:00
}
struct RoomRegistryInner {
rooms: HashMap<RoomId, RoomHandle>,
2023-02-12 22:23:52 +00:00
metric_active_rooms: IntGauge,
2023-08-17 13:41:28 +00:00
storage: Storage,
2023-02-03 22:43:59 +00:00
}
impl RoomRegistryInner {
#[tracing::instrument(skip(self), name = "RoomRegistryInner::get_or_load_room")]
async fn get_or_load_room(&mut self, room_id: &RoomId) -> Result<Option<RoomHandle>> {
if let Some(room_handle) = self.rooms.get(room_id) {
log::debug!("Room {} was loaded already", &room_id.0);
Ok(Some(room_handle.clone()))
} else if let Some(stored_room) = self.storage.retrieve_room_by_name(&*room_id.0).await? {
log::debug!("Loading room {}...", &room_id.0);
let room = Room {
storage_id: stored_room.id,
room_id: room_id.clone(),
subscriptions: HashMap::new(),
members: HashSet::new(), // TODO load members from storage
topic: stored_room.topic.into(),
message_count: stored_room.message_count,
storage: self.storage.clone(),
};
let room_handle = RoomHandle(Arc::new(AsyncRwLock::new(room)));
self.rooms.insert(room_id.clone(), room_handle.clone());
self.metric_active_rooms.inc();
Ok(Some(room_handle))
} else {
tracing::debug!("Room {} does not exist", &room_id.0);
Ok(None)
}
}
}
2023-02-03 22:43:59 +00:00
#[derive(Clone)]
pub struct RoomHandle(Arc<AsyncRwLock<Room>>);
2023-02-03 22:43:59 +00:00
impl RoomHandle {
#[tracing::instrument(skip(self, player_handle), name = "RoomHandle::subscribe")]
pub async fn subscribe(&self, player_id: &PlayerId, player_handle: PlayerHandle) {
let mut lock = self.0.write().await;
tracing::info!("Adding a subscriber to a room");
lock.subscriptions.insert(player_id.clone(), player_handle);
}
#[tracing::instrument(skip(self), name = "RoomHandle::add_member")]
pub async fn add_member(&self, player_id: &PlayerId, player_storage_id: u32) {
let mut lock = self.0.write().await;
tracing::info!("Adding a new member to a room");
let room_storage_id = lock.storage_id;
if !lock.storage.is_room_member(room_storage_id, player_storage_id).await.unwrap() {
lock.storage.add_room_member(room_storage_id, player_storage_id).await.unwrap();
}
lock.members.insert(player_id.clone());
let update = Updates::RoomJoined {
room_id: lock.room_id.clone(),
new_member_id: player_id.clone(),
};
lock.broadcast_update(update, player_id).await;
2023-02-03 22:43:59 +00:00
}
#[tracing::instrument(skip(self), name = "RoomHandle::unsubscribe")]
2023-02-15 17:54:48 +00:00
pub async fn unsubscribe(&self, player_id: &PlayerId) {
let mut lock = self.0.write().await;
lock.subscriptions.remove(player_id);
}
#[tracing::instrument(skip(self), name = "RoomHandle::remove_member")]
pub async fn remove_member(&self, player_id: &PlayerId, player_storage_id: u32) {
let mut lock = self.0.write().await;
tracing::info!("Removing a member from a room");
let room_storage_id = lock.storage_id;
lock.storage.remove_room_member(room_storage_id, player_storage_id).await.unwrap();
lock.members.remove(player_id);
2023-02-15 17:54:48 +00:00
let update = Updates::RoomLeft {
room_id: lock.room_id.clone(),
former_member_id: player_id.clone(),
};
lock.broadcast_update(update, player_id).await;
}
#[tracing::instrument(skip(self, body, created_at), name = "RoomHandle::send_message")]
pub async fn send_message(&self, player_id: &PlayerId, body: Str, created_at: DateTime<Utc>) {
2023-08-18 14:45:48 +00:00
let mut lock = self.0.write().await;
let res = lock.send_message(player_id, body, created_at).await;
2023-09-06 14:34:20 +00:00
if let Err(err) = res {
log::warn!("Failed to send message: {err:?}");
}
2023-02-03 22:43:59 +00:00
}
2023-02-14 00:44:03 +00:00
#[tracing::instrument(skip(self), name = "RoomHandle::get_room_info")]
pub async fn get_room_info(&self) -> RoomInfo {
let lock = self.0.read().await;
RoomInfo {
id: lock.room_id.clone(),
2023-09-30 23:12:11 +00:00
members: lock.subscriptions.keys().map(|x| x.clone()).collect::<Vec<_>>(),
2023-02-14 18:46:42 +00:00
topic: lock.topic.clone(),
}
}
#[tracing::instrument(skip(self, new_topic), name = "RoomHandle::set_topic")]
pub async fn set_topic(&self, changer_id: &PlayerId, new_topic: Str) {
2023-02-14 18:46:42 +00:00
let mut lock = self.0.write().await;
let storage_id = lock.storage_id;
2023-02-14 18:46:42 +00:00
lock.topic = new_topic.clone();
lock.storage.set_room_topic(storage_id, &new_topic).await.unwrap();
let update = Updates::RoomTopicChanged {
room_id: lock.room_id.clone(),
new_topic: new_topic.clone(),
};
lock.broadcast_update(update, changer_id).await;
2023-02-14 00:44:03 +00:00
}
2023-02-03 22:43:59 +00:00
}
struct Room {
/// The numeric node-local id of the room as it is stored in the database.
2023-08-18 14:45:48 +00:00
storage_id: u32,
/// The cluster-global id of the room.
room_id: RoomId,
/// Player actors on the local node which are subscribed to this room's updates.
2023-02-03 22:43:59 +00:00
subscriptions: HashMap<PlayerId, PlayerHandle>,
/// Members of the room.
members: HashSet<PlayerId>,
/// The total number of messages. Used to calculate the id of the new message.
2023-08-17 13:41:28 +00:00
message_count: u32,
2023-04-13 22:38:26 +00:00
topic: Str,
2023-08-18 14:45:48 +00:00
storage: Storage,
2023-02-03 22:43:59 +00:00
}
2023-02-03 22:43:59 +00:00
impl Room {
#[tracing::instrument(skip(self, body, created_at), name = "Room::send_message")]
async fn send_message(&mut self, author_id: &PlayerId, body: Str, created_at: DateTime<Utc>) -> Result<()> {
tracing::info!("Adding a message to room");
self.storage
.insert_message(
self.storage_id,
self.message_count,
&body,
&*author_id.as_inner(),
&created_at,
)
.await?;
2023-08-18 14:45:48 +00:00
self.message_count += 1;
let update = Updates::NewMessage {
room_id: self.room_id.clone(),
author_id: author_id.clone(),
body,
created_at,
};
self.broadcast_update(update, author_id).await;
2023-08-18 14:45:48 +00:00
Ok(())
}
/// Broadcasts an update to all players except the one who caused the update.
///
/// This is called after handling a client command.
/// Sending the update to the player who sent the command is handled by the player actor.
#[tracing::instrument(skip(self, update), name = "Room::broadcast_update")]
async fn broadcast_update(&self, update: Updates, except: &PlayerId) {
2023-09-06 14:34:20 +00:00
tracing::debug!("Broadcasting an update to {} subs", self.subscriptions.len());
for (player_id, sub) in &self.subscriptions {
if player_id == except {
continue;
}
log::info!("Sending a message from room to player");
sub.update(update.clone()).await;
}
2023-02-03 22:43:59 +00:00
}
}
2023-02-14 00:44:03 +00:00
2023-02-15 17:10:54 +00:00
#[derive(Serialize)]
2023-02-14 00:44:03 +00:00
pub struct RoomInfo {
pub id: RoomId,
pub members: Vec<PlayerId>,
2023-04-13 22:38:26 +00:00
pub topic: Str,
2023-02-14 00:44:03 +00:00
}