diff --git a/docusaurus/docs/Flutter/guides/understanding_filters.mdx b/docusaurus/docs/Flutter/guides/understanding_filters.mdx index 9d169a80..e9d72ecf 100644 --- a/docusaurus/docs/Flutter/guides/understanding_filters.mdx +++ b/docusaurus/docs/Flutter/guides/understanding_filters.mdx @@ -107,6 +107,48 @@ 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, +}); +``` + +#### 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 diff --git a/packages/stream_chat/CHANGELOG.md b/packages/stream_chat/CHANGELOG.md index 85bf5956..61f00005 100644 --- a/packages/stream_chat/CHANGELOG.md +++ b/packages/stream_chat/CHANGELOG.md @@ -10,6 +10,10 @@ - `updatePinnedMessageReactions` - `deletePinnedMessageReactionsByMessageId` +✅ Added + +- Added `Filter.contains` and `Filter.empty` + ## 2.2.1 🐞 Fixed diff --git a/packages/stream_chat/lib/src/core/models/filter.dart b/packages/stream_chat/lib/src/core/models/filter.dart index eb3932ff..d4536eab 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 value + 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, 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({ 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 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..6e3dbf64 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']);