notedeck

One damus client to rule them all
git clone git://jb55.com/notedeck
Log | Files | Refs | README | LICENSE

relay_pool_manager.rs (1713B)


      1 use enostr::RelayPool;
      2 pub use enostr::RelayStatus;
      3 
      4 /// The interface to a RelayPool for UI components.
      5 /// Represents all user-facing operations that can be performed for a user's relays
      6 pub struct RelayPoolManager<'a> {
      7     pub pool: &'a mut RelayPool,
      8 }
      9 
     10 pub struct RelayInfo<'a> {
     11     pub relay_url: &'a str,
     12     pub status: RelayStatus,
     13 }
     14 
     15 impl<'a> RelayPoolManager<'a> {
     16     pub fn new(pool: &'a mut RelayPool) -> Self {
     17         RelayPoolManager { pool }
     18     }
     19 
     20     pub fn get_relay_infos(&self) -> Vec<RelayInfo> {
     21         self.pool
     22             .relays
     23             .iter()
     24             .map(|relay| RelayInfo {
     25                 relay_url: relay.url(),
     26                 status: relay.status(),
     27             })
     28             .collect()
     29     }
     30 
     31     /// index of the Vec<RelayInfo> from get_relay_infos
     32     pub fn remove_relay(&mut self, index: usize) {
     33         if index < self.pool.relays.len() {
     34             self.pool.relays.remove(index);
     35         }
     36     }
     37 
     38     /// removes all specified relay indicies shown in get_relay_infos
     39     pub fn remove_relays(&mut self, mut indices: Vec<usize>) {
     40         indices.sort_unstable_by(|a, b| b.cmp(a));
     41         indices.iter().for_each(|index| self.remove_relay(*index));
     42     }
     43 
     44     // FIXME - this is not ever called?
     45     pub fn add_relay(&mut self, ctx: &egui::Context, relay_url: String) {
     46         let _ = self.pool.add_url(relay_url, create_wakeup(ctx));
     47     }
     48 
     49     /// check whether a relay url is valid
     50     pub fn is_valid_relay(&self, url: &str) -> bool {
     51         self.pool.is_valid_url(url)
     52     }
     53 }
     54 
     55 pub fn create_wakeup(ctx: &egui::Context) -> impl Fn() + Send + Sync + Clone + 'static {
     56     let ctx = ctx.clone();
     57     move || {
     58         ctx.request_repaint();
     59     }
     60 }