core: use members instead of subscribers in RoomInfo

This commit is contained in:
Nikita Vilunov 2024-06-03 12:53:55 +02:00
parent e48a6d3b0b
commit d0420ec834
2 changed files with 18 additions and 2 deletions

View File

@ -2,6 +2,7 @@ use anyhow::{anyhow, Result};
use chrono::{DateTime, Utc}; use chrono::{DateTime, Utc};
use sqlx::FromRow; use sqlx::FromRow;
use crate::player::PlayerId;
use crate::repo::Storage; use crate::repo::Storage;
use crate::room::{RoomId, StoredMessage}; use crate::room::{RoomId, StoredMessage};
@ -212,4 +213,18 @@ impl Storage {
res.into_iter().map(|(room_id,)| RoomId::try_from(room_id)).collect() res.into_iter().map(|(room_id,)| RoomId::try_from(room_id)).collect()
} }
pub async fn get_users_of_a_room(&self, room_id: u32) -> Result<Vec<PlayerId>> {
let mut executor = self.conn.lock().await;
let res: Vec<(String,)> = sqlx::query_as(
"select u.name
from memberships m inner join users u on m.user_id = u.id
where m.room_id = ?;",
)
.bind(room_id)
.fetch_all(&mut *executor)
.await?;
Ok(res.into_iter()).and_then(|iter| iter.map(|(user_id,)| PlayerId::from(user_id)).collect())
}
} }

View File

@ -112,11 +112,12 @@ impl RoomRegistryInner {
Ok(Some(room_handle.clone())) Ok(Some(room_handle.clone()))
} else if let Some(stored_room) = services.storage.retrieve_room_by_name(&*room_id.0).await? { } else if let Some(stored_room) = services.storage.retrieve_room_by_name(&*room_id.0).await? {
log::debug!("Loading room {}...", &room_id.0); log::debug!("Loading room {}...", &room_id.0);
let users = services.storage.get_users_of_a_room(stored_room.id).await?;
let room = Room { let room = Room {
storage_id: stored_room.id, storage_id: stored_room.id,
room_id: room_id.clone(), room_id: room_id.clone(),
subscriptions: HashMap::new(), subscriptions: HashMap::new(),
members: HashSet::new(), // TODO load members from storage members: HashSet::from_iter(users.into_iter()),
topic: stored_room.topic.into(), topic: stored_room.topic.into(),
message_count: stored_room.message_count, message_count: stored_room.message_count,
}; };
@ -198,7 +199,7 @@ impl RoomHandle {
let lock = self.0.read().await; let lock = self.0.read().await;
RoomInfo { RoomInfo {
id: lock.room_id.clone(), id: lock.room_id.clone(),
members: lock.subscriptions.keys().map(|x| x.clone()).collect::<Vec<_>>(), members: lock.members.iter().map(|x| x.clone()).collect::<Vec<_>>(),
topic: lock.topic.clone(), topic: lock.topic.clone(),
} }
} }