diff options
| author | DanConwayDev <DanConwayDev@protonmail.com> | 2024-12-10 12:12:14 +0000 |
|---|---|---|
| committer | DanConwayDev <DanConwayDev@protonmail.com> | 2024-12-10 12:12:14 +0000 |
| commit | 36eb9395567dc0022b1700ec9cda1389baf22476 (patch) | |
| tree | b5ed71fc7431faacfa29690739db2fe77a0cdc7d /src | |
| parent | 8d8c21d05e4d8d7af30a96704a5919efc07010dc (diff) | |
fix(nostr_url): use nip05 in nostr url if cached
prioritise using nip05 nostr url format when the nip05, public key
mapping is stored in the (usually local) git config.
Diffstat (limited to 'src')
| -rw-r--r-- | src/bin/ngit/sub_commands/init.rs | 6 | ||||
| -rw-r--r-- | src/lib/git/nostr_url.rs | 10 | ||||
| -rw-r--r-- | src/lib/repo_ref.rs | 20 |
3 files changed, 27 insertions, 9 deletions
diff --git a/src/bin/ngit/sub_commands/init.rs b/src/bin/ngit/sub_commands/init.rs index c9c8873..86b681c 100644 --- a/src/bin/ngit/sub_commands/init.rs +++ b/src/bin/ngit/sub_commands/init.rs | |||
| @@ -522,7 +522,7 @@ async fn prompt_to_set_nostr_url_as_origin(repo_ref: &RepoRef, git_repo: &Repo) | |||
| 522 | } | 522 | } |
| 523 | } | 523 | } |
| 524 | println!("contributors can clone your repository by installing ngit and using this clone url:"); | 524 | println!("contributors can clone your repository by installing ngit and using this clone url:"); |
| 525 | println!("{}", repo_ref.to_nostr_git_url()); | 525 | println!("{}", repo_ref.to_nostr_git_url(&Some(git_repo))); |
| 526 | 526 | ||
| 527 | Ok(()) | 527 | Ok(()) |
| 528 | } | 528 | } |
| @@ -535,7 +535,7 @@ fn ask_to_set_origin_remote(repo_ref: &RepoRef, git_repo: &Repo) -> Result<()> { | |||
| 535 | )? { | 535 | )? { |
| 536 | git_repo | 536 | git_repo |
| 537 | .git_repo | 537 | .git_repo |
| 538 | .remote_set_url("origin", &repo_ref.to_nostr_git_url())?; | 538 | .remote_set_url("origin", &repo_ref.to_nostr_git_url(&Some(git_repo)))?; |
| 539 | } | 539 | } |
| 540 | Ok(()) | 540 | Ok(()) |
| 541 | } | 541 | } |
| @@ -548,7 +548,7 @@ fn ask_to_create_new_origin_remote(repo_ref: &RepoRef, git_repo: &Repo) -> Resul | |||
| 548 | )? { | 548 | )? { |
| 549 | git_repo | 549 | git_repo |
| 550 | .git_repo | 550 | .git_repo |
| 551 | .remote("origin", &repo_ref.to_nostr_git_url())?; | 551 | .remote("origin", &repo_ref.to_nostr_git_url(&Some(git_repo)))?; |
| 552 | } | 552 | } |
| 553 | Ok(()) | 553 | Ok(()) |
| 554 | } | 554 | } |
diff --git a/src/lib/git/nostr_url.rs b/src/lib/git/nostr_url.rs index 8bf458b..bc56e1a 100644 --- a/src/lib/git/nostr_url.rs +++ b/src/lib/git/nostr_url.rs | |||
| @@ -237,6 +237,16 @@ fn resolve_nip05_from_git_config_cache(nip05: &str, git_repo: &Option<&Repo>) -> | |||
| 237 | } | 237 | } |
| 238 | } | 238 | } |
| 239 | 239 | ||
| 240 | pub fn use_nip05_git_config_cache_to_find_nip05_from_public_key( | ||
| 241 | public_key: &PublicKey, | ||
| 242 | git_repo: &Option<&Repo>, | ||
| 243 | ) -> Result<Option<String>> { | ||
| 244 | let h = load_nip_cache(git_repo)?; | ||
| 245 | Ok(h.iter() | ||
| 246 | .find_map(|(k, v)| if *v == *public_key { Some(k) } else { None }) | ||
| 247 | .cloned()) | ||
| 248 | } | ||
| 249 | |||
| 240 | fn save_nip05_to_git_config_cache( | 250 | fn save_nip05_to_git_config_cache( |
| 241 | nip05: &str, | 251 | nip05: &str, |
| 242 | public_key: &PublicKey, | 252 | public_key: &PublicKey, |
diff --git a/src/lib/repo_ref.rs b/src/lib/repo_ref.rs index 089befc..9a3c8f1 100644 --- a/src/lib/repo_ref.rs +++ b/src/lib/repo_ref.rs | |||
| @@ -19,7 +19,10 @@ use crate::{ | |||
| 19 | Interactor, InteractorPrompt, PromptChoiceParms, PromptConfirmParms, PromptInputParms, | 19 | Interactor, InteractorPrompt, PromptChoiceParms, PromptConfirmParms, PromptInputParms, |
| 20 | }, | 20 | }, |
| 21 | client::{consolidate_fetch_reports, get_repo_ref_from_cache, sign_event, Connect}, | 21 | client::{consolidate_fetch_reports, get_repo_ref_from_cache, sign_event, Connect}, |
| 22 | git::{nostr_url::NostrUrlDecoded, Repo, RepoActions}, | 22 | git::{ |
| 23 | nostr_url::{use_nip05_git_config_cache_to_find_nip05_from_public_key, NostrUrlDecoded}, | ||
| 24 | Repo, RepoActions, | ||
| 25 | }, | ||
| 23 | login::user::get_user_details, | 26 | login::user::get_user_details, |
| 24 | }; | 27 | }; |
| 25 | 28 | ||
| @@ -232,15 +235,20 @@ impl RepoRef { | |||
| 232 | .collect::<Vec<(Coordinate, Option<Timestamp>)>>() | 235 | .collect::<Vec<(Coordinate, Option<Timestamp>)>>() |
| 233 | } | 236 | } |
| 234 | 237 | ||
| 235 | pub fn to_nostr_git_url(&self) -> String { | 238 | pub fn to_nostr_git_url(&self, git_repo: &Option<&Repo>) -> String { |
| 239 | let c = self.coordinate_with_hint(); | ||
| 236 | format!( | 240 | format!( |
| 237 | "{}", | 241 | "{}", |
| 238 | NostrUrlDecoded { | 242 | NostrUrlDecoded { |
| 239 | original_string: String::new(), | 243 | original_string: String::new(), |
| 240 | coordinate: self.coordinate_with_hint(), | 244 | nip05: use_nip05_git_config_cache_to_find_nip05_from_public_key( |
| 245 | &c.public_key, | ||
| 246 | git_repo, | ||
| 247 | ) | ||
| 248 | .unwrap_or_default(), | ||
| 249 | coordinate: c, | ||
| 241 | protocol: None, | 250 | protocol: None, |
| 242 | user: None, | 251 | user: None, |
| 243 | nip05: None, // TODO: if nip05 for pubkey saved in local git config use it. | ||
| 244 | } | 252 | } |
| 245 | ) | 253 | ) |
| 246 | } | 254 | } |
| @@ -446,10 +454,10 @@ fn set_or_create_git_remote_with_nostr_url( | |||
| 446 | repo_ref: &RepoRef, | 454 | repo_ref: &RepoRef, |
| 447 | git_repo: &Repo, | 455 | git_repo: &Repo, |
| 448 | ) -> Result<()> { | 456 | ) -> Result<()> { |
| 449 | let url = repo_ref.to_nostr_git_url(); | 457 | let url = repo_ref.to_nostr_git_url(&Some(git_repo)); |
| 450 | if git_repo | 458 | if git_repo |
| 451 | .git_repo | 459 | .git_repo |
| 452 | .remote_set_url(name, &repo_ref.to_nostr_git_url()) | 460 | .remote_set_url(name, &repo_ref.to_nostr_git_url(&Some(git_repo))) |
| 453 | .is_err() | 461 | .is_err() |
| 454 | { | 462 | { |
| 455 | git_repo.git_repo.remote(name, &url)?; | 463 | git_repo.git_repo.remote(name, &url)?; |