From 9fa67f4fadefc6e73c8b8e1b9cc2f68638934ff5 Mon Sep 17 00:00:00 2001 From: Yuki Kishimoto Date: Tue, 16 Jan 2024 17:55:48 +0100 Subject: [PATCH] nostr: remove `new_` and `set_` prefixes from `EventBuilder` constructors --- bindings/nostr-ffi/bindings-python/README.md | 2 +- .../bindings-python/examples/event_builder.py | 4 +- .../bindings-python/examples/tags.py | 2 +- bindings/nostr-ffi/src/event/builder.rs | 56 +++---- bindings/nostr-js/src/event/builder.rs | 57 +++---- .../nostr-sdk-ffi/bindings-python/README.md | 4 +- .../examples/client-with-opts.py | 2 +- .../bindings-python/examples/client.py | 4 +- bindings/nostr-sdk-js/src/client/mod.rs | 12 -- crates/nostr-database/examples/indexes.rs | 8 +- crates/nostr-database/examples/memory.rs | 8 +- .../examples/webapp/src/app.rs | 2 +- crates/nostr-sdk/README.md | 2 +- crates/nostr-sdk/examples/client.rs | 6 +- crates/nostr-sdk/src/client/blocking.rs | 25 ++- crates/nostr-sdk/src/client/mod.rs | 57 ++++--- crates/nostr-sqlite/examples/sqlite.rs | 8 +- crates/nostr/README.md | 6 +- crates/nostr/examples/nip04.rs | 2 +- crates/nostr/examples/nip13.rs | 2 +- crates/nostr/examples/nip15.rs | 4 +- crates/nostr/src/event/builder.rs | 157 +++++++++++++++--- crates/nostr/src/event/kind.rs | 2 +- crates/nostr/src/event/mod.rs | 14 +- 24 files changed, 268 insertions(+), 178 deletions(-) diff --git a/bindings/nostr-ffi/bindings-python/README.md b/bindings/nostr-ffi/bindings-python/README.md index 0f52da1a3..9abb877a1 100644 --- a/bindings/nostr-ffi/bindings-python/README.md +++ b/bindings/nostr-ffi/bindings-python/README.md @@ -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()) ``` diff --git a/bindings/nostr-ffi/bindings-python/examples/event_builder.py b/bindings/nostr-ffi/bindings-python/examples/event_builder.py index 975d38f46..4c3c753c1 100644 --- a/bindings/nostr-ffi/bindings-python/examples/event_builder.py +++ b/bindings/nostr-ffi/bindings-python/examples/event_builder.py @@ -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 diff --git a/bindings/nostr-ffi/bindings-python/examples/tags.py b/bindings/nostr-ffi/bindings-python/examples/tags.py index e83a0e071..d7f9fa788 100644 --- a/bindings/nostr-ffi/bindings-python/examples/tags.py +++ b/bindings/nostr-ffi/bindings-python/examples/tags.py @@ -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:") diff --git a/bindings/nostr-ffi/src/event/builder.rs b/bindings/nostr-ffi/src/event/builder.rs index fd5f6d540..6bd48c4e5 100644 --- a/bindings/nostr-ffi/src/event/builder.rs +++ b/bindings/nostr-ffi/src/event/builder.rs @@ -90,20 +90,12 @@ impl EventBuilder { } #[uniffi::constructor] - pub fn set_metadata(metadata: Arc) -> Arc { + pub fn metadata(metadata: Arc) -> Arc { 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> { - let url = Url::parse(&url)?; - Ok(Arc::new(Self { - inner: nostr::EventBuilder::add_recommended_relay(&url), - })) - } - #[uniffi::constructor] pub fn relay_list(list: HashMap>) -> Arc { let iter = list @@ -115,10 +107,10 @@ impl EventBuilder { } #[uniffi::constructor] - pub fn new_text_note(content: String, tags: Vec>) -> Result> { + pub fn text_note(content: String, tags: Vec>) -> Result> { 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), })) } @@ -131,27 +123,27 @@ impl EventBuilder { } #[uniffi::constructor] - pub fn set_contact_list(list: Vec>) -> Arc { + pub fn contact_list(list: Vec>) -> Arc { let list: Vec = 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, receiver_pubkey: Arc, content: String, reply_to: Option>, ) -> Result> { 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, @@ -183,13 +175,13 @@ impl EventBuilder { } #[uniffi::constructor] - pub fn new_reaction( + pub fn reaction( event_id: Arc, public_key: Arc, content: String, ) -> Arc { Arc::new(Self { - inner: nostr::EventBuilder::new_reaction( + inner: nostr::EventBuilder::reaction( event_id.as_ref().into(), *public_key.as_ref().deref(), content, @@ -198,14 +190,14 @@ impl EventBuilder { } #[uniffi::constructor] - pub fn new_channel(metadata: Arc) -> Arc { + pub fn channel(metadata: Arc) -> Arc { 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, relay_url: Option, metadata: Arc, @@ -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(), @@ -224,17 +216,13 @@ impl EventBuilder { } #[uniffi::constructor] - pub fn new_channel_msg( + pub fn channel_msg( channel_id: Arc, relay_url: String, content: String, ) -> Result> { 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), })) } @@ -331,13 +319,13 @@ impl EventBuilder { } #[uniffi::constructor] - pub fn new_zap_receipt( + pub fn zap_receipt( bolt11: String, preimage: Option, zap_request: Arc, ) -> Arc { Arc::new(Self { - inner: nostr::EventBuilder::new_zap_receipt( + inner: nostr::EventBuilder::zap_receipt( bolt11, preimage, zap_request.as_ref().deref().clone(), @@ -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()), } } } diff --git a/bindings/nostr-js/src/event/builder.rs b/bindings/nostr-js/src/event/builder.rs index dae41c957..bf47555c8 100644 --- a/bindings/nostr-js/src/event/builder.rs +++ b/bindings/nostr-js/src/event/builder.rs @@ -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 { - 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) -> Self { + #[wasm_bindgen(js_name = textNote)] + pub fn text_note(content: String, tags: Vec) -> 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) -> Self { + #[wasm_bindgen(js_name = contactList)] + pub fn contact_list(list: Vec) -> 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, ) -> Result { Ok(Self { - builder: EventBuilder::new_encrypted_direct_msg( + builder: EventBuilder::encrypted_direct_msg( sender_keys.deref(), receiver_pubkey.into(), content, @@ -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, metadata: &JsMetadata, @@ -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 { 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), }) } diff --git a/bindings/nostr-sdk-ffi/bindings-python/README.md b/bindings/nostr-sdk-ffi/bindings-python/README.md index db96a79b5..33a591f0a 100644 --- a/bindings/nostr-sdk-ffi/bindings-python/README.md +++ b/bindings/nostr-sdk-ffi/bindings-python/README.md @@ -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()}") diff --git a/bindings/nostr-sdk-ffi/bindings-python/examples/client-with-opts.py b/bindings/nostr-sdk-ffi/bindings-python/examples/client-with-opts.py index db13da325..0096c2454 100644 --- a/bindings/nostr-sdk-ffi/bindings-python/examples/client-with-opts.py +++ b/bindings/nostr-sdk-ffi/bindings-python/examples/client-with-opts.py @@ -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()}") diff --git a/bindings/nostr-sdk-ffi/bindings-python/examples/client.py b/bindings/nostr-sdk-ffi/bindings-python/examples/client.py index 3b8eec9ef..13dcb0e4c 100644 --- a/bindings/nostr-sdk-ffi/bindings-python/examples/client.py +++ b/bindings/nostr-sdk-ffi/bindings-python/examples/client.py @@ -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()}") diff --git a/bindings/nostr-sdk-js/src/client/mod.rs b/bindings/nostr-sdk-js/src/client/mod.rs index 41b298f00..1590c273a 100644 --- a/bindings/nostr-sdk-js/src/client/mod.rs +++ b/bindings/nostr-sdk-js/src/client/mod.rs @@ -288,18 +288,6 @@ impl JsClient { .map(|id| id.into()) } - /// Add recommended relay - /// - /// - #[wasm_bindgen(js_name = addRecommendedRelay)] - pub async fn add_recommended_relay(&self, url: String) -> Result { - self.inner - .add_recommended_relay(url) - .await - .map_err(into_err) - .map(|id| id.into()) - } - /// Set contact list /// /// diff --git a/crates/nostr-database/examples/indexes.rs b/crates/nostr-database/examples/indexes.rs index fbf494db5..10692d626 100644 --- a/crates/nostr-database/examples/indexes.rs +++ b/crates/nostr-database/examples/indexes.rs @@ -25,12 +25,12 @@ async fn main() { let index = DatabaseIndexes::new(); for i in 0..100_000 { - let event = EventBuilder::new_text_note(format!("Event #{i}"), []) + let event = EventBuilder::text_note(format!("Event #{i}"), []) .to_event(&keys_a) .unwrap(); index.index_event(&event).await; - let event = EventBuilder::new_text_note( + let event = EventBuilder::text_note( format!("Reply to event #{i}"), [Tag::event(event.id()), Tag::public_key(event.author())], ) @@ -41,9 +41,7 @@ async fn main() { for i in 0..1000 { let metadata = Metadata::new().name(format!("Name #{i}")); - let event = EventBuilder::set_metadata(&metadata) - .to_event(&keys_a) - .unwrap(); + let event = EventBuilder::metadata(&metadata).to_event(&keys_a).unwrap(); index.index_event(&event).await; } diff --git a/crates/nostr-database/examples/memory.rs b/crates/nostr-database/examples/memory.rs index 83ffa69f2..3d8de94cf 100644 --- a/crates/nostr-database/examples/memory.rs +++ b/crates/nostr-database/examples/memory.rs @@ -29,12 +29,12 @@ async fn main() { let database = MemoryDatabase::new(opts); for i in 0..100_000 { - let event = EventBuilder::new_text_note(format!("Event #{i}"), []) + let event = EventBuilder::text_note(format!("Event #{i}"), []) .to_event(&keys_a) .unwrap(); database.save_event(&event).await.unwrap(); - let event = EventBuilder::new_text_note( + let event = EventBuilder::text_note( format!("Reply to event #{i}"), [Tag::event(event.id()), Tag::public_key(event.author())], ) @@ -45,9 +45,7 @@ async fn main() { for i in 0..10 { let metadata = Metadata::new().name(format!("Name #{i}")); - let event = EventBuilder::set_metadata(&metadata) - .to_event(&keys_a) - .unwrap(); + let event = EventBuilder::metadata(&metadata).to_event(&keys_a).unwrap(); database.save_event(&event).await.unwrap(); } diff --git a/crates/nostr-indexeddb/examples/webapp/src/app.rs b/crates/nostr-indexeddb/examples/webapp/src/app.rs index 1eb2d3212..c589c17a3 100644 --- a/crates/nostr-indexeddb/examples/webapp/src/app.rs +++ b/crates/nostr-indexeddb/examples/webapp/src/app.rs @@ -22,7 +22,7 @@ pub fn app() -> Html { let database = WebDatabase::open("nostr-sdk-indexeddb-test").await.unwrap(); let metadata = Metadata::new().name("Name"); - let event = EventBuilder::set_metadata(&metadata) + let event = EventBuilder::metadata(&metadata) .to_event(&keys_a) .unwrap(); database.save_event(&event).await.unwrap(); diff --git a/crates/nostr-sdk/README.md b/crates/nostr-sdk/README.md index 2efeb0f23..b2996fd34 100644 --- a/crates/nostr-sdk/README.md +++ b/crates/nostr-sdk/README.md @@ -83,7 +83,7 @@ async fn main() -> Result<()> { client.publish_text_note("My first text note from Nostr SDK!", []).await?; // Create a POW text note - let event: Event = EventBuilder::new_text_note("POW text note from nostr-sdk", []).to_pow_event(&my_keys, 20)?; + let event: Event = EventBuilder::text_note("POW text note from nostr-sdk", []).to_pow_event(&my_keys, 20)?; client.send_event(event).await?; // Send custom event diff --git a/crates/nostr-sdk/examples/client.rs b/crates/nostr-sdk/examples/client.rs index ceca5a8a9..c58dbf730 100644 --- a/crates/nostr-sdk/examples/client.rs +++ b/crates/nostr-sdk/examples/client.rs @@ -23,14 +23,14 @@ async fn main() -> Result<()> { client.publish_text_note("Hello world", []).await?; // Create a text note POW event - let event: Event = EventBuilder::new_text_note("POW text note from nostr-sdk", []) - .to_pow_event(&my_keys, 20)?; + let event: Event = + EventBuilder::text_note("POW text note from nostr-sdk", []).to_pow_event(&my_keys, 20)?; client.send_event(event).await?; // Send multiple events at once let mut events: Vec = Vec::new(); for i in 0..10 { - events.push(EventBuilder::new_text_note(format!("Event #{i}"), []).to_event(&my_keys)?); + events.push(EventBuilder::text_note(format!("Event #{i}"), []).to_event(&my_keys)?); } let opts = RelaySendOptions::default(); client.batch_event(events, opts).await?; diff --git a/crates/nostr-sdk/src/client/blocking.rs b/crates/nostr-sdk/src/client/blocking.rs index a4830e326..aa6366f60 100644 --- a/crates/nostr-sdk/src/client/blocking.rs +++ b/crates/nostr-sdk/src/client/blocking.rs @@ -267,11 +267,13 @@ impl Client { RUNTIME.block_on(async { self.client.publish_text_note(content, tags).await }) } + #[deprecated(since = "0.27.0")] pub fn add_recommended_relay(&self, url: U) -> Result where U: TryIntoUrl, Error: From<::Err>, { + #[allow(deprecated)] RUNTIME.block_on(async { self.client.add_recommended_relay(url).await }) } @@ -411,7 +413,7 @@ impl Client { /// /// #[cfg(feature = "nip57")] - pub fn new_zap_receipt( + pub fn zap_receipt( &self, bolt11: S, preimage: Option, @@ -421,12 +423,27 @@ impl Client { S: Into, { RUNTIME.block_on(async { - self.client - .new_zap_receipt(bolt11, preimage, zap_request) - .await + self.client.zap_receipt(bolt11, preimage, zap_request).await }) } + /// Create zap receipt event + /// + /// + #[cfg(feature = "nip57")] + #[deprecated(since = "0.27.0", note = "Use `zap_receipt` instead")] + pub fn new_zap_receipt( + &self, + bolt11: S, + preimage: Option, + zap_request: Event, + ) -> Result + where + S: Into, + { + self.zap_receipt(bolt11, preimage, zap_request) + } + /// File metadata /// /// diff --git a/crates/nostr-sdk/src/client/mod.rs b/crates/nostr-sdk/src/client/mod.rs index a646b51cb..30e007a25 100644 --- a/crates/nostr-sdk/src/client/mod.rs +++ b/crates/nostr-sdk/src/client/mod.rs @@ -780,7 +780,7 @@ impl Client { /// # } /// ``` pub async fn set_metadata(&self, metadata: &Metadata) -> Result { - let builder = EventBuilder::set_metadata(metadata); + let builder = EventBuilder::metadata(metadata); self.send_event_builder(builder).await } @@ -807,34 +807,21 @@ impl Client { S: Into, I: IntoIterator, { - let builder = EventBuilder::new_text_note(content, tags); + let builder = EventBuilder::text_note(content, tags); self.send_event_builder(builder).await } /// Add recommended relay /// /// - /// - /// # Example - /// ```rust,no_run - /// use nostr_sdk::prelude::*; - /// - /// # #[tokio::main] - /// # async fn main() { - /// # let my_keys = Keys::generate(); - /// # let client = Client::new(&my_keys); - /// client - /// .add_recommended_relay("wss://relay.damus.io") - /// .await - /// .unwrap(); - /// # } - /// ``` + #[deprecated(since = "0.27.0")] pub async fn add_recommended_relay(&self, url: U) -> Result where U: TryIntoUrl, Error: From<::Err>, { let url: Url = url.try_into_url()?; + #[allow(deprecated)] let builder = EventBuilder::add_recommended_relay(&url); self.send_event_builder(builder).await } @@ -846,7 +833,7 @@ impl Client { where I: IntoIterator, { - let builder = EventBuilder::set_contact_list(list); + let builder = EventBuilder::contact_list(list); self.send_event_builder(builder).await } @@ -1001,7 +988,7 @@ impl Client { { let builder: EventBuilder = match self.signer().await? { ClientSigner::Keys(keys) => { - EventBuilder::new_encrypted_direct_msg(&keys, receiver, msg, reply_to)? + EventBuilder::encrypted_direct_msg(&keys, receiver, msg, reply_to)? } #[cfg(all(feature = "nip07", target_arch = "wasm32"))] ClientSigner::NIP07(nip07) => { @@ -1087,7 +1074,7 @@ impl Client { event_id: EventId, public_key: XOnlyPublicKey, ) -> Result { - let builder = EventBuilder::new_reaction(event_id, public_key, "+"); + let builder = EventBuilder::reaction(event_id, public_key, "+"); self.send_event_builder(builder).await } @@ -1121,7 +1108,7 @@ impl Client { event_id: EventId, public_key: XOnlyPublicKey, ) -> Result { - let builder = EventBuilder::new_reaction(event_id, public_key, "-"); + let builder = EventBuilder::reaction(event_id, public_key, "-"); self.send_event_builder(builder).await } @@ -1159,7 +1146,7 @@ impl Client { where S: Into, { - let builder = EventBuilder::new_reaction(event_id, public_key, content); + let builder = EventBuilder::reaction(event_id, public_key, content); self.send_event_builder(builder).await } @@ -1167,7 +1154,7 @@ impl Client { /// /// pub async fn new_channel(&self, metadata: &Metadata) -> Result { - let builder = EventBuilder::new_channel(metadata); + let builder = EventBuilder::channel(metadata); self.send_event_builder(builder).await } @@ -1180,7 +1167,7 @@ impl Client { relay_url: Option, metadata: &Metadata, ) -> Result { - let builder = EventBuilder::set_channel_metadata(channel_id, relay_url, metadata); + let builder = EventBuilder::channel_metadata(channel_id, relay_url, metadata); self.send_event_builder(builder).await } @@ -1196,7 +1183,7 @@ impl Client { where S: Into, { - let builder = EventBuilder::new_channel_msg(channel_id, relay_url, msg); + let builder = EventBuilder::channel_msg(channel_id, relay_url, msg); self.send_event_builder(builder).await } @@ -1245,6 +1232,24 @@ impl Client { /// /// #[cfg(feature = "nip57")] + pub async fn zap_receipt( + &self, + bolt11: S, + preimage: Option, + zap_request: Event, + ) -> Result + where + S: Into, + { + let builder = EventBuilder::zap_receipt(bolt11, preimage, zap_request); + self.send_event_builder(builder).await + } + + /// Create zap receipt event + /// + /// + #[cfg(feature = "nip57")] + #[deprecated(since = "0.27.0", note = "Use `zap_receipt` instead")] pub async fn new_zap_receipt( &self, bolt11: S, @@ -1254,7 +1259,7 @@ impl Client { where S: Into, { - let builder = EventBuilder::new_zap_receipt(bolt11, preimage, zap_request); + let builder = EventBuilder::zap_receipt(bolt11, preimage, zap_request); self.send_event_builder(builder).await } diff --git a/crates/nostr-sqlite/examples/sqlite.rs b/crates/nostr-sqlite/examples/sqlite.rs index e7593271f..31839a5ff 100644 --- a/crates/nostr-sqlite/examples/sqlite.rs +++ b/crates/nostr-sqlite/examples/sqlite.rs @@ -35,12 +35,12 @@ async fn main() { ); // for i in 0..100_000 { - // let event = EventBuilder::new_text_note(format!("Event #{i}"), &[]) + // let event = EventBuilder::text_note(format!("Event #{i}"), &[]) // .to_event(&keys_a) // .unwrap(); // database.save_event(&event).await.unwrap(); // - // let event = EventBuilder::new_text_note( + // let event = EventBuilder::text_note( // format!("Reply to event #{i}"), // &[ // Tag::event(event.id), @@ -54,9 +54,7 @@ async fn main() { for i in 0..10 { let metadata = Metadata::new().name(format!("Name #{i}")); - let event = EventBuilder::set_metadata(&metadata) - .to_event(&keys_a) - .unwrap(); + let event = EventBuilder::metadata(&metadata).to_event(&keys_a).unwrap(); database.save_event(&event).await.unwrap(); } diff --git a/crates/nostr/README.md b/crates/nostr/README.md index 7890f09d8..b810b42e4 100644 --- a/crates/nostr/README.md +++ b/crates/nostr/README.md @@ -48,13 +48,13 @@ fn main() -> Result<()> { .lud16("yuki@getalby.com") .custom_field("custom_field", "my value"); - let event: Event = EventBuilder::set_metadata(&metadata).to_event(&my_keys)?; + let event: Event = EventBuilder::metadata(&metadata).to_event(&my_keys)?; // New text note - let event: Event = EventBuilder::new_text_note("Hello from Nostr SDK", []).to_event(&my_keys)?; + let event: Event = EventBuilder::text_note("Hello from Nostr SDK", []).to_event(&my_keys)?; // New POW text note - let event: Event = EventBuilder::new_text_note("My first POW text note from Nostr SDK", []).to_pow_event(&my_keys, 20)?; + let event: Event = EventBuilder::text_note("My first POW text note from Nostr SDK", []).to_pow_event(&my_keys, 20)?; // Convert client nessage to JSON let json = ClientMessage::new_event(event).as_json(); diff --git a/crates/nostr/examples/nip04.rs b/crates/nostr/examples/nip04.rs index 527901bac..76712cfd1 100644 --- a/crates/nostr/examples/nip04.rs +++ b/crates/nostr/examples/nip04.rs @@ -11,7 +11,7 @@ fn main() -> Result<()> { let alice_keys = Keys::from_sk_str(ALICE_SK)?; let bob_keys = Keys::from_sk_str(BOB_SK)?; - let alice_encrypted_msg = EventBuilder::new_encrypted_direct_msg( + let alice_encrypted_msg = EventBuilder::encrypted_direct_msg( &alice_keys, bob_keys.public_key(), "Hey bob this is alice", diff --git a/crates/nostr/examples/nip13.rs b/crates/nostr/examples/nip13.rs index 20f491edf..50b88d121 100644 --- a/crates/nostr/examples/nip13.rs +++ b/crates/nostr/examples/nip13.rs @@ -15,7 +15,7 @@ fn main() -> Result<()> { let difficulty = 20; // leading zero bits let msg_content = "This is a Nostr message with embedded proof-of-work"; - let builder = EventBuilder::new_text_note(msg_content, []); + let builder = EventBuilder::text_note(msg_content, []); // or // let builder = EventBuilder::new(Kind::TextNote, msg_content, &[]); diff --git a/crates/nostr/examples/nip15.rs b/crates/nostr/examples/nip15.rs index d9d37b463..d76eed85e 100644 --- a/crates/nostr/examples/nip15.rs +++ b/crates/nostr/examples/nip15.rs @@ -14,7 +14,7 @@ fn main() -> Result<()> { .description("this is a test stall") .shipping(vec![shipping.clone()]); - let stall_event = EventBuilder::new_stall_data(stall).to_event(&alice_keys)?; + let stall_event = EventBuilder::stall_data(stall).to_event(&alice_keys)?; println!("{}", stall_event.as_json()); let product = ProductData::new("1", "123", "my test product", "USD") @@ -24,7 +24,7 @@ fn main() -> Result<()> { .images(vec!["https://example.com/image.png".into()]) .categories(vec!["test".into()]); - let product_event = EventBuilder::new_product_data(product).to_event(&alice_keys)?; + let product_event = EventBuilder::product_data(product).to_event(&alice_keys)?; println!("{}", product_event.as_json()); Ok(()) diff --git a/crates/nostr/src/event/builder.rs b/crates/nostr/src/event/builder.rs index 18f08bf0c..1e91c8df4 100644 --- a/crates/nostr/src/event/builder.rs +++ b/crates/nostr/src/event/builder.rs @@ -329,7 +329,7 @@ impl EventBuilder { } impl EventBuilder { - /// Set metadata + /// Profile metadata /// /// /// @@ -346,13 +346,22 @@ impl EventBuilder { /// .nip05("username@example.com") /// .lud16("yuki@getalby.com"); /// - /// let builder = EventBuilder::set_metadata(&metadata); + /// let builder = EventBuilder::metadata(&metadata); /// ``` - pub fn set_metadata(metadata: &Metadata) -> Self { + pub fn metadata(metadata: &Metadata) -> Self { Self::new(Kind::Metadata, metadata.as_json(), []) } + /// Set metadata + /// + /// + #[deprecated(since = "0.27.0", note = "Use `metadata` instead")] + pub fn set_metadata(metadata: &Metadata) -> Self { + Self::metadata(metadata) + } + /// Add recommended relay + #[deprecated(since = "0.27.0")] pub fn add_recommended_relay(url: &Url) -> Self { Self::new(Kind::RecommendRelay, url.as_ref(), []) } @@ -378,9 +387,9 @@ impl EventBuilder { /// ```rust,no_run /// use nostr::EventBuilder; /// - /// let builder = EventBuilder::new_text_note("My first text note from Nostr SDK!", []); + /// let builder = EventBuilder::text_note("My first text note from Nostr SDK!", []); /// ``` - pub fn new_text_note(content: S, tags: I) -> Self + pub fn text_note(content: S, tags: I) -> Self where S: Into, I: IntoIterator, @@ -388,6 +397,18 @@ impl EventBuilder { Self::new(Kind::TextNote, content, tags) } + /// Text note + /// + /// + #[deprecated(since = "0.27.0", note = "Use `text_note` instead")] + pub fn new_text_note(content: S, tags: I) -> Self + where + S: Into, + I: IntoIterator, + { + Self::text_note(content, tags) + } + /// Long-form text note (generally referred to as "articles" or "blog posts"). /// /// @@ -417,8 +438,8 @@ impl EventBuilder { Self::new(Kind::LongFormTextNote, content, tags) } - /// Set contact list - pub fn set_contact_list(contacts: I) -> Self + /// Contact list + pub fn contact_list(contacts: I) -> Self where I: IntoIterator, { @@ -431,6 +452,15 @@ impl EventBuilder { Self::new(Kind::ContactList, "", tags) } + /// Set contact list + #[deprecated(since = "0.27.0", note = "Use `contact_list` instead")] + pub fn set_contact_list(contacts: I) -> Self + where + I: IntoIterator, + { + Self::contact_list(contacts) + } + /// OpenTimestamps Attestations for Events /// /// @@ -452,8 +482,10 @@ impl EventBuilder { } /// Create encrypted direct msg event + /// + /// #[cfg(all(feature = "std", feature = "nip04"))] - pub fn new_encrypted_direct_msg( + pub fn encrypted_direct_msg( sender_keys: &Keys, receiver_pubkey: XOnlyPublicKey, content: S, @@ -473,6 +505,21 @@ impl EventBuilder { )) } + /// Create encrypted direct msg event + #[cfg(all(feature = "std", feature = "nip04"))] + #[deprecated(since = "0.27.0", note = "Use `encrypted_direct_msg` instead")] + pub fn new_encrypted_direct_msg( + sender_keys: &Keys, + receiver_pubkey: XOnlyPublicKey, + content: S, + reply_to: Option, + ) -> Result + where + S: Into, + { + Self::encrypted_direct_msg(sender_keys, receiver_pubkey, content, reply_to) + } + /// Repost event pub fn repost(event_id: EventId, public_key: XOnlyPublicKey) -> Self { Self::new( @@ -506,7 +553,7 @@ impl EventBuilder { } /// Add reaction (like/upvote, dislike/downvote or emoji) to an event - pub fn new_reaction(event_id: EventId, public_key: XOnlyPublicKey, content: S) -> Self + pub fn reaction(event_id: EventId, public_key: XOnlyPublicKey, content: S) -> Self where S: Into, { @@ -517,17 +564,34 @@ impl EventBuilder { ) } + /// Add reaction (like/upvote, dislike/downvote or emoji) to an event + #[deprecated(since = "0.27.0", note = "Use `reaction` instead")] + pub fn new_reaction(event_id: EventId, public_key: XOnlyPublicKey, content: S) -> Self + where + S: Into, + { + Self::reaction(event_id, public_key, content) + } + /// Create new channel /// /// - pub fn new_channel(metadata: &Metadata) -> Self { + pub fn channel(metadata: &Metadata) -> Self { Self::new(Kind::ChannelCreation, metadata.as_json(), []) } - /// Set channel metadata + /// Create new channel /// /// - pub fn set_channel_metadata( + #[deprecated(since = "0.27.0", note = "Use `channel` instead")] + pub fn new_channel(metadata: &Metadata) -> Self { + Self::channel(metadata) + } + + /// Channel metadata + /// + /// + pub fn channel_metadata( channel_id: EventId, relay_url: Option, metadata: &Metadata, @@ -543,10 +607,22 @@ impl EventBuilder { ) } - /// New channel message + /// Set channel metadata /// /// - pub fn new_channel_msg(channel_id: EventId, relay_url: Url, content: S) -> Self + #[deprecated(since = "0.27.0", note = "Use `channel_metadata` instead")] + pub fn set_channel_metadata( + channel_id: EventId, + relay_url: Option, + metadata: &Metadata, + ) -> Self { + Self::channel_metadata(channel_id, relay_url, metadata) + } + + /// Channel message + /// + /// + pub fn channel_msg(channel_id: EventId, relay_url: Url, content: S) -> Self where S: Into, { @@ -561,6 +637,17 @@ impl EventBuilder { ) } + /// Channel message + /// + /// + #[deprecated(since = "0.27.0", note = "Use `channel_msg` instead")] + pub fn new_channel_msg(channel_id: EventId, relay_url: Url, content: S) -> Self + where + S: Into, + { + Self::channel_msg(channel_id, relay_url, content) + } + /// Hide message /// /// @@ -725,7 +812,7 @@ impl EventBuilder { /// /// #[cfg(feature = "nip57")] - pub fn new_zap_receipt(bolt11: S, preimage: Option, zap_request: Event) -> Self + pub fn zap_receipt(bolt11: S, preimage: Option, zap_request: Event) -> Self where S: Into, { @@ -768,6 +855,18 @@ impl EventBuilder { Self::new(Kind::ZapReceipt, "", tags) } + /// Create zap receipt event + /// + /// + #[cfg(feature = "nip57")] + #[deprecated(since = "0.27.0", note = "Use `zap_receipt` instead")] + pub fn new_zap_receipt(bolt11: S, preimage: Option, zap_request: Event) -> Self + where + S: Into, + { + Self::zap_receipt(bolt11, preimage, zap_request) + } + /// Create a badge definition event /// /// @@ -1049,18 +1148,34 @@ impl EventBuilder { /// Set stall data /// /// - pub fn new_stall_data(data: StallData) -> Self { + pub fn stall_data(data: StallData) -> Self { let tags: Vec = data.clone().into(); Self::new(Kind::SetStall, data, tags) } + /// Set stall data + /// + /// + #[deprecated(since = "0.27.0", note = "Use `stall_data` instead")] + pub fn new_stall_data(data: StallData) -> Self { + Self::stall_data(data) + } + /// Set product data /// /// - pub fn new_product_data(data: ProductData) -> Self { + pub fn product_data(data: ProductData) -> Self { let tags: Vec = data.clone().into(); Self::new(Kind::SetProduct, data, tags) } + + /// Set product data + /// + /// + #[deprecated(since = "0.27.0", note = "Use `product_data` instead")] + pub fn new_product_data(data: ProductData) -> Self { + Self::product_data(data) + } } #[cfg(test)] @@ -1081,7 +1196,7 @@ mod tests { .unwrap(), ); - let event = EventBuilder::new_text_note("hello", []) + let event = EventBuilder::text_note("hello", []) .to_event(&keys) .unwrap(); @@ -1104,7 +1219,7 @@ mod tests { ); let content = "Mercury, the Winged Messenger"; - let event = EventBuilder::new_encrypted_direct_msg( + let event = EventBuilder::encrypted_direct_msg( &sender_keys, receiver_keys.public_key(), content, @@ -1126,7 +1241,7 @@ mod tests { )); let zap_request_json = String::from("{\"pubkey\":\"32e1827635450ebb3c5a7d12c1f8e7b2b514439ac10a67eef3d9fd9c5c68e245\",\"content\":\"\",\"id\":\"d9cc14d50fcb8c27539aacf776882942c1a11ea4472f8cdec1dea82fab66279d\",\"created_at\":1674164539,\"sig\":\"77127f636577e9029276be060332ea565deaf89ff215a494ccff16ae3f757065e2bc59b2e8c113dd407917a010b3abd36c8d7ad84c0e3ab7dab3a0b0caa9835d\",\"kind\":9734,\"tags\":[[\"e\",\"3624762a1274dd9636e0c552b53086d70bc88c165bc4dc0f9e836a1eaf86c3b8\"],[\"p\",\"32e1827635450ebb3c5a7d12c1f8e7b2b514439ac10a67eef3d9fd9c5c68e245\"],[\"relays\",\"wss://relay.damus.io\",\"wss://nostr-relay.wlvs.space\",\"wss://nostr.fmt.wiz.biz\",\"wss://relay.nostr.bg\",\"wss://nostr.oxtr.dev\",\"wss://nostr.v0l.io\",\"wss://brb.io\",\"wss://nostr.bitcoiner.social\",\"ws://monad.jb55.com:8080\",\"wss://relay.snort.social\"]]}"); let zap_request_event: Event = Event::from_json(zap_request_json).unwrap(); - let event_builder = EventBuilder::new_zap_receipt(bolt11, preimage, zap_request_event); + let event_builder = EventBuilder::zap_receipt(bolt11, preimage, zap_request_event); assert_eq!(6, event_builder.tags.len()); @@ -1147,7 +1262,7 @@ mod tests { let preimage = None; let zap_request_json = String::from("{\"pubkey\":\"32e1827635450ebb3c5a7d12c1f8e7b2b514439ac10a67eef3d9fd9c5c68e245\",\"content\":\"\",\"id\":\"d9cc14d50fcb8c27539aacf776882942c1a11ea4472f8cdec1dea82fab66279d\",\"created_at\":1674164539,\"sig\":\"77127f636577e9029276be060332ea565deaf89ff215a494ccff16ae3f757065e2bc59b2e8c113dd407917a010b3abd36c8d7ad84c0e3ab7dab3a0b0caa9835d\",\"kind\":9734,\"tags\":[[\"e\",\"3624762a1274dd9636e0c552b53086d70bc88c165bc4dc0f9e836a1eaf86c3b8\"],[\"p\",\"32e1827635450ebb3c5a7d12c1f8e7b2b514439ac10a67eef3d9fd9c5c68e245\"],[\"relays\",\"wss://relay.damus.io\",\"wss://nostr-relay.wlvs.space\",\"wss://nostr.fmt.wiz.biz\",\"wss://relay.nostr.bg\",\"wss://nostr.oxtr.dev\",\"wss://nostr.v0l.io\",\"wss://brb.io\",\"wss://nostr.bitcoiner.social\",\"ws://monad.jb55.com:8080\",\"wss://relay.snort.social\"]]}"); let zap_request_event = Event::from_json(zap_request_json).unwrap(); - let event_builder = EventBuilder::new_zap_receipt(bolt11, preimage, zap_request_event); + let event_builder = EventBuilder::zap_receipt(bolt11, preimage, zap_request_event); assert_eq!(5, event_builder.tags.len()); let has_preimage_tag = event_builder diff --git a/crates/nostr/src/event/kind.rs b/crates/nostr/src/event/kind.rs index 47ca5954d..c20f4df89 100644 --- a/crates/nostr/src/event/kind.rs +++ b/crates/nostr/src/event/kind.rs @@ -34,7 +34,7 @@ pub enum Kind { Metadata, /// Short Text Note (NIP01) TextNote, - /// Recommend Relay (NIP01) + /// Recommend Relay (NIP01 - deprecated) RecommendRelay, /// Contacts (NIP02) ContactList, diff --git a/crates/nostr/src/event/mod.rs b/crates/nostr/src/event/mod.rs index de7e90b1f..0aa49fa70 100644 --- a/crates/nostr/src/event/mod.rs +++ b/crates/nostr/src/event/mod.rs @@ -504,12 +504,10 @@ mod tests { #[cfg(feature = "std")] fn test_event_expired() { let my_keys = Keys::generate(); - let event = EventBuilder::new_text_note( - "my content", - [Tag::Expiration(Timestamp::from(1600000000))], - ) - .to_event(&my_keys) - .unwrap(); + let event = + EventBuilder::text_note("my content", [Tag::Expiration(Timestamp::from(1600000000))]) + .to_event(&my_keys) + .unwrap(); assert!(&event.is_expired()); } @@ -521,7 +519,7 @@ mod tests { let expiry_date: u64 = now.as_u64() * 2; let my_keys = Keys::generate(); - let event = EventBuilder::new_text_note( + let event = EventBuilder::text_note( "my content", [Tag::Expiration(Timestamp::from(expiry_date))], ) @@ -535,7 +533,7 @@ mod tests { #[cfg(feature = "std")] fn test_event_without_expiration_tag() { let my_keys = Keys::generate(); - let event = EventBuilder::new_text_note("my content", []) + let event = EventBuilder::text_note("my content", []) .to_event(&my_keys) .unwrap(); assert!(!&event.is_expired());