mirror of
https://github.com/scsibug/nostr-rs-relay.git
synced 2024-11-14 23:19:07 -05:00
111 lines
3.2 KiB
Rust
111 lines
3.2 KiB
Rust
//! Client connection state
|
|
use crate::close::Close;
|
|
use crate::error::Error;
|
|
use crate::error::Result;
|
|
use crate::event::Event;
|
|
|
|
use crate::subscription::Subscription;
|
|
use std::collections::HashMap;
|
|
use tracing::{debug, info};
|
|
use uuid::Uuid;
|
|
|
|
/// A subscription identifier has a maximum length
|
|
const MAX_SUBSCRIPTION_ID_LEN: usize = 256;
|
|
|
|
/// State for a client connection
|
|
pub struct ClientConn {
|
|
/// Unique client identifier generated at connection time
|
|
client_id: Uuid,
|
|
/// The current set of active client subscriptions
|
|
subscriptions: HashMap<String, Subscription>,
|
|
/// Per-connection maximum concurrent subscriptions
|
|
max_subs: usize,
|
|
}
|
|
|
|
impl Default for ClientConn {
|
|
fn default() -> Self {
|
|
Self::new()
|
|
}
|
|
}
|
|
|
|
impl ClientConn {
|
|
/// Create a new, empty connection state.
|
|
#[must_use]
|
|
pub fn new() -> Self {
|
|
let client_id = Uuid::new_v4();
|
|
ClientConn {
|
|
client_id,
|
|
subscriptions: HashMap::new(),
|
|
max_subs: 32,
|
|
}
|
|
}
|
|
|
|
/// Get a short prefix of the client's unique identifier, suitable
|
|
/// for logging.
|
|
#[must_use]
|
|
pub fn get_client_prefix(&self) -> String {
|
|
self.client_id.to_string().chars().take(8).collect()
|
|
}
|
|
|
|
/// Find all matching subscriptions.
|
|
#[must_use]
|
|
pub fn get_matching_subscriptions(&self, e: &Event) -> Vec<&str> {
|
|
let mut v: Vec<&str> = vec![];
|
|
for (id, sub) in &self.subscriptions {
|
|
if sub.interested_in_event(e) {
|
|
v.push(id);
|
|
}
|
|
}
|
|
v
|
|
}
|
|
|
|
/// Add a new subscription for this connection.
|
|
/// # Errors
|
|
///
|
|
/// Will return `Err` if the client has too many subscriptions, or
|
|
/// if the provided name is excessively long.
|
|
pub fn subscribe(&mut self, s: Subscription) -> Result<()> {
|
|
let k = s.get_id();
|
|
let sub_id_len = k.len();
|
|
// prevent arbitrarily long subscription identifiers from
|
|
// being used.
|
|
if sub_id_len > MAX_SUBSCRIPTION_ID_LEN {
|
|
info!(
|
|
"ignoring sub request with excessive length: ({})",
|
|
sub_id_len
|
|
);
|
|
return Err(Error::SubIdMaxLengthError);
|
|
}
|
|
// check if an existing subscription exists, and replace if so
|
|
if self.subscriptions.contains_key(&k) {
|
|
self.subscriptions.remove(&k);
|
|
self.subscriptions.insert(k, s);
|
|
debug!("replaced existing subscription");
|
|
return Ok(());
|
|
}
|
|
|
|
// check if there is room for another subscription.
|
|
if self.subscriptions.len() >= self.max_subs {
|
|
return Err(Error::SubMaxExceededError);
|
|
}
|
|
// add subscription
|
|
self.subscriptions.insert(k, s);
|
|
debug!(
|
|
"registered new subscription, currently have {} active subs",
|
|
self.subscriptions.len()
|
|
);
|
|
Ok(())
|
|
}
|
|
|
|
/// Remove the subscription for this connection.
|
|
pub fn unsubscribe(&mut self, c: &Close) {
|
|
// TODO: return notice if subscription did not exist.
|
|
self.subscriptions.remove(&c.id);
|
|
debug!(
|
|
"removed subscription, currently have {} active subs (cid={})",
|
|
self.subscriptions.len(),
|
|
self.client_id
|
|
);
|
|
}
|
|
}
|