From c8f8c44405dc24133a5f4292789e1b9cd92dc44b Mon Sep 17 00:00:00 2001 From: Yuki Kishimoto Date: Mon, 4 Dec 2023 09:44:34 +0100 Subject: [PATCH] nostr: add `EventIdOrCoordinate` enum --- crates/nostr/src/event/builder.rs | 16 +++++++++++----- crates/nostr/src/util.rs | 32 +++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+), 5 deletions(-) diff --git a/crates/nostr/src/event/builder.rs b/crates/nostr/src/event/builder.rs index 38513ba81..29ba0da1b 100644 --- a/crates/nostr/src/event/builder.rs +++ b/crates/nostr/src/event/builder.rs @@ -35,6 +35,7 @@ use crate::nips::{nip13, nip58}; use crate::types::time::Instant; use crate::types::time::TimeSupplier; use crate::types::{ChannelId, Contact, Metadata, Timestamp}; +use crate::util::EventIdOrCoordinate; #[cfg(feature = "std")] use crate::SECP256K1; use crate::{JsonUtil, RelayMetadata, UncheckedUrl}; @@ -440,22 +441,27 @@ impl EventBuilder { } /// Create delete event - pub fn delete(ids: I) -> Self + pub fn delete(ids: I) -> Self where - I: IntoIterator, + I: IntoIterator, + T: Into, { Self::delete_with_reason(ids, "") } /// Create delete event with reason - pub fn delete_with_reason(ids: I, reason: S) -> Self + pub fn delete_with_reason(ids: I, reason: S) -> Self where - I: IntoIterator, + I: IntoIterator, + T: Into, S: Into, { let tags: Vec = ids .into_iter() - .map(|id| Tag::Event(id, None, None)) + .map(|t| { + let middle: EventIdOrCoordinate = t.into(); + middle.into() + }) .collect(); Self::new(Kind::EventDeletion, reason.into(), &tags) diff --git a/crates/nostr/src/util.rs b/crates/nostr/src/util.rs index ce04deca7..00b061d5d 100644 --- a/crates/nostr/src/util.rs +++ b/crates/nostr/src/util.rs @@ -14,6 +14,9 @@ use once_cell::sync::Lazy; use serde::de::DeserializeOwned; use serde::Serialize; +use crate::nips::nip01::Coordinate; +use crate::{EventId, Tag}; + /// Generate shared key /// /// **Important: use of a strong cryptographic hash function may be critical to security! Do NOT use @@ -63,3 +66,32 @@ where serde_json::json!(self).to_string() } } + +/// Event ID or Coordinate +pub enum EventIdOrCoordinate { + /// Event ID + Id(EventId), + /// Event Coordinate (`a` tag) + Coordinate(Coordinate), +} + +impl From for Tag { + fn from(value: EventIdOrCoordinate) -> Self { + match value { + EventIdOrCoordinate::Id(id) => id.into(), + EventIdOrCoordinate::Coordinate(a) => a.into(), + } + } +} + +impl From for EventIdOrCoordinate { + fn from(id: EventId) -> Self { + Self::Id(id) + } +} + +impl From for EventIdOrCoordinate { + fn from(coordinate: Coordinate) -> Self { + Self::Coordinate(coordinate) + } +}