add RelayDefaults

Signed-off-by: kernelkind <kernelkind@gmail.com>
This commit is contained in:
kernelkind
2025-06-25 16:29:40 -04:00
parent 41e141d9a9
commit 320dedc8bd
2 changed files with 39 additions and 23 deletions

View File

@@ -1,7 +1,7 @@
use tracing::{debug, error, info}; use tracing::{debug, error, info};
use crate::account::mute::AccountMutedData; use crate::account::mute::AccountMutedData;
use crate::account::relay::AccountRelayData; use crate::account::relay::{AccountRelayData, RelayDefaults};
use crate::{AccountStorage, MuteFun, RelaySpec, SingleUnkIdAction, UnknownIds, UserAccount}; use crate::{AccountStorage, MuteFun, RelaySpec, SingleUnkIdAction, UnknownIds, UserAccount};
use enostr::{ClientMessage, FilledKeypair, Keypair, Pubkey, RelayPool}; use enostr::{ClientMessage, FilledKeypair, Keypair, Pubkey, RelayPool};
use nostrdb::{Ndb, Note, Transaction}; use nostrdb::{Ndb, Note, Transaction};
@@ -18,8 +18,7 @@ pub struct Accounts {
accounts: Vec<UserAccount>, accounts: Vec<UserAccount>,
key_store: Option<AccountStorage>, key_store: Option<AccountStorage>,
account_data: BTreeMap<[u8; 32], AccountData>, account_data: BTreeMap<[u8; 32], AccountData>,
forced_relays: BTreeSet<RelaySpec>, relay_defaults: RelayDefaults,
bootstrap_relays: BTreeSet<RelaySpec>,
needs_relay_config: bool, needs_relay_config: bool,
fallback: Option<Pubkey>, fallback: Option<Pubkey>,
} }
@@ -44,29 +43,15 @@ impl Accounts {
}; };
let account_data = BTreeMap::new(); let account_data = BTreeMap::new();
let forced_relays: BTreeSet<RelaySpec> = forced_relays
.into_iter() let relay_defaults = RelayDefaults::new(forced_relays);
.map(|u| RelaySpec::new(AccountRelayData::canonicalize_url(&u), false, false))
.collect();
let bootstrap_relays = [
"wss://relay.damus.io",
// "wss://pyramid.fiatjaf.com", // Uncomment if needed
"wss://nos.lol",
"wss://nostr.wine",
"wss://purplepag.es",
]
.iter()
.map(|&url| url.to_string())
.map(|u| RelaySpec::new(AccountRelayData::canonicalize_url(&u), false, false))
.collect();
Accounts { Accounts {
currently_selected_account, currently_selected_account,
accounts, accounts,
key_store, key_store,
account_data, account_data,
forced_relays, relay_defaults,
bootstrap_relays,
needs_relay_config: true, needs_relay_config: true,
fallback: None, fallback: None,
} }
@@ -424,7 +409,7 @@ impl Accounts {
); );
// If forced relays are set use them only // If forced relays are set use them only
let mut desired_relays = self.forced_relays.clone(); let mut desired_relays = self.relay_defaults.forced_relays.clone();
// Compose the desired relay lists from the selected account // Compose the desired relay lists from the selected account
if desired_relays.is_empty() { if desired_relays.is_empty() {
@@ -436,7 +421,7 @@ impl Accounts {
// If no relays are specified at this point use the bootstrap list // If no relays are specified at this point use the bootstrap list
if desired_relays.is_empty() { if desired_relays.is_empty() {
desired_relays = self.bootstrap_relays.clone(); desired_relays = self.relay_defaults.bootstrap_relays.clone();
} }
debug!("current relays: {:?}", pool.urls()); debug!("current relays: {:?}", pool.urls());
@@ -575,7 +560,8 @@ impl Accounts {
if advertised.is_empty() { if advertised.is_empty() {
// If the selected account has no advertised relays, // If the selected account has no advertised relays,
// initialize with the bootstrapping set. // initialize with the bootstrapping set.
advertised.extend(self.bootstrap_relays.iter().cloned()); advertised
.extend(self.relay_defaults.bootstrap_relays.iter().cloned());
} }
match action { match action {
RelayAction::Add => { RelayAction::Add => {

View File

@@ -149,3 +149,33 @@ impl AccountRelayData {
pool.send(&enostr::ClientMessage::event(&note).expect("note client message")); pool.send(&enostr::ClientMessage::event(&note).expect("note client message"));
} }
} }
pub(crate) struct RelayDefaults {
pub forced_relays: BTreeSet<RelaySpec>,
pub bootstrap_relays: BTreeSet<RelaySpec>,
}
impl RelayDefaults {
pub(crate) fn new(forced_relays: Vec<String>) -> Self {
let forced_relays: BTreeSet<RelaySpec> = forced_relays
.into_iter()
.map(|u| RelaySpec::new(AccountRelayData::canonicalize_url(&u), false, false))
.collect();
let bootstrap_relays = [
"wss://relay.damus.io",
// "wss://pyramid.fiatjaf.com", // Uncomment if needed
"wss://nos.lol",
"wss://nostr.wine",
"wss://purplepag.es",
]
.iter()
.map(|&url| url.to_string())
.map(|u| RelaySpec::new(AccountRelayData::canonicalize_url(&u), false, false))
.collect();
Self {
forced_relays,
bootstrap_relays,
}
}
}