Skip to content

Commit

Permalink
feat: NIP-50 search capability (#36)
Browse files Browse the repository at this point in the history
Co-authored-by: Jeffrey Sun <telljsun@gmail.com>
  • Loading branch information
jeffbsun and Jeffrey Sun committed Oct 16, 2023
1 parent 85d6257 commit c34718c
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 5 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ flutter pub add nostr
- [x] [NIP 19 bech32-encoded entities](https://github.com/nostr-protocol/nips/blob/master/19.md)
- [x] [NIP 20 Command Results](https://github.com/nostr-protocol/nips/blob/master/20.md)
- [x] [NIP 28 Public Chat](https://github.com/nostr-protocol/nips/blob/master/28.md)
- [x] [NIP 50 Search Capability](https://github.com/nostr-protocol/nips/blob/master/50.md)
- [x] [NIP 51 Lists](https://github.com/nostr-protocol/nips/blob/master/51.md)


Expand Down
8 changes: 7 additions & 1 deletion lib/src/filter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ class Filter {
/// maximum number of events to be returned in the initial query
int? limit;

/// nip-50 search term
String? search;

/// Default constructor
Filter(
{this.ids,
Expand All @@ -37,7 +40,8 @@ class Filter {
this.p,
this.since,
this.until,
this.limit});
this.limit,
this.search});

/// Deserialize a filter from a JSON
Filter.fromJson(Map<String, dynamic> json) {
Expand All @@ -51,6 +55,7 @@ class Filter {
since = json['since'];
until = json['until'];
limit = json['limit'];
search = json['search'];
}

/// Serialize a filter in JSON
Expand All @@ -65,6 +70,7 @@ class Filter {
if (since != null) data['since'] = since;
if (until != null) data['until'] = until;
if (limit != null) data['limit'] = limit;
if (search != null) data['search'] = search;
return data;
}
}
7 changes: 6 additions & 1 deletion test/filter_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ void main() {
int since = 1672477960;
int until = 1674063680;
int limit = 450;
String search = "term";

Filter filter = Filter(
ids: ids,
Expand All @@ -28,6 +29,7 @@ void main() {
since: since,
until: until,
limit: limit,
search: search,
);

expect(filter.ids, ids);
Expand All @@ -39,6 +41,7 @@ void main() {
expect(filter.since, since);
expect(filter.until, until);
expect(filter.limit, limit);
expect(filter.search, search);
});

test('Constructor.fromJson', () {
Expand All @@ -55,7 +58,8 @@ void main() {
"#p": [],
"since": 1672477960,
"until": 1674063680,
"limit": 450
"limit": 450,
"search": "test",
};

Filter filter = Filter.fromJson(json);
Expand All @@ -68,6 +72,7 @@ void main() {
expect(filter.since, json['since']);
expect(filter.until, json['until']);
expect(filter.limit, json['limit']);
expect(filter.search, json['search']);
});
});
}
11 changes: 8 additions & 3 deletions test/request_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ void main() {
since: 1672477960,
until: 1674063680,
limit: 450,
search: "term",
);

Request req = Request("733209259899167", [myFilter]);
Expand All @@ -34,11 +35,12 @@ void main() {
expect(req.filters[0].since, myFilter.since);
expect(req.filters[0].until, myFilter.until);
expect(req.filters[0].limit, myFilter.limit);
expect(req.filters[0].search, myFilter.search);
});

test('Request.serialize', () {
String serialized =
'["REQ","733209259899167",{"ids":["047663d895d56aefa3f528935c7ce7dc8939eb721a0ec76ef2e558a8257955d2"],"authors":["0ba0206887bd61579bf65ec09d7806bea32c64be1cf2c978cf031a811cd238db"],"kinds":[0,1,2,7],"#e":[],"#a":[],"#p":[],"since":1672477960,"until":1674063680,"limit":450},{"kinds":[0,1,2,7],"since":1673980547,"limit":450}]';
'["REQ","733209259899167",{"ids":["047663d895d56aefa3f528935c7ce7dc8939eb721a0ec76ef2e558a8257955d2"],"authors":["0ba0206887bd61579bf65ec09d7806bea32c64be1cf2c978cf031a811cd238db"],"kinds":[0,1,2,7],"#e":[],"#a":[],"#p":[],"since":1672477960,"until":1674063680,"limit":450,"search":"term"},{"kinds":[0,1,2,7],"since":1673980547,"limit":450}]';
var json = [
"REQ",
"733209259899167",
Expand All @@ -55,7 +57,8 @@ void main() {
"#p": [],
"since": 1672477960,
"until": 1674063680,
"limit": 450
"limit": 450,
"search": "term",
},
{
"kinds": [0, 1, 2, 7],
Expand Down Expand Up @@ -84,7 +87,8 @@ void main() {
"#p": [],
"since": 1672477960,
"until": 1674063680,
"limit": 450
"limit": 450,
"search": "term",
},
{
"kinds": [0, 1, 2, 7],
Expand All @@ -105,6 +109,7 @@ void main() {
expect(req.filters[0].since, 1672477960);
expect(req.filters[0].until, 1674063680);
expect(req.filters[0].limit, 450);
expect(req.filters[0].search, "term");
expect(req.filters[1].kinds, [0, 1, 2, 7]);
expect(req.filters[1].since, 1673980547);
expect(req.filters[1].limit, 450);
Expand Down

0 comments on commit c34718c

Please sign in to comment.