many improvements
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
use crate::{Error, Result};
|
||||
use crate::{Error, Pubkey, Result};
|
||||
use serde_derive::{Deserialize, Serialize};
|
||||
use std::hash::{Hash, Hasher};
|
||||
|
||||
@@ -6,10 +6,10 @@ use std::hash::{Hash, Hasher};
|
||||
#[derive(Serialize, Deserialize, Debug, Clone)]
|
||||
pub struct Event {
|
||||
/// 32-bytes sha256 of the the serialized event data
|
||||
pub id: String,
|
||||
pub id: EventId,
|
||||
/// 32-bytes hex-encoded public key of the event creator
|
||||
#[serde(rename = "pubkey")]
|
||||
pub pubkey: String,
|
||||
pub pubkey: Pubkey,
|
||||
/// unix timestamp in seconds
|
||||
pub created_at: u64,
|
||||
/// integer
|
||||
@@ -26,7 +26,7 @@ pub struct Event {
|
||||
// Implement Hash trait
|
||||
impl Hash for Event {
|
||||
fn hash<H: Hasher>(&self, state: &mut H) {
|
||||
self.id.hash(state);
|
||||
self.id.0.hash(state);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -59,8 +59,8 @@ impl Event {
|
||||
sig: &str,
|
||||
) -> Result<Self> {
|
||||
let event = Event {
|
||||
id: id.to_string(),
|
||||
pubkey: pubkey.to_string(),
|
||||
id: id.to_string().into(),
|
||||
pubkey: pubkey.to_string().into(),
|
||||
created_at,
|
||||
kind,
|
||||
tags,
|
||||
@@ -79,3 +79,18 @@ impl std::str::FromStr for Event {
|
||||
Event::from_json(s)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug, Eq, PartialEq, Clone, Hash)]
|
||||
pub struct EventId(String);
|
||||
|
||||
impl From<String> for EventId {
|
||||
fn from(s: String) -> Self {
|
||||
EventId(s)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<EventId> for String {
|
||||
fn from(evid: EventId) -> Self {
|
||||
evid.0
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,19 +1,20 @@
|
||||
use crate::{EventId, Pubkey};
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug, Eq, PartialEq, Clone)]
|
||||
pub struct Filter {
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
ids: Option<Vec<String>>,
|
||||
ids: Option<Vec<EventId>>,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
authors: Option<Vec<String>>,
|
||||
authors: Option<Vec<Pubkey>>,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
kinds: Option<Vec<u64>>,
|
||||
#[serde(rename = "#e")]
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
events: Option<Vec<String>>,
|
||||
events: Option<Vec<EventId>>,
|
||||
#[serde(rename = "#p")]
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pubkeys: Option<Vec<String>>,
|
||||
pubkeys: Option<Vec<Pubkey>>,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
since: Option<u64>, // unix timestamp seconds
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
@@ -36,12 +37,12 @@ impl Filter {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn ids(mut self, ids: Vec<String>) -> Self {
|
||||
pub fn ids(mut self, ids: Vec<EventId>) -> Self {
|
||||
self.ids = Some(ids);
|
||||
self
|
||||
}
|
||||
|
||||
pub fn authors(mut self, authors: Vec<String>) -> Self {
|
||||
pub fn authors(mut self, authors: Vec<Pubkey>) -> Self {
|
||||
self.authors = Some(authors);
|
||||
self
|
||||
}
|
||||
@@ -51,12 +52,12 @@ impl Filter {
|
||||
self
|
||||
}
|
||||
|
||||
pub fn events(mut self, events: Vec<String>) -> Self {
|
||||
pub fn events(mut self, events: Vec<EventId>) -> Self {
|
||||
self.events = Some(events);
|
||||
self
|
||||
}
|
||||
|
||||
pub fn pubkeys(mut self, pubkeys: Vec<String>) -> Self {
|
||||
pub fn pubkeys(mut self, pubkeys: Vec<Pubkey>) -> Self {
|
||||
self.pubkeys = Some(pubkeys);
|
||||
self
|
||||
}
|
||||
|
||||
@@ -2,12 +2,17 @@ mod client;
|
||||
mod error;
|
||||
mod event;
|
||||
mod filter;
|
||||
mod profile;
|
||||
mod pubkey;
|
||||
mod relay;
|
||||
|
||||
pub use client::ClientMessage;
|
||||
pub use error::Error;
|
||||
pub use event::Event;
|
||||
pub use event::{Event, EventId};
|
||||
pub use ewebsock;
|
||||
pub use filter::Filter;
|
||||
pub use profile::Profile;
|
||||
pub use pubkey::Pubkey;
|
||||
pub use relay::message::{RelayEvent, RelayMessage};
|
||||
pub use relay::pool::{PoolEvent, RelayPool};
|
||||
pub use relay::Relay;
|
||||
|
||||
38
enostr/src/profile.rs
Normal file
38
enostr/src/profile.rs
Normal file
@@ -0,0 +1,38 @@
|
||||
use serde_json::Value;
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct Profile(Value);
|
||||
|
||||
impl Profile {
|
||||
pub fn new(value: Value) -> Profile {
|
||||
Profile(value)
|
||||
}
|
||||
|
||||
pub fn name(&self) -> Option<&str> {
|
||||
return self.0["name"].as_str();
|
||||
}
|
||||
|
||||
pub fn display_name(&self) -> Option<&str> {
|
||||
return self.0["display_name"].as_str();
|
||||
}
|
||||
|
||||
pub fn lud06(&self) -> Option<&str> {
|
||||
return self.0["lud06"].as_str();
|
||||
}
|
||||
|
||||
pub fn lud16(&self) -> Option<&str> {
|
||||
return self.0["lud16"].as_str();
|
||||
}
|
||||
|
||||
pub fn about(&self) -> Option<&str> {
|
||||
return self.0["about"].as_str();
|
||||
}
|
||||
|
||||
pub fn picture(&self) -> Option<&str> {
|
||||
return self.0["picture"].as_str();
|
||||
}
|
||||
|
||||
pub fn website(&self) -> Option<&str> {
|
||||
return self.0["website"].as_str();
|
||||
}
|
||||
}
|
||||
22
enostr/src/pubkey.rs
Normal file
22
enostr/src/pubkey.rs
Normal file
@@ -0,0 +1,22 @@
|
||||
use serde_derive::{Deserialize, Serialize};
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug, Eq, PartialEq, Clone, Hash)]
|
||||
pub struct Pubkey(String);
|
||||
|
||||
impl AsRef<str> for Pubkey {
|
||||
fn as_ref(&self) -> &str {
|
||||
&self.0
|
||||
}
|
||||
}
|
||||
|
||||
impl From<String> for Pubkey {
|
||||
fn from(s: String) -> Self {
|
||||
Pubkey(s)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<Pubkey> for String {
|
||||
fn from(pk: Pubkey) -> Self {
|
||||
pk.0
|
||||
}
|
||||
}
|
||||
@@ -58,9 +58,12 @@ impl Relay {
|
||||
})
|
||||
}
|
||||
|
||||
pub fn subscribe(&mut self, subid: String, filters: Vec<Filter>) {
|
||||
let cmd = ClientMessage::req(subid, filters);
|
||||
let txt = WsMessage::Text(cmd.to_json());
|
||||
pub fn send(&mut self, msg: &ClientMessage) {
|
||||
let txt = WsMessage::Text(msg.to_json());
|
||||
self.sender.send(txt);
|
||||
}
|
||||
|
||||
pub fn subscribe(&mut self, subid: String, filters: Vec<Filter>) {
|
||||
self.send(&ClientMessage::req(subid, filters));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
use crate::relay::message::RelayEvent;
|
||||
use crate::relay::Relay;
|
||||
use crate::Result;
|
||||
use tracing::error;
|
||||
use crate::{ClientMessage, Result};
|
||||
use ewebsock::WsMessage;
|
||||
use tracing::{debug, error};
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct PoolEvent<'a> {
|
||||
@@ -34,6 +35,21 @@ impl RelayPool {
|
||||
return false;
|
||||
}
|
||||
|
||||
pub fn send(&mut self, cmd: &ClientMessage) {
|
||||
for relay in &mut self.relays {
|
||||
relay.send(cmd);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn send_to(&mut self, cmd: &ClientMessage, relay_url: &str) {
|
||||
for relay in &mut self.relays {
|
||||
if relay.url == relay_url {
|
||||
relay.send(cmd);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Adds a websocket url to the RelayPool.
|
||||
pub fn add_url(
|
||||
&mut self,
|
||||
@@ -47,11 +63,23 @@ impl RelayPool {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn try_recv(&self) -> Option<PoolEvent<'_>> {
|
||||
for relay in &self.relays {
|
||||
/// Attempts to receive a pool event from a list of relays. The function searches each relay in the list in order, attempting to receive a message from each. If a message is received, return it. If no message is received from any relays, None is returned.
|
||||
pub fn try_recv(&mut self) -> Option<PoolEvent<'_>> {
|
||||
for relay in &mut self.relays {
|
||||
if let Some(msg) = relay.receiver.try_recv() {
|
||||
match msg.try_into() {
|
||||
Ok(event) => {
|
||||
// let's just handle pongs here.
|
||||
// We only need to do this natively.
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
match event {
|
||||
RelayEvent::Other(WsMessage::Ping(ref bs)) => {
|
||||
debug!("pong {}", &relay.url);
|
||||
relay.sender.send(WsMessage::Pong(bs.to_owned()));
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
|
||||
return Some(PoolEvent {
|
||||
event,
|
||||
relay: &relay.url,
|
||||
@@ -68,6 +96,4 @@ impl RelayPool {
|
||||
|
||||
None
|
||||
}
|
||||
|
||||
pub fn connect() {}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user