remove duplicate filter types

only use nostrdb::Filter

Signed-off-by: William Casarin <jb55@jb55.com>
This commit is contained in:
William Casarin
2024-08-04 10:40:59 -07:00
parent 8c458f8f78
commit 9328ef2dff
18 changed files with 156 additions and 225 deletions

View File

@@ -1,8 +1,9 @@
use crate::{Filter, Note};
use crate::{Error, Note};
use nostrdb::Filter;
use serde_json::json;
/// Messages sent by clients, received by relays
#[derive(Debug, Eq, PartialEq)]
#[derive(Debug)]
pub enum ClientMessage {
Event {
note: Note,
@@ -34,23 +35,25 @@ impl ClientMessage {
ClientMessage::Close { sub_id }
}
pub fn to_json(&self) -> String {
match self {
pub fn to_json(&self) -> Result<String, Error> {
Ok(match self {
Self::Event { note } => json!(["EVENT", note]).to_string(),
Self::Raw(raw) => raw.clone(),
Self::Req { sub_id, filters } => {
let mut json = json!(["REQ", sub_id]);
let mut filters = json!(filters);
if let Some(json) = json.as_array_mut() {
if let Some(filters) = filters.as_array_mut() {
json.append(filters);
}
if filters.is_empty() {
format!("[\"REQ\",\"{}\",{{ }}]", sub_id)
} else if filters.len() == 1 {
let filters_json_str = filters[0].json()?;
format!("[\"REQ\",\"{}\",{}]", sub_id, filters_json_str)
} else {
let filters_json_str: Result<Vec<String>, Error> = filters
.into_iter()
.map(|f| f.json().map_err(Into::<Error>::into))
.collect();
format!("[\"REQ\",\"{}\",{}]", sub_id, filters_json_str?.join(","))
}
json.to_string()
}
Self::Close { sub_id } => json!(["CLOSE", sub_id]).to_string(),
}
})
}
}