forked from jmug/stoatchat
feat: implement unoptimised filter_online if redis is not patched
This commit is contained in:
@@ -28,4 +28,4 @@ async-tungstenite = { version = "0.17.0", features = ["async-std-runtime"] }
|
|||||||
async-std = { version = "1.8.0", features = ["tokio1", "tokio02", "attributes"] }
|
async-std = { version = "1.8.0", features = ["tokio1", "tokio02", "attributes"] }
|
||||||
|
|
||||||
# core
|
# core
|
||||||
revolt-presence = { path = "../core/presence" }
|
revolt-presence = { path = "../core/presence", features = [ "redis-is-patched" ] }
|
||||||
|
|||||||
@@ -18,7 +18,7 @@ default = [ "serde", "from_database" ]
|
|||||||
[dependencies]
|
[dependencies]
|
||||||
# Repo
|
# Repo
|
||||||
revolt-database = { version = "0.0.2", path = "../database", optional = true }
|
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
|
# Serialisation
|
||||||
serde = { version = "1", features = ["derive"], optional = true }
|
serde = { version = "1", features = ["derive"], optional = true }
|
||||||
|
|||||||
@@ -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
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
|
[features]
|
||||||
|
redis-is-patched = []
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
# Async
|
# Async
|
||||||
async-std = { version = "1.8.0", features = ["attributes"] }
|
async-std = { version = "1.8.0", features = ["attributes"] }
|
||||||
|
|||||||
@@ -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
|
/// 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<String> {
|
pub async fn filter_online(user_ids: &'_ [String]) -> HashSet<String> {
|
||||||
// Ignore empty list immediately, to save time.
|
// Ignore empty list immediately, to save time.
|
||||||
let mut set = HashSet::new();
|
let mut set = HashSet::new();
|
||||||
@@ -121,9 +122,10 @@ pub async fn filter_online(user_ids: &'_ [String]) -> HashSet<String> {
|
|||||||
// Ok so, if this breaks, that means we've lost the Redis patch which adds SMISMEMBER
|
// 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
|
// Currently it's patched in through a forked repository, investigate what happen to it
|
||||||
let data: Vec<bool> = conn
|
let data: Vec<bool> = conn
|
||||||
.smismember("online", user_ids)
|
.smismember(ONLINE_SET, user_ids)
|
||||||
.await
|
.await
|
||||||
.expect("this shouldn't happen, please read this code! presence/mod.rs");
|
.expect("this shouldn't happen, please read this code! presence/mod.rs");
|
||||||
|
|
||||||
if data.is_empty() {
|
if data.is_empty() {
|
||||||
return set;
|
return set;
|
||||||
}
|
}
|
||||||
@@ -139,6 +141,25 @@ pub async fn filter_online(user_ids: &'_ [String]) -> HashSet<String> {
|
|||||||
set
|
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<String> {
|
||||||
|
if user_ids.is_empty() {
|
||||||
|
HashSet::new()
|
||||||
|
} else if let Ok(mut conn) = get_connection().await {
|
||||||
|
let members: Vec<String> = 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
|
/// Reset any stale presence data
|
||||||
pub async fn clear_region(region_id: Option<&str>) {
|
pub async fn clear_region(region_id: Option<&str>) {
|
||||||
let region_id = region_id.unwrap_or(&*REGION_KEY);
|
let region_id = region_id.unwrap_or(&*REGION_KEY);
|
||||||
|
|||||||
@@ -92,4 +92,4 @@ sentry = "0.25.0"
|
|||||||
|
|
||||||
# Core
|
# Core
|
||||||
revolt-result = { path = "../core/result", features = [ "serde", "schemas" ] }
|
revolt-result = { path = "../core/result", features = [ "serde", "schemas" ] }
|
||||||
revolt-presence = { path = "../core/presence" }
|
revolt-presence = { path = "../core/presence", features = [ "redis-is-patched" ] }
|
||||||
|
|||||||
Reference in New Issue
Block a user