diff --git a/bindings/nostr-js/src/message/client.rs b/bindings/nostr-js/src/message/client.rs index 9bb77c347..fe3922c19 100644 --- a/bindings/nostr-js/src/message/client.rs +++ b/bindings/nostr-js/src/message/client.rs @@ -35,14 +35,14 @@ impl JsClientMessage { /// Create new `EVENT` message pub fn event(event: &JsEvent) -> Self { Self { - inner: ClientMessage::new_event(event.deref().clone()), + inner: ClientMessage::event(event.deref().clone()), } } /// Create new `REQ` message pub fn req(subscription_id: String, filters: Vec) -> Self { Self { - inner: ClientMessage::new_req( + inner: ClientMessage::req( SubscriptionId::new(subscription_id), filters.into_iter().map(|f| f.inner()).collect(), ), @@ -52,7 +52,7 @@ impl JsClientMessage { /// Create new `COUNT` message pub fn count(subscription_id: String, filters: Vec) -> Self { Self { - inner: ClientMessage::new_count( + inner: ClientMessage::count( SubscriptionId::new(subscription_id), filters.into_iter().map(|f| f.inner()).collect(), ), @@ -69,7 +69,7 @@ impl JsClientMessage { /// Create new `AUTH` message pub fn auth(event: &JsEvent) -> Self { Self { - inner: ClientMessage::new_auth(event.deref().clone()), + inner: ClientMessage::auth(event.deref().clone()), } } diff --git a/bindings/nostr-js/src/message/relay.rs b/bindings/nostr-js/src/message/relay.rs index 4cfd8ed4b..0ea33d201 100644 --- a/bindings/nostr-js/src/message/relay.rs +++ b/bindings/nostr-js/src/message/relay.rs @@ -26,52 +26,49 @@ impl JsRelayMessage { /// Create new `EVENT` message pub fn event(subscription_id: String, event: &JsEvent) -> Self { Self { - inner: RelayMessage::new_event( - SubscriptionId::new(subscription_id), - event.deref().clone(), - ), + inner: RelayMessage::event(SubscriptionId::new(subscription_id), event.deref().clone()), } } /// Create new `NOTICE` message pub fn notice(message: String) -> Self { Self { - inner: RelayMessage::new_notice(message), + inner: RelayMessage::notice(message), } } /// Create new `CLOSED` message pub fn closed(subscription_id: String, message: String) -> Self { Self { - inner: RelayMessage::new_closed(SubscriptionId::new(subscription_id), message), + inner: RelayMessage::closed(SubscriptionId::new(subscription_id), message), } } /// Create new `EOSE` message pub fn eose(subscription_id: String) -> Self { Self { - inner: RelayMessage::new_eose(SubscriptionId::new(subscription_id)), + inner: RelayMessage::eose(SubscriptionId::new(subscription_id)), } } /// Create new `OK` message pub fn ok(event_id: &JsEventId, status: bool, message: String) -> Self { Self { - inner: RelayMessage::new_ok(**event_id, status, message), + inner: RelayMessage::ok(**event_id, status, message), } } /// Create new `AUTH` message pub fn auth(challenge: String) -> Self { Self { - inner: RelayMessage::new_auth(challenge), + inner: RelayMessage::auth(challenge), } } /// Create new `EVENT` message pub fn count(subscription_id: String, count: f64) -> Self { Self { - inner: RelayMessage::new_count(SubscriptionId::new(subscription_id), count as usize), + inner: RelayMessage::count(SubscriptionId::new(subscription_id), count as usize), } } diff --git a/crates/nostr-sdk/README.md b/crates/nostr-sdk/README.md index b2996fd34..c1c52f06c 100644 --- a/crates/nostr-sdk/README.md +++ b/crates/nostr-sdk/README.md @@ -89,7 +89,7 @@ async fn main() -> Result<()> { // Send custom event let event_id = EventId::from_bech32("note1z3lwphdc7gdf6n0y4vaaa0x7ck778kg638lk0nqv2yd343qda78sf69t6r")?; let public_key = XOnlyPublicKey::from_bech32("npub14rnkcwkw0q5lnmjye7ffxvy7yxscyjl3u4mrr5qxsks76zctmz3qvuftjz")?; - let event: Event = EventBuilder::new_reaction(event_id, public_key, "🧡").to_event(&my_keys)?; + let event: Event = EventBuilder::reaction(event_id, public_key, "🧡").to_event(&my_keys)?; // Send custom event to all relays // client.send_event(event).await?; diff --git a/crates/nostr-sdk/src/client/blocking.rs b/crates/nostr-sdk/src/client/blocking.rs index aa6366f60..07fbd5d57 100644 --- a/crates/nostr-sdk/src/client/blocking.rs +++ b/crates/nostr-sdk/src/client/blocking.rs @@ -422,9 +422,7 @@ impl Client { where S: Into, { - RUNTIME.block_on(async { - self.client.zap_receipt(bolt11, preimage, zap_request).await - }) + RUNTIME.block_on(async { self.client.zap_receipt(bolt11, preimage, zap_request).await }) } /// Create zap receipt event diff --git a/crates/nostr-sdk/src/client/signer/nip46.rs b/crates/nostr-sdk/src/client/signer/nip46.rs index 7e55e5bcd..a81fbe722 100644 --- a/crates/nostr-sdk/src/client/signer/nip46.rs +++ b/crates/nostr-sdk/src/client/signer/nip46.rs @@ -128,7 +128,7 @@ impl Client { // Subscribe self.send_msg_to( signer.relay_url(), - ClientMessage::new_req(id.clone(), vec![filter]), + ClientMessage::req(id.clone(), vec![filter]), ) .await?; @@ -196,7 +196,7 @@ impl Client { // Subscribe self.send_msg_to( signer.relay_url(), - ClientMessage::new_req(sub_id.clone(), vec![filter]), + ClientMessage::req(sub_id.clone(), vec![filter]), ) .await?; diff --git a/crates/nostr-sdk/src/relay/mod.rs b/crates/nostr-sdk/src/relay/mod.rs index f1df6c5f3..f119f3fdd 100644 --- a/crates/nostr-sdk/src/relay/mod.rs +++ b/crates/nostr-sdk/src/relay/mod.rs @@ -1003,7 +1003,7 @@ impl Relay { } time::timeout(Some(opts.timeout), async { - self.send_msg(ClientMessage::new_event(event), None).await?; + self.send_msg(ClientMessage::event(event), None).await?; let mut notifications = self.notification_sender.subscribe(); while let Ok(notification) = notifications.recv().await { match notification { @@ -1070,7 +1070,7 @@ impl Relay { for event in events.into_iter() { missing.insert(event.id()); - msgs.push(ClientMessage::new_event(event)); + msgs.push(ClientMessage::event(event)); } time::timeout(Some(opts.timeout), async { @@ -1142,7 +1142,7 @@ impl Relay { for (internal_id, sub) in subscriptions.into_iter() { if !sub.filters.is_empty() { - self.send_msg(ClientMessage::new_req(sub.id.clone(), sub.filters), wait) + self.send_msg(ClientMessage::req(sub.id.clone(), sub.filters), wait) .await?; } else { tracing::warn!("Subscription '{internal_id}' has empty filters"); @@ -1165,7 +1165,7 @@ impl Relay { .subscription(&internal_id) .await .ok_or(Error::InternalIdNotFound)?; - self.send_msg(ClientMessage::new_req(sub.id, sub.filters), wait) + self.send_msg(ClientMessage::req(sub.id, sub.filters), wait) .await?; Ok(()) @@ -1356,7 +1356,7 @@ impl Relay { let id = SubscriptionId::generate(); - self.send_msg(ClientMessage::new_req(id.clone(), filters), None) + self.send_msg(ClientMessage::req(id.clone(), filters), None) .await?; self.handle_events_of(id.clone(), timeout, opts, callback) @@ -1404,7 +1404,7 @@ impl Relay { // Subscribe if let Err(e) = relay - .send_msg(ClientMessage::new_req(id.clone(), filters), None) + .send_msg(ClientMessage::req(id.clone(), filters), None) .await { tracing::error!( @@ -1439,7 +1439,7 @@ impl Relay { timeout: Duration, ) -> Result { let id = SubscriptionId::generate(); - self.send_msg(ClientMessage::new_count(id.clone(), filters), None) + self.send_msg(ClientMessage::count(id.clone(), filters), None) .await?; let mut count = 0; @@ -1577,10 +1577,8 @@ impl Relay { let filter = Filter::new().ids(ids); let events: Vec = self.database.query(vec![filter], Order::Desc).await?; - let msgs: Vec = events - .into_iter() - .map(ClientMessage::new_event) - .collect(); + let msgs: Vec = + events.into_iter().map(ClientMessage::event).collect(); if let Err(e) = self .batch_msg(msgs, Some(opts.batch_send_timeout)) .await diff --git a/crates/nostr/README.md b/crates/nostr/README.md index b810b42e4..e2c029ef1 100644 --- a/crates/nostr/README.md +++ b/crates/nostr/README.md @@ -57,7 +57,7 @@ fn main() -> Result<()> { 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(); + let json = ClientMessage::event(event).as_json(); println!("{json}"); Ok(()) diff --git a/crates/nostr/src/message/client.rs b/crates/nostr/src/message/client.rs index baeb6c9ab..8b01cb98b 100644 --- a/crates/nostr/src/message/client.rs +++ b/crates/nostr/src/message/client.rs @@ -87,35 +87,59 @@ impl<'de> Deserialize<'de> for ClientMessage { } impl ClientMessage { + /// Create `EVENT` message + pub fn event(event: Event) -> Self { + Self::Event(Box::new(event)) + } + /// Create new `EVENT` message + #[deprecated(since = "0.27.0", note = "Use `event` instead")] pub fn new_event(event: Event) -> Self { - Self::Event(Box::new(event)) + Self::event(event) } - /// Create new `REQ` message - pub fn new_req(subscription_id: SubscriptionId, filters: Vec) -> Self { + /// Create `REQ` message + pub fn req(subscription_id: SubscriptionId, filters: Vec) -> Self { Self::Req { subscription_id, filters, } } - /// Create new `COUNT` message - pub fn new_count(subscription_id: SubscriptionId, filters: Vec) -> Self { + /// Create new `REQ` message + #[deprecated(since = "0.27.0", note = "Use `req` instead")] + pub fn new_req(subscription_id: SubscriptionId, filters: Vec) -> Self { + Self::req(subscription_id, filters) + } + + /// Create `COUNT` message + pub fn count(subscription_id: SubscriptionId, filters: Vec) -> Self { Self::Count { subscription_id, filters, } } + /// Create new `COUNT` message + #[deprecated(since = "0.27.0", note = "Use `count` instead")] + pub fn new_count(subscription_id: SubscriptionId, filters: Vec) -> Self { + Self::count(subscription_id, filters) + } + /// Create new `CLOSE` message pub fn close(subscription_id: SubscriptionId) -> Self { Self::Close(subscription_id) } + /// Create `AUTH` message + pub fn auth(event: Event) -> Self { + Self::Auth(Box::new(event)) + } + /// Create new `AUTH` message + #[deprecated(since = "0.27.0", note = "Use `auth` instead")] pub fn new_auth(event: Event) -> Self { - Self::Auth(Box::new(event)) + Self::auth(event) } /// Create new `NEG-OPEN` message @@ -225,7 +249,7 @@ impl ClientMessage { if v[0] == "EVENT" { if v_len >= 2 { let event = Event::from_value(v[1].clone())?; - return Ok(Self::new_event(event)); + return Ok(Self::event(event)); } else { return Err(MessageHandleError::InvalidMessageFormat); } @@ -236,11 +260,11 @@ impl ClientMessage { if v[0] == "REQ" { if v_len == 2 { let subscription_id: SubscriptionId = serde_json::from_value(v[1].clone())?; - return Ok(Self::new_req(subscription_id, Vec::new())); + return Ok(Self::req(subscription_id, Vec::new())); } else if v_len >= 3 { let subscription_id: SubscriptionId = serde_json::from_value(v[1].clone())?; let filters: Vec = serde_json::from_value(Value::Array(v[2..].to_vec()))?; - return Ok(Self::new_req(subscription_id, filters)); + return Ok(Self::req(subscription_id, filters)); } else { return Err(MessageHandleError::InvalidMessageFormat); } @@ -250,11 +274,11 @@ impl ClientMessage { if v[0] == "COUNT" { if v_len == 2 { let subscription_id: SubscriptionId = serde_json::from_value(v[1].clone())?; - return Ok(Self::new_count(subscription_id, Vec::new())); + return Ok(Self::count(subscription_id, Vec::new())); } else if v_len >= 3 { let subscription_id: SubscriptionId = serde_json::from_value(v[1].clone())?; let filters: Vec = serde_json::from_value(Value::Array(v[2..].to_vec()))?; - return Ok(Self::new_count(subscription_id, filters)); + return Ok(Self::count(subscription_id, filters)); } else { return Err(MessageHandleError::InvalidMessageFormat); } @@ -276,7 +300,7 @@ impl ClientMessage { if v[0] == "AUTH" { if v_len >= 2 { let event = Event::from_value(v[1].clone())?; - return Ok(Self::new_auth(event)); + return Ok(Self::auth(event)); } else { return Err(MessageHandleError::InvalidMessageFormat); } @@ -375,7 +399,7 @@ mod tests { Filter::new().pubkey(pk), ]; - let client_req = ClientMessage::new_req(SubscriptionId::new("test"), filters); + let client_req = ClientMessage::req(SubscriptionId::new("test"), filters); assert_eq!( client_req.as_json(), r##"["REQ","test",{"kinds":[4]},{"#p":["379e863e8357163b5bce5d2688dc4f1dcc2d505222fb8d74db600f30535dfdfe"]}]"## @@ -393,7 +417,7 @@ mod tests { Filter::new().pubkey(pk), ]; - let client_req = ClientMessage::new_req(SubscriptionId::new("test"), filters); + let client_req = ClientMessage::req(SubscriptionId::new("test"), filters); assert_eq!( client_req.as_json(), r##"["REQ","test",{"kinds":[22]},{"#p":["379e863e8357163b5bce5d2688dc4f1dcc2d505222fb8d74db600f30535dfdfe"]}]"## diff --git a/crates/nostr/src/message/relay/mod.rs b/crates/nostr/src/message/relay/mod.rs index 9d98cfdea..a9b1f85b8 100644 --- a/crates/nostr/src/message/relay/mod.rs +++ b/crates/nostr/src/message/relay/mod.rs @@ -164,16 +164,22 @@ impl<'de> Deserialize<'de> for RelayMessage { } impl RelayMessage { - /// Create new `EVENT` message - pub fn new_event(subscription_id: SubscriptionId, event: Event) -> Self { + /// Create `EVENT` message + pub fn event(subscription_id: SubscriptionId, event: Event) -> Self { Self::Event { subscription_id, event: Box::new(event), } } - /// Create new `NOTICE` message - pub fn new_notice(message: S) -> Self + /// Create new `EVENT` message + #[deprecated(since = "0.27.0", note = "Use `event` instead")] + pub fn new_event(subscription_id: SubscriptionId, event: Event) -> Self { + Self::event(subscription_id, event) + } + + /// Create `NOTICE` message + pub fn notice(message: S) -> Self where S: Into, { @@ -182,8 +188,17 @@ impl RelayMessage { } } - /// Create new `CLOSED` message - pub fn new_closed(subscription_id: SubscriptionId, message: S) -> Self + /// Create new `NOTICE` message + #[deprecated(since = "0.27.0", note = "Use `notice` instead")] + pub fn new_notice(message: S) -> Self + where + S: Into, + { + Self::notice(message) + } + + /// Create `CLOSED` message + pub fn closed(subscription_id: SubscriptionId, message: S) -> Self where S: Into, { @@ -193,13 +208,28 @@ impl RelayMessage { } } + /// Create new `CLOSED` message + #[deprecated(since = "0.27.0", note = "Use `closed` instead")] + pub fn new_closed(subscription_id: SubscriptionId, message: S) -> Self + where + S: Into, + { + Self::closed(subscription_id, message) + } + + /// Create `EOSE` message + pub fn eose(subscription_id: SubscriptionId) -> Self { + Self::EndOfStoredEvents(subscription_id) + } + /// Create new `EOSE` message + #[deprecated(since = "0.27.0", note = "Use `eose` instead")] pub fn new_eose(subscription_id: SubscriptionId) -> Self { - Self::EndOfStoredEvents(subscription_id) + Self::eose(subscription_id) } - /// Create new `OK` message - pub fn new_ok(event_id: EventId, status: bool, message: S) -> Self + /// Create `OK` message + pub fn ok(event_id: EventId, status: bool, message: S) -> Self where S: Into, { @@ -210,8 +240,17 @@ impl RelayMessage { } } - /// Create new `AUTH` message - pub fn new_auth(challenge: S) -> Self + /// Create new `OK` message + #[deprecated(since = "0.27.0", note = "Use `ok` instead")] + pub fn new_ok(event_id: EventId, status: bool, message: S) -> Self + where + S: Into, + { + Self::ok(event_id, status, message) + } + + /// Create `AUTH` message + pub fn auth(challenge: S) -> Self where S: Into, { @@ -220,14 +259,29 @@ impl RelayMessage { } } - /// Create new `EVENT` message - pub fn new_count(subscription_id: SubscriptionId, count: usize) -> Self { + /// Create new `AUTH` message + #[deprecated(since = "0.27.0", note = "Use `auth` instead")] + pub fn new_auth(challenge: S) -> Self + where + S: Into, + { + Self::auth(challenge) + } + + /// Create `EVENT` message + pub fn count(subscription_id: SubscriptionId, count: usize) -> Self { Self::Count { subscription_id, count, } } + /// Create new `EVENT` message + #[deprecated(since = "0.27.0", note = "Use `count` instead")] + pub fn new_count(subscription_id: SubscriptionId, count: usize) -> Self { + Self::count(subscription_id, count) + } + fn as_value(&self) -> Value { match self { Self::Event { @@ -363,8 +417,7 @@ mod tests { #[test] fn test_handle_valid_notice() { let valid_notice_msg = r#"["NOTICE","Invalid event format!"]"#; - let handled_valid_notice_msg = - RelayMessage::new_notice(String::from("Invalid event format!")); + let handled_valid_notice_msg = RelayMessage::notice(String::from("Invalid event format!")); assert_eq!( RelayMessage::from_json(valid_notice_msg).unwrap(), @@ -386,7 +439,7 @@ mod tests { fn test_handle_valid_closed() { let valid_closed_msg = r#"["CLOSED","random-subscription-id","reason"]"#; let handled_valid_closed_msg = - RelayMessage::new_closed(SubscriptionId::new("random-subscription-id"), "reason"); + RelayMessage::closed(SubscriptionId::new("random-subscription-id"), "reason"); assert_eq!( RelayMessage::from_json(valid_closed_msg).unwrap(), @@ -426,7 +479,7 @@ mod tests { assert_eq!( RelayMessage::from_json(valid_event_msg).unwrap(), - RelayMessage::new_event(SubscriptionId::new("random_string"), handled_event) + RelayMessage::event(SubscriptionId::new("random_string"), handled_event) ); let message = RelayMessage::from_json(r#"["EVENT","bf7da933d6c6d67e5c97f94f17cf8762",{"content":"Think about this.\n\nThe most powerful centralized institutions in the world have been replaced by a protocol that protects the individual. #bitcoin\n\nDo you doubt that we can replace everything else?\n\nBullish on the future of humanity\nnostr:nevent1qqs9ljegkuk2m2ewfjlhxy054n6ld5dfngwzuep0ddhs64gc49q0nmqpzdmhxue69uhhyetvv9ukzcnvv5hx7un8qgsw3mfhnrr0l6ll5zzsrtpeufckv2lazc8k3ru5c3wkjtv8vlwngksrqsqqqqqpttgr27","created_at":1703184271,"id":"38acf9b08d06859e49237688a9fd6558c448766f47457236c2331f93538992c6","kind":1,"pubkey":"e8ed3798c6ffebffa08501ac39e271662bfd160f688f94c45d692d8767dd345a","sig":"f76d5ecc8e7de688ac12b9d19edaacdcffb8f0c8fa2a44c00767363af3f04dbc069542ddc5d2f63c94cb5e6ce701589d538cf2db3b1f1211a96596fabb6ecafe","tags":[["e","5fcb28b72cadab2e4cbf7311f4acf5f6d1a99a1c2e642f6b6f0d5518a940f9ec","","mention"],["p","e8ed3798c6ffebffa08501ac39e271662bfd160f688f94c45d692d8767dd345a","","mention"],["t","bitcoin"],["t","bitcoin"]]}]"#).unwrap(); @@ -453,7 +506,7 @@ mod tests { fn test_handle_valid_eose() { let valid_eose_msg = r#"["EOSE","random-subscription-id"]"#; let handled_valid_eose_msg = - RelayMessage::new_eose(SubscriptionId::new("random-subscription-id")); + RelayMessage::eose(SubscriptionId::new("random-subscription-id")); assert_eq!( RelayMessage::from_json(valid_eose_msg).unwrap(), @@ -472,7 +525,7 @@ mod tests { #[test] fn test_handle_valid_ok() { let valid_ok_msg = r#"["OK", "b1a649ebe8b435ec71d3784793f3bbf4b93e64e17568a741aecd4c7ddeafce30", true, "pow: difficulty 25>=24"]"#; - let handled_valid_ok_msg = RelayMessage::new_ok( + let handled_valid_ok_msg = RelayMessage::ok( EventId::from_hex("b1a649ebe8b435ec71d3784793f3bbf4b93e64e17568a741aecd4c7ddeafce30") .unwrap(), true, @@ -533,7 +586,7 @@ mod tests { assert_eq!( parsed_event, - RelayMessage::new_event(SubscriptionId::new("random_string"), event) + RelayMessage::event(SubscriptionId::new("random_string"), event) ); }