Skip to content

Commit cd12395

Browse files
committed
nostr: add EventDeletionRequest struct
- Add `EventDeletionRequest` struct - Remove `EventIdOrCoordinate` enum - Add unit tests for NIP09 stuff Signed-off-by: Yuki Kishimoto <[email protected]>
1 parent f7b2b01 commit cd12395

File tree

14 files changed

+253
-99
lines changed

14 files changed

+253
-99
lines changed

CHANGELOG.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,10 @@
4545
* nostr: change `EventBuilder::git_patch` constructor signature ([Yuki Kishimoto])
4646
* nostr: `TagStandard::parse` now returns `Err(Error::UnknownStandardizedTag)` for non-lowercase hashtags as per NIP-24 ([awiteb])
4747
* nostr: update `NostrWalletConnectURI` to support multiple relay URLs ([Yuki Kishimoto])
48+
* nostr: remove `EventIdOrCoordinate` enum ([Yuki Kishimoto])
49+
* nostr: change `EventBuilder::delete` arguments ([Yuki Kishimoto])
4850
* pool: drop `RelayFiltering` ([Yuki Kishimoto])
49-
* sdk: change `Client::fetch_metadata` output ([Yuki Kishimoto])
51+
* sdk: change `Client::fetch_metadata` output ([Yuki Kishimoto])
5052

5153
### Changed
5254

@@ -88,6 +90,7 @@
8890
* nostr: add `TagStandard::Client` variant ([Yuki Kishimoto])
8991
* nostr: add `EventBuilder::dedup_tags` method ([Yuki Kishimoto])
9092
* nostr: impl `FromIterator<Tag>` for `Tags` ([Yuki Kishimoto])
93+
* nostr: add `EventDeletionRequest` struct ([Yuki Kishimoto])
9194
* pool: event verification cache ([Yuki Kishimoto])
9295
* pool: add `AdmitPolicy` trait ([Yuki Kishimoto])
9396
* ffi: add Mac Catalyst support in Swift package ([Yuki Kishimoto])

bindings/nostr-sdk-ffi/src/protocol/event/builder.rs

Lines changed: 6 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ use std::collections::HashMap;
66
use std::ops::Deref;
77
use std::sync::Arc;
88

9-
use nostr::util::EventIdOrCoordinate;
109
use nostr::{RelayUrl, Url};
1110
use uniffi::Object;
1211

@@ -15,6 +14,7 @@ use crate::error::Result;
1514
use crate::protocol::event::{PublicKey, Tag, Timestamp, UnsignedEvent};
1615
use crate::protocol::key::Keys;
1716
use crate::protocol::nips::nip01::{Coordinate, Metadata};
17+
use crate::protocol::nips::nip09::EventDeletionRequest;
1818
use crate::protocol::nips::nip15::{ProductData, StallData};
1919
use crate::protocol::nips::nip34::{GitIssue, GitPatch, GitRepositoryAnnouncement};
2020
use crate::protocol::nips::nip46::NostrConnectMessage;
@@ -253,29 +253,13 @@ impl EventBuilder {
253253
})
254254
}
255255

