From cc87576cdfdd3aa18df6e94fbfa079d9e4f0241a Mon Sep 17 00:00:00 2001 From: Laszlo Megyer Date: Mon, 16 Dec 2024 11:38:06 +0100 Subject: feat(init): default to nip05 git nostr url If the user has NIP-05 set up in profile, and it resolves at the time of running `ngit init`, NIP-05 will be used in the nostr remote url. --- src/lib/git/nostr_url.rs | 12 ++++++++---- src/lib/login/user.rs | 8 +++++++- src/lib/repo_ref.rs | 10 ++++++++++ 3 files changed, 25 insertions(+), 5 deletions(-) (limited to 'src/lib') diff --git a/src/lib/git/nostr_url.rs b/src/lib/git/nostr_url.rs index 6b38a93..de7953f 100644 --- a/src/lib/git/nostr_url.rs +++ b/src/lib/git/nostr_url.rs @@ -55,7 +55,7 @@ impl FromStr for ServerProtocol { } } -#[derive(Debug, PartialEq)] +#[derive(Debug, PartialEq, Clone)] pub struct NostrUrlDecoded { pub original_string: String, pub coordinate: Coordinate, @@ -73,7 +73,11 @@ impl fmt::Display for NostrUrlDecoded { if let Some(protocol) = &self.protocol { write!(f, "{}/", protocol)?; } - write!(f, "{}/", self.coordinate.public_key.to_bech32().unwrap())?; + if let Some(nip05) = &self.nip05 { + write!(f, "{}/", nip05)?; + } else { + write!(f, "{}/", self.coordinate.public_key.to_bech32().unwrap())?; + } if let Some(relay) = self.coordinate.relays.first() { write!( f, @@ -97,6 +101,7 @@ impl NostrUrlDecoded { let mut protocol = None; let mut user = None; let mut relays = vec![]; + let mut nip05 = None; if !url.starts_with("nostr://") { bail!("nostr git url must start with nostr://"); @@ -157,7 +162,6 @@ impl NostrUrlDecoded { } // extract naddr npub//identifer let part = parts.first().context(INCORRECT_NOSTR_URL_FORMAT_ERROR)?; - let mut nip05 = None; // naddr used let coordinate = if let Ok(coordinate) = Coordinate::parse(part) { if coordinate.kind.eq(&nostr_sdk::Kind::GitRepoAnnouncement) { @@ -254,7 +258,7 @@ pub fn use_nip05_git_config_cache_to_find_nip05_from_public_key( .cloned()) } -fn save_nip05_to_git_config_cache( +pub fn save_nip05_to_git_config_cache( nip05: &str, public_key: &PublicKey, git_repo: &Option<&Repo>, diff --git a/src/lib/login/user.rs b/src/lib/login/user.rs index 107e765..071cb25 100644 --- a/src/lib/login/user.rs +++ b/src/lib/login/user.rs @@ -22,6 +22,7 @@ pub struct UserRef { pub struct UserMetadata { pub name: String, pub created_at: Timestamp, + pub nip05: Option, } #[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq)] @@ -149,7 +150,7 @@ pub fn extract_user_metadata( }; Ok(UserMetadata { - name: if let Some(metadata) = metadata { + name: if let Some(metadata) = metadata.clone() { if let Some(n) = metadata.name { n } else if let Some(n) = metadata.custom.get("displayName") { @@ -167,6 +168,11 @@ pub fn extract_user_metadata( } else { public_key.to_bech32()? }, + nip05: if let Some(metadata) = metadata { + metadata.nip05 + } else { + None + }, created_at: if let Some(event) = event { event.created_at } else { diff --git a/src/lib/repo_ref.rs b/src/lib/repo_ref.rs index a9d1186..8705597 100644 --- a/src/lib/repo_ref.rs +++ b/src/lib/repo_ref.rs @@ -38,6 +38,7 @@ pub struct RepoRef { pub maintainers: Vec, pub trusted_maintainer: PublicKey, pub events: HashMap, + pub nostr_git_url: Option, } impl TryFrom<(nostr::Event, Option)> for RepoRef { @@ -60,6 +61,7 @@ impl TryFrom<(nostr::Event, Option)> for RepoRef { maintainers: Vec::new(), trusted_maintainer: trusted_maintainer.unwrap_or(event.pubkey), events: HashMap::new(), + nostr_git_url: None, }; for tag in event.tags.iter() { @@ -235,7 +237,14 @@ impl RepoRef { .collect::)>>() } + pub fn set_nostr_git_url(&mut self, nostr_git_url: NostrUrlDecoded) { + self.nostr_git_url = Some(nostr_git_url) + } + pub fn to_nostr_git_url(&self, git_repo: &Option<&Repo>) -> String { + if let Some(nostr_git_url) = &self.nostr_git_url { + return nostr_git_url.original_string.clone(); + } let c = self.coordinate_with_hint(); format!("{}", NostrUrlDecoded { original_string: String::new(), @@ -550,6 +559,7 @@ mod tests { trusted_maintainer: TEST_KEY_1_KEYS.public_key(), maintainers: vec![TEST_KEY_1_KEYS.public_key(), TEST_KEY_2_KEYS.public_key()], events: HashMap::new(), + nostr_git_url: None, } .to_event(&TEST_KEY_1_SIGNER) .await -- cgit v1.2.3