Querying for author/authors

This commit is contained in:
Greg Heartsfield 2021-11-24 22:37:09 -06:00
parent 580342f3a5
commit c758495de9

View File

@ -102,11 +102,8 @@ impl Subscription {
}
impl ReqFilter {
// check if the given author matches the query (against author and authors)
fn author_match(&self, author: &str) -> bool {
if self.author.as_ref().map(|v| v == author).unwrap_or(true) {
return true;
} else if self
fn authors_match(&self, author: &str) -> bool {
if self
.authors
.as_ref()
.map(|vs| vs.contains(&author.to_owned()))
@ -136,7 +133,7 @@ impl ReqFilter {
.unwrap_or(true)
{
false
} else if !self.author_match(&event.pubkey) {
} else if !self.authors_match(&event.pubkey) {
false
} else if !self
.event
@ -272,4 +269,71 @@ mod tests {
assert_eq!(s.interested_in_event(&e), true);
Ok(())
}
#[test]
fn author_single() -> Result<()> {
// subscription with a filter for ID
let s: Subscription = serde_json::from_str(r#"["REQ","xyz",{"author":"abc"}]"#)?;
let e = Event {
id: "123".to_owned(),
pubkey: "abc".to_owned(),
created_at: 0,
kind: 0,
tags: Vec::new(),
content: "".to_owned(),
sig: "".to_owned(),
};
assert_eq!(s.interested_in_event(&e), true);
Ok(())
}
#[test]
fn authors_single() -> Result<()> {
// subscription with a filter for ID
let s: Subscription = serde_json::from_str(r#"["REQ","xyz",{"authors":["abc"]}]"#)?;
let e = Event {
id: "123".to_owned(),
pubkey: "abc".to_owned(),
created_at: 0,
kind: 0,
tags: Vec::new(),
content: "".to_owned(),
sig: "".to_owned(),
};
assert_eq!(s.interested_in_event(&e), true);
Ok(())
}
#[test]
fn authors_multi_pubkey() -> Result<()> {
// check for any of a set of authors, against the pubkey
let s: Subscription = serde_json::from_str(r#"["REQ","xyz",{"authors":["abc", "bcd"]}]"#)?;
let e = Event {
id: "123".to_owned(),
pubkey: "bcd".to_owned(),
created_at: 0,
kind: 0,
tags: Vec::new(),
content: "".to_owned(),
sig: "".to_owned(),
};
assert_eq!(s.interested_in_event(&e), true);
Ok(())
}
#[test]
fn authors_multi_no_match() -> Result<()> {
// check for any of a set of authors, against the pubkey
let s: Subscription = serde_json::from_str(r#"["REQ","xyz",{"authors":["abc", "bcd"]}]"#)?;
let e = Event {
id: "123".to_owned(),
pubkey: "xyz".to_owned(),
created_at: 0,
kind: 0,
tags: Vec::new(),
content: "".to_owned(),
sig: "".to_owned(),
};
assert_eq!(s.interested_in_event(&e), false);
Ok(())
}
}