add Accounts::add_advertised_relay

This commit is contained in:
Ken Sedgwick
2025-01-21 16:03:44 -08:00
parent e436be400e
commit fe3e2dad14
3 changed files with 45 additions and 7 deletions

View File

@@ -589,6 +589,33 @@ impl Accounts {
None
}
pub fn add_advertised_relay(&mut self, relay_to_add: &str) {
info!("add advertised relay \"{}\"", relay_to_add);
match self.currently_selected_account {
None => error!("no account is currently selected."),
Some(index) => match self.accounts.get(index) {
None => error!("selected account index {} is out of range.", index),
Some(keypair) => {
let key_bytes: [u8; 32] = *keypair.pubkey.bytes();
match self.account_data.get_mut(&key_bytes) {
None => error!("no account data found for the provided key."),
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.
advertised.extend(self.bootstrap_relays.iter().cloned());
}
advertised.insert(relay_to_add.to_string());
self.needs_relay_config = true;
// FIXME - need to publish the advertised set
}
}
}
},
}
}
}
fn get_selected_index(accounts: &[UserAccount], keystore: &KeyStorageType) -> Option<usize> {

View File

@@ -291,7 +291,7 @@ fn render_nav_body(
}
Route::Relays => {
let manager = RelayPoolManager::new(ctx.pool);
RelayView::new(manager, &mut app.view_state.id_string_map).ui(ui);
RelayView::new(ctx.accounts, manager, &mut app.view_state.id_string_map).ui(ui);
None
}
Route::ComposeNote => {

View File

@@ -6,7 +6,7 @@ use crate::ui::{Preview, PreviewConfig, View};
use egui::{Align, Button, Frame, Id, Image, Layout, Margin, Rgba, RichText, Rounding, Ui, Vec2};
use enostr::RelayPool;
use notedeck::NotedeckTextStyle;
use notedeck::{Accounts, NotedeckTextStyle};
use tracing::debug;
@@ -14,6 +14,7 @@ use super::add_column::sized_button;
use super::padding;
pub struct RelayView<'a> {
accounts: &'a mut Accounts,
manager: RelayPoolManager<'a>,
id_string_map: &'a mut HashMap<Id, String>,
}
@@ -40,16 +41,21 @@ impl View for RelayView<'_> {
self.manager.remove_relays(indices);
}
ui.add_space(8.0);
if let Some(add_relay) = self.show_add_relay_ui(ui) {
debug!("add relay \"{}\"", add_relay);
if let Some(relay_to_add) = self.show_add_relay_ui(ui) {
self.accounts.add_advertised_relay(&relay_to_add);
}
});
}
}
impl<'a> RelayView<'a> {
pub fn new(manager: RelayPoolManager<'a>, id_string_map: &'a mut HashMap<Id, String>) -> Self {
pub fn new(
accounts: &'a mut Accounts,
manager: RelayPoolManager<'a>,
id_string_map: &'a mut HashMap<Id, String>,
) -> Self {
RelayView {
accounts,
manager,
id_string_map,
}
@@ -278,10 +284,15 @@ mod preview {
}
impl App for RelayViewPreview {
fn update(&mut self, _app: &mut AppContext<'_>, ui: &mut egui::Ui) {
fn update(&mut self, app: &mut AppContext<'_>, ui: &mut egui::Ui) {
self.pool.try_recv();
let mut id_string_map = HashMap::new();
RelayView::new(RelayPoolManager::new(&mut self.pool), &mut id_string_map).ui(ui);
RelayView::new(
app.accounts,
RelayPoolManager::new(&mut self.pool),
&mut id_string_map,
)
.ui(ui);
}
}