Skip to content

Commit 0c3db9a

Browse files
committed
Merge additional account relay list improvements from Ken
Ken Sedgwick (1): additional account relay list improvements
2 parents 8ad9ad2 + 1e0801f commit 0c3db9a

File tree

4 files changed

+66
-18
lines changed

4 files changed

+66
-18
lines changed

crates/notedeck/src/accounts.rs

Lines changed: 56 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -343,6 +343,10 @@ impl Accounts {
343343
}
344344
}
345345

346+
pub fn needs_relay_config(&mut self) {
347+
self.needs_relay_config = true;
348+
}
349+
346350
fn contains_account(&self, pubkey: &[u8; 32]) -> Option<ContainsAccount> {
347351
for (index, account) in self.accounts.iter().enumerate() {
348352
let has_pubkey = account.pubkey.bytes() == pubkey;
@@ -422,6 +426,15 @@ impl Accounts {
422426
}
423427
}
424428

429+
pub fn get_selected_account_data(&self) -> Option<&AccountData> {
430+
if let Some(account) = self.get_selected_account() {
431+
if let Some(account_data) = self.account_data.get(account.pubkey.bytes()) {
432+
return Some(account_data);
433+
}
434+
}
435+
None
436+
}
437+
425438
pub fn select_account(&mut self, index: usize) {
426439
if let Some(account) = self.accounts.get(index) {
427440
self.currently_selected_account = Some(index);
@@ -532,12 +545,17 @@ impl Accounts {
532545
pool: &mut RelayPool,
533546
wakeup: impl Fn() + Send + Sync + Clone + 'static,
534547
) {
548+
debug!(
549+
"updating relay configuration for currently selected account {:?}",
550+
self.currently_selected_account
551+
);
552+
535553
// If forced relays are set use them only
536554
let mut desired_relays = self.forced_relays.clone();
537555

538-
// Compose the desired relay lists from the accounts
556+
// Compose the desired relay lists from the selected account
539557
if desired_relays.is_empty() {
540-
for data in self.account_data.values() {
558+
if let Some(data) = self.get_selected_account_data() {
541559
desired_relays.extend(data.relay.local.iter().cloned());
542560
desired_relays.extend(data.relay.advertised.iter().cloned());
543561
}
@@ -621,9 +639,17 @@ impl Accounts {
621639
None
622640
}
623641

624-
pub fn add_advertised_relay(&mut self, relay_to_add: &str, pool: &mut RelayPool) {
625-
let relay_to_add = AccountRelayData::canonicalize_url(relay_to_add);
626-
info!("add advertised relay \"{}\"", relay_to_add);
642+
fn modify_advertised_relays(
643+
&mut self,
644+
relay_url: &str,
645+
pool: &mut RelayPool,
646+
action: RelayAction,
647+
) {
648+
let relay_url = AccountRelayData::canonicalize_url(relay_url);
649+
match action {
650+
RelayAction::Add => info!("add advertised relay \"{}\"", relay_url),
651+
RelayAction::Remove => info!("remove advertised relay \"{}\"", relay_url),
652+
}
627653
match self.currently_selected_account {
628654
None => error!("no account is currently selected."),
629655
Some(index) => match self.accounts.get(index) {
@@ -635,13 +661,21 @@ impl Accounts {
635661
Some(account_data) => {
636662
let advertised = &mut account_data.relay.advertised;
637663
if advertised.is_empty() {
638-
// If the selected account has no advertised relays
639-
// iniitialize with the bootstrapping set.
664+
// If the selected account has no advertised relays,
665+
// initialize with the bootstrapping set.
640666
advertised.extend(self.bootstrap_relays.iter().cloned());
641667
}
642-
advertised.insert(RelaySpec::new(relay_to_add, false, false));
668+
match action {
669+
RelayAction::Add => {
670+
advertised.insert(RelaySpec::new(relay_url, false, false));
671+
}
672+
RelayAction::Remove => {
673+
advertised.remove(&RelaySpec::new(relay_url, false, false));
674+
}
675+
}
643676
self.needs_relay_config = true;
644-
// If we have the secret key publish the nip-65 relay list
677+
678+
// If we have the secret key publish the NIP-65 relay list
645679
if let Some(secretkey) = &keypair.secret_key {
646680
account_data
647681
.relay
@@ -653,6 +687,19 @@ impl Accounts {
653687
},
654688
}
655689
}
690+
691+
pub fn add_advertised_relay(&mut self, relay_to_add: &str, pool: &mut RelayPool) {
692+
self.modify_advertised_relays(relay_to_add, pool, RelayAction::Add);
693+
}
694+
695+
pub fn remove_advertised_relay(&mut self, relay_to_remove: &str, pool: &mut RelayPool) {
696+
self.modify_advertised_relays(relay_to_remove, pool, RelayAction::Remove);
697+
}
698+
}
699+
700+
enum RelayAction {
701+
Add,
702+
Remove,
656703
}
657704

658705
fn get_selected_index(accounts: &[UserAccount], keystore: &KeyStorageType) -> Option<usize> {

crates/notedeck_columns/src/accounts/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ pub fn process_accounts_view_response(
9797
router.route_to(Route::add_account());
9898
}
9999
}
100-
100+
accounts.needs_relay_config();
101101
selection
102102
}
103103

crates/notedeck_columns/src/relay_pool_manager.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ impl<'a> RelayPoolManager<'a> {
4141
indices.iter().for_each(|index| self.remove_relay(*index));
4242
}
4343

44+
// FIXME - this is not ever called?
4445
pub fn add_relay(&mut self, ctx: &egui::Context, relay_url: String) {
4546
let _ = self.pool.add_url(relay_url, create_wakeup(ctx));
4647
}

crates/notedeck_columns/src/ui/relay.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,9 @@ impl View for RelayView<'_> {
3737
.scroll_bar_visibility(egui::scroll_area::ScrollBarVisibility::AlwaysHidden)
3838
.auto_shrink([false; 2])
3939
.show(ui, |ui| {
40-
if let Some(indices) = self.show_relays(ui) {
41-
self.manager.remove_relays(indices);
40+
if let Some(relay_to_remove) = self.show_relays(ui) {
41+
self.accounts
42+
.remove_advertised_relay(&relay_to_remove, self.manager.pool);
4243
}
4344
ui.add_space(8.0);
4445
if let Some(relay_to_add) = self.show_add_relay_ui(ui) {
@@ -66,9 +67,9 @@ impl<'a> RelayView<'a> {
6667
egui::CentralPanel::default().show(ui.ctx(), |ui| self.ui(ui));
6768
}
6869

69-
/// Show the current relays, and returns the indices of relays the user requested to delete
70-
fn show_relays(&'a self, ui: &mut Ui) -> Option<Vec<usize>> {
71-
let mut indices_to_remove: Option<Vec<usize>> = None;
70+
/// Show the current relays and return a relay the user selected to delete
71+
fn show_relays(&'a self, ui: &mut Ui) -> Option<String> {
72+
let mut relay_to_remove = None;
7273
for (index, relay_info) in self.manager.get_relay_infos().iter().enumerate() {
7374
ui.add_space(8.0);
7475
ui.vertical_centered_justified(|ui| {
@@ -106,7 +107,7 @@ impl<'a> RelayView<'a> {
106107

107108
ui.with_layout(Layout::right_to_left(Align::Center), |ui| {
108109
if ui.add(delete_button(ui.visuals().dark_mode)).clicked() {
109-
indices_to_remove.get_or_insert_with(Vec::new).push(index);
110+
relay_to_remove = Some(relay_info.relay_url.to_string());
110111
};
111112

112113
show_connection_status(ui, relay_info.status);
@@ -115,8 +116,7 @@ impl<'a> RelayView<'a> {
115116
});
116117
});
117118
}
118-
119-
indices_to_remove
119+
relay_to_remove
120120
}
121121

122122
const RELAY_PREFILL: &'static str = "wss://";

0 commit comments

Comments
 (0)