Skip to content

Commit 26be463

Browse files
author
Franco Bugnano
committed
Implemented lastMessageTs feedFilter
1 parent 74bac2a commit 26be463

File tree

2 files changed

+94
-3
lines changed

2 files changed

+94
-3
lines changed

lib/src/predicate.dart

Lines changed: 84 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,75 @@ class CustomFieldPredicate extends FieldPredicate<String> {
128128
);
129129
}
130130

131+
class NumberPredicate {
132+
final String _operand;
133+
double? _value;
134+
List<double>? _values;
135+
136+
NumberPredicate.greaterThan(double value) : _operand = '>', _value = value;
137+
NumberPredicate.lessThan(double value) : _operand = '<', _value = value;
138+
NumberPredicate.greaterOrEquals(double value) : _operand = '>=', _value = value;
139+
NumberPredicate.lessOrEquals(double value) : _operand = '<=', _value = value;
140+
NumberPredicate.between(List<double> values) : _operand = 'between', _values = List<double>.of(values);
141+
NumberPredicate.notBetween(List<double> values) : _operand = '!between', _values = List<double>.of(values);
142+
143+
NumberPredicate.of(NumberPredicate other)
144+
: _operand = other._operand,
145+
_value = other._value,
146+
_values = (other._values != null ? List<double>.of(other._values!) : null);
147+
148+
@override
149+
String toString() {
150+
return json.encode(this);
151+
}
152+
153+
dynamic toJson() {
154+
final result = <dynamic>[];
155+
156+
result.add(_operand);
157+
158+
if (_value != null) {
159+
result.add(_value);
160+
}
161+
162+
if (_values != null) {
163+
result.add(_values);
164+
}
165+
166+
return result;
167+
}
168+
169+
bool operator ==(Object other) {
170+
if (identical(this, other)) {
171+
return true;
172+
}
173+
174+
if (!(other is NumberPredicate)) {
175+
return false;
176+
}
177+
178+
if (_operand != other._operand) {
179+
return false;
180+
}
181+
182+
if (_value != other._value) {
183+
return false;
184+
}
185+
186+
if (!listEquals(_values, other._values)) {
187+
return false;
188+
}
189+
190+
return true;
191+
}
192+
193+
int get hashCode => Object.hash(
194+
_operand,
195+
_value,
196+
(_values != null ? Object.hashAll(_values!) : _values),
197+
);
198+
}
199+
131200
class ConversationAccessLevel {
132201
final String _value;
133202

@@ -151,12 +220,16 @@ class ConversationPredicate {
151220
/// Set this field to only select conversations that have, or don't have any, unread messages.
152221
final bool? hasUnreadMessages;
153222

154-
const ConversationPredicate({this.access, this.custom, this.hasUnreadMessages});
223+
/// Only select conversations that have the last message sent in a particular time interval.
224+
final NumberPredicate? lastMessageTs;
225+
226+
const ConversationPredicate({this.access, this.custom, this.hasUnreadMessages, this.lastMessageTs});
155227

156228
ConversationPredicate.of(ConversationPredicate other)
157229
: access = (other.access != null ? FieldPredicate<ConversationAccessLevel>.of(other.access!) : null),
158230
custom = (other.custom != null ? Map<String, CustomFieldPredicate>.of(other.custom!) : null),
159-
hasUnreadMessages = other.hasUnreadMessages;
231+
hasUnreadMessages = other.hasUnreadMessages,
232+
lastMessageTs = (other.lastMessageTs != null ? NumberPredicate.of(other.lastMessageTs!) : null);
160233

161234
@override
162235
String toString() {
@@ -178,6 +251,10 @@ class ConversationPredicate {
178251
result['hasUnreadMessages'] = hasUnreadMessages;
179252
}
180253

254+
if (lastMessageTs != null) {
255+
result['lastMessageTs'] = lastMessageTs;
256+
}
257+
181258
return result;
182259
}
183260

@@ -202,6 +279,10 @@ class ConversationPredicate {
202279
return false;
203280
}
204281

282+
if (lastMessageTs != other.lastMessageTs) {
283+
return false;
284+
}
285+
205286
return true;
206287
}
207288

@@ -210,6 +291,7 @@ class ConversationPredicate {
210291
(custom != null ? Object.hashAll(custom!.keys) : custom),
211292
(custom != null ? Object.hashAll(custom!.values) : custom),
212293
hasUnreadMessages,
294+
lastMessageTs,
213295
);
214296
}
215297

test/talkjs_test.dart

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ void main() {
3131
'visibility': CustomFieldPredicate.equals('visible'),
3232
},
3333
hasUnreadMessages: false,
34+
lastMessageTs: NumberPredicate.greaterThan(1679298371586),
3435
) == ConversationPredicate(
3536
access: FieldPredicate.notEquals(ConversationAccessLevel.none),
3637
custom: {
@@ -39,6 +40,7 @@ void main() {
3940
'visibility': CustomFieldPredicate.equals('visible'),
4041
},
4142
hasUnreadMessages: false,
43+
lastMessageTs: NumberPredicate.greaterThan(1679298371586),
4244
)
4345
, true);
4446
});
@@ -120,6 +122,10 @@ void main() {
120122
expect(CustomFieldPredicate.of(CustomFieldPredicate.oneOf(['it', 'fr'])) == CustomFieldPredicate.oneOf(['it', 'fr']), true);
121123
});
122124

125+
test('test NumberPredicate.of', () {
126+
expect(NumberPredicate.of(NumberPredicate.notBetween([100, 300])) == NumberPredicate.notBetween([100, 300]), true);
127+
});
128+
123129
test('test ConversationPredicate of', () {
124130
expect(
125131
ConversationPredicate.of(ConversationPredicate(
@@ -130,6 +136,7 @@ void main() {
130136
'visibility': CustomFieldPredicate.equals('visible'),
131137
},
132138
hasUnreadMessages: false,
139+
lastMessageTs: NumberPredicate.greaterThan(1679298371586),
133140
)) == ConversationPredicate(
134141
access: FieldPredicate.notEquals(ConversationAccessLevel.none),
135142
custom: {
@@ -138,6 +145,7 @@ void main() {
138145
'visibility': CustomFieldPredicate.equals('visible'),
139146
},
140147
hasUnreadMessages: false,
148+
lastMessageTs: NumberPredicate.greaterThan(1679298371586),
141149
)
142150
, true);
143151
});
@@ -218,8 +226,9 @@ void main() {
218226
'visibility': CustomFieldPredicate.equals('visible'),
219227
},
220228
hasUnreadMessages: false,
229+
lastMessageTs: NumberPredicate.greaterThan(1679298371586),
221230
)),
222-
'{"access":["!=","None"],"custom":{"seller":"exists","category":["oneOf",["shoes","sandals"]],"visibility":["==","visible"]},"hasUnreadMessages":false}'
231+
'{"access":["!=","None"],"custom":{"seller":"exists","category":["oneOf",["shoes","sandals"]],"visibility":["==","visible"]},"hasUnreadMessages":false,"lastMessageTs":[">",1679298371586.0]}'
223232
);
224233
});
225234

0 commit comments

Comments
 (0)