256-
/// Event deletion
256+
/// Event deletion request
257257
///
258258
/// <https://github.com/nostr-protocol/nips/blob/master/09.md>
259-
#[uniffi::constructor(default(ids = [], coordinates = [], reason = None))]
260-
pub fn delete(
261-
ids: &[Arc<EventId>],
262-
coordinates: &[Arc<Coordinate>],
263-
reason: Option<String>,
264-
) -> Self {
265-
let coordinates = coordinates
266-
.iter()
267-
.map(|c| c.as_ref().deref().clone())
268-
.map(EventIdOrCoordinate::from);
269-
let ids = ids
270-
.iter()
271-
.map(|e| ***e)
272-
.map(EventIdOrCoordinate::from)
273-
.chain(coordinates);
274-
Self {
275-
inner: match reason {
276-
Some(reason) => nostr::EventBuilder::delete_with_reason(ids, reason),
277-
None => nostr::EventBuilder::delete(ids),
278-
},
259+
#[uniffi::constructor]
260+
pub fn delete(request: EventDeletionRequest) -> Self {
261+
Self {
262+
inner: nostr::EventBuilder::delete(request.into()),
279263
}
280264
}
281265

bindings/nostr-sdk-ffi/src/protocol/nips/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
pub mod nip01;
66
pub mod nip04;
77
pub mod nip05;
8+
pub mod nip09;
89
pub mod nip10;
910
pub mod nip11;
1011
pub mod nip13;
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// Copyright (c) 2023-2025 Rust Nostr Developers
2+
// Distributed under the MIT software license
3+
4+
use std::ops::Deref;
5+
use std::sync::Arc;
6+
7+
use nostr::nips::nip09;
8+
use uniffi::Record;
9+
10+
use super::nip01::Coordinate;
11+
use crate::protocol::event::EventId;
12+
13+
/// Event deletion request
14+
#[derive(Record)]
15+
pub struct EventDeletionRequest {
16+
/// Event IDs
17+
pub ids: Vec<Arc<EventId>>,
18+
/// Event coordinates
19+
pub coordinates: Vec<Arc<Coordinate>>,
20+
/// Optional reason
21+
pub reason: Option<String>,
22+
}
23+
24+
impl From<EventDeletionRequest> for nip09::EventDeletionRequest {
25+
fn from(request: EventDeletionRequest) -> Self {
26+
Self {
27+
ids: request.ids.into_iter().map(|id| **id).collect(),
28+
coordinates: request
29+
.coordinates
30+
.into_iter()
31+
.map(|c| c.as_ref().deref().clone())
32+
.collect(),
33+
reason: request.reason,
34+
}
35+
}
36+
}

bindings/nostr-sdk-js/src/protocol/event/builder.rs

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ use super::{JsEvent, JsEventId, JsKind, JsTag, JsUnsignedEvent};
1111
use crate::error::{into_err, Result};
1212
use crate::protocol::key::{JsKeys, JsPublicKey};
1313
use crate::protocol::nips::nip01::{JsCoordinate, JsMetadata};
14+
use crate::protocol::nips::nip09::JsEventDeletionRequest;
1415
use crate::protocol::nips::nip15::{JsProductData, JsStallData};
1516
use crate::protocol::nips::nip34::{JsGitIssue, JsGitRepositoryAnnouncement};
1617
use crate::protocol::nips::nip51::{
@@ -247,25 +248,9 @@ impl JsEventBuilder {
247248
///
248249
/// <https://github.com/nostr-protocol/nips/blob/master/09.md>
249250
#[wasm_bindgen]
250-
pub fn delete(
251-
ids: Vec<JsEventId>,
252-
coordinates: Vec<JsCoordinate>,
253-
reason: Option<String>,
254-
) -> Self {
255-
let coordinates = coordinates
256-
.into_iter()
257-
.map(|c| c.deref().clone())
258-
.map(EventIdOrCoordinate::from);
259-
let ids = ids
260-
.into_iter()
261-
.map(|id| *id)
262-
.map(EventIdOrCoordinate::from)
263-
.chain(coordinates);
264-
Self {
265-
inner: match reason {
266-
Some(reason) => EventBuilder::delete_with_reason(ids, reason),
267-
None => EventBuilder::delete(ids),
268-
},
251+
pub fn delete(request: JsEventDeletionRequest) -> Self {
252+
Self {
253+
inner: EventBuilder::delete(request.into()),
269254
}
270255
}
271256

bindings/nostr-sdk-js/src/protocol/nips/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ pub mod nip01;
66
pub mod nip04;
77
pub mod nip05;
88
pub mod nip07;
9+
pub mod nip09;
910
pub mod nip11;
1011
pub mod nip15;
1112
pub mod nip19;

bindings/nostr-sdk-js/src/protocol/nips/nip01.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,12 @@ impl From<Coordinate> for JsCoordinate {
3030
}
3131
}
3232

33+
impl From<JsCoordinate> for Coordinate {
34+
fn from(coordinate: JsCoordinate) -> Self {
35+
coordinate.inner
36+
}
37+
}
38+
3339
#[wasm_bindgen(js_class = Coordinate)]
3440
impl JsCoordinate {
3541
#[wasm_bindgen(constructor)]
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// Copyright (c) 2023-2025 Rust Nostr Developers
2+
// Distributed under the MIT software license
3+
4+
use nostr_sdk::prelude::*;
5+
use wasm_bindgen::prelude::*;
6+
7+
use super::nip01::JsCoordinate;
8+
use crate::protocol::event::id::JsEventId;
9+
10+
/// Event deletion request
11+
#[wasm_bindgen(js_name = GitRepositoryAnnouncement)]
12+
pub struct JsEventDeletionRequest {
13+
/// Event IDs
14+
#[wasm_bindgen(getter_with_clone)]
15+
pub ids: Vec<JsEventId>,
16+
/// Event coordinates
17+
#[wasm_bindgen(getter_with_clone)]
18+
pub coordinates: Vec<JsCoordinate>,
19+
/// Optional reason
20+
#[wasm_bindgen(getter_with_clone)]
21+
pub reason: Option<String>,
22+
}
23+
24+
impl From<JsEventDeletionRequest> for EventDeletionRequest {
25+
fn from(request: JsEventDeletionRequest) -> Self {
26+
Self {
27+
ids: request.ids.into_iter().map(Into::into).collect(),
28+
coordinates: request.coordinates.into_iter().map(Into::into).collect(),
29+
reason: request.reason,
30+
}
31+
}
32+
}

crates/nostr/examples/nip09.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,11 @@ fn main() -> Result<()> {
1010
let event_id =
1111
EventId::from_hex("7469af3be8c8e06e1b50ef1caceba30392ddc0b6614507398b7d7daa4c218e96")?;
1212

13-
let event: Event =
14-
EventBuilder::delete_with_reason(vec![event_id], "these posts were published by accident")
15-
.sign_with_keys(&keys)?;
13+
let request = EventDeletionRequest::new()
14+
.id(event_id)
15+
.reason("these posts were published by accident");
16+
17+
let event: Event = EventBuilder::delete(request).sign_with_keys(&keys)?;
1618
println!("{}", event.as_json());
1719

1820
Ok(())

crates/nostr/src/event/builder.rs

Lines changed: 3 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -700,32 +700,12 @@ impl EventBuilder {
700700
}
701701
}
702702

703-
/// Event deletion
703+
/// Event deletion request
704704
///
705705
/// <https://github.com/nostr-protocol/nips/blob/master/09.md>
706706
#[inline]
707-
pub fn delete<I, T>(ids: I) -> Self
708-
where
709-
I: IntoIterator<Item = T>,
710-
T: Into<EventIdOrCoordinate>,
711-
{
712-
Self::delete_with_reason(ids, "")
713-
}
714-
715-
/// Event deletion with reason
716-
///
717-
/// <https://github.com/nostr-protocol/nips/blob/master/09.md>
718-
pub fn delete_with_reason<I, T, S>(ids: I, reason: S) -> Self
719-
where
720-
I: IntoIterator<Item = T>,
721-
T: Into<EventIdOrCoordinate>,
722-
S: Into<String>,
723-
{
724-
let tags = ids.into_iter().map(|t| {
725-
let middle: EventIdOrCoordinate = t.into();
726-
middle.into()
727-
});
728-
Self::new(Kind::EventDeletion, reason.into()).tags(tags)
707+
pub fn delete(request: EventDeletionRequest) -> Self {
708+
request.to_event_builder()
729709
}
730710

731711
/// Request to vanish

0 commit comments

Comments
 (0)