Skip to content

Commit

Permalink
nostr: remove new_ and set_ prefixes from EventBuilder construc…
Browse files Browse the repository at this point in the history
…tors
  • Loading branch information
yukibtc committed Jan 16, 2024
1 parent 1bb3fcc commit 9fa67f4
Show file tree
Hide file tree
Showing 24 changed files with 268 additions and 178 deletions.
2 changes: 1 addition & 1 deletion bindings/nostr-ffi/bindings-python/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ print(keys.secret_key().to_bech32())
print(keys.public_key().to_bech32())

print("Mining a POW text note...")
event = EventBuilder.new_text_note("Hello from Rust Nostr Python bindings!", []).to_pow_event(keys, 20)
event = EventBuilder.text_note("Hello from Rust Nostr Python bindings!", []).to_pow_event(keys, 20)
print(event.as_json())
```

Expand Down
4 changes: 2 additions & 2 deletions bindings/nostr-ffi/bindings-python/examples/event_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
keys = Keys.generate()

# Build a text note
event = EventBuilder.new_text_note("New note from Rust Nostr python bindings", []).to_event(keys)
event = EventBuilder.text_note("New note from Rust Nostr python bindings", []).to_event(keys)
print(event.as_json())

# Build a DM
receiver_pk = PublicKey.from_bech32("npub14f8usejl26twx0dhuxjh9cas7keav9vr0v8nvtwtrjqx3vycc76qqh9nsy")
event = EventBuilder.new_encrypted_direct_msg(keys, receiver_pk, "New note from Rust Nostr python bindings", None).to_event(keys)
event = EventBuilder.encrypted_direct_msg(keys, receiver_pk, "New note from Rust Nostr python bindings", None).to_event(keys)
print(event.as_json())

# Build a custom event
Expand Down
2 changes: 1 addition & 1 deletion bindings/nostr-ffi/bindings-python/examples/tags.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
# OR
tag = Tag.public_key(other_user_pk)

event = EventBuilder.new_text_note("New note from Rust Nostr python bindings", [tag]).to_event(keys)
event = EventBuilder.text_note("New note from Rust Nostr python bindings", [tag]).to_event(keys)
print(event.as_json())

print("\nTags:")
Expand Down
56 changes: 22 additions & 34 deletions bindings/nostr-ffi/src/event/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,20 +90,12 @@ impl EventBuilder {
}

#[uniffi::constructor]
pub fn set_metadata(metadata: Arc<Metadata>) -> Arc<Self> {
pub fn metadata(metadata: Arc<Metadata>) -> Arc<Self> {
Arc::new(Self {
inner: nostr::EventBuilder::set_metadata(metadata.as_ref().deref()),
inner: nostr::EventBuilder::metadata(metadata.as_ref().deref()),
})
}

#[uniffi::constructor]
pub fn add_recommended_relay(url: String) -> Result<Arc<Self>> {
let url = Url::parse(&url)?;
Ok(Arc::new(Self {
inner: nostr::EventBuilder::add_recommended_relay(&url),
}))
}

#[uniffi::constructor]
pub fn relay_list(list: HashMap<String, Option<RelayMetadata>>) -> Arc<Self> {
let iter = list
Expand All @@ -115,10 +107,10 @@ impl EventBuilder {
}

#[uniffi::constructor]
pub fn new_text_note(content: String, tags: Vec<Arc<Tag>>) -> Result<Arc<Self>> {
pub fn text_note(content: String, tags: Vec<Arc<Tag>>) -> Result<Arc<Self>> {
let tags = tags.into_iter().map(|t| t.as_ref().deref().clone());
Ok(Arc::new(Self {
inner: nostr::EventBuilder::new_text_note(content, tags),
inner: nostr::EventBuilder::text_note(content, tags),
}))
}

Expand All @@ -131,27 +123,27 @@ impl EventBuilder {
}

#[uniffi::constructor]
pub fn set_contact_list(list: Vec<Arc<Contact>>) -> Arc<Self> {
pub fn contact_list(list: Vec<Arc<Contact>>) -> Arc<Self> {
let list: Vec<ContactSdk> = list
.into_iter()
.map(|c| c.as_ref().deref().clone())
.collect();

Arc::new(Self {
inner: nostr::EventBuilder::set_contact_list(list),
inner: nostr::EventBuilder::contact_list(list),
})
}

/// Create encrypted direct msg event
#[uniffi::constructor]
pub fn new_encrypted_direct_msg(
pub fn encrypted_direct_msg(
sender_keys: Arc<Keys>,
receiver_pubkey: Arc<PublicKey>,
content: String,
reply_to: Option<Arc<EventId>>,
) -> Result<Arc<Self>> {
Ok(Arc::new(Self {
inner: nostr::EventBuilder::new_encrypted_direct_msg(
inner: nostr::EventBuilder::encrypted_direct_msg(
sender_keys.deref(),
*receiver_pubkey.as_ref().deref(),
content,
Expand Down Expand Up @@ -183,13 +175,13 @@ impl EventBuilder {
}

#[uniffi::constructor]
pub fn new_reaction(
pub fn reaction(
event_id: Arc<EventId>,
public_key: Arc<PublicKey>,
content: String,
) -> Arc<Self> {
Arc::new(Self {
inner: nostr::EventBuilder::new_reaction(
inner: nostr::EventBuilder::reaction(
event_id.as_ref().into(),
*public_key.as_ref().deref(),
content,
Expand All @@ -198,14 +190,14 @@ impl EventBuilder {
}

#[uniffi::constructor]
pub fn new_channel(metadata: Arc<Metadata>) -> Arc<Self> {
pub fn channel(metadata: Arc<Metadata>) -> Arc<Self> {
Arc::new(Self {
inner: nostr::EventBuilder::new_channel(metadata.as_ref().deref()),
inner: nostr::EventBuilder::channel(metadata.as_ref().deref()),
})
}

#[uniffi::constructor]
pub fn set_channel_metadata(
pub fn channel_metadata(
channel_id: Arc<EventId>,
relay_url: Option<String>,
metadata: Arc<Metadata>,
Expand All @@ -215,7 +207,7 @@ impl EventBuilder {
None => None,
};
Ok(Arc::new(Self {
inner: nostr::EventBuilder::set_channel_metadata(
inner: nostr::EventBuilder::channel_metadata(
**channel_id,
relay_url,
metadata.as_ref().deref(),
Expand All @@ -224,17 +216,13 @@ impl EventBuilder {
}

#[uniffi::constructor]
pub fn new_channel_msg(
pub fn channel_msg(
channel_id: Arc<EventId>,
relay_url: String,
content: String,
) -> Result<Arc<Self>> {
Ok(Arc::new(Self {
inner: nostr::EventBuilder::new_channel_msg(
**channel_id,
Url::parse(&relay_url)?,
content,
),
inner: nostr::EventBuilder::channel_msg(**channel_id, Url::parse(&relay_url)?, content),
}))
}

Expand Down Expand Up @@ -331,13 +319,13 @@ impl EventBuilder {
}

#[uniffi::constructor]
pub fn new_zap_receipt(
pub fn zap_receipt(
bolt11: String,
preimage: Option<String>,
zap_request: Arc<Event>,
) -> Arc<Self> {
Arc::new(Self {
inner: nostr::EventBuilder::new_zap_receipt(
inner: nostr::EventBuilder::zap_receipt(
bolt11,
preimage,
zap_request.as_ref().deref().clone(),
Expand Down Expand Up @@ -477,16 +465,16 @@ impl EventBuilder {
}

#[uniffi::constructor]
pub fn new_stall_data(data: StallData) -> Self {
pub fn stall_data(data: StallData) -> Self {
Self {
inner: nostr::EventBuilder::new_stall_data(data.into()),
inner: nostr::EventBuilder::stall_data(data.into()),
}
}

#[uniffi::constructor]
pub fn new_product_data(data: ProductData) -> Self {
pub fn product_data(data: ProductData) -> Self {
Self {
inner: nostr::EventBuilder::new_product_data(data.into()),
inner: nostr::EventBuilder::product_data(data.into()),
}
}
}
57 changes: 21 additions & 36 deletions bindings/nostr-js/src/event/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,45 +71,36 @@ impl JsEventBuilder {
.into()
}

#[wasm_bindgen(js_name = setMetadata)]
pub fn set_metadata(metadata: &JsMetadata) -> Self {
pub fn metadata(metadata: &JsMetadata) -> Self {
Self {
builder: EventBuilder::set_metadata(metadata.deref()),
builder: EventBuilder::metadata(metadata.deref()),
}
}

#[wasm_bindgen(js_name = addRecommendedRelay)]
pub fn add_recommended_relay(url: String) -> Result<JsEventBuilder> {
let url = Url::parse(&url).map_err(into_err)?;
Ok(Self {
builder: EventBuilder::add_recommended_relay(&url),
})
}

#[wasm_bindgen(js_name = newTextNote)]
pub fn new_text_note(content: String, tags: Vec<JsTag>) -> Self {
#[wasm_bindgen(js_name = textNote)]
pub fn text_note(content: String, tags: Vec<JsTag>) -> Self {
Self {
builder: EventBuilder::new_text_note(content, tags.into_iter().map(|t| t.into())),
builder: EventBuilder::text_note(content, tags.into_iter().map(|t| t.into())),
}
}

#[wasm_bindgen(js_name = setContactList)]
pub fn set_contact_list(list: Vec<JsContact>) -> Self {
#[wasm_bindgen(js_name = contactList)]
pub fn contact_list(list: Vec<JsContact>) -> Self {
let list = list.into_iter().map(|c| c.inner());
Self {
builder: EventBuilder::set_contact_list(list),
builder: EventBuilder::contact_list(list),
}
}

#[wasm_bindgen(js_name = newEncryptedDirectMsg)]
pub fn new_encrypted_direct_msg(
#[wasm_bindgen(js_name = encryptedDirectMsg)]
pub fn encrypted_direct_msg(
sender_keys: &JsKeys,
receiver_pubkey: &JsPublicKey,
content: String,
reply_to: Option<JsEventId>,
) -> Result<JsEventBuilder> {
Ok(Self {
builder: EventBuilder::new_encrypted_direct_msg(
builder: EventBuilder::encrypted_direct_msg(
sender_keys.deref(),
receiver_pubkey.into(),
content,
Expand Down Expand Up @@ -137,22 +128,20 @@ impl JsEventBuilder {
}
}

#[wasm_bindgen(js_name = newReaction)]
pub fn new_reaction(event_id: &JsEventId, public_key: &JsPublicKey, content: String) -> Self {
pub fn reaction(event_id: &JsEventId, public_key: &JsPublicKey, content: String) -> Self {
Self {
builder: EventBuilder::new_reaction(event_id.into(), public_key.into(), content),
builder: EventBuilder::reaction(event_id.into(), public_key.into(), content),
}
}

#[wasm_bindgen(js_name = newChannel)]
pub fn new_channel(metadata: &JsMetadata) -> Self {
pub fn channel(metadata: &JsMetadata) -> Self {
Self {
builder: EventBuilder::new_channel(metadata.deref()),
builder: EventBuilder::channel(metadata.deref()),
}
}

#[wasm_bindgen(js_name = setChannelMetadata)]
pub fn set_channel_metadata(
#[wasm_bindgen(js_name = channelMetadata)]
pub fn channel_metadata(
channel_id: &JsEventId,
relay_url: Option<String>,
metadata: &JsMetadata,
Expand All @@ -162,23 +151,19 @@ impl JsEventBuilder {
None => None,
};
Ok(Self {
builder: EventBuilder::set_channel_metadata(
channel_id.into(),
relay_url,
metadata.deref(),
),
builder: EventBuilder::channel_metadata(channel_id.into(), relay_url, metadata.deref()),
})
}

#[wasm_bindgen(js_name = newChannelMsg)]
pub fn new_channel_msg(
#[wasm_bindgen(js_name = channelMsg)]
pub fn channel_msg(
channel_id: &JsEventId,
relay_url: String,
content: String,
) -> Result<JsEventBuilder> {
let relay_url: Url = Url::parse(&relay_url).map_err(into_err)?;
Ok(Self {
builder: EventBuilder::new_channel_msg(channel_id.into(), relay_url, content),
builder: EventBuilder::channel_msg(channel_id.into(), relay_url, content),
})
}

Expand Down
4 changes: 2 additions & 2 deletions bindings/nostr-sdk-ffi/bindings-python/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,14 @@ client.add_relays(["wss://relay.damus.io", "wss://nos.lol"])
client.connect()

# Send an event using the Client Signer
builder = EventBuilder.new_text_note("Test from Rust Nostr Python!", [])
builder = EventBuilder.text_note("Test from Rust Nostr Python!", [])
client.send_event_builder(builder)
client.set_metadata(Metadata().set_name("Testing Rust Nostr"))

# Mine a POW event and sign it with custom keys
custom_keys = Keys.generate()
print("Mining a POW text note...")
event = EventBuilder.new_text_note("Hello from Rust Nostr Python bindings!", []).to_pow_event(custom_keys, 20)
event = EventBuilder.text_note("Hello from Rust Nostr Python bindings!", []).to_pow_event(custom_keys, 20)
event_id = client.send_event(event)
print("Event sent:")
print(f" hex: {event_id.to_hex()}")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
client.add_relay("wss://relay.damus.io")
client.connect()

event = EventBuilder.new_text_note("Hello from Rust Nostr Python bindings!", []).to_event(keys)
event = EventBuilder.text_note("Hello from Rust Nostr Python bindings!", []).to_event(keys)
event_id = client.send_event(event)
print("Event sent:")
print(f" hex: {event_id.to_hex()}")
Expand Down
4 changes: 2 additions & 2 deletions bindings/nostr-sdk-ffi/bindings-python/examples/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,14 @@
client.connect()

# Send an event using the Client Signer
builder = EventBuilder.new_text_note("Test from Rust Nostr Python!", [])
builder = EventBuilder.text_note("Test from Rust Nostr Python!", [])
client.send_event_builder(builder)
client.set_metadata(Metadata().set_name("Testing Rust Nostr"))

# Mine a POW event and sign it with custom keys
custom_keys = Keys.generate()
print("Mining a POW text note...")
event = EventBuilder.new_text_note("Hello from Rust Nostr Python bindings!", []).to_pow_event(custom_keys, 20)
event = EventBuilder.text_note("Hello from Rust Nostr Python bindings!", []).to_pow_event(custom_keys, 20)
event_id = client.send_event(event)
print("Event sent:")
print(f" hex: {event_id.to_hex()}")
Expand Down
12 changes: 0 additions & 12 deletions bindings/nostr-sdk-js/src/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -288,18 +288,6 @@ impl JsClient {
.map(|id| id.into())
}

/// Add recommended relay
///
/// <https://github.com/nostr-protocol/nips/blob/master/01.md>
#[wasm_bindgen(js_name = addRecommendedRelay)]
pub async fn add_recommended_relay(&self, url: String) -> Result<JsEventId> {
self.inner
.add_recommended_relay(url)
.await
.map_err(into_err)
.map(|id| id.into())
}

/// Set contact list
///
/// <https://github.com/nostr-protocol/nips/blob/master/02.md>
Expand Down
Loading

0 comments on commit 9fa67f4

Please sign in to comment.