Skip to content

Commit

Permalink
nostr: adapt EventBuilder::repost to last NIP18 changes
Browse files Browse the repository at this point in the history
Add `Tag::Kind` and `Kind::GenericRepost`
  • Loading branch information
yukibtc committed Mar 4, 2024
1 parent 1f4a9fa commit 3900827
Show file tree
Hide file tree
Showing 9 changed files with 82 additions and 29 deletions.
10 changes: 5 additions & 5 deletions bindings/nostr-ffi/src/event/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,13 +160,13 @@ impl EventBuilder {
}

#[uniffi::constructor]
pub fn repost(event_id: Arc<EventId>, public_key: Arc<PublicKey>) -> Arc<Self> {
Arc::new(Self {
pub fn repost(event: Arc<Event>, relay_url: Option<String>) -> Self {
Self {
inner: nostr::EventBuilder::repost(
event_id.as_ref().into(),
*public_key.as_ref().deref(),
event.as_ref().deref(),
relay_url.map(UncheckedUrl::from),
),
})
}
}

/// Create delete event
Expand Down
11 changes: 11 additions & 0 deletions bindings/nostr-ffi/src/event/tag.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,8 @@ pub enum TagKind {
U,
/// SHA256
X,
/// Kind
K,
/// Relay
RelayUrl,
/// Nonce
Expand Down Expand Up @@ -269,6 +271,7 @@ impl From<tag::TagKind> for TagKind {
tag::TagKind::M => Self::M,
tag::TagKind::U => Self::U,
tag::TagKind::X => Self::X,
tag::TagKind::K => Self::K,
tag::TagKind::Relay => Self::RelayUrl,
tag::TagKind::Nonce => Self::Nonce,
tag::TagKind::Delegation => Self::Delegation,
Expand Down Expand Up @@ -328,6 +331,7 @@ impl From<TagKind> for tag::TagKind {
TagKind::M => Self::M,
TagKind::U => Self::U,
TagKind::X => Self::X,
TagKind::K => Self::K,
TagKind::RelayUrl => Self::Relay,
TagKind::Nonce => Self::Nonce,
TagKind::Delegation => Self::Delegation,
Expand Down Expand Up @@ -429,6 +433,9 @@ pub enum TagEnum {
identifier: String,
relay_url: Option<String>,
},
Kind {
kind: u64,
},
RelayUrl {
relay_url: String,
},
Expand Down Expand Up @@ -637,6 +644,9 @@ impl From<tag::Tag> for TagEnum {
tag::Tag::ExternalIdentity(identity) => Self::ExternalIdentityTag {
identity: identity.into(),
},
tag::Tag::Kind(kind) => Self::Kind {
kind: kind.as_u64(),
},
tag::Tag::Relay(url) => Self::RelayUrl {
relay_url: url.to_string(),
},
Expand Down Expand Up @@ -810,6 +820,7 @@ impl TryFrom<TagEnum> for tag::Tag {
identifier,
relay_url: relay_url.map(UncheckedUrl::from),
}),
TagEnum::Kind { kind } => Ok(Self::Kind(Kind::from(kind))),
TagEnum::RelayUrl { relay_url } => Ok(Self::Relay(UncheckedUrl::from(relay_url))),
TagEnum::POW { nonce, difficulty } => Ok(Self::POW {
nonce: nonce.parse()?,
Expand Down
6 changes: 3 additions & 3 deletions bindings/nostr-js/src/event/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,10 +122,10 @@ impl JsEventBuilder {
})
}

#[wasm_bindgen]
pub fn repost(event_id: &JsEventId, public_key: &JsPublicKey) -> Self {
/// Repost
pub fn repost(event: &JsEvent, relay_url: Option<String>) -> Self {
Self {
builder: EventBuilder::repost(event_id.into(), public_key.into()),
builder: EventBuilder::repost(&*event, relay_url.map(UncheckedUrl::from)),
}
}

Expand Down
14 changes: 13 additions & 1 deletion bindings/nostr-sdk-ffi/src/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use nostr_ffi::{
};
use nostr_sdk::client::Client as ClientSdk;
use nostr_sdk::pool::RelayPoolNotification as RelayPoolNotificationSdk;
use nostr_sdk::{block_on, spawn_blocking};
use nostr_sdk::{block_on, spawn_blocking, UncheckedUrl};
use uniffi::Object;

mod builder;
Expand Down Expand Up @@ -306,6 +306,18 @@ impl Client {
})
}

/// Repost
pub fn repost(&self, event: Arc<Event>, relay_url: Option<String>) -> Result<Arc<EventId>> {
block_on(async move {
Ok(Arc::new(
self.inner
.repost(event.as_ref().deref(), relay_url.map(UncheckedUrl::from))
.await?
.into(),
))
})
}

/// Send a Zap!
///
/// This method automatically create a split zap to support Rust Nostr development.
Expand Down
11 changes: 3 additions & 8 deletions bindings/nostr-sdk-js/src/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -403,15 +403,10 @@ impl JsClient {
.map(|id| id.into())
}

/// Repost event
#[wasm_bindgen(js_name = repostEvent)]
pub async fn repost_event(
&self,
event_id: &JsEventId,
public_key: &JsPublicKey,
) -> Result<JsEventId> {
/// Repost
pub async fn repost(&self, event: &JsEvent, relay_url: Option<String>) -> Result<JsEventId> {
self.inner
.repost_event(event_id.into(), public_key.into())
.repost(&*event, relay_url.map(UncheckedUrl::from))
.await
.map_err(into_err)
.map(|id| id.into())
Expand Down
10 changes: 5 additions & 5 deletions crates/nostr-sdk/src/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -994,13 +994,13 @@ impl Client {
self.send_event_builder(builder).await
}

/// Repost event
pub async fn repost_event(
/// Repost
pub async fn repost(
&self,
event_id: EventId,
public_key: PublicKey,
event: &Event,
relay_url: Option<UncheckedUrl>,
) -> Result<EventId, Error> {
let builder = EventBuilder::repost(event_id, public_key);
let builder = EventBuilder::repost(event, relay_url);
self.send_event_builder(builder).await
}

Expand Down
37 changes: 30 additions & 7 deletions crates/nostr/src/event/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -482,13 +482,36 @@ impl EventBuilder {
))
}

/// Repost event
pub fn repost(event_id: EventId, public_key: PublicKey) -> Self {
Self::new(
Kind::Repost,
String::new(),
[Tag::event(event_id), Tag::public_key(public_key)],
)
/// Repost
pub fn repost(event: &Event, relay_url: Option<UncheckedUrl>) -> Self {
if event.kind == Kind::TextNote {
Self::new(
Kind::Repost,
event.as_json(),
[
Tag::Event {
event_id: event.id(),
relay_url,
marker: None,
},
Tag::public_key(event.author()),
],
)
} else {
Self::new(
Kind::GenericRepost,
event.as_json(),
[
Tag::Event {
event_id: event.id(),
relay_url,
marker: None,
},
Tag::public_key(event.author()),
Tag::Kind(event.kind()),
],
)
}
}

/// Create delete event
Expand Down
4 changes: 4 additions & 0 deletions crates/nostr/src/event/kind.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ pub enum Kind {
EventDeletion,
/// Repost (NIP18)
Repost,
/// Generic Repost (NIP18)
GenericRepost,
/// Reaction (NIP25)
Reaction,
/// Badge Award (NIP58)
Expand Down Expand Up @@ -249,6 +251,7 @@ impl From<u64> for Kind {
4 => Self::EncryptedDirectMessage,
5 => Self::EventDeletion,
6 => Self::Repost,
16 => Self::GenericRepost,
7 => Self::Reaction,
8 => Self::BadgeAward,
40 => Self::ChannelCreation,
Expand Down Expand Up @@ -313,6 +316,7 @@ impl From<Kind> for u64 {
Kind::EncryptedDirectMessage => 4,
Kind::EventDeletion => 5,
Kind::Repost => 6,
Kind::GenericRepost => 16,
Kind::Reaction => 7,
Kind::BadgeAward => 8,
Kind::ChannelCreation => 40,
Expand Down
8 changes: 8 additions & 0 deletions crates/nostr/src/event/tag.rs
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,8 @@ pub enum TagKind {
U,
/// SHA256
X,
/// Kind
K,
/// Relay
Relay,
/// Nonce
Expand Down Expand Up @@ -456,6 +458,7 @@ impl fmt::Display for TagKind {
Self::M => write!(f, "m"),
Self::U => write!(f, "u"),
Self::X => write!(f, "x"),
Self::K => write!(f, "k"),
Self::Relay => write!(f, "relay"),
Self::Nonce => write!(f, "nonce"),
Self::Delegation => write!(f, "delegation"),
Expand Down Expand Up @@ -518,6 +521,7 @@ where
"m" => Self::M,
"u" => Self::U,
"x" => Self::X,
"k" => Self::K,
"relay" => Self::Relay,
"nonce" => Self::Nonce,
"delegation" => Self::Delegation,
Expand Down Expand Up @@ -598,6 +602,7 @@ pub enum Tag {
identifier: String,
relay_url: Option<UncheckedUrl>,
},
Kind(Kind),
Relay(UncheckedUrl),
POW {
nonce: u128,
Expand Down Expand Up @@ -738,6 +743,7 @@ impl Tag {
TagKind::T => Ok(Self::Hashtag(tag_1.to_owned())),
TagKind::G => Ok(Self::Geohash(tag_1.to_owned())),
TagKind::D => Ok(Self::Identifier(tag_1.to_owned())),
TagKind::K => Ok(Self::Kind(Kind::from_str(tag_1)?)),
TagKind::Relay => Ok(Self::Relay(UncheckedUrl::from(tag_1))),
TagKind::ContentWarning => Ok(Self::ContentWarning {
reason: Some(tag_1.to_owned()),
Expand Down Expand Up @@ -1020,6 +1026,7 @@ impl Tag {
Self::Identifier(..) => TagKind::D,
Self::ExternalIdentity(..) => TagKind::I,
Self::A { .. } => TagKind::A,
Self::Kind(..) => TagKind::K,
Self::Relay(..) => TagKind::Relay,
Self::POW { .. } => TagKind::Nonce,
Self::Delegation { .. } => TagKind::Delegation,
Expand Down Expand Up @@ -1157,6 +1164,7 @@ impl From<Tag> for Vec<String> {
vec
}
Tag::ExternalIdentity(identity) => identity.into(),
Tag::Kind(kind) => vec![TagKind::K.to_string(), kind.to_string()],
Tag::Relay(url) => vec![TagKind::Relay.to_string(), url.to_string()],
Tag::POW { nonce, difficulty } => vec![
TagKind::Nonce.to_string(),
Expand Down

0 comments on commit 3900827

Please sign in to comment.