Compare commits

...

3 Commits

Author SHA1 Message Date
Mikhail 1aec44d4e7 Fix query 2024-05-07 16:27:06 +02:00
Mikhail 53adc93d25 Format 2024-05-07 16:13:53 +02:00
Mikhail 6f743584a8 Check if user is member 2024-05-07 16:11:39 +02:00
2 changed files with 26 additions and 1 deletions

View File

@ -1,8 +1,27 @@
use crate::player::PlayerConnection;
use anyhow::Result;
use crate::repo::Storage;
impl Storage {
pub async fn is_room_member(&self, room_id: u32, player_id: u32) -> Result<bool> {
let mut executor = self.conn.lock().await;
let res: (u32,) = sqlx::query_as(
"
select
count(*)
from
memberships
where
user_id = ? and room_id = ?;
",
)
.bind(player_id)
.bind(room_id)
.fetch_one(&mut *executor)
.await?;
Ok(res.0 > 0)
}
pub async fn add_room_member(&self, room_id: u32, player_id: u32) -> Result<()> {
let mut executor = self.conn.lock().await;
sqlx::query(

View File

@ -13,6 +13,7 @@ use crate::repo::Storage;
/// Opaque room id
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize)]
pub struct RoomId(Str);
impl RoomId {
pub fn from(str: impl Into<Str>) -> Result<RoomId> {
let bytes = str.into();
@ -35,6 +36,7 @@ impl RoomId {
/// Shared data structure for storing metadata about rooms.
#[derive(Clone)]
pub struct RoomRegistry(Arc<AsyncRwLock<RoomRegistryInner>>);
impl RoomRegistry {
pub fn new(metrics: &mut MetricRegistry, storage: Storage) -> Result<RoomRegistry> {
let metric_active_rooms = IntGauge::new("chat_rooms_active", "Number of alive room actors")?;
@ -125,6 +127,7 @@ impl RoomRegistryInner {
#[derive(Clone)]
pub struct RoomHandle(Arc<AsyncRwLock<Room>>);
impl RoomHandle {
pub async fn subscribe(&self, player_id: &PlayerId, player_handle: PlayerHandle) {
let mut lock = self.0.write().await;
@ -136,7 +139,9 @@ impl RoomHandle {
let mut lock = self.0.write().await;
tracing::info!("Adding a new member to a room");
let room_storage_id = lock.storage_id;
lock.storage.add_room_member(room_storage_id, player_storage_id).await.unwrap();
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(),
@ -207,6 +212,7 @@ struct Room {
topic: Str,
storage: Storage,
}
impl Room {
async fn send_message(&mut self, author_id: &PlayerId, body: Str) -> Result<()> {
tracing::info!("Adding a message to room");