From d8f4f7641312bff32f772cbc070b3f99ced0c8fe Mon Sep 17 00:00:00 2001 From: DanConwayDev Date: Mon, 9 Dec 2024 15:35:18 +0000 Subject: fix: fetch user relays for `send`,`push` & `init` get the latest user relay list before pushing patches and repo announcement events --- src/bin/git_remote_nostr/main.rs | 13 +++++++++++-- src/bin/git_remote_nostr/push.rs | 2 +- src/bin/ngit/sub_commands/export_keys.rs | 1 + src/bin/ngit/sub_commands/init.rs | 1 + src/bin/ngit/sub_commands/login.rs | 13 +++++++++++-- src/bin/ngit/sub_commands/logout.rs | 13 +++++++++++-- src/bin/ngit/sub_commands/send.rs | 1 + src/lib/login/existing.rs | 3 +++ src/lib/login/fresh.rs | 3 +++ src/lib/login/mod.rs | 14 ++++++++++++-- src/lib/login/user.rs | 15 +++++++++++++++ src/lib/repo_ref.rs | 2 +- 12 files changed, 71 insertions(+), 10 deletions(-) (limited to 'src') diff --git a/src/bin/git_remote_nostr/main.rs b/src/bin/git_remote_nostr/main.rs index 8e12d68..5f9f712 100644 --- a/src/bin/git_remote_nostr/main.rs +++ b/src/bin/git_remote_nostr/main.rs @@ -36,8 +36,17 @@ async fn main() -> Result<()> { let mut client = Client::default(); - if let Ok((signer, _, _)) = - load_existing_login(&Some(&git_repo), &None, &None, &None, None, true, false).await + if let Ok((signer, _, _)) = load_existing_login( + &Some(&git_repo), + &None, + &None, + &None, + None, + true, + false, + false, + ) + .await { // signer for to respond to relay auth request client.set_signer(signer).await; diff --git a/src/bin/git_remote_nostr/push.rs b/src/bin/git_remote_nostr/push.rs index 40e9584..6116fe2 100644 --- a/src/bin/git_remote_nostr/push.rs +++ b/src/bin/git_remote_nostr/push.rs @@ -127,7 +127,7 @@ pub async fn run_push( } let (signer, user_ref, _) = - login::login_or_signup(&Some(git_repo), &None, &None, Some(client)).await?; + login::login_or_signup(&Some(git_repo), &None, &None, Some(client), true).await?; if !repo_ref.maintainers.contains(&user_ref.public_key) { for refspec in &git_server_refspecs { diff --git a/src/bin/ngit/sub_commands/export_keys.rs b/src/bin/ngit/sub_commands/export_keys.rs index 12fc380..54a716f 100644 --- a/src/bin/ngit/sub_commands/export_keys.rs +++ b/src/bin/ngit/sub_commands/export_keys.rs @@ -28,6 +28,7 @@ pub async fn launch() -> Result<()> { None, true, false, + false, ) .await { diff --git a/src/bin/ngit/sub_commands/init.rs b/src/bin/ngit/sub_commands/init.rs index 6fc1ec4..7ed98f5 100644 --- a/src/bin/ngit/sub_commands/init.rs +++ b/src/bin/ngit/sub_commands/init.rs @@ -84,6 +84,7 @@ pub async fn launch(cli_args: &Cli, args: &SubCommandArgs) -> Result<()> { &extract_signer_cli_arguments(cli_args).unwrap_or(None), &cli_args.password, Some(&client), + true, ) .await?; diff --git a/src/bin/ngit/sub_commands/login.rs b/src/bin/ngit/sub_commands/login.rs index 7670bc3..ccbdf01 100644 --- a/src/bin/ngit/sub_commands/login.rs +++ b/src/bin/ngit/sub_commands/login.rs @@ -64,8 +64,17 @@ async fn logout(git_repo: Option<&Repo>, local_only: bool) -> Result<(bool, bool } else { vec![SignerInfoSource::GitLocal, SignerInfoSource::GitGlobal] } { - if let Ok((_, user_ref, source)) = - load_existing_login(&git_repo, &None, &None, &Some(source), None, true, false).await + if let Ok((_, user_ref, source)) = load_existing_login( + &git_repo, + &None, + &None, + &Some(source), + None, + true, + false, + false, + ) + .await { match Interactor::default().choice( PromptChoiceParms::default() diff --git a/src/bin/ngit/sub_commands/logout.rs b/src/bin/ngit/sub_commands/logout.rs index 682c017..f3f9620 100644 --- a/src/bin/ngit/sub_commands/logout.rs +++ b/src/bin/ngit/sub_commands/logout.rs @@ -26,8 +26,17 @@ async fn logout(git_repo: Option<&Repo>) -> Result<()> { } else { vec![SignerInfoSource::GitLocal, SignerInfoSource::GitGlobal] } { - if let Ok((_, user_ref, source)) = - load_existing_login(&git_repo, &None, &None, &Some(source), None, true, false).await + if let Ok((_, user_ref, source)) = load_existing_login( + &git_repo, + &None, + &None, + &Some(source), + None, + true, + false, + false, + ) + .await { for item in [ "nostr.nsec", diff --git a/src/bin/ngit/sub_commands/send.rs b/src/bin/ngit/sub_commands/send.rs index e19c38b..c6c75c9 100644 --- a/src/bin/ngit/sub_commands/send.rs +++ b/src/bin/ngit/sub_commands/send.rs @@ -181,6 +181,7 @@ pub async fn launch(cli_args: &Cli, args: &SubCommandArgs, no_fetch: bool) -> Re &extract_signer_cli_arguments(cli_args).unwrap_or(None), &cli_args.password, Some(&client), + true, ) .await?; diff --git a/src/lib/login/existing.rs b/src/lib/login/existing.rs index 45727b0..4606c22 100644 --- a/src/lib/login/existing.rs +++ b/src/lib/login/existing.rs @@ -27,6 +27,7 @@ use crate::{ /// - `client`: include client to fetch profiles from relays that are missing /// from cache /// - `silent`: do not print outcome in termianl +#[allow(clippy::too_many_arguments)] pub async fn load_existing_login( git_repo: &Option<&Repo>, signer_info: &Option, @@ -36,6 +37,7 @@ pub async fn load_existing_login( #[cfg(not(test))] client: Option<&Client>, silent: bool, prompt_for_password: bool, + fetch_profile_updates: bool, ) -> Result<(Arc, UserRef, SignerInfoSource)> { let (signer_info, source) = get_signer_info(git_repo, signer_info, password, source)?; @@ -50,6 +52,7 @@ pub async fn load_existing_login( None }, silent, + fetch_profile_updates, ) .await?; diff --git a/src/lib/login/fresh.rs b/src/lib/login/fresh.rs index 615c0a6..7cdbde8 100644 --- a/src/lib/login/fresh.rs +++ b/src/lib/login/fresh.rs @@ -46,6 +46,7 @@ pub async fn fresh_login_or_signup( client, true, true, + false, ) .await?; break (signer, user_ref.public_key, signer_info, source); @@ -102,6 +103,7 @@ pub async fn fresh_login_or_signup( None }, false, + false, ) .await?; print_logged_in_as(&user_ref, client.is_none(), &source)?; @@ -549,6 +551,7 @@ async fn save_to_git_config( None, true, true, + false, ) .await { diff --git a/src/lib/login/mod.rs b/src/lib/login/mod.rs index 00dbb17..0be1e5d 100644 --- a/src/lib/login/mod.rs +++ b/src/lib/login/mod.rs @@ -24,9 +24,19 @@ pub async fn login_or_signup( password: &Option, #[cfg(test)] client: Option<&MockConnect>, #[cfg(not(test))] client: Option<&Client>, + fetch_profile_updates: bool, ) -> Result<(Arc, UserRef, SignerInfoSource)> { - let res = - load_existing_login(git_repo, signer_info, password, &None, client, false, true).await; + let res = load_existing_login( + git_repo, + signer_info, + password, + &None, + client, + false, + true, + fetch_profile_updates, + ) + .await; if res.is_ok() { res } else { diff --git a/src/lib/login/user.rs b/src/lib/login/user.rs index 1898a1f..c13cdf5 100644 --- a/src/lib/login/user.rs +++ b/src/lib/login/user.rs @@ -60,8 +60,23 @@ pub async fn get_user_details( #[cfg(not(test))] client: Option<&Client>, git_repo_path: Option<&Path>, cache_only: bool, + fetch_profile_updates: bool, ) -> Result { if let Ok(user_ref) = get_user_ref_from_cache(git_repo_path, public_key).await { + if fetch_profile_updates { + if let Some(client) = client { + let term = console::Term::stderr(); + term.write_line("searching for profile updates...")?; + let (reports, progress_reporter) = client + .fetch_all(git_repo_path, None, &HashSet::from_iter(vec![*public_key])) + .await?; + if !reports.iter().any(|r| r.is_err()) { + progress_reporter.clear()?; + term.clear_last_lines(1)?; + } + return get_user_ref_from_cache(git_repo_path, public_key).await; + } + } Ok(user_ref) } else { let empty = UserRef { diff --git a/src/lib/repo_ref.rs b/src/lib/repo_ref.rs index 1b25ccf..da76182 100644 --- a/src/lib/repo_ref.rs +++ b/src/lib/repo_ref.rs @@ -308,7 +308,7 @@ async fn get_nostr_git_remote_selection_labels( for (remote, c) in remote_coordinates { res.push(format!( "{remote} - {}/{}", - get_user_details(&c.public_key, None, Some(git_repo.get_path()?), true) + get_user_details(&c.public_key, None, Some(git_repo.get_path()?), true, false) .await? .metadata .name, -- cgit v1.2.3