Skip to content

Commit 3bf5522

Browse files
author
Franco Bugnano
committed
Started working on ConversationPredicate and MessagePredicate
1 parent 8cc6b29 commit 3bf5522

File tree

3 files changed

+225
-2
lines changed

3 files changed

+225
-2
lines changed

lib/src/chatbox.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ class ChatBox extends StatefulWidget {
4848
final TranslationToggle? showTranslationToggle;
4949
final String? theme;
5050
final TranslateConversations? translateConversations;
51-
final List<String> highlightedWords;
51+
final List<String> highlightedWords = const <String>[];
5252

5353
final Conversation? conversation;
5454
final bool? asGuest;
@@ -65,7 +65,7 @@ class ChatBox extends StatefulWidget {
6565
this.showTranslationToggle,
6666
this.theme,
6767
this.translateConversations,
68-
this.highlightedWords = const <String>[],
68+
//this.highlightedWords = const <String>[], // Commented out due to bug #1953
6969
this.conversation,
7070
this.asGuest,
7171
this.onSendMessage,

lib/src/predicate.dart

Lines changed: 222 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,222 @@
1+
import 'dart:convert';
2+
3+
class FieldPredicate<T> {
4+
final String _operand;
5+
String? _value;
6+
List<String>? _values;
7+
8+
FieldPredicate.equals(T value) : _operand = '==', _value = value.toString();
9+
FieldPredicate.notEquals(T value) : _operand = '!=', _value = value.toString();
10+
FieldPredicate.oneOf(List<T> values) : _operand = 'oneOf', _values = values.map((value) => value.toString()).toList();
11+
FieldPredicate.notOneOf(List<T> values) : _operand = '!oneOf', _values = values.map((value) => value.toString()).toList();
12+
13+
@override
14+
String toString() {
15+
return json.encode(this);
16+
}
17+
18+
dynamic toJson() {
19+
final result = <dynamic>[];
20+
21+
result.add(_operand);
22+
23+
if (_value != null) {
24+
result.add(_value);
25+
}
26+
27+
if (_values != null) {
28+
result.add(_values);
29+
}
30+
31+
return result;
32+
}
33+
}
34+
35+
class CustomFieldPredicate extends FieldPredicate<String> {
36+
bool? _exists;
37+
38+
CustomFieldPredicate.equals(String value) : super.equals(value);
39+
CustomFieldPredicate.notEquals(String value) : super.notEquals(value);
40+
CustomFieldPredicate.oneOf(List<String> values) : super.oneOf(values);
41+
CustomFieldPredicate.notOneOf(List<String> values) : super.notOneOf(values);
42+
CustomFieldPredicate.exists() : _exists = true, super.equals('');
43+
CustomFieldPredicate.notExists() : _exists = false, super.notEquals('');
44+
45+
@override
46+
String toString() {
47+
if (_exists == null) {
48+
return super.toString();
49+
} else if (_exists!) {
50+
return 'exists';
51+
} else {
52+
return '!exists';
53+
}
54+
}
55+
56+
@override
57+
dynamic toJson() {
58+
if (_exists == null) {
59+
return super.toJson();
60+
} else if (_exists!) {
61+
return 'exists';
62+
} else {
63+
return '!exists';
64+
}
65+
}
66+
}
67+
68+
class ConversationAccessLevel {
69+
final String _value;
70+
71+
const ConversationAccessLevel._(this._value);
72+
73+
static const ConversationAccessLevel none = ConversationAccessLevel._('None');
74+
static const ConversationAccessLevel read = ConversationAccessLevel._('Read');
75+
static const ConversationAccessLevel readWrite = ConversationAccessLevel._('ReadWrite');
76+
77+
@override
78+
String toString() => _value;
79+
}
80+
81+
class ConversationPredicate {
82+
/// Only select conversations that the current user as specific access to.
83+
final FieldPredicate<ConversationAccessLevel>? access;
84+
85+
/// Only select conversations that have particular custom fields set to particular values.
86+
final Map<String, CustomFieldPredicate>? custom;
87+
88+
/// Set this field to only select conversations that have, or don't have any, unread messages.
89+
final bool? hasUnreadMessages;
90+
91+
const ConversationPredicate({this.access, this.custom, this.hasUnreadMessages});
92+
93+
@override
94+
String toString() {
95+
return json.encode(this);
96+
}
97+
98+
Map<String, dynamic> toJson() {
99+
final result = <String, dynamic>{};
100+
101+
if (access != null) {
102+
result['access'] = access;
103+
}
104+
105+
if (custom != null) {
106+
result['custom'] = custom;
107+
}
108+
109+
if (hasUnreadMessages != null) {
110+
result['hasUnreadMessages'] = hasUnreadMessages;
111+
}
112+
113+
return result;
114+
}
115+
}
116+
117+
class MessageOrigin {
118+
final String _value;
119+
120+
const MessageOrigin._(this._value);
121+
122+
static const MessageOrigin web = MessageOrigin._('web');
123+
static const MessageOrigin rest = MessageOrigin._('rest');
124+
static const MessageOrigin email = MessageOrigin._('email');
125+
static const MessageOrigin import = MessageOrigin._('import');
126+
127+
@override
128+
String toString() => _value;
129+
}
130+
131+
class MessageType {
132+
final String _value;
133+
134+
const MessageType._(this._value);
135+
136+
static const MessageType userMessage = MessageType._('UserMessage');
137+
static const MessageType systemMessage = MessageType._('SystemMessage');
138+
139+
@override
140+
String toString() => _value;
141+
}
142+
143+
class SenderPredicate {
144+
final FieldPredicate<String>? id;
145+
final Map<String, CustomFieldPredicate>? custom;
146+
final FieldPredicate<String>? locale;
147+
final FieldPredicate<String>? role;
148+
149+
const SenderPredicate({this.id, this.custom, this.locale, this.role});
150+
151+
@override
152+
String toString() {
153+
return json.encode(this);
154+
}
155+
156+
Map<String, dynamic> toJson() {
157+
final result = <String, dynamic>{};
158+
159+
if (id != null) {
160+
result['id'] = id;
161+
}
162+
163+
if (custom != null) {
164+
result['custom'] = custom;
165+
}
166+
167+
if (locale != null) {
168+
result['locale'] = locale;
169+
}
170+
171+
if (role != null) {
172+
result['role'] = role;
173+
}
174+
175+
return result;
176+
}
177+
}
178+
179+
class MessagePredicate {
180+
/// Only select messages that have particular custom fields set to particular values.
181+
final Map<String, CustomFieldPredicate>? custom;
182+
183+
/// Only show messages that were sent by users (web), through the REST API (rest), via
184+
/// reply-to-email (email) or via the import REST API (import).
185+
final FieldPredicate<MessageOrigin>? origin;
186+
187+
/// Only show messages that are sent by a sender that has all of the given properties
188+
final SenderPredicate? sender;
189+
190+
/// Only show messages of a given type
191+
final FieldPredicate<MessageType>? type;
192+
193+
const MessagePredicate({this.custom, this.origin, this.sender, this.type});
194+
195+
@override
196+
String toString() {
197+
return json.encode(this);
198+
}
199+
200+
Map<String, dynamic> toJson() {
201+
final result = <String, dynamic>{};
202+
203+
if (custom != null) {
204+
result['custom'] = custom;
205+
}
206+
207+
if (origin != null) {
208+
result['origin'] = origin;
209+
}
210+
211+
if (sender != null) {
212+
result['sender'] = sender;
213+
}
214+
215+
if (type != null) {
216+
result['type'] = type;
217+
}
218+
219+
return result;
220+
}
221+
}
222+

lib/talkjs.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ export 'src/session.dart';
99
export 'src/chatbox.dart';
1010
export 'src/user.dart';
1111
export 'src/conversationlist.dart';
12+
export 'src/predicate.dart';
1213

1314
/// The [Talk] object provides utility functions to help use TalkJS.
1415
class Talk {

0 commit comments

Comments
 (0)