Skip to content

Commit 6b4a83f

Browse files
committed
api: Add send-message route
1 parent 57294ea commit 6b4a83f

File tree

2 files changed

+65
-0
lines changed

2 files changed

+65
-0
lines changed

lib/api/route/messages.dart

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,3 +46,56 @@ class GetMessagesResult {
4646

4747
Map<String, dynamic> toJson() => _$GetMessagesResultToJson(this);
4848
}
49+
50+
// https://zulip.com/api/send-message#parameter-topic
51+
const int kMaxTopicLength = 60;
52+
53+
// https://zulip.com/api/send-message#parameter-content
54+
const int kMaxMessageLengthCodePoints = 10000;
55+
56+
/// The topic servers understand to mean "there is no topic".
57+
///
58+
/// This should match
59+
/// https://github.com/zulip/zulip/blob/6.0/zerver/actions/message_edit.py#L940
60+
/// or similar logic at the latest `main`.
61+
// This is hardcoded in the server, and therefore untranslated; that's
62+
// zulip/zulip#3639.
63+
const String kNoTopicTopic = '(no topic)';
64+
65+
/// https://zulip.com/api/send-message
66+
// TODO currently only handles stream messages; fix
67+
Future<SendMessageResult> sendMessage(
68+
ApiConnection connection, {
69+
required String content,
70+
required String topic,
71+
}) async {
72+
// assert() is less verbose but would have no effect in production, I think:
73+
// https://dart.dev/guides/language/language-tour#assert
74+
if (Uri.parse(connection.auth.realmUrl).origin != 'https://chat.zulip.org') {
75+
throw AssertionError('This binding can currently only be used on https://chat.zulip.org.');
76+
}
77+
78+
final data = await connection.post('messages', {
79+
'type': RawParameter('stream'), // TODO parametrize
80+
'to': 7, // TODO parametrize; this is `#test here`
81+
'topic': RawParameter(topic),
82+
'content': RawParameter(content),
83+
});
84+
return SendMessageResult.fromJson(jsonDecode(data));
85+
}
86+
87+
@JsonSerializable()
88+
class SendMessageResult {
89+
final int id;
90+
final String? deliver_at;
91+
92+
SendMessageResult({
93+
required this.id,
94+
this.deliver_at,
95+
});
96+
97+
factory SendMessageResult.fromJson(Map<String, dynamic> json) =>
98+
_$SendMessageResultFromJson(json);
99+
100+
Map<String, dynamic> toJson() => _$SendMessageResultToJson(this);
101+
}

lib/api/route/messages.g.dart

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)