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 | 0753e0bcdd3d606f8f0226a3980bcd817117abaa (patch) | |
| tree | 8eab6115e2840cbf1c16de441e1168e3fdf18eb8 /src/sub_commands/claim.rs | |
| parent | 1b740dd135aafb52b94b710b3ae24e4aaaa99632 (diff) | |
feat(claim) create basic event
replacable event with root-commit, name, description and relay tags
Diffstat (limited to 'src/sub_commands/claim.rs')
| -rw-r--r-- | src/sub_commands/claim.rs | 114 |
1 files changed, 114 insertions, 0 deletions
diff --git a/src/sub_commands/claim.rs b/src/sub_commands/claim.rs new file mode 100644 index 0000000..5eb66bb --- /dev/null +++ b/src/sub_commands/claim.rs | |||
| @@ -0,0 +1,114 @@ | |||
| 1 | use anyhow::{Context, Result}; | ||
| 2 | use nostr::{EventBuilder, Tag}; | ||
| 3 | |||
| 4 | use super::prs::create::send_events; | ||
| 5 | #[cfg(not(test))] | ||
| 6 | use crate::client::Client; | ||
| 7 | #[cfg(test)] | ||
| 8 | use crate::client::MockConnect; | ||
| 9 | use crate::{ | ||
| 10 | cli_interactor::{Interactor, InteractorPrompt, PromptInputParms}, | ||
| 11 | client::Connect, | ||
| 12 | git::{Repo, RepoActions}, | ||
| 13 | login, Cli, | ||
| 14 | }; | ||
| 15 | |||
| 16 | #[derive(Debug, clap::Args)] | ||
| 17 | pub struct SubCommandArgs { | ||
| 18 | #[clap(short, long)] | ||
| 19 | /// name of repository | ||
| 20 | title: Option<String>, | ||
| 21 | #[clap(short, long)] | ||
| 22 | /// optional description | ||
| 23 | description: Option<String>, | ||
| 24 | } | ||
| 25 | |||
| 26 | pub async fn launch(cli_args: &Cli, args: &SubCommandArgs) -> Result<()> { | ||
| 27 | let git_repo = Repo::discover().context("cannot find a git repository")?; | ||
| 28 | |||
| 29 | let (main_or_master_branch_name, _) = git_repo | ||
| 30 | .get_main_or_master_branch() | ||
| 31 | .context("no main or master branch")?; | ||
| 32 | |||
| 33 | let root_commit = git_repo | ||
| 34 | .get_root_commit(main_or_master_branch_name) | ||
| 35 | .context("failed to get root commit of the repository")?; | ||
| 36 | |||
| 37 | // TODO: check for empty repo | ||
| 38 | // TODO: check for existing maintaiers file | ||
| 39 | // TODO: check for other claims | ||
| 40 | |||
| 41 | let name = match &args.title { | ||
| 42 | Some(t) => t.clone(), | ||
| 43 | None => Interactor::default().input(PromptInputParms::default().with_prompt("name"))?, | ||
| 44 | }; | ||
| 45 | |||
| 46 | let description = match &args.description { | ||
| 47 | Some(t) => t.clone(), | ||
| 48 | None => Interactor::default() | ||
| 49 | .input(PromptInputParms::default().with_prompt("description (Optional)"))?, | ||
| 50 | }; | ||
| 51 | |||
| 52 | #[cfg(not(test))] | ||
| 53 | let mut client = Client::default(); | ||
| 54 | #[cfg(test)] | ||
| 55 | let mut client = <MockConnect as std::default::Default>::default(); | ||
| 56 | |||
| 57 | let (keys, user_ref) = login::launch(&cli_args.nsec, &cli_args.password, Some(&client)).await?; | ||
| 58 | |||
| 59 | client.set_keys(&keys).await; | ||
| 60 | |||
| 61 | // TODO: choice input defaulting to user relay list filtered by non paid relays | ||
| 62 | let repo_relays: Vec<String> = vec![ | ||
| 63 | "ws://localhost:8055".to_string(), | ||
| 64 | "ws://localhost:8056".to_string(), | ||
| 65 | ]; | ||
| 66 | |||
| 67 | println!("publishing repostory reference..."); | ||
| 68 | |||
| 69 | send_events( | ||
| 70 | &client, | ||
| 71 | vec![generate_repo_event( | ||
| 72 | &name, | ||
| 73 | &description, | ||
| 74 | &repo_relays, | ||
| 75 | &root_commit.to_string(), | ||
| 76 | &keys, | ||
| 77 | )?], | ||
| 78 | user_ref.relays.write(), | ||
| 79 | repo_relays, | ||
| 80 | !cli_args.disable_cli_spinners, | ||
| 81 | ) | ||
| 82 | .await?; | ||
| 83 | |||
| 84 | Ok(()) | ||
| 85 | } | ||
| 86 | |||
| 87 | fn generate_repo_event( | ||
| 88 | name: &str, | ||
| 89 | description: &str, | ||
| 90 | relays: &[String], | ||
| 91 | // git_server: String, | ||
| 92 | root_commit: &String, | ||
| 93 | keys: &nostr::Keys, | ||
| 94 | ) -> Result<nostr::Event> { | ||
| 95 | EventBuilder::new( | ||
| 96 | nostr::event::Kind::Custom(30017), | ||
| 97 | "", | ||
| 98 | &[ | ||
| 99 | vec![ | ||
| 100 | Tag::Identifier(root_commit.to_string()), | ||
| 101 | Tag::Reference(format!("r-{root_commit}")), | ||
| 102 | Tag::Name(name.to_owned()), | ||
| 103 | Tag::Description(description.to_owned()), | ||
| 104 | ], | ||
| 105 | relays.iter().map(|r| Tag::Relay(r.into())).collect(), | ||
| 106 | // git_servers | ||
| 107 | // other maintainers | ||
| 108 | // code languages and hashtags | ||
| 109 | ] | ||
| 110 | .concat(), | ||
| 111 | ) | ||
| 112 | .to_event(keys) | ||
| 113 | .context("failed to create pr event") | ||
| 114 | } | ||