nostr-rs-relay

My dev fork of nostr-rs-relay
git clone git://jb55.com/nostr-rs-relay
Log | Files | Refs | README | LICENSE

commit be8170342edb6af1cbd3974a22f65d53e5c306e0
parent 0a3b15f41f7c65122d998e1cb17583a98c554d94
Author: Greg Heartsfield <scsibug@imap.cc>
Date:   Thu, 11 Aug 2022 22:16:10 -0700

fix(NIP-12): multi-tag searches returns correct results

Logic of generated SQL was incorrect, causing multiple tag searches
(as defined in NIP-12) to produce no results.

fixes: https://todo.sr.ht/~gheartsfield/nostr-rs-relay/36

Diffstat:
Msrc/db.rs | 7++++---
1 file changed, 4 insertions(+), 3 deletions(-)

diff --git a/src/db.rs b/src/db.rs @@ -388,7 +388,7 @@ fn query_from_filter(f: &ReqFilter) -> (String, Vec<Box<dyn ToSql>>) { // if the filter is malformed, don't return anything. if f.force_no_match { let empty_query = - "SELECT DISTINCT(e.content), e.created_at FROM event e LEFT JOIN tag t ON e.id=t.event_id WHERE 1=0" + "SELECT DISTINCT(e.content), e.created_at FROM event e WHERE 1=0" .to_owned(); // query parameters for SQLite let empty_params: Vec<Box<dyn ToSql>> = vec![]; @@ -396,7 +396,7 @@ fn query_from_filter(f: &ReqFilter) -> (String, Vec<Box<dyn ToSql>>) { } let mut query = - "SELECT DISTINCT(e.content), e.created_at FROM event e LEFT JOIN tag t ON e.id=t.event_id " + "SELECT DISTINCT(e.content), e.created_at FROM event e " .to_owned(); // query parameters for SQLite let mut params: Vec<Box<dyn ToSql>> = vec![]; @@ -481,7 +481,8 @@ fn query_from_filter(f: &ReqFilter) -> (String, Vec<Box<dyn ToSql>>) { // create clauses with "?" params for each tag value being searched let str_clause = format!("value IN ({})", repeat_vars(str_vals.len())); let blob_clause = format!("value_hex IN ({})", repeat_vars(blob_vals.len())); - let tag_clause = format!("(name=? AND ({} OR {}))", str_clause, blob_clause); + // find evidence of the target tag name/value existing for this event. + let tag_clause = format!("e.id IN (SELECT e.id FROM event e LEFT JOIN tag t on e.id=t.event_id WHERE hidden!=TRUE and (name=? AND ({} OR {})))", str_clause, blob_clause); // add the tag name as the first parameter params.push(Box::new(key.to_string())); // add all tag values that are plain strings as params