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:
authorDanConwayDev <DanConwayDev@protonmail.com>2024-07-19 22:10:23 +0100
committerDanConwayDev <DanConwayDev@protonmail.com>2024-07-19 22:10:23 +0100
commit29a093993ce7d0210ac39ceb1a25acc9350492e7 (patch)
treeb8ae2a13b975b051bda7e9461f1b8fddd986b3ca /src
parentdde029b4f5988cfa830ebc36bee0f35c93bd2544 (diff)
feat: save created events to cache
as soon as they are successfully sent to at least one relay
Diffstat (limited to 'src')
-rw-r--r--src/client.rs21
-rw-r--r--src/sub_commands/init.rs2
-rw-r--r--src/sub_commands/push.rs1
-rw-r--r--src/sub_commands/send.rs7
4 files changed, 27 insertions, 4 deletions
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 {
62 fn get_fallback_relays(&self) -> &Vec<String>; 62 fn get_fallback_relays(&self) -> &Vec<String>;
63 fn get_more_fallback_relays(&self) -> &Vec<String>; 63 fn get_more_fallback_relays(&self) -> &Vec<String>;
64 fn get_blaster_relays(&self) -> &Vec<String>; 64 fn get_blaster_relays(&self) -> &Vec<String>;
65 async fn send_event_to(&self, url: &str, event: nostr::event::Event) -> Result<nostr::EventId>; 65 async fn send_event_to(
66 &self,
67 git_repo_path: &Path,
68 url: &str,
69 event: nostr::event::Event,
70 ) -> Result<nostr::EventId>;
66 async fn get_events( 71 async fn get_events(
67 &self, 72 &self,
68 relays: Vec<String>, 73 relays: Vec<String>,
@@ -189,11 +194,21 @@ impl Connect for Client {
189 &self.blaster_relays 194 &self.blaster_relays
190 } 195 }
191 196
192 async fn send_event_to(&self, url: &str, event: Event) -> Result<nostr::EventId> { 197 async fn send_event_to(
198 &self,
199 git_repo_path: &Path,
200 url: &str,
201 event: Event,
202 ) -> Result<nostr::EventId> {
193 self.client.add_relay(url).await?; 203 self.client.add_relay(url).await?;
194 #[allow(clippy::large_futures)] 204 #[allow(clippy::large_futures)]
195 self.client.connect_relay(url).await?; 205 self.client.connect_relay(url).await?;
196 Ok(self.client.send_event_to(vec![url], event).await?) 206 let res = self.client.send_event_to(vec![url], event.clone()).await?;
207 save_event_in_cache(git_repo_path, &event).await?;
208 if event.kind().eq(&Kind::Custom(REPO_REF_KIND)) {
209 save_event_in_global_cache(git_repo_path, &event).await?;
210 }
211 Ok(res)
197 } 212 }
198 213
199 async fn get_events( 214 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 {
52#[allow(clippy::too_many_lines)] 52#[allow(clippy::too_many_lines)]
53pub async fn launch(cli_args: &Cli, args: &SubCommandArgs) -> Result<()> { 53pub async fn launch(cli_args: &Cli, args: &SubCommandArgs) -> Result<()> {
54 let git_repo = Repo::discover().context("cannot find a git repository")?; 54 let git_repo = Repo::discover().context("cannot find a git repository")?;
55 let git_repo_path = git_repo.get_path()?;
55 56
56 let root_commit = git_repo 57 let root_commit = git_repo
57 .get_root_commit() 58 .get_root_commit()
@@ -313,6 +314,7 @@ pub async fn launch(cli_args: &Cli, args: &SubCommandArgs) -> Result<()> {
313 314
314 send_events( 315 send_events(
315 &client, 316 &client,
317 git_repo_path,
316 vec![repo_event], 318 vec![repo_event],
317 user_ref.relays.write(), 319 user_ref.relays.write(),
318 relays.clone(), 320 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<()> {
191 191
192 send_events( 192 send_events(
193 &client, 193 &client,
194 git_repo_path,
194 patch_events, 195 patch_events,
195 user_ref.relays.write(), 196 user_ref.relays.write(),
196 repo_ref.relays.clone(), 197 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
239 239
240 send_events( 240 send_events(
241 &client, 241 &client,
242 git_repo_path,
242 events.clone(), 243 events.clone(),
243 user_ref.relays.write(), 244 user_ref.relays.write(),
244 repo_ref.relays.clone(), 245 repo_ref.relays.clone(),
@@ -279,6 +280,7 @@ pub async fn launch(cli_args: &Cli, args: &SubCommandArgs, no_fetch: bool) -> Re
279pub async fn send_events( 280pub async fn send_events(
280 #[cfg(test)] client: &crate::client::MockConnect, 281 #[cfg(test)] client: &crate::client::MockConnect,
281 #[cfg(not(test))] client: &Client, 282 #[cfg(not(test))] client: &Client,
283 git_repo_path: &Path,
282 events: Vec<nostr::Event>, 284 events: Vec<nostr::Event>,
283 my_write_relays: Vec<String>, 285 my_write_relays: Vec<String>,
284 repo_read_relays: Vec<String>, 286 repo_read_relays: Vec<String>,
@@ -392,7 +394,10 @@ pub async fn send_events(
392 pb.inc(0); // need to make pb display intially 394 pb.inc(0); // need to make pb display intially
393 let mut failed = false; 395 let mut failed = false;
394 for event in &events { 396 for event in &events {
395 match client.send_event_to(relay.as_str(), event.clone()).await { 397 match client
398 .send_event_to(git_repo_path, relay.as_str(), event.clone())
399 .await
400 {
396 Ok(_) => pb.inc(1), 401 Ok(_) => pb.inc(1),
397 Err(e) => { 402 Err(e) => {
398 pb.set_style(pb_after_style_failed.clone()); 403 pb.set_style(pb_after_style_failed.clone());