From d2478dbca6c5d3f61331ceabe20c6d9315cd6840 Mon Sep 17 00:00:00 2001 From: DanConwayDev Date: Tue, 3 Dec 2024 15:29:06 +0000 Subject: refactor: limit fetch to 1 trusted coordinate previously fetch supported fetching multiple trusted coordinates at once. The additional complexity trade off isn't worth it. It will still fetch events related to the coordinates of maintainers that the trusted maintainer lists. A scenario where there might be multiple trusted coordinates is where a large repo is split into multiple nostr repos to manage pull requests and issues in seperate sub-units. I might therefore have different remotes that target different identifiers. There also might be different set of maintainers groups that are have different views on the latest state of the same codebase and they haven't split off into using different identifiers. Its simplier ngit to fetch these different nostr repos independantly when requested. git-remote-nostr will do this automatically as it works through remotes but for ngit cmds a further change is required so `get_repo_coordinates` and `try_and_get_repo_coordinates` prompts to select the desired one. --- src/lib/client.rs | 86 +++++++++++++++++++++++++++++------------------- src/lib/git/nostr_url.rs | 72 +++++++++++++++++++--------------------- src/lib/login/user.rs | 6 +--- src/lib/repo_ref.rs | 23 +++++++++---- 4 files changed, 103 insertions(+), 84 deletions(-) (limited to 'src/lib') diff --git a/src/lib/client.rs b/src/lib/client.rs index 4b87cd8..32f5bd7 100644 --- a/src/lib/client.rs +++ b/src/lib/client.rs @@ -89,7 +89,7 @@ pub trait Connect { async fn fetch_all<'a>( &self, git_repo_path: Option<&'a Path>, - repo_coordinates: &HashSet, + repo_coordinates: Option<&'a Coordinate>, user_profiles: &HashSet, ) -> Result<(Vec>, MultiProgress)>; async fn fetch_all_from_relay<'a>( @@ -329,7 +329,7 @@ impl Connect for Client { async fn fetch_all<'a>( &self, git_repo_path: Option<&'a Path>, - repo_coordinates: &HashSet, + trusted_maintainer_coordinate: Option<&'a Coordinate>, user_profiles: &HashSet, ) -> Result<(Vec>, MultiProgress)> { let fallback_relays = &self @@ -340,7 +340,7 @@ impl Connect for Client { let mut request = create_relays_request( git_repo_path, - repo_coordinates, + trusted_maintainer_coordinate, user_profiles, fallback_relays.clone(), ) @@ -468,8 +468,12 @@ impl Connect for Client { } processed_relays.extend(relays.clone()); - if let Ok(repo_ref) = get_repo_ref_from_cache(git_repo_path, repo_coordinates).await { - request.repo_relays = repo_ref.relays.iter().cloned().collect(); + if let Some(trusted_maintainer_coordinate) = trusted_maintainer_coordinate { + if let Ok(repo_ref) = + get_repo_ref_from_cache(git_repo_path, trusted_maintainer_coordinate).await + { + request.repo_relays = repo_ref.relays.iter().cloned().collect(); + } } request.user_relays_for_profiles = { @@ -809,18 +813,24 @@ pub async fn save_event_in_global_cache( pub async fn get_repo_ref_from_cache( git_repo_path: Option<&Path>, - repo_coordinates: &HashSet, + repo_coordinate: &Coordinate, ) -> Result { let mut maintainers = HashSet::new(); let mut new_coordinate: bool; - for c in repo_coordinates { - maintainers.insert(c.public_key); - } + maintainers.insert(repo_coordinate.public_key); let mut repo_events = vec![]; loop { new_coordinate = false; - let repo_events_filter = get_filter_repo_events(repo_coordinates); + let repo_events_filter = + get_filter_repo_events(&HashSet::from_iter(maintainers.iter().map(|m| { + Coordinate { + kind: Kind::GitRepoAnnouncement, + public_key: *m, + identifier: repo_coordinate.identifier.to_string(), + relays: vec![], + } + }))); let events = [ get_event_from_global_cache(git_repo_path, vec![repo_events_filter.clone()]).await?, @@ -832,13 +842,7 @@ pub async fn get_repo_ref_from_cache( ] .concat(); for e in events { - if let Ok(repo_ref) = RepoRef::try_from(( - e.clone(), - repo_coordinates - .iter() - .next() - .map(|coordinate| coordinate.public_key), - )) { + if let Ok(repo_ref) = RepoRef::try_from((e.clone(), None)) { for m in repo_ref.maintainers { if maintainers.insert(m) { new_coordinate = true; @@ -857,10 +861,7 @@ pub async fn get_repo_ref_from_cache( .first() .context("no repo announcement event found at specified coordinates. if you are the repository maintainer consider running `ngit init` to create one")? .clone(), - repo_coordinates - .iter() - .next() - .map(|coordinate| coordinate.public_key), + Some(repo_coordinate.public_key), ))?; let mut events: HashMap = HashMap::new(); @@ -913,27 +914,40 @@ pub async fn get_state_from_cache( #[allow(clippy::too_many_lines)] async fn create_relays_request( git_repo_path: Option<&Path>, - repo_coordinates: &HashSet, + trusted_maintainer_coordinate: Option<&Coordinate>, user_profiles: &HashSet, fallback_relays: HashSet, ) -> Result { - let repo_ref = get_repo_ref_from_cache(git_repo_path, repo_coordinates).await; + let repo_ref = if let Some(trusted_maintainer_coordinate) = trusted_maintainer_coordinate { + if let Ok(repo_ref) = + get_repo_ref_from_cache(git_repo_path, trusted_maintainer_coordinate).await + { + Some(repo_ref) + } else { + None + } + } else { + None + }; let repo_coordinates = { // add coordinates of users listed in maintainers to explicitly specified // coodinates - let mut repo_coordinates = repo_coordinates.clone(); - if let Ok(repo_ref) = &repo_ref { + let mut set: HashSet = HashSet::new(); + if let Some(trusted_maintainer_coordinate) = trusted_maintainer_coordinate { + set.insert(trusted_maintainer_coordinate.clone()); + } + if let Some(repo_ref) = &repo_ref { for c in repo_ref.coordinates() { - if !repo_coordinates + if !set .iter() .any(|e| e.identifier.eq(&c.identifier) && e.public_key.eq(&c.public_key)) { - repo_coordinates.insert(c); + set.insert(c); } } } - repo_coordinates + set }; let repo_coordinates_without_relays = { @@ -954,7 +968,7 @@ async fn create_relays_request( let mut contributors: HashSet = HashSet::new(); if !repo_coordinates_without_relays.is_empty() { - if let Ok(repo_ref) = &repo_ref { + if let Some(repo_ref) = &repo_ref { for m in &repo_ref.maintainers { contributors.insert(m.to_owned()); } @@ -1077,7 +1091,7 @@ async fn create_relays_request( let relays = { let mut relays = fallback_relays; - if let Ok(repo_ref) = &repo_ref { + if let Some(repo_ref) = &repo_ref { for r in repo_ref.relays.clone() { relays.insert(r); } @@ -1113,7 +1127,7 @@ async fn create_relays_request( selected_relay: None, repo_relays: relays, relay_column_width, - repo_coordinates_without_relays: if let Ok(repo_ref) = &repo_ref { + repo_coordinates_without_relays: if let Some(repo_ref) = &repo_ref { repo_ref.coordinates_with_timestamps() } else { repo_coordinates_without_relays @@ -1121,7 +1135,7 @@ async fn create_relays_request( .map(|c| (c.clone(), None)) .collect() }, - state: if let Ok(repo_ref) = &repo_ref { + state: if let Some(repo_ref) = &repo_ref { if let Ok(existing_state) = get_state_from_cache(git_repo_path, repo_ref).await { Some((existing_state.event.created_at, existing_state.event.id)) } else { @@ -1523,12 +1537,16 @@ pub async fn fetching_with_report( git_repo_path: &Path, #[cfg(test)] client: &crate::client::MockConnect, #[cfg(not(test))] client: &Client, - repo_coordinates: &HashSet, + trusted_maintainer_coordinate: &Coordinate, ) -> Result { let term = console::Term::stderr(); term.write_line("fetching updates...")?; let (relay_reports, progress_reporter) = client - .fetch_all(Some(git_repo_path), repo_coordinates, &HashSet::new()) + .fetch_all( + Some(git_repo_path), + Some(trusted_maintainer_coordinate), + &HashSet::new(), + ) .await?; if !relay_reports.iter().any(std::result::Result::is_err) { let _ = progress_reporter.clear(); diff --git a/src/lib/git/nostr_url.rs b/src/lib/git/nostr_url.rs index a501765..c26bb2e 100644 --- a/src/lib/git/nostr_url.rs +++ b/src/lib/git/nostr_url.rs @@ -1,5 +1,5 @@ use core::fmt; -use std::{collections::HashSet, str::FromStr}; +use std::str::FromStr; use anyhow::{anyhow, bail, Context, Error, Result}; use nostr::nips::nip01::Coordinate; @@ -56,7 +56,7 @@ impl FromStr for ServerProtocol { #[derive(Debug, PartialEq)] pub struct NostrUrlDecoded { pub original_string: String, - pub coordinates: HashSet, + pub coordinate: Coordinate, pub protocol: Option, pub user: Option, } @@ -70,9 +70,8 @@ impl fmt::Display for NostrUrlDecoded { if let Some(protocol) = &self.protocol { write!(f, "{}/", protocol)?; } - let c = self.coordinates.iter().next().unwrap(); - write!(f, "{}/", c.public_key.to_bech32().unwrap())?; - if let Some(relay) = c.relays.first() { + write!(f, "{}/", self.coordinate.public_key.to_bech32().unwrap())?; + if let Some(relay) = self.coordinate.relays.first() { write!( f, "{}/", @@ -84,7 +83,7 @@ impl fmt::Display for NostrUrlDecoded { ) )?; } - write!(f, "{}", c.identifier) + write!(f, "{}", self.coordinate.identifier) } } @@ -94,7 +93,6 @@ impl std::str::FromStr for NostrUrlDecoded { type Err = anyhow::Error; fn from_str(url: &str) -> Result { - let mut coordinates = HashSet::new(); let mut protocol = None; let mut user = None; let mut relays = vec![]; @@ -157,9 +155,9 @@ impl std::str::FromStr for NostrUrlDecoded { // extract naddr npub//identifer let part = parts.first().context(INCORRECT_NOSTR_URL_FORMAT_ERROR)?; // naddr used - if let Ok(coordinate) = Coordinate::parse(part) { + let coordinate = if let Ok(coordinate) = Coordinate::parse(part) { if coordinate.kind.eq(&nostr_sdk::Kind::GitRepoAnnouncement) { - coordinates.insert(coordinate); + coordinate } else { bail!("naddr doesnt point to a git repository announcement"); } @@ -181,19 +179,19 @@ impl std::str::FromStr for NostrUrlDecoded { RelayUrl::parse(&decoded).context("could not parse relays in nostr git url")?; relays.push(url); } - coordinates.insert(Coordinate { + Coordinate { identifier, public_key, kind: nostr_sdk::Kind::GitRepoAnnouncement, relays, - }); + } } else { bail!(INCORRECT_NOSTR_URL_FORMAT_ERROR); - } + }; Ok(Self { original_string: url.to_string(), - coordinates, + coordinate, protocol, user, }) @@ -865,8 +863,6 @@ mod tests { } } mod nostr_git_url_format { - use std::collections::HashSet; - use nostr::nips::nip01::Coordinate; use nostr_sdk::PublicKey; @@ -880,7 +876,7 @@ mod tests { "{}", NostrUrlDecoded { original_string: String::new(), - coordinates: HashSet::from_iter(vec![Coordinate { + coordinate: Coordinate { identifier: "ngit".to_string(), public_key: PublicKey::parse( "npub15qydau2hjma6ngxkl2cyar74wzyjshvl65za5k5rl69264ar2exs5cyejr", @@ -888,7 +884,7 @@ mod tests { .unwrap(), kind: nostr_sdk::Kind::GitRepoAnnouncement, relays: vec![RelayUrl::parse("wss://nos.lol").unwrap()], - }]), + }, protocol: None, user: None, } @@ -905,7 +901,7 @@ mod tests { "{}", NostrUrlDecoded { original_string: String::new(), - coordinates: HashSet::from_iter(vec![Coordinate { + coordinate: Coordinate { identifier: "ngit".to_string(), public_key: PublicKey::parse( "npub15qydau2hjma6ngxkl2cyar74wzyjshvl65za5k5rl69264ar2exs5cyejr", @@ -913,7 +909,7 @@ mod tests { .unwrap(), kind: nostr_sdk::Kind::GitRepoAnnouncement, relays: vec![], - }]), + }, protocol: None, user: None, } @@ -930,7 +926,7 @@ mod tests { "{}", NostrUrlDecoded { original_string: String::new(), - coordinates: HashSet::from_iter(vec![Coordinate { + coordinate: Coordinate { identifier: "ngit".to_string(), public_key: PublicKey::parse( "npub15qydau2hjma6ngxkl2cyar74wzyjshvl65za5k5rl69264ar2exs5cyejr", @@ -938,7 +934,7 @@ mod tests { .unwrap(), kind: nostr_sdk::Kind::GitRepoAnnouncement, relays: vec![RelayUrl::parse("wss://nos.lol").unwrap()], - }]), + }, protocol: Some(ServerProtocol::Ssh), user: None, } @@ -955,7 +951,7 @@ mod tests { "{}", NostrUrlDecoded { original_string: String::new(), - coordinates: HashSet::from_iter(vec![Coordinate { + coordinate: Coordinate { identifier: "ngit".to_string(), public_key: PublicKey::parse( "npub15qydau2hjma6ngxkl2cyar74wzyjshvl65za5k5rl69264ar2exs5cyejr", @@ -963,7 +959,7 @@ mod tests { .unwrap(), kind: nostr_sdk::Kind::GitRepoAnnouncement, relays: vec![RelayUrl::parse("wss://nos.lol").unwrap()], - }]), + }, protocol: Some(ServerProtocol::Ssh), user: Some("bla".to_string()), } @@ -1002,7 +998,7 @@ mod tests { NostrUrlDecoded::from_str(&url)?, NostrUrlDecoded { original_string: url.clone(), - coordinates: HashSet::from([Coordinate { + coordinate: Coordinate { identifier: "ngit".to_string(), public_key: PublicKey::parse( "npub15qydau2hjma6ngxkl2cyar74wzyjshvl65za5k5rl69264ar2exs5cyejr", @@ -1011,7 +1007,7 @@ mod tests { kind: nostr_sdk::Kind::GitRepoAnnouncement, relays: vec![RelayUrl::parse("wss://nos.lol").unwrap()], /* wont add the * slash */ - }]), + }, protocol: None, user: None, }, @@ -1031,7 +1027,7 @@ mod tests { NostrUrlDecoded::from_str(&url)?, NostrUrlDecoded { original_string: url.clone(), - coordinates: HashSet::from([get_model_coordinate(false)]), + coordinate: get_model_coordinate(false), protocol: None, user: None, }, @@ -1049,7 +1045,7 @@ mod tests { NostrUrlDecoded::from_str(&url)?, NostrUrlDecoded { original_string: url.clone(), - coordinates: HashSet::from([get_model_coordinate(true)]), + coordinate: get_model_coordinate(true), protocol: None, user: None, }, @@ -1067,7 +1063,7 @@ mod tests { NostrUrlDecoded::from_str(&url)?, NostrUrlDecoded { original_string: url.clone(), - coordinates: HashSet::from([get_model_coordinate(true)]), + coordinate: get_model_coordinate(true), protocol: None, user: None, }, @@ -1086,7 +1082,7 @@ mod tests { NostrUrlDecoded::from_str(&url)?, NostrUrlDecoded { original_string: url.clone(), - coordinates: HashSet::from([Coordinate { + coordinate: Coordinate { identifier: "ngit".to_string(), public_key: PublicKey::parse( "npub15qydau2hjma6ngxkl2cyar74wzyjshvl65za5k5rl69264ar2exs5cyejr", @@ -1097,7 +1093,7 @@ mod tests { RelayUrl::parse("wss://nos.lol/").unwrap(), RelayUrl::parse("wss://relay.damus.io/").unwrap(), ], - }]), + }, protocol: None, user: None, }, @@ -1112,7 +1108,7 @@ mod tests { NostrUrlDecoded::from_str(&url)?, NostrUrlDecoded { original_string: url.clone(), - coordinates: HashSet::from([get_model_coordinate(false)]), + coordinate: get_model_coordinate(false), protocol: Some(ServerProtocol::Ssh), user: None, }, @@ -1127,7 +1123,7 @@ mod tests { NostrUrlDecoded::from_str(&url)?, NostrUrlDecoded { original_string: url.clone(), - coordinates: HashSet::from([get_model_coordinate(false)]), + coordinate: get_model_coordinate(false), protocol: Some(ServerProtocol::Ssh), user: Some("fred".to_string()), }, @@ -1146,7 +1142,7 @@ mod tests { NostrUrlDecoded::from_str(&url)?, NostrUrlDecoded { original_string: url.clone(), - coordinates: HashSet::from([get_model_coordinate(true)]), + coordinate: get_model_coordinate(true), protocol: None, user: None, }, @@ -1164,7 +1160,7 @@ mod tests { NostrUrlDecoded::from_str(&url)?, NostrUrlDecoded { original_string: url.clone(), - coordinates: HashSet::from([get_model_coordinate(true)]), + coordinate: get_model_coordinate(true), protocol: None, user: None, }, @@ -1183,7 +1179,7 @@ mod tests { NostrUrlDecoded::from_str(&url)?, NostrUrlDecoded { original_string: url.clone(), - coordinates: HashSet::from([Coordinate { + coordinate: Coordinate { identifier: "ngit".to_string(), public_key: PublicKey::parse( "npub15qydau2hjma6ngxkl2cyar74wzyjshvl65za5k5rl69264ar2exs5cyejr", @@ -1194,7 +1190,7 @@ mod tests { RelayUrl::parse("wss://nos.lol/").unwrap(), RelayUrl::parse("wss://relay.damus.io/").unwrap(), ], - }]), + }, protocol: None, user: None, }, @@ -1209,7 +1205,7 @@ mod tests { NostrUrlDecoded::from_str(&url)?, NostrUrlDecoded { original_string: url.clone(), - coordinates: HashSet::from([get_model_coordinate(false)]), + coordinate: get_model_coordinate(false), protocol: Some(ServerProtocol::Ssh), user: None, }, @@ -1224,7 +1220,7 @@ mod tests { NostrUrlDecoded::from_str(&url)?, NostrUrlDecoded { original_string: url.clone(), - coordinates: HashSet::from([get_model_coordinate(false)]), + coordinate: get_model_coordinate(false), protocol: Some(ServerProtocol::Ssh), user: Some("fred".to_string()), }, diff --git a/src/lib/login/user.rs b/src/lib/login/user.rs index 9534bbe..1898a1f 100644 --- a/src/lib/login/user.rs +++ b/src/lib/login/user.rs @@ -75,11 +75,7 @@ pub async fn get_user_details( let term = console::Term::stderr(); term.write_line("searching for profile...")?; let (_, progress_reporter) = client - .fetch_all( - git_repo_path, - &HashSet::new(), - &HashSet::from_iter(vec![*public_key]), - ) + .fetch_all(git_repo_path, None, &HashSet::from_iter(vec![*public_key])) .await?; if let Ok(user_ref) = get_user_ref_from_cache(git_repo_path, public_key).await { progress_reporter.clear()?; diff --git a/src/lib/repo_ref.rs b/src/lib/repo_ref.rs index 69fbe64..988c87d 100644 --- a/src/lib/repo_ref.rs +++ b/src/lib/repo_ref.rs @@ -188,6 +188,13 @@ impl RepoRef { /// coordinates without relay hints pub fn coordinates(&self) -> HashSet { let mut res = HashSet::new(); + res.insert(Coordinate { + kind: Kind::GitRepoAnnouncement, + public_key: self.trusted_maintainer, + identifier: self.identifier.clone(), + relays: vec![], + }); + for m in &self.maintainers { res.insert(Coordinate { kind: Kind::GitRepoAnnouncement, @@ -226,7 +233,7 @@ impl RepoRef { "{}", NostrUrlDecoded { original_string: String::new(), - coordinates: HashSet::from_iter(vec![self.coordinate_with_hint()]), + coordinate: self.coordinate_with_hint(), protocol: None, user: None, } @@ -238,7 +245,7 @@ pub async fn get_repo_coordinates( git_repo: &Repo, #[cfg(test)] client: &crate::client::MockConnect, #[cfg(not(test))] client: &Client, -) -> Result> { +) -> Result { try_and_get_repo_coordinates(git_repo, client, true).await } @@ -247,7 +254,7 @@ pub async fn try_and_get_repo_coordinates( #[cfg(test)] client: &crate::client::MockConnect, #[cfg(not(test))] client: &Client, prompt_user: bool, -) -> Result> { +) -> Result { let mut repo_coordinates = get_repo_coordinates_from_git_config(git_repo)?; if repo_coordinates.is_empty() { @@ -265,7 +272,11 @@ pub async fn try_and_get_repo_coordinates( bail!("couldn't find repo coordinates in git config nostr.repo or in maintainers.yaml"); } } - Ok(repo_coordinates) + Ok(repo_coordinates + .iter() + .next() + .context("would have bailed if no coordinates found")? + .clone()) } fn get_repo_coordinates_from_git_config(git_repo: &Repo) -> Result> { @@ -285,9 +296,7 @@ fn get_repo_coordinates_from_nostr_remotes(git_repo: &Repo) -> Result