From 7ebe4dfaec1832ff585c264c32d9f5987db69d7d Mon Sep 17 00:00:00 2001 From: DanConwayDev Date: Tue, 16 Jul 2024 11:22:17 +0100 Subject: refactor(fetch): improve FetchRequest im preparation for identifying new inbox relays --- src/client.rs | 143 +++++++++++++++++++++------------------------------------- 1 file changed, 52 insertions(+), 91 deletions(-) (limited to 'src/client.rs') diff --git a/src/client.rs b/src/client.rs index 4054e7c..7c81f70 100644 --- a/src/client.rs +++ b/src/client.rs @@ -81,9 +81,7 @@ pub trait Connect { async fn fetch_all_from_relay( &self, git_repo_path: &Path, - relay_url: Url, request: FetchRequest, - // progress_reporter: &MultiProgress, pb: &Option, ) -> Result; } @@ -284,7 +282,6 @@ impl Connect for Client { Ok((relay_results, progress_reporter)) } - #[allow(clippy::too_many_lines)] async fn fetch_all( &self, git_repo_path: &Path, @@ -297,11 +294,12 @@ impl Connect for Client { fallback_relays.insert(url); } } - let (relays, request) = + let request = create_relays_request(git_repo_path, repo_coordinates, fallback_relays).await?; + let progress_reporter = MultiProgress::new(); - for relay in &relays { + for relay in &request.relays { self.client .add_relay(relay.as_str()) .await @@ -310,24 +308,27 @@ impl Connect for Client { let dim = Style::new().color256(247); - let futures: Vec<_> = relays + let futures: Vec<_> = request + .relays .iter() // don't look for events on blaster .filter(|r| !r.as_str().contains("nostr.mutinywallet.com")) - .map(|r| (r.clone(), request.clone())) - .map(|(relay, request)| async { + .map(|r| FetchRequest { + selected_relay: Some(r.clone()), + ..request.clone() + }) + .map(|request| async { let relay_column_width = request.relay_column_width; + let relay_url = request + .selected_relay + .clone() + .context("fetch_all_from_relay called without a relay")?; + let pb = if std::env::var("NGITTEST").is_err() { let pb = progress_reporter.add( ProgressBar::new(1) - .with_prefix( - dim.apply_to(format!( - "{: { if let Some(pb) = pb { pb.set_style(pb_after_style(false)); pb.set_prefix( - dim.apply_to(format!( - "{: { - if let Some(pb) = pb { - pb.set_style(pb_after_style(true)); - pb.set_prefix( - dim.apply_to(format!( - "{: Ok(res), } }) .collect(); @@ -395,9 +368,9 @@ impl Connect for Client { let report = consolidate_fetch_reports(relay_reports); if report.to_string().is_empty() { - println!("no updates found"); + println!("no updates"); } else { - println!("fetched updates: {report}"); + println!("updates: {report}"); } Ok(report) } @@ -405,9 +378,7 @@ impl Connect for Client { async fn fetch_all_from_relay( &self, git_repo_path: &Path, - relay_url: Url, request: FetchRequest, - // progress_reporter: &MultiProgress, pb: &Option, ) -> Result { let mut fresh_coordinates: HashSet = HashSet::new(); @@ -417,27 +388,17 @@ impl Connect for Client { let mut fresh_proposal_roots = request.proposals.clone(); let mut fresh_authors = request.contributor_profiles.clone(); - let mut report = FetchReport { - relay: Some(relay_url.clone()), - ..Default::default() - }; + let mut report = FetchReport::default(); - // let pb = if std::env::var("NGITTEST").is_err() { - // let pb = progress_reporter.add( - // ProgressBar::new(1) - // .with_prefix(format!("{: <11}{}", "connecting", relay_url)) - // .with_style(pb_style()?), - // ); - // pb.enable_steady_tick(Duration::from_millis(300)); - // Some(pb) - // } else { - // None - // }; - - self.connect(&relay_url).await?; + let relay_url = request + .selected_relay + .clone() + .context("fetch_all_from_relay called without a relay")?; let relay_column_width = request.relay_column_width; + self.connect(&relay_url).await?; + let dim = Style::new().color256(247); loop { @@ -447,12 +408,12 @@ impl Connect for Client { if let Some(pb) = &pb { pb.set_prefix( dim.apply_to(format!( - "{: , fallback_relays: HashSet, -) -> Result<(HashSet, FetchRequest)> { +) -> Result { let repo_ref = get_repo_ref_from_cache(git_repo_path, repo_coordinates).await; let relays = { @@ -834,20 +795,19 @@ async fn create_relays_request( } existing_events }; - Ok(( + Ok(FetchRequest { + selected_relay: None, relays, - FetchRequest { - relay_column_width, - repo_coordinates: if let Ok(repo_ref) = repo_ref { - repo_ref.coordinates_with_timestamps() - } else { - repo_coordinates.iter().map(|c| (c.clone(), None)).collect() - }, - proposals, - contributor_profiles, - existing_events, + relay_column_width, + repo_coordinates: if let Ok(repo_ref) = repo_ref { + repo_ref.coordinates_with_timestamps() + } else { + repo_coordinates.iter().map(|c| (c.clone(), None)).collect() }, - )) + proposals, + contributor_profiles, + existing_events, + }) } async fn process_fetched_event( @@ -1027,7 +987,6 @@ pub fn get_filter_repo_events(repo_coordinates: &HashSet) -> nostr:: #[derive(Default)] pub struct FetchReport { - relay: Option, repo_coordinates: Vec, updated_repo_announcements: Vec<(Coordinate, Timestamp)>, proposals: HashSet, @@ -1101,6 +1060,8 @@ impl Display for FetchReport { #[derive(Default, Clone)] pub struct FetchRequest { + relays: HashSet, + selected_relay: Option, relay_column_width: usize, repo_coordinates: Vec<(Coordinate, Option)>, proposals: HashSet, -- cgit v1.2.3