From 7227db8d2a506912eb5ffd3d25dee1c95544cbf4 Mon Sep 17 00:00:00 2001 From: DanConwayDev Date: Wed, 17 Jul 2024 16:44:00 +0100 Subject: feat(init): set repo pointer in git config set repo.nostr to naddr reflecting the announcement just issued --- src/sub_commands/init.rs | 27 ++++++++++++---- tests/init.rs | 80 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 101 insertions(+), 6 deletions(-) diff --git a/src/sub_commands/init.rs b/src/sub_commands/init.rs index db90acd..44e288f 100644 --- a/src/sub_commands/init.rs +++ b/src/sub_commands/init.rs @@ -1,7 +1,8 @@ use std::collections::HashMap; use anyhow::{Context, Result}; -use nostr::{FromBech32, PublicKey, ToBech32}; +use nostr::{nips::nip01::Coordinate, FromBech32, PublicKey, ToBech32}; +use nostr_sdk::Kind; use super::send::send_events; #[cfg(not(test))] @@ -13,7 +14,10 @@ use crate::{ client::Connect, git::{Repo, RepoActions}, login, - repo_ref::{self, extract_pks, get_repo_config_from_yaml, save_repo_config_to_yaml, RepoRef}, + repo_ref::{ + self, extract_pks, get_repo_config_from_yaml, save_repo_config_to_yaml, RepoRef, + REPO_REF_KIND, + }, Cli, }; @@ -292,7 +296,7 @@ pub async fn launch(cli_args: &Cli, args: &SubCommandArgs) -> Result<()> { println!("publishing repostory reference..."); - let repo_event = RepoRef { + let repo_ref = RepoRef { identifier: identifier.clone(), name, description, @@ -302,9 +306,8 @@ pub async fn launch(cli_args: &Cli, args: &SubCommandArgs) -> Result<()> { relays: relays.clone(), maintainers: maintainers.clone(), events: HashMap::new(), - } - .to_event(&signer) - .await?; + }; + let repo_event = repo_ref.to_event(&signer).await?; client.set_signer(signer).await; @@ -317,6 +320,18 @@ pub async fn launch(cli_args: &Cli, args: &SubCommandArgs) -> Result<()> { ) .await?; + git_repo.save_git_config_item( + "nostr.repo", + &Coordinate { + kind: Kind::Custom(REPO_REF_KIND), + public_key: user_ref.public_key, + identifier: identifier.clone(), + relays: vec![], + } + .to_bech32()?, + false, + )?; + // if yaml file doesnt exist or needs updating if match &repo_config_result { Ok(config) => { diff --git a/tests/init.rs b/tests/init.rs index d7ba164..5209898 100644 --- a/tests/init.rs +++ b/tests/init.rs @@ -276,6 +276,86 @@ mod when_repo_not_previously_claimed { } } + mod git_config_updated { + + use nostr::nips::nip01::Coordinate; + use nostr_sdk::ToBech32; + + use super::*; + + async fn async_run_test() -> Result<()> { + let git_repo = prep_git_repo()?; + // fallback (51,52) user write (53, 55) repo (55, 56) blaster (57) + let (mut r51, mut r52, mut r53, mut r55, mut r56, mut r57) = ( + Relay::new( + 8051, + None, + Some(&|relay, client_id, subscription_id, _| -> Result<()> { + relay.respond_events( + client_id, + &subscription_id, + &vec![ + generate_test_key_1_metadata_event("fred"), + generate_test_key_1_relay_list_event(), + ], + )?; + Ok(()) + }), + ), + Relay::new(8052, None, None), + Relay::new(8053, None, None), + Relay::new(8055, None, None), + Relay::new(8056, None, None), + Relay::new(8057, None, None), + ); + + // // check relay had the right number of events + let cli_tester_handle = std::thread::spawn(move || -> Result<()> { + let mut p = cli_tester_init(&git_repo); + p.expect_end_eventually()?; + for p in [51, 52, 53, 55, 56, 57] { + relay::shutdown_relay(8000 + p)?; + } + assert_eq!( + git_repo + .git_repo + .config()? + .get_entry("nostr.repo")? + .value() + .unwrap(), + Coordinate { + kind: nostr_sdk::Kind::Custom(REPOSITORY_KIND), + identifier: "example-identifier".to_string(), + public_key: TEST_KEY_1_KEYS.public_key(), + relays: vec![], + } + .to_bech32()?, + ); + + Ok(()) + }); + + // launch relay + let _ = join!( + r51.listen_until_close(), + r52.listen_until_close(), + r53.listen_until_close(), + r55.listen_until_close(), + r56.listen_until_close(), + r57.listen_until_close(), + ); + cli_tester_handle.join().unwrap()?; + Ok(()) + } + + #[tokio::test] + #[serial] + async fn with_nostr_repo_set_to_user_and_identifer_naddr() -> Result<()> { + async_run_test().await?; + Ok(()) + } + } + mod tags_as_specified_in_args { use super::*; -- cgit v1.2.3