@@ -92,26 +92,52 @@ const int kMaxMessageLengthCodePoints = 10000;
92
92
const String kNoTopicTopic = '(no topic)' ;
93
93
94
94
/// https://zulip.com/api/send-message
95
- // TODO currently only handles stream messages; fix
96
95
Future <SendMessageResult > sendMessage (
97
96
ApiConnection connection, {
97
+ required MessageDestination destination,
98
98
required String content,
99
- required String topic,
100
99
}) {
101
- // assert() is less verbose but would have no effect in production, I think:
102
- // https://dart.dev/guides/language/language-tour#assert
103
- if (connection.realmUrl.origin != 'https://chat.zulip.org' ) {
104
- throw Exception ('This binding can currently only be used on https://chat.zulip.org.' );
105
- }
106
-
107
100
return connection.post ('sendMessage' , SendMessageResult .fromJson, 'messages' , {
108
- 'type' : RawParameter ('stream' ), // TODO parametrize
109
- 'to' : 7 , // TODO parametrize; this is `#test here`
110
- 'topic' : RawParameter (topic),
101
+ if (destination is StreamDestination ) ...{
102
+ 'type' : RawParameter ('stream' ),
103
+ 'to' : destination.streamId,
104
+ 'topic' : RawParameter (destination.topic),
105
+ } else if (destination is PmDestination ) ...{
106
+ 'type' : RawParameter ('private' ), // TODO(server-7)
107
+ 'to' : destination.userIds,
108
+ } else ...(
109
+ throw Exception ('impossible destination' ) // TODO(dart-3) show this statically
110
+ ),
111
111
'content' : RawParameter (content),
112
112
});
113
113
}
114
114
115
+ /// Which conversation to send a message to, in [sendMessage] .
116
+ ///
117
+ /// This is either a [StreamDestination] or a [PmDestination] .
118
+ sealed class MessageDestination {}
119
+
120
+ /// A conversation in a stream, for specifying to [sendMessage] .
121
+ ///
122
+ /// The server accepts a stream name as an alternative to a stream ID,
123
+ /// but this binding currently doesn't.
124
+ class StreamDestination extends MessageDestination {
125
+ StreamDestination (this .streamId, this .topic);
126
+
127
+ final int streamId;
128
+ final String topic;
129
+ }
130
+
131
+ /// A PM conversation, for specifying to [sendMessage] .
132
+ ///
133
+ /// The server accepts a list of Zulip API emails as an alternative to
134
+ /// a list of user IDs, but this binding currently doesn't.
135
+ class PmDestination extends MessageDestination {
136
+ PmDestination ({required this .userIds});
137
+
138
+ final List <int > userIds;
139
+ }
140
+
115
141
@JsonSerializable (fieldRename: FieldRename .snake)
116
142
class SendMessageResult {
117
143
final int id;
0 commit comments