use autoincrement for IDs
This commit is contained in:
+21
-20
@@ -5,19 +5,19 @@ use tokio::sync::{Mutex, broadcast};
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct PresenceUser {
|
||||
pub user_id: String,
|
||||
pub user_id: i64,
|
||||
pub display_name: String,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub enum HubEvent {
|
||||
ListChanged { list_id: String, revision: i64 },
|
||||
PresenceChanged { list_id: String },
|
||||
ListChanged { list_id: i64, revision: i64 },
|
||||
PresenceChanged { list_id: i64 },
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
struct ConnectionInfo {
|
||||
user_id: String,
|
||||
user_id: i64,
|
||||
display_name: String,
|
||||
}
|
||||
|
||||
@@ -34,18 +34,18 @@ pub struct Subscription {
|
||||
|
||||
#[derive(Clone, Default)]
|
||||
pub struct Hub {
|
||||
rooms: Arc<Mutex<HashMap<String, Room>>>,
|
||||
rooms: Arc<Mutex<HashMap<i64, Room>>>,
|
||||
}
|
||||
|
||||
impl Hub {
|
||||
pub async fn join(
|
||||
&self,
|
||||
list_id: String,
|
||||
user_id: String,
|
||||
list_id: i64,
|
||||
user_id: i64,
|
||||
display_name: String,
|
||||
) -> Subscription {
|
||||
let mut rooms = self.rooms.lock().await;
|
||||
let room = rooms.entry(list_id.clone()).or_insert_with(|| {
|
||||
let room = rooms.entry(list_id).or_insert_with(|| {
|
||||
let (sender, _) = broadcast::channel(64);
|
||||
Room {
|
||||
sender,
|
||||
@@ -79,10 +79,10 @@ impl Hub {
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn leave(&self, list_id: &str, connection_id: &str) {
|
||||
pub async fn leave(&self, list_id: i64, connection_id: &str) {
|
||||
let mut rooms = self.rooms.lock().await;
|
||||
let mut remove_room = false;
|
||||
if let Some(room) = rooms.get_mut(list_id) {
|
||||
if let Some(room) = rooms.get_mut(&list_id) {
|
||||
let removed = room.connections.remove(connection_id);
|
||||
if let Some(removed) = removed {
|
||||
let still_present = room
|
||||
@@ -90,19 +90,17 @@ impl Hub {
|
||||
.values()
|
||||
.any(|connection| connection.user_id == removed.user_id);
|
||||
if !still_present {
|
||||
let _ = room.sender.send(HubEvent::PresenceChanged {
|
||||
list_id: list_id.to_owned(),
|
||||
});
|
||||
let _ = room.sender.send(HubEvent::PresenceChanged { list_id });
|
||||
}
|
||||
}
|
||||
remove_room = room.connections.is_empty();
|
||||
}
|
||||
if remove_room {
|
||||
rooms.remove(list_id);
|
||||
rooms.remove(&list_id);
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn publish_list_changed(&self, list_id: String, revision: i64) {
|
||||
pub async fn publish_list_changed(&self, list_id: i64, revision: i64) {
|
||||
let rooms = self.rooms.lock().await;
|
||||
if let Some(room) = rooms.get(&list_id) {
|
||||
let _ = room
|
||||
@@ -111,19 +109,22 @@ impl Hub {
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn presence(&self, list_id: &str) -> Vec<PresenceUser> {
|
||||
pub async fn presence(&self, list_id: i64) -> Vec<PresenceUser> {
|
||||
let rooms = self.rooms.lock().await;
|
||||
rooms.get(list_id).map(current_presence).unwrap_or_default()
|
||||
rooms
|
||||
.get(&list_id)
|
||||
.map(current_presence)
|
||||
.unwrap_or_default()
|
||||
}
|
||||
}
|
||||
|
||||
fn current_presence(room: &Room) -> Vec<PresenceUser> {
|
||||
let mut users = HashMap::<String, PresenceUser>::new();
|
||||
let mut users = HashMap::<i64, PresenceUser>::new();
|
||||
for connection in room.connections.values() {
|
||||
users
|
||||
.entry(connection.user_id.clone())
|
||||
.entry(connection.user_id)
|
||||
.or_insert_with(|| PresenceUser {
|
||||
user_id: connection.user_id.clone(),
|
||||
user_id: connection.user_id,
|
||||
display_name: connection.display_name.clone(),
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user