@@ -343,6 +343,10 @@ impl Accounts {
343
343
}
344
344
}
345
345
346
+ pub fn needs_relay_config ( & mut self ) {
347
+ self . needs_relay_config = true ;
348
+ }
349
+
346
350
fn contains_account ( & self , pubkey : & [ u8 ; 32 ] ) -> Option < ContainsAccount > {
347
351
for ( index, account) in self . accounts . iter ( ) . enumerate ( ) {
348
352
let has_pubkey = account. pubkey . bytes ( ) == pubkey;
@@ -422,6 +426,15 @@ impl Accounts {
422
426
}
423
427
}
424
428
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
+
425
438
pub fn select_account ( & mut self , index : usize ) {
426
439
if let Some ( account) = self . accounts . get ( index) {
427
440
self . currently_selected_account = Some ( index) ;
@@ -532,12 +545,17 @@ impl Accounts {
532
545
pool : & mut RelayPool ,
533
546
wakeup : impl Fn ( ) + Send + Sync + Clone + ' static ,
534
547
) {
548
+ debug ! (
549
+ "updating relay configuration for currently selected account {:?}" ,
550
+ self . currently_selected_account
551
+ ) ;
552
+
535
553
// If forced relays are set use them only
536
554
let mut desired_relays = self . forced_relays . clone ( ) ;
537
555
538
- // Compose the desired relay lists from the accounts
556
+ // Compose the desired relay lists from the selected account
539
557
if desired_relays. is_empty ( ) {
540
- for data in self . account_data . values ( ) {
558
+ if let Some ( data) = self . get_selected_account_data ( ) {
541
559
desired_relays. extend ( data. relay . local . iter ( ) . cloned ( ) ) ;
542
560
desired_relays. extend ( data. relay . advertised . iter ( ) . cloned ( ) ) ;
543
561
}
@@ -621,9 +639,17 @@ impl Accounts {
621
639
None
622
640
}
623
641
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
+ }
627
653
match self . currently_selected_account {
628
654
None => error ! ( "no account is currently selected." ) ,
629
655
Some ( index) => match self . accounts . get ( index) {
@@ -635,13 +661,21 @@ impl Accounts {
635
661
Some ( account_data) => {
636
662
let advertised = & mut account_data. relay . advertised ;
637
663
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.
640
666
advertised. extend ( self . bootstrap_relays . iter ( ) . cloned ( ) ) ;
641
667
}
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
+ }
643
676
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
645
679
if let Some ( secretkey) = & keypair. secret_key {
646
680
account_data
647
681
. relay
@@ -653,6 +687,19 @@ impl Accounts {
653
687
} ,
654
688
}
655
689
}
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 ,
656
703
}
657
704
658
705
fn get_selected_index ( accounts : & [ UserAccount ] , keystore : & KeyStorageType ) -> Option < usize > {
0 commit comments