Skip to content

Commit

Permalink
Merge additional account relay list improvements from Ken
Browse files Browse the repository at this point in the history
Ken Sedgwick (1):
      additional account relay list improvements
  • Loading branch information
jb55 committed Jan 26, 2025
2 parents 8ad9ad2 + 1e0801f commit 0c3db9a
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 18 deletions.
65 changes: 56 additions & 9 deletions crates/notedeck/src/accounts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,10 @@ impl Accounts {
}
}

pub fn needs_relay_config(&mut self) {
self.needs_relay_config = true;
}

fn contains_account(&self, pubkey: &[u8; 32]) -> Option<ContainsAccount> {
for (index, account) in self.accounts.iter().enumerate() {
let has_pubkey = account.pubkey.bytes() == pubkey;
Expand Down Expand Up @@ -422,6 +426,15 @@ impl Accounts {
}
}

pub fn get_selected_account_data(&self) -> Option<&AccountData> {
if let Some(account) = self.get_selected_account() {
if let Some(account_data) = self.account_data.get(account.pubkey.bytes()) {
return Some(account_data);
}
}
None
}

pub fn select_account(&mut self, index: usize) {
if let Some(account) = self.accounts.get(index) {
self.currently_selected_account = Some(index);
Expand Down Expand Up @@ -532,12 +545,17 @@ impl Accounts {
pool: &mut RelayPool,
wakeup: impl Fn() + Send + Sync + Clone + 'static,
) {
debug!(
"updating relay configuration for currently selected account {:?}",
self.currently_selected_account
);

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

// Compose the desired relay lists from the accounts
// Compose the desired relay lists from the selected account
if desired_relays.is_empty() {
for data in self.account_data.values() {
if let Some(data) = self.get_selected_account_data() {
desired_relays.extend(data.relay.local.iter().cloned());
desired_relays.extend(data.relay.advertised.iter().cloned());
}
Expand Down Expand Up @@ -621,9 +639,17 @@ impl Accounts {
None
}

pub fn add_advertised_relay(&mut self, relay_to_add: &str, pool: &mut RelayPool) {
let relay_to_add = AccountRelayData::canonicalize_url(relay_to_add);
info!("add advertised relay \"{}\"", relay_to_add);
fn modify_advertised_relays(
&mut self,
relay_url: &str,
pool: &mut RelayPool,
action: RelayAction,
) {
let relay_url = AccountRelayData::canonicalize_url(relay_url);
match action {
RelayAction::Add => info!("add advertised relay \"{}\"", relay_url),
RelayAction::Remove => info!("remove advertised relay \"{}\"", relay_url),
}
match self.currently_selected_account {
None => error!("no account is currently selected."),
Some(index) => match self.accounts.get(index) {
Expand All @@ -635,13 +661,21 @@ impl Accounts {
Some(account_data) => {
let advertised = &mut account_data.relay.advertised;
if advertised.is_empty() {
// If the selected account has no advertised relays
// iniitialize with the bootstrapping set.
// If the selected account has no advertised relays,
// initialize with the bootstrapping set.
advertised.extend(self.bootstrap_relays.iter().cloned());
}
advertised.insert(RelaySpec::new(relay_to_add, false, false));
match action {
RelayAction::Add => {
advertised.insert(RelaySpec::new(relay_url, false, false));
}
RelayAction::Remove => {
advertised.remove(&RelaySpec::new(relay_url, false, false));
}
}
self.needs_relay_config = true;
// If we have the secret key publish the nip-65 relay list

// If we have the secret key publish the NIP-65 relay list
if let Some(secretkey) = &keypair.secret_key {
account_data
.relay
Expand All @@ -653,6 +687,19 @@ impl Accounts {
},
}
}

pub fn add_advertised_relay(&mut self, relay_to_add: &str, pool: &mut RelayPool) {
self.modify_advertised_relays(relay_to_add, pool, RelayAction::Add);
}

pub fn remove_advertised_relay(&mut self, relay_to_remove: &str, pool: &mut RelayPool) {
self.modify_advertised_relays(relay_to_remove, pool, RelayAction::Remove);
}
}

enum RelayAction {
Add,
Remove,
}

fn get_selected_index(accounts: &[UserAccount], keystore: &KeyStorageType) -> Option<usize> {
Expand Down
2 changes: 1 addition & 1 deletion crates/notedeck_columns/src/accounts/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ pub fn process_accounts_view_response(
router.route_to(Route::add_account());
}
}

accounts.needs_relay_config();
selection
}

Expand Down
1 change: 1 addition & 0 deletions crates/notedeck_columns/src/relay_pool_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ impl<'a> RelayPoolManager<'a> {
indices.iter().for_each(|index| self.remove_relay(*index));
}

// FIXME - this is not ever called?
pub fn add_relay(&mut self, ctx: &egui::Context, relay_url: String) {
let _ = self.pool.add_url(relay_url, create_wakeup(ctx));
}
Expand Down
16 changes: 8 additions & 8 deletions crates/notedeck_columns/src/ui/relay.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,9 @@ impl View for RelayView<'_> {
.scroll_bar_visibility(egui::scroll_area::ScrollBarVisibility::AlwaysHidden)
.auto_shrink([false; 2])
.show(ui, |ui| {
if let Some(indices) = self.show_relays(ui) {
self.manager.remove_relays(indices);
if let Some(relay_to_remove) = self.show_relays(ui) {
self.accounts
.remove_advertised_relay(&relay_to_remove, self.manager.pool);
}
ui.add_space(8.0);
if let Some(relay_to_add) = self.show_add_relay_ui(ui) {
Expand Down Expand Up @@ -66,9 +67,9 @@ impl<'a> RelayView<'a> {
egui::CentralPanel::default().show(ui.ctx(), |ui| self.ui(ui));
}

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

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

show_connection_status(ui, relay_info.status);
Expand All @@ -115,8 +116,7 @@ impl<'a> RelayView<'a> {
});
});
}

indices_to_remove
relay_to_remove
}

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

0 comments on commit 0c3db9a

Please sign in to comment.