diff options
| author | DanConwayDev <DanConwayDev@protonmail.com> | 2023-11-01 00:00:00 +0000 |
|---|---|---|
| committer | DanConwayDev <DanConwayDev@protonmail.com> | 2023-11-01 00:00:00 +0000 |
| commit | 1b740dd135aafb52b94b710b3ae24e4aaaa99632 (patch) | |
| tree | 986e4a3785e7899552cc616d46f0c3e3f480a992 /src | |
| parent | 47a5075b6385a8c0d28bae04c5862d93d0a6ffa6 (diff) | |
feat(prs-create) send to user relays
reuse client across login and send events
Diffstat (limited to 'src')
| -rw-r--r-- | src/client.rs | 11 | ||||
| -rw-r--r-- | src/login.rs | 4 | ||||
| -rw-r--r-- | src/sub_commands/prs/create.rs | 55 |
3 files changed, 21 insertions, 49 deletions
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 { | |||
| 132 | pub fallback_relays: Vec<String>, | 132 | pub fallback_relays: Vec<String>, |
| 133 | } | 133 | } |
| 134 | 134 | ||
| 135 | impl Params { | ||
| 136 | pub fn with_keys(mut self, keys: nostr::Keys) -> Self { | ||
| 137 | self.keys = Some(keys); | ||
| 138 | self | ||
| 139 | } | ||
| 140 | pub fn with_fallback_relays(mut self, fallback_relays: Vec<String>) -> Self { | ||
| 141 | self.fallback_relays = fallback_relays; | ||
| 142 | self | ||
| 143 | } | ||
| 144 | } | ||
| 145 | |||
| 146 | fn get_dedup_events(relay_results: Vec<Result<Vec<nostr::Event>>>) -> Vec<Event> { | 135 | fn get_dedup_events(relay_results: Vec<Result<Vec<nostr::Event>>>) -> Vec<Event> { |
| 147 | let mut dedup_events: Vec<Event> = vec![]; | 136 | let mut dedup_events: Vec<Event> = vec![]; |
| 148 | for events in relay_results.into_iter().flatten() { | 137 | 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( | |||
| 21 | password: &Option<String>, | 21 | password: &Option<String>, |
| 22 | #[cfg(test)] client: Option<&MockConnect>, | 22 | #[cfg(test)] client: Option<&MockConnect>, |
| 23 | #[cfg(not(test))] client: Option<&Client>, | 23 | #[cfg(not(test))] client: Option<&Client>, |
| 24 | ) -> Result<nostr::Keys> { | 24 | ) -> Result<(nostr::Keys, UserRef)> { |
| 25 | // if nsec parameter | 25 | // if nsec parameter |
| 26 | let key = if let Some(nsec_unwrapped) = nsec { | 26 | let key = if let Some(nsec_unwrapped) = nsec { |
| 27 | // get key or fail without prompts | 27 | // get key or fail without prompts |
| @@ -87,7 +87,7 @@ pub async fn launch( | |||
| 87 | // print logged in | 87 | // print logged in |
| 88 | println!("logged in as {}", user_ref.metadata.name); | 88 | println!("logged in as {}", user_ref.metadata.name); |
| 89 | 89 | ||
| 90 | Ok(key) | 90 | Ok((key, user_ref.clone())) |
| 91 | } | 91 | } |
| 92 | 92 | ||
| 93 | async fn get_user_details( | 93 | 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 @@ | |||
| 1 | use std::time::Duration; | 1 | use std::time::Duration; |
| 2 | 2 | ||
| 3 | use anyhow::{bail, Context, Result}; | 3 | use anyhow::{bail, Context, Result}; |
| 4 | use console::Term; | ||
| 5 | use futures::future::join_all; | 4 | use futures::future::join_all; |
| 6 | use indicatif::{MultiProgress, ProgressBar, ProgressStyle}; | 5 | use indicatif::{MultiProgress, ProgressBar, ProgressStyle}; |
| 7 | use nostr::{prelude::sha1::Hash as Sha1Hash, EventBuilder, Marker, Tag, TagKind}; | 6 | use nostr::{prelude::sha1::Hash as Sha1Hash, EventBuilder, Marker, Tag, TagKind}; |
| 8 | 7 | ||
| 8 | #[cfg(not(test))] | ||
| 9 | use crate::client::Client; | ||
| 10 | #[cfg(test)] | ||
| 11 | use crate::client::MockConnect; | ||
| 9 | use crate::{ | 12 | use crate::{ |
| 10 | cli_interactor::{Interactor, InteractorPrompt, PromptConfirmParms, PromptInputParms}, | 13 | cli_interactor::{Interactor, InteractorPrompt, PromptConfirmParms, PromptInputParms}, |
| 11 | client::{Client, Connect, Params as ClientParams}, | 14 | client::Connect, |
| 12 | git::{Repo, RepoActions}, | 15 | git::{Repo, RepoActions}, |
| 13 | login, Cli, | 16 | login, Cli, |
| 14 | }; | 17 | }; |
| @@ -84,67 +87,47 @@ pub async fn launch( | |||
| 84 | .input(PromptInputParms::default().with_prompt("description (Optional)"))?, | 87 | .input(PromptInputParms::default().with_prompt("description (Optional)"))?, |
| 85 | }; | 88 | }; |
| 86 | 89 | ||
| 87 | // create PR event | 90 | #[cfg(not(test))] |
| 91 | let mut client = Client::default(); | ||
| 92 | #[cfg(test)] | ||
| 93 | let mut client = <MockConnect as std::default::Default>::default(); | ||
| 94 | |||
| 95 | let (keys, user_ref) = login::launch(&cli_args.nsec, &cli_args.password, Some(&client)).await?; | ||
| 88 | 96 | ||
| 89 | // TODO add client here | 97 | client.set_keys(&keys).await; |
| 90 | let keys = login::launch(&cli_args.nsec, &cli_args.password, None).await?; | ||
| 91 | 98 | ||
| 92 | let events = | 99 | let events = |
| 93 | generate_pr_and_patch_events(&title, &description, &to_branch, &git_repo, &ahead, &keys)?; | 100 | generate_pr_and_patch_events(&title, &description, &to_branch, &git_repo, &ahead, &keys)?; |
| 94 | 101 | ||
| 95 | let my_write_relays: Vec<String> = vec![ | 102 | // TODO: get relays from repo event |
| 96 | "ws://localhost:8051".to_string(), | ||
| 97 | "ws://localhost:8052".to_string(), | ||
| 98 | ]; | ||
| 99 | |||
| 100 | let repo_read_relays: Vec<String> = vec![ | 103 | let repo_read_relays: Vec<String> = vec![ |
| 101 | "ws://localhost:8051".to_string(), | 104 | "ws://localhost:8055".to_string(), |
| 102 | "ws://localhost:8053".to_string(), | 105 | "ws://localhost:8056".to_string(), |
| 103 | ]; | 106 | ]; |
| 104 | 107 | ||
| 105 | send_events( | 108 | send_events( |
| 109 | &client, | ||
| 106 | events, | 110 | events, |
| 107 | keys, | 111 | user_ref.relays.write(), |
| 108 | my_write_relays, | ||
| 109 | repo_read_relays, | 112 | repo_read_relays, |
| 110 | !cli_args.disable_cli_spinners, | 113 | !cli_args.disable_cli_spinners, |
| 111 | ) | 114 | ) |
| 112 | .await?; | 115 | .await?; |
| 113 | // TODO check if there is already a similarly named PR | 116 | // TODO check if there is already a similarly named PR |
| 114 | 117 | ||
| 115 | // println!("failures:"); | ||
| 116 | // println!("ws://relay.anon.io Error: Payment Required"); | ||
| 117 | |||
| 118 | // should we have a relays in Repository event? | ||
| 119 | // yes | ||
| 120 | // | ||
| 121 | |||
| 122 | // TODO connect to relays and post | ||
| 123 | |||
| 124 | Ok(()) | 118 | Ok(()) |
| 125 | } | 119 | } |
| 126 | 120 | ||
| 127 | async fn send_events( | 121 | async fn send_events( |
| 122 | #[cfg(test)] client: &crate::client::MockConnect, | ||
| 123 | #[cfg(not(test))] client: &Client, | ||
| 128 | events: Vec<nostr::Event>, | 124 | events: Vec<nostr::Event>, |
| 129 | keys: nostr::Keys, | ||
| 130 | my_write_relays: Vec<String>, | 125 | my_write_relays: Vec<String>, |
| 131 | repo_read_relays: Vec<String>, | 126 | repo_read_relays: Vec<String>, |
| 132 | animate: bool, | 127 | animate: bool, |
| 133 | ) -> Result<()> { | 128 | ) -> Result<()> { |
| 134 | let (_, _, _, all) = unique_and_duplicate_all(&my_write_relays, &repo_read_relays); | 129 | let (_, _, _, all) = unique_and_duplicate_all(&my_write_relays, &repo_read_relays); |
| 135 | 130 | ||
| 136 | let client = Client::new( | ||
| 137 | ClientParams::default() | ||
| 138 | .with_keys(keys) | ||
| 139 | // .with_fallback_relays(vec!["ws://localhost:8080".to_string()]), | ||
| 140 | .with_fallback_relays(all.iter().map(std::string::ToString::to_string).collect()), | ||
| 141 | ); | ||
| 142 | |||
| 143 | let term = Term::stdout(); | ||
| 144 | term.write_line("connecting to relays...")?; | ||
| 145 | client.connect().await?; | ||
| 146 | term.clear_last_lines(1)?; | ||
| 147 | |||
| 148 | println!( | 131 | println!( |
| 149 | "posting 1 pull request with {} commits...", | 132 | "posting 1 pull request with {} commits...", |
| 150 | events.len() - 1 | 133 | events.len() - 1 |