upleb.uk

Public git repos — served from a NIP-34 GRASP relay at git.upleb.uk

summaryrefslogtreecommitdiff
path: root/src/sub_commands
diff options
context:
space:
mode:
authorDanConwayDev <DanConwayDev@protonmail.com>2024-07-19 20:38:00 +0100
committerDanConwayDev <DanConwayDev@protonmail.com>2024-07-19 20:38:00 +0100
commitab1214060a7a2d55068a7ccc9c7f6a04fd7d5aa2 (patch)
treee561bcc093d393c2d7056a6d6c5f52e886eead27 /src/sub_commands
parent2e54dd09a1a3b42903eee00adf4472d8b679dcb1 (diff)
feat: intergrate `fetch` into `send`
reworking the tests and test suite as appropriate
Diffstat (limited to 'src/sub_commands')
-rw-r--r--src/sub_commands/fetch.rs13
-rw-r--r--src/sub_commands/send.rs55
2 files changed, 22 insertions, 46 deletions
diff --git a/src/sub_commands/fetch.rs b/src/sub_commands/fetch.rs
index 28eb960..ab6e0fc 100644
--- a/src/sub_commands/fetch.rs
+++ b/src/sub_commands/fetch.rs
@@ -9,7 +9,7 @@ use crate::client::Client;
9#[cfg(test)] 9#[cfg(test)]
10use crate::client::MockConnect; 10use crate::client::MockConnect;
11use crate::{ 11use crate::{
12 client::{consolidate_fetch_reports, Connect}, 12 client::{fetching_with_report, Connect},
13 git::{Repo, RepoActions}, 13 git::{Repo, RepoActions},
14 repo_ref::get_repo_coordinates, 14 repo_ref::get_repo_coordinates,
15 Cli, 15 Cli,
@@ -38,16 +38,7 @@ pub async fn launch(args: &Cli, command_args: &SubCommandArgs) -> Result<()> {
38 } 38 }
39 repo_coordinates 39 repo_coordinates
40 }; 40 };
41 println!("fetching updates..."); 41 fetching_with_report(git_repo.get_path()?, &client, &repo_coordinates).await?;
42 let (relay_reports, _) = client
43 .fetch_all(git_repo.get_path()?, &repo_coordinates, &HashSet::new())
44 .await?;
45 let report = consolidate_fetch_reports(relay_reports);
46 if report.to_string().is_empty() {
47 println!("no updates");
48 } else {
49 println!("updates: {report}");
50 }
51 client.disconnect().await?; 42 client.disconnect().await?;
52 Ok(()) 43 Ok(())
53} 44}
diff --git a/src/sub_commands/send.rs b/src/sub_commands/send.rs
index 410e119..a289def 100644
--- a/src/sub_commands/send.rs
+++ b/src/sub_commands/send.rs
@@ -1,4 +1,4 @@
1use std::{str::FromStr, time::Duration}; 1use std::{path::Path, str::FromStr, time::Duration};
2 2
3use anyhow::{bail, Context, Result}; 3use anyhow::{bail, Context, Result};
4use console::Style; 4use console::Style;
@@ -19,10 +19,12 @@ use crate::{
19 cli_interactor::{ 19 cli_interactor::{
20 Interactor, InteractorPrompt, PromptConfirmParms, PromptInputParms, PromptMultiChoiceParms, 20 Interactor, InteractorPrompt, PromptConfirmParms, PromptInputParms, PromptMultiChoiceParms,
21 }, 21 },
22 client::{sign_event, Connect}, 22 client::{
23 fetching_with_report, get_events_from_cache, get_repo_ref_from_cache, sign_event, Connect,
24 },
23 git::{Repo, RepoActions}, 25 git::{Repo, RepoActions},
24 login, 26 login,
25 repo_ref::{self, RepoRef, REPO_REF_KIND}, 27 repo_ref::{get_repo_coordinates, RepoRef, REPO_REF_KIND},
26 Cli, 28 Cli,
27}; 29};
28 30
@@ -49,6 +51,7 @@ pub struct SubCommandArgs {
49#[allow(clippy::too_many_lines)] 51#[allow(clippy::too_many_lines)]
50pub async fn launch(cli_args: &Cli, args: &SubCommandArgs) -> Result<()> { 52pub async fn launch(cli_args: &Cli, args: &SubCommandArgs) -> Result<()> {
51 let git_repo = Repo::discover().context("cannot find a git repository")?; 53 let git_repo = Repo::discover().context("cannot find a git repository")?;
54 let git_repo_path = git_repo.get_path()?;
52 55
53 let (main_branch_name, main_tip) = git_repo 56 let (main_branch_name, main_tip) = git_repo
54 .get_main_or_master_branch() 57 .get_main_or_master_branch()
@@ -59,13 +62,13 @@ pub async fn launch(cli_args: &Cli, args: &SubCommandArgs) -> Result<()> {
59 #[cfg(test)] 62 #[cfg(test)]
60 let mut client = <MockConnect as std::default::Default>::default(); 63 let mut client = <MockConnect as std::default::Default>::default();
61 64
62 let (root_proposal_id, mention_tags) = get_root_proposal_id_and_mentions_from_in_reply_to( 65 let repo_coordinates = get_repo_coordinates(&git_repo, &client).await?;
63 &client, 66
64 // TODO: user repo relays when when event cache is in place 67 fetching_with_report(git_repo_path, &client, &repo_coordinates).await?;
65 client.get_fallback_relays(), 68
66 &args.in_reply_to, 69 let (root_proposal_id, mention_tags) =
67 ) 70 get_root_proposal_id_and_mentions_from_in_reply_to(git_repo.get_path()?, &args.in_reply_to)
68 .await?; 71 .await?;
69 72
70 if let Some(root_ref) = args.in_reply_to.first() { 73 if let Some(root_ref) = args.in_reply_to.first() {
71 if root_proposal_id.is_some() { 74 if root_proposal_id.is_some() {
@@ -191,17 +194,7 @@ pub async fn launch(cli_args: &Cli, args: &SubCommandArgs) -> Result<()> {
191 194
192 client.set_signer(signer.clone()).await; 195 client.set_signer(signer.clone()).await;
193 196
194 let repo_ref = repo_ref::fetch( 197 let repo_ref = get_repo_ref_from_cache(git_repo_path, &repo_coordinates).await?;
195 &git_repo,
196 git_repo
197 .get_root_commit()
198 .context("failed to get root commit of the repository")?
199 .to_string(),
200 &client,
201 user_ref.relays.write(),
202 true,
203 )
204 .await?;
205 198
206 // oldest first 199 // oldest first
207 commits.reverse(); 200 commits.reverse();
@@ -520,9 +513,7 @@ fn summarise_commit_for_selection(git_repo: &Repo, commit: &Sha1Hash) -> Result<
520} 513}
521 514
522async fn get_root_proposal_id_and_mentions_from_in_reply_to( 515async fn get_root_proposal_id_and_mentions_from_in_reply_to(
523 #[cfg(test)] client: &crate::client::MockConnect, 516 git_repo_path: &Path,
524 #[cfg(not(test))] client: &Client,
525 repo_relays: &[String],
526 in_reply_to: &[String], 517 in_reply_to: &[String],
527) -> Result<(Option<String>, Vec<nostr::Tag>)> { 518) -> Result<(Option<String>, Vec<nostr::Tag>)> {
528 let root_proposal_id = if let Some(first) = in_reply_to.first() { 519 let root_proposal_id = if let Some(first) = in_reply_to.first() {
@@ -535,13 +526,10 @@ async fn get_root_proposal_id_and_mentions_from_in_reply_to(
535 marker: _, 526 marker: _,
536 public_key: _, 527 public_key: _,
537 }) => { 528 }) => {
538 let events = client 529 let events =
539 .get_events( 530 get_events_from_cache(git_repo_path, vec![nostr::Filter::new().id(*event_id)])
540 repo_relays.to_vec(), 531 .await?;
541 vec![nostr::Filter::new().id(*event_id)], 532
542 )
543 .await
544 .context("whilst getting events specified in --in-reply-to")?;
545 if let Some(first) = events.iter().find(|e| e.id.eq(event_id)) { 533 if let Some(first) = events.iter().find(|e| e.id.eq(event_id)) {
546 if event_is_patch_set_root(first) { 534 if event_is_patch_set_root(first) {
547 Some(event_id.to_string()) 535 Some(event_id.to_string())
@@ -549,10 +537,7 @@ async fn get_root_proposal_id_and_mentions_from_in_reply_to(
549 None 537 None
550 } 538 }
551 } else { 539 } else {
552 bail!( 540 None
553 "cannot find first event specified in --in-reply-to \"{}\"",
554 first,
555 )
556 } 541 }
557 } 542 }
558 _ => None, 543 _ => None,