notedeck

One damus client to rule them all
git clone git://jb55.com/notedeck
Log | Files | Refs | README | LICENSE

commit 80fc623bed756c99624a76d49057979a2100add2
parent 12d629e36a48b1b5b59faa1ebbac69bc4809643f
Author: kernelkind <kernelkind@gmail.com>
Date:   Sun,  1 Feb 2026 20:39:39 -0500

refactor(websocket): move `Relay` from mod.rs -> websocket.rs

Signed-off-by: kernelkind <kernelkind@gmail.com>

Diffstat:
Mcrates/enostr/src/relay/mod.rs | 86+++----------------------------------------------------------------------------
Acrates/enostr/src/relay/websocket.rs | 85+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 88 insertions(+), 83 deletions(-)

diff --git a/crates/enostr/src/relay/mod.rs b/crates/enostr/src/relay/mod.rs @@ -1,14 +1,10 @@ -use ewebsock::{Options, WsMessage, WsReceiver, WsSender}; - -use crate::{ClientMessage, Result}; -use std::fmt; -use std::hash::{Hash, Hasher}; -use tracing::{debug, error}; - pub mod message; mod multicast; pub mod pool; pub mod subs_debug; +mod websocket; + +pub use websocket::Relay; #[derive(Debug, Copy, Clone)] pub enum RelayStatus { @@ -16,79 +12,3 @@ pub enum RelayStatus { Connecting, Disconnected, } - -pub struct Relay { - pub url: nostr::RelayUrl, - pub status: RelayStatus, - pub sender: WsSender, - pub receiver: WsReceiver, -} - -impl fmt::Debug for Relay { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - f.debug_struct("Relay") - .field("url", &self.url) - .field("status", &self.status) - .finish() - } -} - -impl Hash for Relay { - fn hash<H: Hasher>(&self, state: &mut H) { - // Hashes the Relay by hashing the URL - self.url.hash(state); - } -} - -impl PartialEq for Relay { - fn eq(&self, other: &Self) -> bool { - self.url == other.url - } -} - -impl Eq for Relay {} - -impl Relay { - pub fn new(url: nostr::RelayUrl, wakeup: impl Fn() + Send + Sync + 'static) -> Result<Self> { - let status = RelayStatus::Connecting; - let (sender, receiver) = - ewebsock::connect_with_wakeup(url.as_str(), Options::default(), wakeup)?; - - Ok(Self { - url, - sender, - receiver, - status, - }) - } - - pub fn send(&mut self, msg: &ClientMessage) { - let json = match msg.to_json() { - Ok(json) => { - debug!("sending {} to {}", json, self.url); - json - } - Err(e) => { - error!("error serializing json for filter: {e}"); - return; - } - }; - - let txt = WsMessage::Text(json); - self.sender.send(txt); - } - - pub fn connect(&mut self, wakeup: impl Fn() + Send + Sync + 'static) -> Result<()> { - let (sender, receiver) = - ewebsock::connect_with_wakeup(self.url.as_str(), Options::default(), wakeup)?; - self.status = RelayStatus::Connecting; - self.sender = sender; - self.receiver = receiver; - Ok(()) - } - - pub fn ping(&mut self) { - let msg = WsMessage::Ping(vec![]); - self.sender.send(msg); - } -} diff --git a/crates/enostr/src/relay/websocket.rs b/crates/enostr/src/relay/websocket.rs @@ -0,0 +1,85 @@ +use crate::{relay::RelayStatus, ClientMessage, Result}; + +use std::{ + fmt, + hash::{Hash, Hasher}, +}; + +use ewebsock::{Options, WsMessage, WsReceiver, WsSender}; +use tracing::{debug, error}; + +pub struct Relay { + pub url: nostr::RelayUrl, + pub status: RelayStatus, + pub sender: WsSender, + pub receiver: WsReceiver, +} + +impl fmt::Debug for Relay { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.debug_struct("Relay") + .field("url", &self.url) + .field("status", &self.status) + .finish() + } +} + +impl Hash for Relay { + fn hash<H: Hasher>(&self, state: &mut H) { + // Hashes the Relay by hashing the URL + self.url.hash(state); + } +} + +impl PartialEq for Relay { + fn eq(&self, other: &Self) -> bool { + self.url == other.url + } +} + +impl Eq for Relay {} + +impl Relay { + pub fn new(url: nostr::RelayUrl, wakeup: impl Fn() + Send + Sync + 'static) -> Result<Self> { + let status = RelayStatus::Connecting; + let (sender, receiver) = + ewebsock::connect_with_wakeup(url.as_str(), Options::default(), wakeup)?; + + Ok(Self { + url, + sender, + receiver, + status, + }) + } + + pub fn send(&mut self, msg: &ClientMessage) { + let json = match msg.to_json() { + Ok(json) => { + debug!("sending {} to {}", json, self.url); + json + } + Err(e) => { + error!("error serializing json for filter: {e}"); + return; + } + }; + + let txt = WsMessage::Text(json); + self.sender.send(txt); + } + + pub fn connect(&mut self, wakeup: impl Fn() + Send + Sync + 'static) -> Result<()> { + let (sender, receiver) = + ewebsock::connect_with_wakeup(self.url.as_str(), Options::default(), wakeup)?; + self.status = RelayStatus::Connecting; + self.sender = sender; + self.receiver = receiver; + Ok(()) + } + + pub fn ping(&mut self) { + let msg = WsMessage::Ping(vec![]); + self.sender.send(msg); + } +}