Files
vega/src-tauri/src/relay/sub.rs
Jure e3f5020eeb Add embedded Nostr relay with catch-up sync on startup
Embedded relay (strfry-lite in Rust) stores events locally in SQLite.
On startup, syncs user notes, follow feed (24h), mentions (7d), and
profile/contacts from remote relays into the local relay. Uses since
timestamp for incremental syncs. Toggle in Settings with event count
and DB size display. Add webkit2gtk GPU acceleration workaround and
connectToRelays safety timeout for NDK hang.
2026-04-01 12:10:11 +02:00

52 lines
1.3 KiB
Rust

use crate::relay::event::Event;
use crate::relay::filter::Filter;
use std::collections::HashMap;
pub struct Subscription {
pub id: String,
pub filters: Vec<Filter>,
}
impl Subscription {
/// Returns true if the event matches any filter in this subscription.
pub fn matches(&self, event: &Event) -> bool {
self.filters.iter().any(|f| f.matches(event))
}
}
/// Per-connection subscription state.
pub struct SubscriptionMap {
subs: HashMap<String, Subscription>,
}
impl SubscriptionMap {
pub fn new() -> Self {
Self {
subs: HashMap::new(),
}
}
/// Add or replace a subscription. Returns the old one if replaced.
pub fn upsert(&mut self, id: String, filters: Vec<Filter>) -> Option<Subscription> {
let sub = Subscription {
id: id.clone(),
filters,
};
self.subs.insert(id, sub)
}
/// Remove a subscription by ID.
pub fn remove(&mut self, id: &str) -> Option<Subscription> {
self.subs.remove(id)
}
/// Get all subscription IDs that match the given event.
pub fn matching_sub_ids(&self, event: &Event) -> Vec<String> {
self.subs
.values()
.filter(|sub| sub.matches(event))
.map(|sub| sub.id.clone())
.collect()
}
}