Skip to content

Commit

Permalink
nostr: remove new_ prefix from ClientMessage and RelayMessage c…
Browse files Browse the repository at this point in the history
…onstructors
  • Loading branch information
yukibtc committed Jan 16, 2024
1 parent 9fa67f4 commit b3aee21
Show file tree
Hide file tree
Showing 9 changed files with 136 additions and 66 deletions.
8 changes: 4 additions & 4 deletions bindings/nostr-js/src/message/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<JsFilter>) -> Self {
Self {
inner: ClientMessage::new_req(
inner: ClientMessage::req(
SubscriptionId::new(subscription_id),
filters.into_iter().map(|f| f.inner()).collect(),
),
Expand All @@ -52,7 +52,7 @@ impl JsClientMessage {
/// Create new `COUNT` message
pub fn count(subscription_id: String, filters: Vec<JsFilter>) -> Self {
Self {
inner: ClientMessage::new_count(
inner: ClientMessage::count(
SubscriptionId::new(subscription_id),
filters.into_iter().map(|f| f.inner()).collect(),
),
Expand All @@ -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()),
}
}

Expand Down
17 changes: 7 additions & 10 deletions bindings/nostr-js/src/message/relay.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
}
}

Expand Down
2 changes: 1 addition & 1 deletion crates/nostr-sdk/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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?;
Expand Down
4 changes: 1 addition & 3 deletions crates/nostr-sdk/src/client/blocking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -422,9 +422,7 @@ impl Client {
where
S: Into<String>,
{
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
Expand Down
4 changes: 2 additions & 2 deletions crates/nostr-sdk/src/client/signer/nip46.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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?;

Expand Down Expand Up @@ -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?;

Expand Down
20 changes: 9 additions & 11 deletions crates/nostr-sdk/src/relay/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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");
Expand All @@ -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(())
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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!(
Expand Down Expand Up @@ -1439,7 +1439,7 @@ impl Relay {
timeout: Duration,
) -> Result<usize, Error> {
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;
Expand Down Expand Up @@ -1577,10 +1577,8 @@ impl Relay {
let filter = Filter::new().ids(ids);
let events: Vec<Event> =
self.database.query(vec![filter], Order::Desc).await?;
let msgs: Vec<ClientMessage> = events
.into_iter()
.map(ClientMessage::new_event)
.collect();
let msgs: Vec<ClientMessage> =
events.into_iter().map(ClientMessage::event).collect();
if let Err(e) = self
.batch_msg(msgs, Some(opts.batch_send_timeout))
.await
Expand Down
2 changes: 1 addition & 1 deletion crates/nostr/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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(())
Expand Down
52 changes: 38 additions & 14 deletions crates/nostr/src/message/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Filter>) -> Self {
/// Create `REQ` message
pub fn req(subscription_id: SubscriptionId, filters: Vec<Filter>) -> Self {
Self::Req {
subscription_id,
filters,
}
}

/// Create new `COUNT` message
pub fn new_count(subscription_id: SubscriptionId, filters: Vec<Filter>) -> Self {
/// Create new `REQ` message
#[deprecated(since = "0.27.0", note = "Use `req` instead")]
pub fn new_req(subscription_id: SubscriptionId, filters: Vec<Filter>) -> Self {
Self::req(subscription_id, filters)
}

/// Create `COUNT` message
pub fn count(subscription_id: SubscriptionId, filters: Vec<Filter>) -> 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<Filter>) -> 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
Expand Down Expand Up @@ -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);
}
Expand All @@ -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<Filter> = 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);
}
Expand All @@ -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<Filter> = 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);
}
Expand All @@ -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);
}
Expand Down Expand Up @@ -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"]}]"##
Expand All @@ -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"]}]"##
Expand Down
Loading

0 comments on commit b3aee21

Please sign in to comment.