Skip to content

Commit

Permalink
refactor track_user_relays for clarity
Browse files Browse the repository at this point in the history
  • Loading branch information
ksedgwic committed Oct 21, 2024
1 parent befc1fb commit 88f52d5
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 36 deletions.
2 changes: 1 addition & 1 deletion src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -479,7 +479,7 @@ fn update_damus(damus: &mut Damus, ctx: &egui::Context) {

let damusref = damus.reference();
tokio::spawn(async move {
task::setup_user_relays(damusref).await;
task::track_user_relays(damusref).await;
});
}

Expand Down
74 changes: 39 additions & 35 deletions src/task.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,60 +8,64 @@ use crate::dispatcher;
use crate::note::NoteRef;
use crate::{with_mut_damus, DamusRef};

pub async fn setup_user_relays(damusref: DamusRef) {
debug!("do_setup_user_relays starting");
pub async fn track_user_relays(damusref: DamusRef) {
debug!("track_user_relays starting");

let filter = with_mut_damus(&damusref, |damus| {
debug!("setup_user_relays: acquired damus for filter");
let filter = user_relay_filter(&damusref);

let account = damus
.accounts
.get_selected_account()
.as_ref()
.map(|a| a.pubkey.bytes())
.expect("selected account");

// NIP-65
Filter::new()
.authors([account])
.kinds([10002])
.limit(1)
.build()
});

let mut sub = with_mut_damus(&damusref, |damus| {
debug!("setup_user_relays: acquired damus for query + subscribe");
// Do we have a user relay list stored in nostrdb? Start with that ...
with_mut_damus(&damusref, |damus| {
let txn = Transaction::new(&damus.ndb).expect("transaction");
let relays = query_nip65_relays(&damus.ndb, &txn, &filter);
debug!("setup_user_relays: query #1 relays: {:#?}", relays);
debug!("track_user_relays: initial from nostrdb: {:#?}", relays);
set_relays(&mut damus.pool, relays);
});

// Add a relay subscription to the pool
// Subscribe to user relay list updates
let mut sub = with_mut_damus(&damusref, |damus| {
dispatcher::subscribe(damus, &[filter.clone()], 10).expect("subscribe")
});
debug!("setup_user_relays: sub {}", sub.id);
debug!("track_user_relays: sub {}", sub.id);

// Track user relay list updates
loop {
match sub.receiver.next().await {
Some(ev) => {
debug!("setup_user_relays: saw {:?}", ev);
with_mut_damus(&damusref, |damus| {
let txn = Transaction::new(&damus.ndb).expect("transaction");
let relays = query_nip65_relays(&damus.ndb, &txn, &filter);
debug!("setup_user_relays: query #2 relays: {:#?}", relays);
set_relays(&mut damus.pool, relays);
})
}
Some(_ev) => with_mut_damus(&damusref, |damus| {
let txn = Transaction::new(&damus.ndb).expect("transaction");
let relays = query_nip65_relays(&damus.ndb, &txn, &filter);
debug!("track_user_relays update: {:#?}", relays);
set_relays(&mut damus.pool, relays);
}),
None => {
debug!("setup_user_relays: saw None");
debug!("track_user_relays: saw None");
break;
}
}
}

debug!("do_setup_user_relays finished");
// Should only get here if the channel is closed
debug!("track_user_relays finished");
}

fn user_relay_filter(damusref: &DamusRef) -> Filter {
with_mut_damus(&damusref, |damus| {

Check failure on line 51 in src/task.rs

View workflow job for this annotation

GitHub Actions / Clippy

this expression creates a reference which is immediately dereferenced by the compiler
let account = damus
.accounts
.get_selected_account()
.as_ref()
.map(|a| a.pubkey.bytes())
.expect("selected account");

// NIP-65
Filter::new()
.authors([account])
.kinds([10002])
.limit(1)
.build()
})
}

// useful for debugging
fn _query_note_json(ndb: &Ndb, txn: &Transaction, filter: &Filter) -> Vec<String> {
let lim = filter.limit().unwrap_or(crate::filter::default_limit()) as i32;
let results = ndb
Expand Down

0 comments on commit 88f52d5

Please sign in to comment.