From 29a093993ce7d0210ac39ceb1a25acc9350492e7 Mon Sep 17 00:00:00 2001 From: DanConwayDev Date: Fri, 19 Jul 2024 22:10:23 +0100 Subject: feat: save created events to cache as soon as they are successfully sent to at least one relay --- src/client.rs | 21 ++++++++++++++++++--- src/sub_commands/init.rs | 2 ++ src/sub_commands/push.rs | 1 + src/sub_commands/send.rs | 7 ++++++- 4 files changed, 27 insertions(+), 4 deletions(-) (limited to 'src') diff --git a/src/client.rs b/src/client.rs index 3b18ad8..880cb6b 100644 --- a/src/client.rs +++ b/src/client.rs @@ -62,7 +62,12 @@ pub trait Connect { fn get_fallback_relays(&self) -> &Vec; fn get_more_fallback_relays(&self) -> &Vec; fn get_blaster_relays(&self) -> &Vec; - async fn send_event_to(&self, url: &str, event: nostr::event::Event) -> Result; + async fn send_event_to( + &self, + git_repo_path: &Path, + url: &str, + event: nostr::event::Event, + ) -> Result; async fn get_events( &self, relays: Vec, @@ -189,11 +194,21 @@ impl Connect for Client { &self.blaster_relays } - async fn send_event_to(&self, url: &str, event: Event) -> Result { + async fn send_event_to( + &self, + git_repo_path: &Path, + url: &str, + event: Event, + ) -> Result { self.client.add_relay(url).await?; #[allow(clippy::large_futures)] self.client.connect_relay(url).await?; - Ok(self.client.send_event_to(vec![url], event).await?) + let res = self.client.send_event_to(vec![url], event.clone()).await?; + save_event_in_cache(git_repo_path, &event).await?; + if event.kind().eq(&Kind::Custom(REPO_REF_KIND)) { + save_event_in_global_cache(git_repo_path, &event).await?; + } + Ok(res) } async fn get_events( diff --git a/src/sub_commands/init.rs b/src/sub_commands/init.rs index 44e288f..e46bf74 100644 --- a/src/sub_commands/init.rs +++ b/src/sub_commands/init.rs @@ -52,6 +52,7 @@ pub struct SubCommandArgs { #[allow(clippy::too_many_lines)] pub async fn launch(cli_args: &Cli, args: &SubCommandArgs) -> Result<()> { let git_repo = Repo::discover().context("cannot find a git repository")?; + let git_repo_path = git_repo.get_path()?; let root_commit = git_repo .get_root_commit() @@ -313,6 +314,7 @@ pub async fn launch(cli_args: &Cli, args: &SubCommandArgs) -> Result<()> { send_events( &client, + git_repo_path, vec![repo_event], user_ref.relays.write(), relays.clone(), diff --git a/src/sub_commands/push.rs b/src/sub_commands/push.rs index 111a14a..d05158f 100644 --- a/src/sub_commands/push.rs +++ b/src/sub_commands/push.rs @@ -191,6 +191,7 @@ pub async fn launch(cli_args: &Cli, args: &SubCommandArgs) -> Result<()> { send_events( &client, + git_repo_path, patch_events, user_ref.relays.write(), repo_ref.relays.clone(), diff --git a/src/sub_commands/send.rs b/src/sub_commands/send.rs index f94eed3..95d3eb0 100644 --- a/src/sub_commands/send.rs +++ b/src/sub_commands/send.rs @@ -239,6 +239,7 @@ pub async fn launch(cli_args: &Cli, args: &SubCommandArgs, no_fetch: bool) -> Re send_events( &client, + git_repo_path, events.clone(), user_ref.relays.write(), repo_ref.relays.clone(), @@ -279,6 +280,7 @@ pub async fn launch(cli_args: &Cli, args: &SubCommandArgs, no_fetch: bool) -> Re pub async fn send_events( #[cfg(test)] client: &crate::client::MockConnect, #[cfg(not(test))] client: &Client, + git_repo_path: &Path, events: Vec, my_write_relays: Vec, repo_read_relays: Vec, @@ -392,7 +394,10 @@ pub async fn send_events( pb.inc(0); // need to make pb display intially let mut failed = false; for event in &events { - match client.send_event_to(relay.as_str(), event.clone()).await { + match client + .send_event_to(git_repo_path, relay.as_str(), event.clone()) + .await + { Ok(_) => pb.inc(1), Err(e) => { pb.set_style(pb_after_style_failed.clone()); -- cgit v1.2.3