diff --git a/crates/bonfire/Cargo.toml b/crates/bonfire/Cargo.toml index c7d4604d..6659d0e4 100644 --- a/crates/bonfire/Cargo.toml +++ b/crates/bonfire/Cargo.toml @@ -28,4 +28,4 @@ async-tungstenite = { version = "0.17.0", features = ["async-std-runtime"] } async-std = { version = "1.8.0", features = ["tokio1", "tokio02", "attributes"] } # core -revolt-presence = { path = "../core/presence" } +revolt-presence = { path = "../core/presence", features = [ "redis-is-patched" ] } diff --git a/crates/core/models/Cargo.toml b/crates/core/models/Cargo.toml index 87bf2972..216610fa 100644 --- a/crates/core/models/Cargo.toml +++ b/crates/core/models/Cargo.toml @@ -18,7 +18,7 @@ default = [ "serde", "from_database" ] [dependencies] # Repo revolt-database = { version = "0.0.2", path = "../database", optional = true } -revolt-presence = { version = "0.0.2", path = "../presence", optional = true } +revolt-presence = { version = "0.0.2", path = "../presence", optional = true, features = [ "redis-is-patched" ] } # Serialisation serde = { version = "1", features = ["derive"], optional = true } diff --git a/crates/core/presence/Cargo.toml b/crates/core/presence/Cargo.toml index 191fc726..54598d13 100644 --- a/crates/core/presence/Cargo.toml +++ b/crates/core/presence/Cargo.toml @@ -8,6 +8,9 @@ description = "Revolt Backend: User Presence" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html +[features] +redis-is-patched = [] + [dev-dependencies] # Async async-std = { version = "1.8.0", features = ["attributes"] } diff --git a/crates/core/presence/src/lib.rs b/crates/core/presence/src/lib.rs index 72418734..c510e494 100644 --- a/crates/core/presence/src/lib.rs +++ b/crates/core/presence/src/lib.rs @@ -94,6 +94,7 @@ pub async fn is_online(user_id: &str) -> bool { } /// Check whether a set of users is online, returns a set of the online user IDs +#[cfg(feature = "redis-is-patched")] pub async fn filter_online(user_ids: &'_ [String]) -> HashSet { // Ignore empty list immediately, to save time. let mut set = HashSet::new(); @@ -121,9 +122,10 @@ pub async fn filter_online(user_ids: &'_ [String]) -> HashSet { // Ok so, if this breaks, that means we've lost the Redis patch which adds SMISMEMBER // Currently it's patched in through a forked repository, investigate what happen to it let data: Vec = conn - .smismember("online", user_ids) + .smismember(ONLINE_SET, user_ids) .await .expect("this shouldn't happen, please read this code! presence/mod.rs"); + if data.is_empty() { return set; } @@ -139,6 +141,25 @@ pub async fn filter_online(user_ids: &'_ [String]) -> HashSet { set } +/// Check whether a set of users is online, returns a set of the online user IDs +#[cfg(not(feature = "redis-is-patched"))] +pub async fn filter_online(user_ids: &'_ [String]) -> HashSet { + if user_ids.is_empty() { + HashSet::new() + } else if let Ok(mut conn) = get_connection().await { + let members: Vec = conn.smembers(ONLINE_SET).await.unwrap_or_default(); + let members: HashSet<&String> = members.iter().collect(); + let user_ids: HashSet<&String> = user_ids.iter().collect(); + + members + .intersection(&user_ids) + .map(|x| x.to_string()) + .collect() + } else { + HashSet::new() + } +} + /// Reset any stale presence data pub async fn clear_region(region_id: Option<&str>) { let region_id = region_id.unwrap_or(&*REGION_KEY); diff --git a/crates/quark/Cargo.toml b/crates/quark/Cargo.toml index 06f14a96..8ebe7bb2 100644 --- a/crates/quark/Cargo.toml +++ b/crates/quark/Cargo.toml @@ -92,4 +92,4 @@ sentry = "0.25.0" # Core revolt-result = { path = "../core/result", features = [ "serde", "schemas" ] } -revolt-presence = { path = "../core/presence" } +revolt-presence = { path = "../core/presence", features = [ "redis-is-patched" ] }