Files
notedeck/crates/notedeck_columns/src/relay_pool_manager.rs
William Casarin fe6206c546 Note multicasting
This is an initial implementation of note multicast, which sends posted
notes to other notedecks on the same network.

This came about after I nerd sniped myself thinking about p2p nostr on
local networks[1]

You can test this exclusively without joining any other relays by
passing -r multicast on the command line.

[1] https://damus.io/note1j50pseqwma38g3aqrsnhvld0m0ysdgppw6fjnvvcj0haeulgswgq80lpca

Signed-off-by: William Casarin <jb55@jb55.com>
2025-01-04 10:37:11 -08:00

55 lines
1.5 KiB
Rust

use enostr::RelayPool;
pub use enostr::RelayStatus;
/// The interface to a RelayPool for UI components.
/// Represents all user-facing operations that can be performed for a user's relays
pub struct RelayPoolManager<'a> {
pub pool: &'a mut RelayPool,
}
pub struct RelayInfo<'a> {
pub relay_url: &'a str,
pub status: RelayStatus,
}
impl<'a> RelayPoolManager<'a> {
pub fn new(pool: &'a mut RelayPool) -> Self {
RelayPoolManager { pool }
}
pub fn get_relay_infos(&self) -> Vec<RelayInfo> {
self.pool
.relays
.iter()
.map(|relay| RelayInfo {
relay_url: relay.url(),
status: relay.status(),
})
.collect()
}
/// index of the Vec<RelayInfo> from get_relay_infos
pub fn remove_relay(&mut self, index: usize) {
if index < self.pool.relays.len() {
self.pool.relays.remove(index);
}
}
/// removes all specified relay indicies shown in get_relay_infos
pub fn remove_relays(&mut self, mut indices: Vec<usize>) {
indices.sort_unstable_by(|a, b| b.cmp(a));
indices.iter().for_each(|index| self.remove_relay(*index));
}
pub fn add_relay(&mut self, ctx: &egui::Context, relay_url: String) {
let _ = self.pool.add_url(relay_url, create_wakeup(ctx));
}
}
pub fn create_wakeup(ctx: &egui::Context) -> impl Fn() + Send + Sync + Clone + 'static {
let ctx = ctx.clone();
move || {
ctx.request_repaint();
}
}