Skip to content

Commit 8644036

Browse files
chrisbobbegnprice
authored andcommitted
api: Add get-message route (for single message)
Fixes: zulip#140
1 parent 8db5269 commit 8644036

File tree

3 files changed

+89
-0
lines changed

3 files changed

+89
-0
lines changed

lib/api/route/messages.dart

+29
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,35 @@ import '../model/narrow.dart';
66

77
part 'messages.g.dart';
88

9+
/// https://zulip.com/api/get-message
10+
///
11+
/// This binding only supports feature levels 120+.
12+
// TODO(server-5) remove FL 120+ mention in doc, and the related `assert`
13+
Future<GetMessageResult> getMessage(ApiConnection connection, {
14+
required int messageId,
15+
bool? applyMarkdown,
16+
}) {
17+
assert(connection.zulipFeatureLevel! >= 120);
18+
return connection.get('getMessage', GetMessageResult.fromJson, 'messages/$messageId', {
19+
if (applyMarkdown != null) 'apply_markdown': applyMarkdown,
20+
});
21+
}
22+
23+
@JsonSerializable(fieldRename: FieldRename.snake)
24+
class GetMessageResult {
25+
// final String rawContent; // deprecated; ignore
26+
final Message message;
27+
28+
GetMessageResult({
29+
required this.message,
30+
});
31+
32+
factory GetMessageResult.fromJson(Map<String, dynamic> json) =>
33+
_$GetMessageResultFromJson(json);
34+
35+
Map<String, dynamic> toJson() => _$GetMessageResultToJson(this);
36+
}
37+
938
/// https://zulip.com/api/get-messages
1039
Future<GetMessagesResult> getMessages(ApiConnection connection, {
1140
required ApiNarrow narrow,

lib/api/route/messages.g.dart

+10
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/api/route/messages_test.dart

+50
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,56 @@ import '../fake_api.dart';
1313
import 'route_checks.dart';
1414

1515
void main() {
16+
group('getMessage', () {
17+
Future<GetMessageResult> checkGetMessage(
18+
FakeApiConnection connection, {
19+
required int messageId,
20+
bool? applyMarkdown,
21+
required Map<String, String> expected,
22+
}) async {
23+
final result = await getMessage(connection,
24+
messageId: messageId,
25+
applyMarkdown: applyMarkdown,
26+
);
27+
check(connection.lastRequest).isNotNull().isA<http.Request>()
28+
..method.equals('GET')
29+
..url.path.equals('/api/v1/messages/$messageId')
30+
..url.queryParameters.deepEquals(expected);
31+
return result;
32+
}
33+
34+
final fakeResult = GetMessageResult(message: eg.streamMessage());
35+
36+
test('applyMarkdown true', () {
37+
return FakeApiConnection.with_((connection) async {
38+
connection.prepare(json: fakeResult.toJson());
39+
await checkGetMessage(connection,
40+
messageId: 1,
41+
applyMarkdown: true,
42+
expected: {'apply_markdown': 'true'});
43+
});
44+
});
45+
46+
test('applyMarkdown false', () {
47+
return FakeApiConnection.with_((connection) async {
48+
connection.prepare(json: fakeResult.toJson());
49+
await checkGetMessage(connection,
50+
messageId: 1,
51+
applyMarkdown: false,
52+
expected: {'apply_markdown': 'false'});
53+
});
54+
});
55+
56+
test('Throws assertion error when FL <120', () {
57+
return FakeApiConnection.with_(zulipFeatureLevel: 119, (connection) async {
58+
connection.prepare(json: fakeResult.toJson());
59+
check(() => getMessage(connection,
60+
messageId: 1,
61+
)).throws<AssertionError>();
62+
});
63+
});
64+
});
65+
1666
group('getMessages', () {
1767
Future<GetMessagesResult> checkGetMessages(
1868
FakeApiConnection connection, {

0 commit comments

Comments
 (0)