diff options
Diffstat (limited to 'grasp-audit/src/client.rs')
| -rw-r--r-- | grasp-audit/src/client.rs | 45 |
1 files changed, 44 insertions, 1 deletions
diff --git a/grasp-audit/src/client.rs b/grasp-audit/src/client.rs index 259a317..5995483 100644 --- a/grasp-audit/src/client.rs +++ b/grasp-audit/src/client.rs | |||
| @@ -2,7 +2,7 @@ | |||
| 2 | 2 | ||
| 3 | use crate::audit::{AuditConfig, AuditEventBuilder, AuditMode}; | 3 | use crate::audit::{AuditConfig, AuditEventBuilder, AuditMode}; |
| 4 | use crate::fixtures::FixtureKind; | 4 | use crate::fixtures::FixtureKind; |
| 5 | use anyhow::{anyhow, Result}; | 5 | use anyhow::{anyhow, Context, Result}; |
| 6 | use nostr_sdk::prelude::*; | 6 | use nostr_sdk::prelude::*; |
| 7 | use std::collections::HashMap; | 7 | use std::collections::HashMap; |
| 8 | use std::sync::{Arc, Mutex}; | 8 | use std::sync::{Arc, Mutex}; |
| @@ -181,6 +181,49 @@ impl AuditClient { | |||
| 181 | Ok(event_id) | 181 | Ok(event_id) |
| 182 | } | 182 | } |
| 183 | 183 | ||
| 184 | /// Send an event (with audit tags automatically added) and expect OK, success, 'purgatory:', verify not served - TODO the msg verificaiton is not implemented | ||
| 185 | pub async fn send_event_expect_purgatory_not_served(&self, event: Event) -> Result<EventId> { | ||
| 186 | if self.config.read_only { | ||
| 187 | return Err(anyhow!("Client is in read-only mode")); | ||
| 188 | } | ||
| 189 | |||
| 190 | let output = self.client.send_event(&event).await?; | ||
| 191 | let event_id = *output.id(); | ||
| 192 | |||
| 193 | // Check if any relay rejected the event and return the error message | ||
| 194 | if !output.failed.is_empty() { | ||
| 195 | // Get the first failed relay error message | ||
| 196 | let (relay_url, error) = output.failed.iter().next().unwrap(); | ||
| 197 | return Err(anyhow!("Relay {} rejected event: {}", relay_url, error)); | ||
| 198 | } | ||
| 199 | |||
| 200 | // Wait a bit for event to propagate | ||
| 201 | tokio::time::sleep(Duration::from_millis(300)).await; | ||
| 202 | |||
| 203 | // ------------------------------------------------------ | ||
| 204 | // TODO Magically enable purgatory by uncommenting this: | ||
| 205 | // ------------------------------------------------------ | ||
| 206 | // ------------------------------------------------------ | ||
| 207 | // if !self.is_event_on_relay(event.id).await? { | ||
| 208 | // return Err(anyhow!( | ||
| 209 | // "event sent to relay was served instead of being put in purgatory" | ||
| 210 | // )); | ||
| 211 | // } | ||
| 212 | // ------------------------------------------------------ | ||
| 213 | |||
| 214 | Ok(event_id) | ||
| 215 | } | ||
| 216 | |||
| 217 | /// check if an event is on the relay | ||
| 218 | pub async fn is_event_on_relay(&self, id: EventId) -> Result<bool> { | ||
| 219 | Ok(!self | ||
| 220 | .client | ||
| 221 | .fetch_events(vec![Filter::new().id(id)], Duration::from_secs(1)) | ||
| 222 | .await | ||
| 223 | .context("error trying to query relay for event")? | ||
| 224 | .is_empty()) | ||
| 225 | } | ||
| 226 | |||
| 184 | /// Create an event builder that automatically includes audit tags | 227 | /// Create an event builder that automatically includes audit tags |
| 185 | /// | 228 | /// |
| 186 | /// All events built through this method will automatically have audit tags appended | 229 | /// All events built through this method will automatically have audit tags appended |