Skip to content

Commit

Permalink
sdk: fix filters empty error when gossip option is enabled
Browse files Browse the repository at this point in the history
Fixes rust-nostr#594

Signed-off-by: Yuki Kishimoto <[email protected]>
  • Loading branch information
yukibtc committed Oct 28, 2024
1 parent 7f10a32 commit 9a7f5ab
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 15 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@
* lmdb: add missing commit method call in `Store::delete` ([Yuki Kishimoto])
* lmdb: fix unit tests ([Yuki Kishimoto])
* lmdb: fix `Store::save_event` issues ([Yuki Kishimoto])
* sdk: fix `filters empty` error when gossip option is enabled ([Yuki Kishimoto])

### Removed

Expand Down
20 changes: 11 additions & 9 deletions crates/nostr-sdk/src/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1855,15 +1855,17 @@ impl Client {
.await;

// Extend filters with read relays and "other" filters (the filters that aren't linked to public keys)
for url in read_relays.into_keys() {
broken_down
.filters
.entry(url)
.and_modify(|f| {
f.extend(broken_down.other.clone());
})
.or_default()
.extend(broken_down.other.clone())
if let Some(other) = broken_down.other {
for url in read_relays.into_keys() {
broken_down
.filters
.entry(url)
.and_modify(|f| {
f.extend(other.clone());
})
.or_default()
.extend(other.clone())
}
}

// Add outbox relays
Expand Down
12 changes: 6 additions & 6 deletions crates/nostr-sdk/src/gossip/graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ pub struct BrokenDownFilters {
/// Filters by url
pub filters: HashMap<Url, Vec<Filter>>,
/// Filters that can be sent to read relays (generic query, not related to public keys)
pub other: Vec<Filter>,
pub other: Option<Vec<Filter>>,
pub outbox_urls: HashSet<Url>,
pub inbox_urls: HashSet<Url>,
}
Expand Down Expand Up @@ -313,7 +313,7 @@ impl GossipGraph {
.into_iter()
.map(|(u, f)| (u, f.into_iter().collect::<Vec<_>>()))
.collect(),
other,
other: if other.is_empty() { None } else { Some(other) },
outbox_urls,
inbox_urls,
}
Expand Down Expand Up @@ -387,7 +387,7 @@ mod tests {
assert_eq!(broken_down.filters.get(&nostr_bg_url).unwrap(), &filters);
assert_eq!(broken_down.filters.get(&nos_lol_url).unwrap(), &filters);
assert!(!broken_down.filters.contains_key(&nostr_mom_url));
assert!(broken_down.other.is_empty());
assert!(broken_down.other.is_none());

// Multiple filters, multiple authors
let authors_filter = Filter::new().authors([keys_a.public_key, keys_b.public_key]);
Expand Down Expand Up @@ -417,7 +417,7 @@ mod tests {
&vec![Filter::new().author(keys_b.public_key)]
);
assert!(!broken_down.filters.contains_key(&snort_url));
assert_eq!(broken_down.other, vec![search_filter]);
assert_eq!(broken_down.other, Some(vec![search_filter]));

// Multiple filters, multiple authors and single p tags
let authors_filter = Filter::new().authors([keys_a.public_key, keys_b.public_key]);
Expand Down Expand Up @@ -458,7 +458,7 @@ mod tests {
&vec![Filter::new().author(keys_b.public_key)]
);
assert!(!broken_down.filters.contains_key(&snort_url));
assert_eq!(broken_down.other, vec![search_filter]);
assert_eq!(broken_down.other, Some(vec![search_filter]));

// Single filter, both author and p tag
let filters = vec![Filter::new()
Expand All @@ -473,6 +473,6 @@ mod tests {
assert_eq!(broken_down.filters.get(&nostr_info_url).unwrap(), &filters);
assert_eq!(broken_down.filters.get(&relay_rip_url).unwrap(), &filters);
assert_eq!(broken_down.filters.get(&snort_url).unwrap(), &filters);
assert!(broken_down.other.is_empty());
assert!(broken_down.other.is_none());
}
}

0 comments on commit 9a7f5ab

Please sign in to comment.