Refactor user ID filtering in WebSocket server: Enhanced the logic to only filter messages based on user_id if the target user ID is numerically valid. Updated comments for clarity on the filtering behavior and historical context regarding user ID handling.
This commit is contained in:
@@ -489,30 +489,37 @@ async fn handle_connection<S>(
|
|||||||
Err(_) => break,
|
Err(_) => break,
|
||||||
};
|
};
|
||||||
|
|
||||||
// Filter nach user_id, falls gesetzt
|
// Filter nach user_id, falls gesetzt und numerisch interpretierbar.
|
||||||
|
// Historisch wurde hier der Falukant-User (numerisch) verwendet.
|
||||||
|
// Wenn die gesetzte User-ID kein Integer ist (z.B. Benutzername),
|
||||||
|
// wird *nicht* gefiltert und alle Nachrichten durchgelassen –
|
||||||
|
// das Frontend muss dann selbst selektieren.
|
||||||
let target_user = {
|
let target_user = {
|
||||||
let guard = user_id_for_broker.lock().await;
|
let guard = user_id_for_broker.lock().await;
|
||||||
guard.clone()
|
guard.clone()
|
||||||
};
|
};
|
||||||
|
|
||||||
if let Some(uid) = target_user.clone() {
|
if let Some(uid) = target_user.clone() {
|
||||||
if let Ok(json) = serde_json::from_str::<Json>(&msg) {
|
// Nur filtern, wenn uid numerisch ist
|
||||||
let matches_user = json
|
if uid.parse::<i64>().is_ok() {
|
||||||
.get("user_id")
|
if let Ok(json) = serde_json::from_str::<Json>(&msg) {
|
||||||
.and_then(|v| {
|
let matches_user = json
|
||||||
if let Some(s) = v.as_str() {
|
.get("user_id")
|
||||||
Some(s.to_string())
|
.and_then(|v| {
|
||||||
} else if let Some(n) = v.as_i64() {
|
if let Some(s) = v.as_str() {
|
||||||
Some(n.to_string())
|
s.parse::<i64>().ok()
|
||||||
} else {
|
} else if let Some(n) = v.as_i64() {
|
||||||
None
|
Some(n)
|
||||||
}
|
} else {
|
||||||
})
|
None
|
||||||
.map(|v| v == uid)
|
}
|
||||||
.unwrap_or(false);
|
})
|
||||||
|
.map(|v| v.to_string() == uid)
|
||||||
|
.unwrap_or(false);
|
||||||
|
|
||||||
if !matches_user {
|
if !matches_user {
|
||||||
continue;
|
continue;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user