move update_relay_configuration to account/relay.rs
Signed-off-by: kernelkind <kernelkind@gmail.com>
This commit is contained in:
@@ -2,13 +2,12 @@ use tracing::{debug, info};
|
|||||||
|
|
||||||
use crate::account::cache::AccountCache;
|
use crate::account::cache::AccountCache;
|
||||||
use crate::account::mute::AccountMutedData;
|
use crate::account::mute::AccountMutedData;
|
||||||
use crate::account::relay::{AccountRelayData, RelayDefaults};
|
use crate::account::relay::{update_relay_configuration, AccountRelayData, RelayDefaults};
|
||||||
use crate::storage::AccountStorageWriter;
|
use crate::storage::AccountStorageWriter;
|
||||||
use crate::user_account::UserAccountSerializable;
|
use crate::user_account::UserAccountSerializable;
|
||||||
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};
|
||||||
use std::collections::BTreeSet;
|
|
||||||
|
|
||||||
// TODO: remove this
|
// TODO: remove this
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
@@ -273,59 +272,6 @@ impl Accounts {
|
|||||||
changed
|
changed
|
||||||
}
|
}
|
||||||
|
|
||||||
fn update_relay_configuration(
|
|
||||||
&mut self,
|
|
||||||
pool: &mut RelayPool,
|
|
||||||
wakeup: impl Fn() + Send + Sync + Clone + 'static,
|
|
||||||
) {
|
|
||||||
debug!(
|
|
||||||
"updating relay configuration for currently selected {:?}",
|
|
||||||
self.cache.selected().key.pubkey.hex()
|
|
||||||
);
|
|
||||||
|
|
||||||
// If forced relays are set use them only
|
|
||||||
let mut desired_relays = self.relay_defaults.forced_relays.clone();
|
|
||||||
|
|
||||||
// Compose the desired relay lists from the selected account
|
|
||||||
if desired_relays.is_empty() {
|
|
||||||
let data = self.get_selected_account_data_mut();
|
|
||||||
desired_relays.extend(data.relay.local.iter().cloned());
|
|
||||||
desired_relays.extend(data.relay.advertised.iter().cloned());
|
|
||||||
}
|
|
||||||
|
|
||||||
// If no relays are specified at this point use the bootstrap list
|
|
||||||
if desired_relays.is_empty() {
|
|
||||||
desired_relays = self.relay_defaults.bootstrap_relays.clone();
|
|
||||||
}
|
|
||||||
|
|
||||||
debug!("current relays: {:?}", pool.urls());
|
|
||||||
debug!("desired relays: {:?}", desired_relays);
|
|
||||||
|
|
||||||
let pool_specs = pool
|
|
||||||
.urls()
|
|
||||||
.iter()
|
|
||||||
.map(|url| RelaySpec::new(url.clone(), false, false))
|
|
||||||
.collect();
|
|
||||||
let add: BTreeSet<RelaySpec> = desired_relays.difference(&pool_specs).cloned().collect();
|
|
||||||
let mut sub: BTreeSet<RelaySpec> =
|
|
||||||
pool_specs.difference(&desired_relays).cloned().collect();
|
|
||||||
if !add.is_empty() {
|
|
||||||
debug!("configuring added relays: {:?}", add);
|
|
||||||
let _ = pool.add_urls(add.iter().map(|r| r.url.clone()).collect(), wakeup);
|
|
||||||
}
|
|
||||||
if !sub.is_empty() {
|
|
||||||
// certain relays are persistent like the multicast relay,
|
|
||||||
// although we should probably have a way to explicitly
|
|
||||||
// disable it
|
|
||||||
sub.remove(&RelaySpec::new("multicast", false, false));
|
|
||||||
|
|
||||||
debug!("removing unwanted relays: {:?}", sub);
|
|
||||||
pool.remove_urls(&sub.iter().map(|r| r.url.clone()).collect());
|
|
||||||
}
|
|
||||||
|
|
||||||
debug!("current relays: {:?}", pool.urls());
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn update(&mut self, ndb: &mut Ndb, pool: &mut RelayPool, ctx: &egui::Context) {
|
pub fn update(&mut self, ndb: &mut Ndb, pool: &mut RelayPool, ctx: &egui::Context) {
|
||||||
// IMPORTANT - This function is called in the UI update loop,
|
// IMPORTANT - This function is called in the UI update loop,
|
||||||
// make sure it is fast when idle
|
// make sure it is fast when idle
|
||||||
@@ -333,11 +279,6 @@ impl Accounts {
|
|||||||
// On the initial update the relays need config even if nothing changes below
|
// On the initial update the relays need config even if nothing changes below
|
||||||
let mut need_reconfig = self.needs_relay_config;
|
let mut need_reconfig = self.needs_relay_config;
|
||||||
|
|
||||||
let ctx2 = ctx.clone();
|
|
||||||
let wakeup = move || {
|
|
||||||
ctx2.request_repaint();
|
|
||||||
};
|
|
||||||
|
|
||||||
// Do we need to deactivate any existing account subs?
|
// Do we need to deactivate any existing account subs?
|
||||||
|
|
||||||
let selected = self.cache.selected().key.pubkey;
|
let selected = self.cache.selected().key.pubkey;
|
||||||
@@ -370,7 +311,14 @@ impl Accounts {
|
|||||||
|
|
||||||
// If needed, update the relay configuration
|
// If needed, update the relay configuration
|
||||||
if need_reconfig {
|
if need_reconfig {
|
||||||
self.update_relay_configuration(pool, wakeup);
|
let acc = self.cache.selected();
|
||||||
|
update_relay_configuration(
|
||||||
|
pool,
|
||||||
|
&self.relay_defaults,
|
||||||
|
&acc.key.pubkey,
|
||||||
|
&acc.data,
|
||||||
|
create_wakeup(ctx),
|
||||||
|
);
|
||||||
self.needs_relay_config = false;
|
self.needs_relay_config = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -452,6 +400,13 @@ impl<'a> AccType<'a> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn create_wakeup(ctx: &egui::Context) -> impl Fn() + Send + Sync + Clone + 'static {
|
||||||
|
let ctx = ctx.clone();
|
||||||
|
move || {
|
||||||
|
ctx.request_repaint();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn add_account_from_storage(
|
fn add_account_from_storage(
|
||||||
cache: &mut AccountCache,
|
cache: &mut AccountCache,
|
||||||
ndb: &Ndb,
|
ndb: &Ndb,
|
||||||
|
|||||||
@@ -1,12 +1,12 @@
|
|||||||
use std::collections::BTreeSet;
|
use std::collections::BTreeSet;
|
||||||
|
|
||||||
use enostr::RelayPool;
|
use enostr::{Pubkey, RelayPool};
|
||||||
use nostrdb::{Filter, Ndb, NoteBuilder, NoteKey, Subscription, Transaction};
|
use nostrdb::{Filter, Ndb, NoteBuilder, NoteKey, Subscription, Transaction};
|
||||||
use tracing::{debug, error};
|
use tracing::{debug, error};
|
||||||
use url::Url;
|
use url::Url;
|
||||||
use uuid::Uuid;
|
use uuid::Uuid;
|
||||||
|
|
||||||
use crate::RelaySpec;
|
use crate::{AccountData, RelaySpec};
|
||||||
|
|
||||||
pub(crate) struct AccountRelayData {
|
pub(crate) struct AccountRelayData {
|
||||||
pub filter: Filter,
|
pub filter: Filter,
|
||||||
@@ -178,3 +178,56 @@ impl RelayDefaults {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub(super) fn update_relay_configuration(
|
||||||
|
pool: &mut RelayPool,
|
||||||
|
relay_defaults: &RelayDefaults,
|
||||||
|
pk: &Pubkey,
|
||||||
|
data: &AccountData,
|
||||||
|
wakeup: impl Fn() + Send + Sync + Clone + 'static,
|
||||||
|
) {
|
||||||
|
debug!(
|
||||||
|
"updating relay configuration for currently selected {:?}",
|
||||||
|
pk.hex()
|
||||||
|
);
|
||||||
|
|
||||||
|
// If forced relays are set use them only
|
||||||
|
let mut desired_relays = relay_defaults.forced_relays.clone();
|
||||||
|
|
||||||
|
// Compose the desired relay lists from the selected account
|
||||||
|
if desired_relays.is_empty() {
|
||||||
|
desired_relays.extend(data.relay.local.iter().cloned());
|
||||||
|
desired_relays.extend(data.relay.advertised.iter().cloned());
|
||||||
|
}
|
||||||
|
|
||||||
|
// If no relays are specified at this point use the bootstrap list
|
||||||
|
if desired_relays.is_empty() {
|
||||||
|
desired_relays = relay_defaults.bootstrap_relays.clone();
|
||||||
|
}
|
||||||
|
|
||||||
|
debug!("current relays: {:?}", pool.urls());
|
||||||
|
debug!("desired relays: {:?}", desired_relays);
|
||||||
|
|
||||||
|
let pool_specs = pool
|
||||||
|
.urls()
|
||||||
|
.iter()
|
||||||
|
.map(|url| RelaySpec::new(url.clone(), false, false))
|
||||||
|
.collect();
|
||||||
|
let add: BTreeSet<RelaySpec> = desired_relays.difference(&pool_specs).cloned().collect();
|
||||||
|
let mut sub: BTreeSet<RelaySpec> = pool_specs.difference(&desired_relays).cloned().collect();
|
||||||
|
if !add.is_empty() {
|
||||||
|
debug!("configuring added relays: {:?}", add);
|
||||||
|
let _ = pool.add_urls(add.iter().map(|r| r.url.clone()).collect(), wakeup);
|
||||||
|
}
|
||||||
|
if !sub.is_empty() {
|
||||||
|
// certain relays are persistent like the multicast relay,
|
||||||
|
// although we should probably have a way to explicitly
|
||||||
|
// disable it
|
||||||
|
sub.remove(&RelaySpec::new("multicast", false, false));
|
||||||
|
|
||||||
|
debug!("removing unwanted relays: {:?}", sub);
|
||||||
|
pool.remove_urls(&sub.iter().map(|r| r.url.clone()).collect());
|
||||||
|
}
|
||||||
|
|
||||||
|
debug!("current relays: {:?}", pool.urls());
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user