upleb.uk

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

summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/client.rs31
-rw-r--r--src/repo_ref.rs6
-rw-r--r--src/sub_commands/fetch.rs13
-rw-r--r--src/sub_commands/send.rs55
4 files changed, 55 insertions, 50 deletions
diff --git a/src/client.rs b/src/client.rs
index ccd5cfb..3b18ad8 100644
--- a/src/client.rs
+++ b/src/client.rs
@@ -517,7 +517,13 @@ impl Connect for Client {
517 fresh_profiles = HashSet::new(); 517 fresh_profiles = HashSet::new();
518 518
519 let relay = self.client.relay(&relay_url).await?; 519 let relay = self.client.relay(&relay_url).await?;
520 let events: Vec<nostr::Event> = get_events_of(&relay, filters, &None).await?; 520 let events: Vec<nostr::Event> = get_events_of(&relay, filters.clone(), &None)
521 .await?
522 .iter()
523 // don't process events that don't match filters
524 .filter(|e| filters.iter().any(|f| f.match_event(e)))
525 .cloned()
526 .collect();
521 // TODO: try reconcile 527 // TODO: try reconcile
522 528
523 process_fetched_events( 529 process_fetched_events(
@@ -1363,3 +1369,26 @@ pub struct FetchRequest {
1363 profiles_to_fetch_from_user_relays: HashMap<PublicKey, (Timestamp, Timestamp)>, 1369 profiles_to_fetch_from_user_relays: HashMap<PublicKey, (Timestamp, Timestamp)>,
1364 user_relays_for_profiles: HashSet<Url>, 1370 user_relays_for_profiles: HashSet<Url>,
1365} 1371}
1372
1373pub async fn fetching_with_report(
1374 git_repo_path: &Path,
1375 #[cfg(test)] client: &crate::client::MockConnect,
1376 #[cfg(not(test))] client: &Client,
1377 repo_coordinates: &HashSet<Coordinate>,
1378) -> Result<FetchReport> {
1379 let term = console::Term::stderr();
1380 term.write_line("fetching updates...")?;
1381 let (relay_reports, progress_reporter) = client
1382 .fetch_all(git_repo_path, repo_coordinates, &HashSet::new())
1383 .await?;
1384 if !relay_reports.iter().any(std::result::Result::is_err) {
1385 let _ = progress_reporter.clear();
1386 }
1387 let report = consolidate_fetch_reports(relay_reports);
1388 if report.to_string().is_empty() {
1389 println!("no updates");
1390 } else {
1391 println!("updates: {report}");
1392 }
1393 Ok(report)
1394}
diff --git a/src/repo_ref.rs b/src/repo_ref.rs
index 9bc3201..3e1fd22 100644
--- a/src/repo_ref.rs
+++ b/src/repo_ref.rs
@@ -313,14 +313,14 @@ pub async fn get_repo_coordinates(
313} 313}
314 314
315fn ask_for_naddr() -> Result<Coordinate> { 315fn ask_for_naddr() -> Result<Coordinate> {
316 let mut prompt = "repository naddr";
317 Ok(loop { 316 Ok(loop {
318 if let Ok(c) = Coordinate::parse( 317 if let Ok(c) = Coordinate::parse(
319 Interactor::default().input(PromptInputParms::default().with_prompt(prompt))?, 318 Interactor::default()
319 .input(PromptInputParms::default().with_prompt("repository naddr"))?,
320 ) { 320 ) {
321 break c; 321 break c;
322 } 322 }
323 prompt = "repository valid naddr"; 323 println!("not a valid naddr");
324 }) 324 })
325} 325}
326 326
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,