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)]
|
||||
pub struct Event {
|
||||
#[serde(deserialize_with = "u32_from_string")]
|
||||
id: u32,
|
||||
#[serde(deserialize_with = "u32_from_string")]
|
||||
pubkey: u32,
|
||||
created_at: u64,
|
||||
kind: u8,
|
||||
pub(crate) id: String,
|
||||
pub(crate) pubkey: String,
|
||||
pub(crate) created_at: u64,
|
||||
pub(crate) kind: u8,
|
||||
#[serde(deserialize_with = "tag_from_string")]
|
||||
tags: Vec<Vec<String>>,
|
||||
content: String,
|
||||
#[serde(deserialize_with = "u64_from_string")]
|
||||
sig: u64,
|
||||
pub(crate) tags: Vec<Vec<String>>,
|
||||
pub(crate) content: String,
|
||||
pub(crate) sig: String,
|
||||
}
|
||||
|
||||
type Tag = Vec<Vec<String>>;
|
||||
|
@ -66,13 +63,13 @@ mod tests {
|
|||
use super::*;
|
||||
fn simple_event() -> Event {
|
||||
super::Event {
|
||||
id: 0,
|
||||
pubkey: 0,
|
||||
id: "0".to_owned(),
|
||||
pubkey: "0".to_owned(),
|
||||
created_at: 0,
|
||||
kind: 0,
|
||||
tags: vec![],
|
||||
content: "".to_owned(),
|
||||
sig: 0,
|
||||
sig: "0".to_owned(),
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -80,7 +77,7 @@ mod tests {
|
|||
fn event_creation() {
|
||||
// create an event
|
||||
let event = simple_event();
|
||||
assert_eq!(event.id, 0);
|
||||
assert_eq!(event.id, "0");
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
@ -88,7 +85,7 @@ mod tests {
|
|||
// serialize an event to JSON string
|
||||
let event = simple_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(())
|
||||
}
|
||||
|
||||
|
@ -109,7 +106,7 @@ mod tests {
|
|||
],
|
||||
];
|
||||
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(())
|
||||
}
|
||||
|
||||
|
|
|
@ -97,16 +97,28 @@ impl Subscription {
|
|||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
impl ReqFilter {
|
||||
pub fn interested_in_event(&self, event: &Event) -> bool {
|
||||
// determine if all populated fields in this filter match the provided event.
|
||||
|
||||
todo!();
|
||||
// a filter matches an event if all the populated fields match.
|
||||
// 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
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
|
|
Loading…
Reference in New Issue
Block a user