Skip to content

Commit 67ce3a2

Browse files
committed
nostr: add NIP-50 support to Filter::match_event method
Signed-off-by: Yuki Kishimoto <[email protected]>
1 parent adf3de1 commit 67ce3a2

File tree

4 files changed

+19
-6
lines changed

4 files changed

+19
-6
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
* nostr: ensure that NIP-59 rumor has `EventId` ([Yuki Kishimoto])
3434
* nostr: update `PartialEvent` methods ([Yuki Kishimoto])
3535
* nostr: change `EventBuilder::award_badge` fingerprint ([Yuki Kishimoto])
36+
* nostr: add NIP-50 support to `Filter::match_event` method ([Yuki Kishimoto])
3637
* pool: take mutex ownership instead of clone in `InternalRelayPool::get_events_from` ([Yuki Kishimoto])
3738
* pool: remove IDs collection from `InternalRelayPool::get_events_from` ([Yuki Kishimoto])
3839
* pool: better checks before perform queries or send messages to relays ([Yuki Kishimoto])

bindings/nostr-ffi/src/types/filter.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -406,8 +406,7 @@ impl Filter {
406406
}
407407

408408
/// Determine if `Filter` match given `Event`.
409-
///
410-
/// The `search` filed is not supported yet!
409+
#[inline]
411410
pub fn match_event(&self, event: &Event) -> bool {
412411
self.inner.match_event(event.deref())
413412
}

bindings/nostr-js/src/types/filter.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -449,8 +449,7 @@ impl JsFilter {
449449
}
450450

451451
/// Determine if `Filter` match given `Event`.
452-
///
453-
/// The `search` filed is not supported yet!
452+
#[inline]
454453
#[wasm_bindgen(js_name = matchEvent)]
455454
pub fn match_event(&self, event: &JsEvent) -> bool {
456455
self.inner.match_event(event.deref())

crates/nostr/src/types/filter.rs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -745,9 +745,18 @@ impl Filter {
745745
})
746746
}
747747

748+
#[inline]
749+
fn search_match(&self, event: &Event) -> bool {
750+
match &self.search {
751+
Some(query) => event
752+
.content
753+
.split_whitespace()
754+
.any(|word| word.eq_ignore_ascii_case(query)),
755+
None => true,
756+
}
757+
}
758+
748759
/// Determine if [Filter] match given [Event].
749-
///
750-
/// The `search` field is not supported yet!
751760
#[inline]
752761
pub fn match_event(&self, event: &Event) -> bool {
753762
self.ids_match(event)
@@ -756,6 +765,7 @@ impl Filter {
756765
&& self.since.map_or(true, |t| event.created_at >= t)
757766
&& self.until.map_or(true, |t| event.created_at <= t)
758767
&& self.tag_match(event)
768+
&& self.search_match(event)
759769
}
760770
}
761771

@@ -1109,6 +1119,10 @@ mod tests {
11091119
let filter: Filter = Filter::new().hashtag("this-should-not-match");
11101120
assert!(!filter.match_event(&event));
11111121
assert!(!filter.match_event(&event_with_empty_tags));
1122+
1123+
// Test match search
1124+
let filter: Filter = Filter::new().search("test").into();
1125+
assert!(filter.match_event(&event));
11121126
}
11131127
}
11141128

0 commit comments

Comments
 (0)