Skip to content

Commit

Permalink
nostr: add EventIdOrCoordinate enum
Browse files Browse the repository at this point in the history
  • Loading branch information
yukibtc committed Dec 4, 2023
1 parent c507a12 commit c8f8c44
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 5 deletions.
16 changes: 11 additions & 5 deletions crates/nostr/src/event/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -440,22 +441,27 @@ impl EventBuilder {
}

/// Create delete event
pub fn delete<I>(ids: I) -> Self
pub fn delete<I, T>(ids: I) -> Self
where
I: IntoIterator<Item = EventId>,
I: IntoIterator<Item = T>,
T: Into<EventIdOrCoordinate>,
{
Self::delete_with_reason(ids, "")
}

/// Create delete event with reason
pub fn delete_with_reason<I, S>(ids: I, reason: S) -> Self
pub fn delete_with_reason<I, T, S>(ids: I, reason: S) -> Self
where
I: IntoIterator<Item = EventId>,
I: IntoIterator<Item = T>,
T: Into<EventIdOrCoordinate>,
S: Into<String>,
{
let tags: Vec<Tag> = 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)
Expand Down
32 changes: 32 additions & 0 deletions crates/nostr/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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<EventIdOrCoordinate> for Tag {
fn from(value: EventIdOrCoordinate) -> Self {
match value {
EventIdOrCoordinate::Id(id) => id.into(),
EventIdOrCoordinate::Coordinate(a) => a.into(),
}
}
}

impl From<EventId> for EventIdOrCoordinate {
fn from(id: EventId) -> Self {
Self::Id(id)
}
}

impl From<Coordinate> for EventIdOrCoordinate {
fn from(coordinate: Coordinate) -> Self {
Self::Coordinate(coordinate)
}
}

0 comments on commit c8f8c44

Please sign in to comment.