mirror of
https://github.com/scsibug/nostr-rs-relay.git
synced 2024-11-14 23:19:07 -05:00
Start work on filtering events with subscriptions
This commit is contained in:
parent
03bc1fab18
commit
296857d372
29
src/event.rs
29
src/event.rs
|
@ -5,17 +5,14 @@ use serde::{Deserialize, Deserializer, Serialize};
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize, PartialEq, Debug, Clone)]
|
#[derive(Serialize, Deserialize, PartialEq, Debug, Clone)]
|
||||||
pub struct Event {
|
pub struct Event {
|
||||||
#[serde(deserialize_with = "u32_from_string")]
|
pub(crate) id: String,
|
||||||
id: u32,
|
pub(crate) pubkey: String,
|
||||||
#[serde(deserialize_with = "u32_from_string")]
|
pub(crate) created_at: u64,
|
||||||
pubkey: u32,
|
pub(crate) kind: u8,
|
||||||
created_at: u64,
|
|
||||||
kind: u8,
|
|
||||||
#[serde(deserialize_with = "tag_from_string")]
|
#[serde(deserialize_with = "tag_from_string")]
|
||||||
tags: Vec<Vec<String>>,
|
pub(crate) tags: Vec<Vec<String>>,
|
||||||
content: String,
|
pub(crate) content: String,
|
||||||
#[serde(deserialize_with = "u64_from_string")]
|
pub(crate) sig: String,
|
||||||
sig: u64,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type Tag = Vec<Vec<String>>;
|
type Tag = Vec<Vec<String>>;
|
||||||
|
@ -66,13 +63,13 @@ mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
fn simple_event() -> Event {
|
fn simple_event() -> Event {
|
||||||
super::Event {
|
super::Event {
|
||||||
id: 0,
|
id: "0".to_owned(),
|
||||||
pubkey: 0,
|
pubkey: "0".to_owned(),
|
||||||
created_at: 0,
|
created_at: 0,
|
||||||
kind: 0,
|
kind: 0,
|
||||||
tags: vec![],
|
tags: vec![],
|
||||||
content: "".to_owned(),
|
content: "".to_owned(),
|
||||||
sig: 0,
|
sig: "0".to_owned(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -80,7 +77,7 @@ mod tests {
|
||||||
fn event_creation() {
|
fn event_creation() {
|
||||||
// create an event
|
// create an event
|
||||||
let event = simple_event();
|
let event = simple_event();
|
||||||
assert_eq!(event.id, 0);
|
assert_eq!(event.id, "0");
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
@ -88,7 +85,7 @@ mod tests {
|
||||||
// serialize an event to JSON string
|
// serialize an event to JSON string
|
||||||
let event = simple_event();
|
let event = simple_event();
|
||||||
let j = serde_json::to_string(&event)?;
|
let j = serde_json::to_string(&event)?;
|
||||||
assert_eq!(j, "{\"id\":0,\"pubkey\":0,\"created_at\":0,\"kind\":0,\"tags\":[],\"content\":\"\",\"sig\":0}");
|
assert_eq!(j, "{\"id\":\"0\",\"pubkey\":\"0\",\"created_at\":0,\"kind\":0,\"tags\":[],\"content\":\"\",\"sig\":\"0\"}");
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -109,7 +106,7 @@ mod tests {
|
||||||
],
|
],
|
||||||
];
|
];
|
||||||
let j = serde_json::to_string(&event)?;
|
let j = serde_json::to_string(&event)?;
|
||||||
assert_eq!(j, "{\"id\":0,\"pubkey\":0,\"created_at\":0,\"kind\":0,\"tags\":[[\"e\",\"xxxx\",\"wss://example.com\"],[\"p\",\"yyyyy\",\"wss://example.com:3033\"]],\"content\":\"\",\"sig\":0}");
|
assert_eq!(j, "{\"id\":\"0\",\"pubkey\":\"0\",\"created_at\":0,\"kind\":0,\"tags\":[[\"e\",\"xxxx\",\"wss://example.com\"],[\"p\",\"yyyyy\",\"wss://example.com:3033\"]],\"content\":\"\",\"sig\":\"0\"}");
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -97,17 +97,29 @@ impl Subscription {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ReqFilter {
|
impl ReqFilter {
|
||||||
pub fn interested_in_event(&self, event: &Event) -> bool {
|
pub fn interested_in_event(&self, event: &Event) -> bool {
|
||||||
// determine if all populated fields in this filter match the provided event.
|
// determine if all populated fields in this filter match the provided event.
|
||||||
|
// a filter matches an event if all the populated fields match.
|
||||||
todo!();
|
// Iterate through each filter field, return false if the field exists and doesn't match the event.
|
||||||
|
if !self.id.as_ref().map(|v| v == &event.id).unwrap_or(true) {
|
||||||
|
false
|
||||||
|
} else if !self
|
||||||
|
.author
|
||||||
|
.as_ref()
|
||||||
|
.map(|v| v == &event.pubkey)
|
||||||
|
.unwrap_or(true)
|
||||||
|
{
|
||||||
|
false
|
||||||
|
} else {
|
||||||
true
|
true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
|
|
Loading…
Reference in New Issue
Block a user