Skip to content

Commit

Permalink
prune account code; don't manage pool, deprecate local and forced relays
Browse files Browse the repository at this point in the history
- local relays are very doable, but not sure if we want them
- forced relays want to be implemented in the pool/subman and need
  more definition
  • Loading branch information
ksedgwic committed Feb 23, 2025
1 parent 5b720d5 commit 2ecde85
Showing 1 changed file with 7 additions and 79 deletions.
86 changes: 7 additions & 79 deletions crates/notedeck/src/accounts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ pub enum AccountsAction {
pub struct AccountRelayData {
filter: Filter,
subrmtid: Option<RmtId>,
local: BTreeSet<RelaySpec>, // used locally but not advertised
_local: BTreeSet<RelaySpec>, // used locally but not advertised
advertised: Arc<Mutex<BTreeSet<RelaySpec>>>, // advertised via NIP-65
}

Expand Down Expand Up @@ -82,7 +82,7 @@ impl AccountRelayData {
AccountRelayData {
filter,
subrmtid: None,
local: BTreeSet::new(),
_local: BTreeSet::new(),
advertised: Arc::new(Mutex::new(relays.into_iter().collect())),
}
}
Expand Down Expand Up @@ -343,13 +343,13 @@ pub struct Accounts {
accounts: Vec<UserAccount>,
key_store: KeyStorageType,
account_data: BTreeMap<[u8; 32], AccountData>,
forced_relays: BTreeSet<RelaySpec>,
_forced_relays: BTreeSet<RelaySpec>,
bootstrap_relays: BTreeSet<RelaySpec>,
needs_relay_config: bool,
}

impl Accounts {
pub fn new(key_store: KeyStorageType, forced_relays: Vec<String>) -> Self {
pub fn new(key_store: KeyStorageType, _forced_relays: Vec<String>) -> Self {
let accounts = if let KeyStorageResponse::ReceivedResult(res) = key_store.get_keys() {
res.unwrap_or_default()
} else {
Expand All @@ -358,7 +358,7 @@ impl Accounts {

let currently_selected_account = get_selected_index(&accounts, &key_store);
let account_data = BTreeMap::new();
let forced_relays: BTreeSet<RelaySpec> = forced_relays
let _forced_relays: BTreeSet<RelaySpec> = _forced_relays
.into_iter()
.map(|u| RelaySpec::new(AccountRelayData::canonicalize_url(&u), false, false))
.collect();
Expand All @@ -379,7 +379,7 @@ impl Accounts {
accounts,
key_store,
account_data,
forced_relays,
_forced_relays,
bootstrap_relays,
needs_relay_config: true,
}
Expand Down Expand Up @@ -592,61 +592,6 @@ impl Accounts {
self.account_data.remove(pubkey);
}

fn update_relay_configuration(
&mut self,
pool: &mut RelayPool,
wakeup: impl Fn() + Send + Sync + Clone + 'static,
) {
debug!(
"updating relay configuration for currently selected {:?}",
self.currently_selected_account
.map(|i| hex::encode(self.accounts.get(i).unwrap().pubkey.bytes()))
);

// If forced relays are set use them only
let mut desired_relays = self.forced_relays.clone();

// Compose the desired relay lists from the selected account
if desired_relays.is_empty() {
if let Some(data) = self.get_selected_account_data() {
desired_relays.extend(data.relay.local.iter().cloned());
desired_relays.extend(data.relay.advertised.lock().unwrap().iter().cloned());
}
}

// If no relays are specified at this point use the bootstrap list
if desired_relays.is_empty() {
desired_relays = self.bootstrap_relays.clone();
}

debug!("current relays: {:?}", pool.urls());
debug!("desired relays: {:?}", desired_relays);

let pool_specs = pool
.urls()
.iter()
.map(|url| RelaySpec::new(url.clone(), false, false))
.collect();
let add: BTreeSet<RelaySpec> = desired_relays.difference(&pool_specs).cloned().collect();
let mut sub: BTreeSet<RelaySpec> =
pool_specs.difference(&desired_relays).cloned().collect();
if !add.is_empty() {
debug!("configuring added relays: {:?}", add);
let _ = pool.add_urls(add.iter().map(|r| r.url.clone()).collect(), wakeup);
}
if !sub.is_empty() {
// certain relays are persistent like the multicast relay,
// although we should probably have a way to explicitly
// disable it
sub.remove(&RelaySpec::new("multicast", false, false));

debug!("removing unwanted relays: {:?}", sub);
pool.remove_urls(&sub.iter().map(|r| r.url.clone()).collect());
}

debug!("current relays: {:?}", pool.urls());
}

fn get_combined_relays(
&self,
data_option: Option<&AccountData>,
Expand Down Expand Up @@ -688,18 +633,10 @@ impl Accounts {
self.get_combined_relays(self.get_selected_account_data(), |_| true)
}

pub fn update(&mut self, subman: &mut SubMan, ctx: &egui::Context) {
pub fn update(&mut self, subman: &mut SubMan, _ctx: &egui::Context) {
// IMPORTANT - This function is called in the UI update loop,
// make sure it is fast when idle

// On the initial update the relays need config even if nothing changes below
let mut need_reconfig = self.needs_relay_config;

let ctx2 = ctx.clone();
let wakeup = move || {
ctx2.request_repaint();
};

// Do we need to deactivate any existing account subs?
for (ndx, account) in self.accounts.iter().enumerate() {
if Some(ndx) != self.currently_selected_account {
Expand All @@ -718,23 +655,14 @@ impl Accounts {
}

let ndb = subman.ndb().clone();
let pool = subman.pool();

// Were any accounts added or removed?
let (added, removed) = self.delta_accounts();
for pk in added {
self.handle_added_account(&ndb, &pk);
need_reconfig = true;
}
for pk in removed {
self.handle_removed_account(&pk);
need_reconfig = true;
}

// If needed, update the relay configuration
if need_reconfig {
self.update_relay_configuration(pool, wakeup);
self.needs_relay_config = false;
}

// Do we need to activate account subs?
Expand Down

0 comments on commit 2ecde85

Please sign in to comment.