From 395f5e6c643b4ce358eabc8365a470542f7def31 Mon Sep 17 00:00:00 2001 From: Salvatore Giordano Date: Tue, 7 Sep 2021 10:02:05 +0200 Subject: [PATCH 1/6] feat(llc): add contains and empty filters --- packages/stream_chat/lib/src/core/models/filter.dart | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/packages/stream_chat/lib/src/core/models/filter.dart b/packages/stream_chat/lib/src/core/models/filter.dart index eb3932ff..6551d403 100644 --- a/packages/stream_chat/lib/src/core/models/filter.dart +++ b/packages/stream_chat/lib/src/core/models/filter.dart @@ -51,6 +51,9 @@ enum FilterOperator { /// Matches none of the values specified in an array. nor, + + /// Matches any list that contains the specified values + contains, } /// Helper extension for [FilterOperator] @@ -71,6 +74,7 @@ extension FilterOperatorX on FilterOperator { FilterOperator.and: '\$and', FilterOperator.or: '\$or', FilterOperator.nor: '\$nor', + FilterOperator.contains: '\$contains', }[this]!; } @@ -157,6 +161,10 @@ class Filter extends Equatable { factory Filter.exists(String key, {bool exists = true}) => Filter._(operator: FilterOperator.exists, key: key, value: exists); + /// Matches any list that contains the specified values + factory Filter.contains(String key, List values) => + Filter._(operator: FilterOperator.contains, key: key, value: values); + /// Creates a custom [Filter] if there isn't one already available. const factory Filter.custom({ required Object value, @@ -164,6 +172,9 @@ class Filter extends Equatable { String? key, }) = Filter.__; + /// An empty filter + factory Filter.empty() => const Filter.raw(value: {}); + /// Creates a custom [Filter] from a raw map value /// /// ```dart From d18286dc8df052379f863bac85f47ea32f46c9a7 Mon Sep 17 00:00:00 2001 From: Salvatore Giordano Date: Tue, 7 Sep 2021 10:10:10 +0200 Subject: [PATCH 2/6] feat(llc): add tests --- .../test/src/core/models/filter_test.dart | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/packages/stream_chat/test/src/core/models/filter_test.dart b/packages/stream_chat/test/src/core/models/filter_test.dart index d60ebb50..6a70f313 100644 --- a/packages/stream_chat/test/src/core/models/filter_test.dart +++ b/packages/stream_chat/test/src/core/models/filter_test.dart @@ -138,6 +138,20 @@ void main() { expect(filter.value, value); }); + test('empty', () { + final filter = Filter.empty(); + expect(filter.value, {}); + }); + + test('contains', () { + const key = 'testKey'; + const values = ['testValue']; + final filter = Filter.contains(key, values); + expect(filter.key, key); + expect(filter.value, values); + expect(filter.operator, FilterOperator.contains.rawValue); + }); + group('groupedOperator', () { final filter1 = Filter.equal('testKey', 'testValue'); final filter2 = Filter.in_('testKey', const ['testValue']); From fe73cf0b71e0f5474746f8aadf568f0684067932 Mon Sep 17 00:00:00 2001 From: Salvatore Giordano Date: Tue, 7 Sep 2021 10:36:53 +0200 Subject: [PATCH 3/6] fix(llc): fix contains filter params --- packages/stream_chat/lib/src/core/models/filter.dart | 6 +++--- packages/stream_chat/test/src/core/models/filter_test.dart | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/packages/stream_chat/lib/src/core/models/filter.dart b/packages/stream_chat/lib/src/core/models/filter.dart index 6551d403..d4536eab 100644 --- a/packages/stream_chat/lib/src/core/models/filter.dart +++ b/packages/stream_chat/lib/src/core/models/filter.dart @@ -52,7 +52,7 @@ enum FilterOperator { /// Matches none of the values specified in an array. nor, - /// Matches any list that contains the specified values + /// Matches any list that contains the specified value contains, } @@ -162,8 +162,8 @@ class Filter extends Equatable { Filter._(operator: FilterOperator.exists, key: key, value: exists); /// Matches any list that contains the specified values - factory Filter.contains(String key, List values) => - Filter._(operator: FilterOperator.contains, key: key, value: values); + factory Filter.contains(String key, Object value) => + Filter._(operator: FilterOperator.contains, key: key, value: value); /// Creates a custom [Filter] if there isn't one already available. const factory Filter.custom({ diff --git a/packages/stream_chat/test/src/core/models/filter_test.dart b/packages/stream_chat/test/src/core/models/filter_test.dart index 6a70f313..6e3dbf64 100644 --- a/packages/stream_chat/test/src/core/models/filter_test.dart +++ b/packages/stream_chat/test/src/core/models/filter_test.dart @@ -145,7 +145,7 @@ void main() { test('contains', () { const key = 'testKey'; - const values = ['testValue']; + const values = 'testValue'; final filter = Filter.contains(key, values); expect(filter.key, key); expect(filter.value, values); From 3e1d5f7f929fb33701919f6ef8de62182371b355 Mon Sep 17 00:00:00 2001 From: Salvatore Giordano Date: Tue, 7 Sep 2021 10:37:13 +0200 Subject: [PATCH 4/6] docs(llc): add documentation for new filters --- .../Flutter/guides/understanding_filters.mdx | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/docusaurus/docs/Flutter/guides/understanding_filters.mdx b/docusaurus/docs/Flutter/guides/understanding_filters.mdx index 9d169a80..72b5eca3 100644 --- a/docusaurus/docs/Flutter/guides/understanding_filters.mdx +++ b/docusaurus/docs/Flutter/guides/understanding_filters.mdx @@ -107,6 +107,36 @@ The 'exists' filter matches values that exist, or don't exist, based on the spec Filter.exists('name', true) ``` +#### Filter.contains + +The 'contains' filter matches any list that contains the specified value. + +```dart +Filter.contains('teams', 'red') +``` + +#### Filter.empty + +The 'empty' filter constructor returns an empty filter. It's the equivalent of an empty map `{}`; + +```dart +Filter.empty(); +``` + +#### Filter.raw + +The 'raw' filter constructor lets you specify a raw filter. We suggest using this only if you can't manage to build what you want using the other constructors. + +```dart +Filter.raw(value: { + 'members': [ + ..._selectedUsers.map((e) => e.id), + chatState.currentUser!.id, + ], + 'distinct': true, +}); +``` + ### Group Queries #### Filter.and From 6f75af2658fbfacc21dbf1c4795e89bce66b7988 Mon Sep 17 00:00:00 2001 From: Salvatore Giordano Date: Tue, 7 Sep 2021 10:38:14 +0200 Subject: [PATCH 5/6] chore(llc): update changelog --- packages/stream_chat/CHANGELOG.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/packages/stream_chat/CHANGELOG.md b/packages/stream_chat/CHANGELOG.md index 67a80e17..ca4eec3f 100644 --- a/packages/stream_chat/CHANGELOG.md +++ b/packages/stream_chat/CHANGELOG.md @@ -1,3 +1,9 @@ +## Upcoming + +✅ Added + +- Added `Filter.contains` and `Filter.empty` + ## 2.2.1 🐞 Fixed From 9dc04acf8295e20dd92d33c1a9ca769a4ef6fc08 Mon Sep 17 00:00:00 2001 From: Salvatore Giordano Date: Tue, 7 Sep 2021 15:43:04 +0200 Subject: [PATCH 6/6] docs(llc): add documentation for filter.custom --- .../docs/Flutter/guides/understanding_filters.mdx | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/docusaurus/docs/Flutter/guides/understanding_filters.mdx b/docusaurus/docs/Flutter/guides/understanding_filters.mdx index 72b5eca3..e9d72ecf 100644 --- a/docusaurus/docs/Flutter/guides/understanding_filters.mdx +++ b/docusaurus/docs/Flutter/guides/understanding_filters.mdx @@ -137,6 +137,18 @@ Filter.raw(value: { }); ``` +#### Filter.custom + +The 'custom' filter is used to create a custom filter in case it does not exists or it's not been added to the SDK yet. +Note that the filter must be supported by the Stream backend in order to work. + +```dart +Filter.custom( + operator: '\$max', + value: 10, +) +``` + ### Group Queries #### Filter.and