From 1b740dd135aafb52b94b710b3ae24e4aaaa99632 Mon Sep 17 00:00:00 2001 From: DanConwayDev Date: Wed, 1 Nov 2023 00:00:00 +0000 Subject: feat(prs-create) send to user relays reuse client across login and send events --- src/client.rs | 11 --------- src/login.rs | 4 +-- src/sub_commands/prs/create.rs | 55 +++++++++++++++--------------------------- 3 files changed, 21 insertions(+), 49 deletions(-) (limited to 'src') diff --git a/src/client.rs b/src/client.rs index 84bfb11..1037b1b 100644 --- a/src/client.rs +++ b/src/client.rs @@ -132,17 +132,6 @@ pub struct Params { pub fallback_relays: Vec, } -impl Params { - pub fn with_keys(mut self, keys: nostr::Keys) -> Self { - self.keys = Some(keys); - self - } - pub fn with_fallback_relays(mut self, fallback_relays: Vec) -> Self { - self.fallback_relays = fallback_relays; - self - } -} - fn get_dedup_events(relay_results: Vec>>) -> Vec { let mut dedup_events: Vec = vec![]; for events in relay_results.into_iter().flatten() { diff --git a/src/login.rs b/src/login.rs index e73373a..66fce55 100644 --- a/src/login.rs +++ b/src/login.rs @@ -21,7 +21,7 @@ pub async fn launch( password: &Option, #[cfg(test)] client: Option<&MockConnect>, #[cfg(not(test))] client: Option<&Client>, -) -> Result { +) -> Result<(nostr::Keys, UserRef)> { // if nsec parameter let key = if let Some(nsec_unwrapped) = nsec { // get key or fail without prompts @@ -87,7 +87,7 @@ pub async fn launch( // print logged in println!("logged in as {}", user_ref.metadata.name); - Ok(key) + Ok((key, user_ref.clone())) } async fn get_user_details( diff --git a/src/sub_commands/prs/create.rs b/src/sub_commands/prs/create.rs index 5986235..aad80f4 100644 --- a/src/sub_commands/prs/create.rs +++ b/src/sub_commands/prs/create.rs @@ -1,14 +1,17 @@ use std::time::Duration; use anyhow::{bail, Context, Result}; -use console::Term; use futures::future::join_all; use indicatif::{MultiProgress, ProgressBar, ProgressStyle}; use nostr::{prelude::sha1::Hash as Sha1Hash, EventBuilder, Marker, Tag, TagKind}; +#[cfg(not(test))] +use crate::client::Client; +#[cfg(test)] +use crate::client::MockConnect; use crate::{ cli_interactor::{Interactor, InteractorPrompt, PromptConfirmParms, PromptInputParms}, - client::{Client, Connect, Params as ClientParams}, + client::Connect, git::{Repo, RepoActions}, login, Cli, }; @@ -84,67 +87,47 @@ pub async fn launch( .input(PromptInputParms::default().with_prompt("description (Optional)"))?, }; - // create PR event + #[cfg(not(test))] + let mut client = Client::default(); + #[cfg(test)] + let mut client = ::default(); + + let (keys, user_ref) = login::launch(&cli_args.nsec, &cli_args.password, Some(&client)).await?; - // TODO add client here - let keys = login::launch(&cli_args.nsec, &cli_args.password, None).await?; + client.set_keys(&keys).await; let events = generate_pr_and_patch_events(&title, &description, &to_branch, &git_repo, &ahead, &keys)?; - let my_write_relays: Vec = vec![ - "ws://localhost:8051".to_string(), - "ws://localhost:8052".to_string(), - ]; - + // TODO: get relays from repo event let repo_read_relays: Vec = vec![ - "ws://localhost:8051".to_string(), - "ws://localhost:8053".to_string(), + "ws://localhost:8055".to_string(), + "ws://localhost:8056".to_string(), ]; send_events( + &client, events, - keys, - my_write_relays, + user_ref.relays.write(), repo_read_relays, !cli_args.disable_cli_spinners, ) .await?; // TODO check if there is already a similarly named PR - // println!("failures:"); - // println!("ws://relay.anon.io Error: Payment Required"); - - // should we have a relays in Repository event? - // yes - // - - // TODO connect to relays and post - Ok(()) } async fn send_events( + #[cfg(test)] client: &crate::client::MockConnect, + #[cfg(not(test))] client: &Client, events: Vec, - keys: nostr::Keys, my_write_relays: Vec, repo_read_relays: Vec, animate: bool, ) -> Result<()> { let (_, _, _, all) = unique_and_duplicate_all(&my_write_relays, &repo_read_relays); - let client = Client::new( - ClientParams::default() - .with_keys(keys) - // .with_fallback_relays(vec!["ws://localhost:8080".to_string()]), - .with_fallback_relays(all.iter().map(std::string::ToString::to_string).collect()), - ); - - let term = Term::stdout(); - term.write_line("connecting to relays...")?; - client.connect().await?; - term.clear_last_lines(1)?; - println!( "posting 1 pull request with {} commits...", events.len() - 1 -- cgit v1.2.3