receiving initial messages!
This commit is contained in:
@@ -1,4 +1,5 @@
|
|||||||
use crate::{Event, Filter}
|
use crate::{Event, Filter};
|
||||||
|
use serde_json::json;
|
||||||
|
|
||||||
/// Messages sent by clients, received by relays
|
/// Messages sent by clients, received by relays
|
||||||
#[derive(Debug, Eq, PartialEq)]
|
#[derive(Debug, Eq, PartialEq)]
|
||||||
@@ -16,8 +17,8 @@ pub enum ClientMessage {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl ClientMessage {
|
impl ClientMessage {
|
||||||
pub fn event(ev: Event) -> Self {
|
pub fn event(event: Event) -> Self {
|
||||||
ClientMessage::Event {ev}
|
ClientMessage::Event { event }
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn req(sub_id: String, filters: Vec<Filter>) -> Self {
|
pub fn req(sub_id: String, filters: Vec<Filter>) -> Self {
|
||||||
@@ -31,11 +32,8 @@ impl ClientMessage {
|
|||||||
pub fn to_json(&self) -> String {
|
pub fn to_json(&self) -> String {
|
||||||
match self {
|
match self {
|
||||||
Self::Event { event } => json!(["EVENT", event]).to_string(),
|
Self::Event { event } => json!(["EVENT", event]).to_string(),
|
||||||
Self::Req {
|
Self::Req { sub_id, filters } => {
|
||||||
subscription_id,
|
let mut json = json!(["REQ", sub_id]);
|
||||||
filters,
|
|
||||||
} => {
|
|
||||||
let mut json = json!(["REQ", subscription_id]);
|
|
||||||
let mut filters = json!(filters);
|
let mut filters = json!(filters);
|
||||||
|
|
||||||
if let Some(json) = json.as_array_mut() {
|
if let Some(json) = json.as_array_mut() {
|
||||||
@@ -46,7 +44,7 @@ impl ClientMessage {
|
|||||||
|
|
||||||
json.to_string()
|
json.to_string()
|
||||||
}
|
}
|
||||||
Self::Close { subscription_id } => json!(["CLOSE", subscription_id]).to_string(),
|
Self::Close { sub_id } => json!(["CLOSE", sub_id]).to_string(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1 +1,3 @@
|
|||||||
mod message;
|
mod message;
|
||||||
|
|
||||||
|
pub use message::ClientMessage;
|
||||||
|
|||||||
@@ -21,3 +21,58 @@ pub struct Filter {
|
|||||||
#[serde(skip_serializing_if = "Option::is_none")]
|
#[serde(skip_serializing_if = "Option::is_none")]
|
||||||
limit: Option<u16>,
|
limit: Option<u16>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl Filter {
|
||||||
|
pub fn new() -> Filter {
|
||||||
|
Filter {
|
||||||
|
ids: None,
|
||||||
|
authors: None,
|
||||||
|
kinds: None,
|
||||||
|
events: None,
|
||||||
|
pubkeys: None,
|
||||||
|
since: None,
|
||||||
|
until: None,
|
||||||
|
limit: None,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn ids(mut self, ids: Vec<String>) -> Self {
|
||||||
|
self.ids = Some(ids);
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn authors(mut self, authors: Vec<String>) -> Self {
|
||||||
|
self.authors = Some(authors);
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn kinds(mut self, kinds: Vec<u64>) -> Self {
|
||||||
|
self.kinds = Some(kinds);
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn events(mut self, events: Vec<String>) -> Self {
|
||||||
|
self.events = Some(events);
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn pubkeys(mut self, pubkeys: Vec<String>) -> Self {
|
||||||
|
self.pubkeys = Some(pubkeys);
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn since(mut self, since: u64) -> Self {
|
||||||
|
self.since = Some(since);
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn until(mut self, until: u64) -> Self {
|
||||||
|
self.until = Some(until);
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn limit(mut self, limit: u16) -> Self {
|
||||||
|
self.limit = Some(limit);
|
||||||
|
self
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,12 +1,15 @@
|
|||||||
|
mod client;
|
||||||
mod error;
|
mod error;
|
||||||
mod event;
|
mod event;
|
||||||
mod filter;
|
mod filter;
|
||||||
mod relay;
|
mod relay;
|
||||||
|
|
||||||
|
pub use client::ClientMessage;
|
||||||
pub use error::Error;
|
pub use error::Error;
|
||||||
pub use event::Event;
|
pub use event::Event;
|
||||||
pub use filter::Filter;
|
pub use filter::Filter;
|
||||||
pub use relay::pool::RelayPool;
|
pub use relay::message::{RelayEvent, RelayMessage};
|
||||||
|
pub use relay::pool::{PoolEvent, RelayPool};
|
||||||
pub use relay::Relay;
|
pub use relay::Relay;
|
||||||
|
|
||||||
pub type Result<T> = std::result::Result<T, error::Error>;
|
pub type Result<T> = std::result::Result<T, error::Error>;
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
use ewebsock::{WsReceiver, WsSender};
|
use ewebsock::{WsMessage, WsReceiver, WsSender};
|
||||||
|
|
||||||
use crate::Result;
|
use crate::{ClientMessage, Filter, Result};
|
||||||
use std::fmt;
|
use std::fmt;
|
||||||
use std::hash::{Hash, Hasher};
|
use std::hash::{Hash, Hasher};
|
||||||
|
|
||||||
@@ -57,4 +57,10 @@ impl Relay {
|
|||||||
status,
|
status,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn subscribe(&mut self, subid: String, filters: Vec<Filter>) {
|
||||||
|
let cmd = ClientMessage::req(subid, filters);
|
||||||
|
let txt = WsMessage::Text(cmd.to_json());
|
||||||
|
self.sender.send(txt);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,13 +4,13 @@ use crate::Result;
|
|||||||
use tracing::error;
|
use tracing::error;
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct PoolMessage<'a> {
|
pub struct PoolEvent<'a> {
|
||||||
relay: &'a str,
|
pub relay: &'a str,
|
||||||
event: RelayEvent,
|
pub event: RelayEvent,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct RelayPool {
|
pub struct RelayPool {
|
||||||
relays: Vec<Relay>,
|
pub relays: Vec<Relay>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Default for RelayPool {
|
impl Default for RelayPool {
|
||||||
@@ -47,12 +47,12 @@ impl RelayPool {
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn try_recv(&self) -> Option<PoolMessage<'_>> {
|
pub fn try_recv(&self) -> Option<PoolEvent<'_>> {
|
||||||
for relay in &self.relays {
|
for relay in &self.relays {
|
||||||
if let Some(msg) = relay.receiver.try_recv() {
|
if let Some(msg) = relay.receiver.try_recv() {
|
||||||
match msg.try_into() {
|
match msg.try_into() {
|
||||||
Ok(event) => {
|
Ok(event) => {
|
||||||
return Some(PoolMessage {
|
return Some(PoolEvent {
|
||||||
event,
|
event,
|
||||||
relay: &relay.url,
|
relay: &relay.url,
|
||||||
});
|
});
|
||||||
|
|||||||
39
src/app.rs
39
src/app.rs
@@ -2,6 +2,7 @@
|
|||||||
//use egui_android::SimpleApp;
|
//use egui_android::SimpleApp;
|
||||||
use egui_extras::RetainedImage;
|
use egui_extras::RetainedImage;
|
||||||
|
|
||||||
|
use enostr::{Filter, RelayEvent, RelayMessage};
|
||||||
//use nostr_rust::events::Event;
|
//use nostr_rust::events::Event;
|
||||||
use poll_promise::Promise;
|
use poll_promise::Promise;
|
||||||
//use std::borrow::{Borrow, Cow};
|
//use std::borrow::{Borrow, Cow};
|
||||||
@@ -9,7 +10,7 @@ use egui::Context;
|
|||||||
//use log::error;
|
//use log::error;
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use std::hash::Hash;
|
use std::hash::Hash;
|
||||||
use tracing::{debug, error, info};
|
use tracing::{debug, error, info, warn};
|
||||||
|
|
||||||
use enostr::{Event, RelayPool};
|
use enostr::{Event, RelayPool};
|
||||||
|
|
||||||
@@ -73,6 +74,34 @@ fn relay_setup(pool: &mut RelayPool, ctx: &egui::Context) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn send_initial_filters(pool: &mut RelayPool, relay_url: &str) {
|
||||||
|
let filter = Filter::new().limit(20).kinds(vec![1, 42]);
|
||||||
|
let subid = "initial";
|
||||||
|
for relay in &mut pool.relays {
|
||||||
|
if relay.url == relay_url {
|
||||||
|
relay.subscribe(subid.to_string(), vec![filter]);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn try_process_event(damus: &mut Damus) {
|
||||||
|
let m_ev = damus.pool.try_recv();
|
||||||
|
if m_ev.is_none() {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
let ev = m_ev.unwrap();
|
||||||
|
let relay = ev.relay.to_owned();
|
||||||
|
|
||||||
|
match &ev.event {
|
||||||
|
RelayEvent::Opened => send_initial_filters(&mut damus.pool, &relay),
|
||||||
|
RelayEvent::Closed => warn!("{} connection closed", &relay), /* TODO: handle reconnects */
|
||||||
|
RelayEvent::Other(msg) => debug!("Other ws message: {:?}", msg),
|
||||||
|
RelayEvent::Message(msg) => process_message(damus, &msg),
|
||||||
|
}
|
||||||
|
//info!("recv {:?}", ev)
|
||||||
|
}
|
||||||
|
|
||||||
fn update_damus(damus: &mut Damus, ctx: &egui::Context) {
|
fn update_damus(damus: &mut Damus, ctx: &egui::Context) {
|
||||||
if damus.state == DamusState::Initializing {
|
if damus.state == DamusState::Initializing {
|
||||||
damus.pool = RelayPool::new();
|
damus.pool = RelayPool::new();
|
||||||
@@ -80,9 +109,11 @@ fn update_damus(damus: &mut Damus, ctx: &egui::Context) {
|
|||||||
damus.state = DamusState::Initialized;
|
damus.state = DamusState::Initialized;
|
||||||
}
|
}
|
||||||
|
|
||||||
if let Some(ev) = damus.pool.try_recv() {
|
try_process_event(damus);
|
||||||
info!("recv {:?}", ev)
|
}
|
||||||
}
|
|
||||||
|
fn process_message(damus: &mut Damus, msg: &RelayMessage) {
|
||||||
|
info!("process message {:?}", msg);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn render_damus(damus: &mut Damus, ctx: &Context) {
|
fn render_damus(damus: &mut Damus, ctx: &Context) {
|
||||||
|
|||||||
Reference in New Issue
Block a user