move polling responsibility to AccountData
Signed-off-by: kernelkind <kernelkind@gmail.com>
This commit is contained in:
@@ -1,4 +1,3 @@
|
|||||||
use tracing::debug;
|
|
||||||
use uuid::Uuid;
|
use uuid::Uuid;
|
||||||
|
|
||||||
use crate::account::cache::AccountCache;
|
use crate::account::cache::AccountCache;
|
||||||
@@ -263,51 +262,31 @@ impl Accounts {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn poll_for_updates(&mut self, ndb: &Ndb) -> bool {
|
|
||||||
let mut changed = false;
|
|
||||||
let relay_sub = self.subs.relay.local;
|
|
||||||
let mute_sub = self.subs.mute.local;
|
|
||||||
let acc = self.get_selected_account_mut();
|
|
||||||
|
|
||||||
let nks = ndb.poll_for_notes(relay_sub, 1);
|
|
||||||
if !nks.is_empty() {
|
|
||||||
let txn = Transaction::new(ndb).expect("txn");
|
|
||||||
let relays = AccountRelayData::harvest_nip65_relays(ndb, &txn, &nks);
|
|
||||||
debug!(
|
|
||||||
"pubkey {}: updated relays {:?}",
|
|
||||||
acc.key.pubkey.hex(),
|
|
||||||
relays
|
|
||||||
);
|
|
||||||
acc.data.relay.advertised = relays.into_iter().collect();
|
|
||||||
changed = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
let nks = ndb.poll_for_notes(mute_sub, 1);
|
|
||||||
if !nks.is_empty() {
|
|
||||||
let txn = Transaction::new(ndb).expect("txn");
|
|
||||||
let muted = AccountMutedData::harvest_nip51_muted(ndb, &txn, &nks);
|
|
||||||
debug!("pubkey {}: updated muted {:?}", acc.key.pubkey.hex(), muted);
|
|
||||||
acc.data.muted.muted = Arc::new(muted);
|
|
||||||
changed = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
changed
|
|
||||||
}
|
|
||||||
|
|
||||||
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
|
||||||
|
|
||||||
// If needed, update the relay configuration
|
let Some(update) = self
|
||||||
if self.poll_for_updates(ndb) {
|
.cache
|
||||||
let acc = self.cache.selected();
|
.selected_mut()
|
||||||
update_relay_configuration(
|
.data
|
||||||
pool,
|
.poll_for_updates(ndb, &self.subs)
|
||||||
&self.relay_defaults,
|
else {
|
||||||
&acc.key.pubkey,
|
return;
|
||||||
&acc.data,
|
};
|
||||||
create_wakeup(ctx),
|
|
||||||
);
|
match update {
|
||||||
|
// If needed, update the relay configuration
|
||||||
|
AccountDataUpdate::Relay => {
|
||||||
|
let acc = self.cache.selected();
|
||||||
|
update_relay_configuration(
|
||||||
|
pool,
|
||||||
|
&self.relay_defaults,
|
||||||
|
&acc.key.pubkey,
|
||||||
|
&acc.data.relay,
|
||||||
|
create_wakeup(ctx),
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -328,7 +307,7 @@ impl Accounts {
|
|||||||
pool,
|
pool,
|
||||||
&self.relay_defaults,
|
&self.relay_defaults,
|
||||||
&acc.key.pubkey,
|
&acc.key.pubkey,
|
||||||
&acc.data,
|
&acc.data.relay,
|
||||||
create_wakeup(ctx),
|
create_wakeup(ctx),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@@ -405,12 +384,34 @@ pub struct AccountData {
|
|||||||
pub(crate) muted: AccountMutedData,
|
pub(crate) muted: AccountMutedData,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl AccountData {
|
||||||
|
pub(super) fn poll_for_updates(
|
||||||
|
&mut self,
|
||||||
|
ndb: &Ndb,
|
||||||
|
subs: &AccountSubs,
|
||||||
|
) -> Option<AccountDataUpdate> {
|
||||||
|
let txn = Transaction::new(ndb).expect("txn");
|
||||||
|
let mut resp = None;
|
||||||
|
if self.relay.poll_for_updates(ndb, &txn, subs.relay.local) {
|
||||||
|
resp = Some(AccountDataUpdate::Relay);
|
||||||
|
}
|
||||||
|
|
||||||
|
self.muted.poll_for_updates(ndb, &txn, subs.mute.local);
|
||||||
|
|
||||||
|
resp
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(super) enum AccountDataUpdate {
|
||||||
|
Relay,
|
||||||
|
}
|
||||||
|
|
||||||
pub struct AddAccountResponse {
|
pub struct AddAccountResponse {
|
||||||
pub switch_to: Pubkey,
|
pub switch_to: Pubkey,
|
||||||
pub unk_id_action: SingleUnkIdAction,
|
pub unk_id_action: SingleUnkIdAction,
|
||||||
}
|
}
|
||||||
|
|
||||||
struct AccountSubs {
|
pub(super) struct AccountSubs {
|
||||||
relay: UnifiedSubscription,
|
relay: UnifiedSubscription,
|
||||||
mute: UnifiedSubscription,
|
mute: UnifiedSubscription,
|
||||||
}
|
}
|
||||||
@@ -426,7 +427,7 @@ impl AccountSubs {
|
|||||||
) -> Self {
|
) -> Self {
|
||||||
let relay = subscribe(ndb, pool, &data.relay.filter);
|
let relay = subscribe(ndb, pool, &data.relay.filter);
|
||||||
let mute = subscribe(ndb, pool, &data.muted.filter);
|
let mute = subscribe(ndb, pool, &data.muted.filter);
|
||||||
update_relay_configuration(pool, relay_defaults, pk, data, wakeup);
|
update_relay_configuration(pool, relay_defaults, pk, &data.relay, wakeup);
|
||||||
|
|
||||||
Self { relay, mute }
|
Self { relay, mute }
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
|
|
||||||
use nostrdb::{Filter, Ndb, NoteKey, Transaction};
|
use nostrdb::{Filter, Ndb, NoteKey, Subscription, Transaction};
|
||||||
use tracing::{debug, error};
|
use tracing::{debug, error};
|
||||||
|
|
||||||
use crate::Muted;
|
use crate::Muted;
|
||||||
@@ -76,4 +76,16 @@ impl AccountMutedData {
|
|||||||
}
|
}
|
||||||
muted
|
muted
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub(super) fn poll_for_updates(&mut self, ndb: &Ndb, txn: &Transaction, sub: Subscription) {
|
||||||
|
let nks = ndb.poll_for_notes(sub, 1);
|
||||||
|
|
||||||
|
if nks.is_empty() {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
let muted = AccountMutedData::harvest_nip51_muted(ndb, txn, &nks);
|
||||||
|
debug!("updated muted {:?}", muted);
|
||||||
|
self.muted = Arc::new(muted);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
use std::collections::BTreeSet;
|
use std::collections::BTreeSet;
|
||||||
|
|
||||||
use enostr::{Keypair, Pubkey, RelayPool};
|
use enostr::{Keypair, Pubkey, RelayPool};
|
||||||
use nostrdb::{Filter, Ndb, NoteBuilder, NoteKey, Transaction};
|
use nostrdb::{Filter, Ndb, NoteBuilder, NoteKey, Subscription, Transaction};
|
||||||
use tracing::{debug, error, info};
|
use tracing::{debug, error, info};
|
||||||
use url::Url;
|
use url::Url;
|
||||||
|
|
||||||
@@ -106,6 +106,20 @@ impl AccountRelayData {
|
|||||||
let note = builder.sign(seckey).build().expect("note build");
|
let note = builder.sign(seckey).build().expect("note build");
|
||||||
pool.send(&enostr::ClientMessage::event(¬e).expect("note client message"));
|
pool.send(&enostr::ClientMessage::event(¬e).expect("note client message"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn poll_for_updates(&mut self, ndb: &Ndb, txn: &Transaction, sub: Subscription) -> bool {
|
||||||
|
let nks = ndb.poll_for_notes(sub, 1);
|
||||||
|
|
||||||
|
if nks.is_empty() {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
let relays = AccountRelayData::harvest_nip65_relays(ndb, txn, &nks);
|
||||||
|
debug!("updated relays {:?}", relays);
|
||||||
|
self.advertised = relays.into_iter().collect();
|
||||||
|
|
||||||
|
true
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) struct RelayDefaults {
|
pub(crate) struct RelayDefaults {
|
||||||
@@ -142,7 +156,7 @@ pub(super) fn update_relay_configuration(
|
|||||||
pool: &mut RelayPool,
|
pool: &mut RelayPool,
|
||||||
relay_defaults: &RelayDefaults,
|
relay_defaults: &RelayDefaults,
|
||||||
pk: &Pubkey,
|
pk: &Pubkey,
|
||||||
data: &AccountData,
|
data: &AccountRelayData,
|
||||||
wakeup: impl Fn() + Send + Sync + Clone + 'static,
|
wakeup: impl Fn() + Send + Sync + Clone + 'static,
|
||||||
) {
|
) {
|
||||||
debug!(
|
debug!(
|
||||||
@@ -155,8 +169,8 @@ pub(super) fn update_relay_configuration(
|
|||||||
|
|
||||||
// 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() {
|
||||||
desired_relays.extend(data.relay.local.iter().cloned());
|
desired_relays.extend(data.local.iter().cloned());
|
||||||
desired_relays.extend(data.relay.advertised.iter().cloned());
|
desired_relays.extend(data.advertised.iter().cloned());
|
||||||
}
|
}
|
||||||
|
|
||||||
// If no relays are specified at this point use the bootstrap list
|
// If no relays are specified at this point use the bootstrap list
|
||||||
|
|||||||
Reference in New Issue
Block a user