Skip to content

Commit 665bd08

Browse files
committed
api: Remove legacy getMessageCompat, relying on server 5+, FL 120+
See "Feature level 120" from Zulip API changelog: https://zulip.com/api/changelog See also commit 631f4d6. Fixes: #268 Signed-off-by: Zixuan James Li <[email protected]>
1 parent e174ee4 commit 665bd08

File tree

3 files changed

+9
-171
lines changed

3 files changed

+9
-171
lines changed

lib/api/route/messages.dart

-48
Original file line numberDiff line numberDiff line change
@@ -1,64 +1,16 @@
11
import 'package:json_annotation/json_annotation.dart';
22

33
import '../core.dart';
4-
import '../exception.dart';
54
import '../model/model.dart';
65
import '../model/narrow.dart';
76

87
part 'messages.g.dart';
98

10-
/// Convenience function to get a single message from any server.
11-
///
12-
/// This encapsulates a server-feature check.
13-
///
14-
/// Gives null if the server reports that the message doesn't exist.
15-
// TODO(server-5) Simplify this away; just use getMessage.
16-
Future<Message?> getMessageCompat(ApiConnection connection, {
17-
required int messageId,
18-
bool? applyMarkdown,
19-
}) async {
20-
final useLegacyApi = connection.zulipFeatureLevel! < 120;
21-
if (useLegacyApi) {
22-
final response = await getMessages(connection,
23-
narrow: [ApiNarrowMessageId(messageId)],
24-
anchor: NumericAnchor(messageId),
25-
numBefore: 0,
26-
numAfter: 0,
27-
applyMarkdown: applyMarkdown,
28-
29-
// Hard-code this param to `true`, as the new single-message API
30-
// effectively does:
31-
// https://chat.zulip.org/#narrow/stream/378-api-design/topic/.60client_gravatar.60.20in.20.60messages.2F.7Bmessage_id.7D.60/near/1418337
32-
clientGravatar: true,
33-
);
34-
return response.messages.firstOrNull;
35-
} else {
36-
try {
37-
final response = await getMessage(connection,
38-
messageId: messageId,
39-
applyMarkdown: applyMarkdown,
40-
);
41-
return response.message;
42-
} on ZulipApiException catch (e) {
43-
if (e.code == 'BAD_REQUEST') {
44-
// Servers use this code when the message doesn't exist, according to
45-
// the example in the doc.
46-
return null;
47-
}
48-
rethrow;
49-
}
50-
}
51-
}
52-
539
/// https://zulip.com/api/get-message
54-
///
55-
/// This binding only supports feature levels 120+.
56-
// TODO(server-5) remove FL 120+ mention in doc, and the related `assert`
5710
Future<GetMessageResult> getMessage(ApiConnection connection, {
5811
required int messageId,
5912
bool? applyMarkdown,
6013
}) {
61-
assert(connection.zulipFeatureLevel! >= 120);
6214
return connection.get('getMessage', GetMessageResult.fromJson, 'messages/$messageId', {
6315
if (applyMarkdown != null) 'apply_markdown': applyMarkdown,
6416
});

lib/widgets/action_sheet.dart

+9-6
Original file line numberDiff line numberDiff line change
@@ -190,17 +190,20 @@ Future<String?> fetchRawContentWithFeedback({
190190
// On final failure or success, auto-dismiss the snackbar.
191191
final zulipLocalizations = ZulipLocalizations.of(context);
192192
try {
193-
fetchedMessage = await getMessageCompat(PerAccountStoreWidget.of(context).connection,
193+
fetchedMessage = (await getMessage(PerAccountStoreWidget.of(context).connection,
194194
messageId: messageId,
195195
applyMarkdown: false,
196-
);
197-
if (fetchedMessage == null) {
198-
errorMessage = zulipLocalizations.errorMessageDoesNotSeemToExist;
199-
}
196+
)).message;
200197
} catch (e) {
201198
switch (e) {
202199
case ZulipApiException():
203-
errorMessage = e.message;
200+
if (e.code == 'BAD_REQUEST') {
201+
// Servers use this code when the message doesn't exist, according
202+
// to the example in the doc.
203+
errorMessage = zulipLocalizations.errorMessageDoesNotSeemToExist;
204+
} else {
205+
errorMessage = e.message;
206+
}
204207
// TODO specific messages for common errors, like network errors
205208
// (support with reusable code)
206209
default:

test/api/route/messages_test.dart

-117
Original file line numberDiff line numberDiff line change
@@ -15,114 +15,6 @@ import '../fake_api.dart';
1515
import 'route_checks.dart';
1616

1717
void main() {
18-
group('getMessageCompat', () {
19-
Future<Message?> checkGetMessageCompat(FakeApiConnection connection, {
20-
required bool expectLegacy,
21-
required int messageId,
22-
bool? applyMarkdown,
23-
}) async {
24-
final result = await getMessageCompat(connection,
25-
messageId: messageId,
26-
applyMarkdown: applyMarkdown,
27-
);
28-
if (expectLegacy) {
29-
check(connection.lastRequest).isA<http.Request>()
30-
..method.equals('GET')
31-
..url.path.equals('/api/v1/messages')
32-
..url.queryParameters.deepEquals({
33-
'narrow': jsonEncode([ApiNarrowMessageId(messageId)]),
34-
'anchor': messageId.toString(),
35-
'num_before': '0',
36-
'num_after': '0',
37-
if (applyMarkdown != null) 'apply_markdown': applyMarkdown.toString(),
38-
'client_gravatar': 'true',
39-
});
40-
} else {
41-
check(connection.lastRequest).isA<http.Request>()
42-
..method.equals('GET')
43-
..url.path.equals('/api/v1/messages/$messageId')
44-
..url.queryParameters.deepEquals({
45-
if (applyMarkdown != null) 'apply_markdown': applyMarkdown.toString(),
46-
});
47-
}
48-
return result;
49-
}
50-
51-
test('modern; message found', () {
52-
return FakeApiConnection.with_((connection) async {
53-
final message = eg.streamMessage();
54-
final fakeResult = GetMessageResult(message: message);
55-
connection.prepare(json: fakeResult.toJson());
56-
final result = await checkGetMessageCompat(connection,
57-
expectLegacy: false,
58-
messageId: message.id,
59-
applyMarkdown: true,
60-
);
61-
check(result).isNotNull().jsonEquals(message);
62-
});
63-
});
64-
65-
test('modern; message not found', () {
66-
return FakeApiConnection.with_((connection) async {
67-
final message = eg.streamMessage();
68-
final fakeResponseJson = {
69-
'code': 'BAD_REQUEST',
70-
'msg': 'Invalid message(s)',
71-
'result': 'error',
72-
};
73-
connection.prepare(httpStatus: 400, json: fakeResponseJson);
74-
final result = await checkGetMessageCompat(connection,
75-
expectLegacy: false,
76-
messageId: message.id,
77-
applyMarkdown: true,
78-
);
79-
check(result).isNull();
80-
});
81-
});
82-
83-
test('legacy; message found', () {
84-
return FakeApiConnection.with_(zulipFeatureLevel: 119, (connection) async {
85-
final message = eg.streamMessage();
86-
final fakeResult = GetMessagesResult(
87-
anchor: message.id,
88-
foundNewest: false,
89-
foundOldest: false,
90-
foundAnchor: true,
91-
historyLimited: false,
92-
messages: [message],
93-
);
94-
connection.prepare(json: fakeResult.toJson());
95-
final result = await checkGetMessageCompat(connection,
96-
expectLegacy: true,
97-
messageId: message.id,
98-
applyMarkdown: true,
99-
);
100-
check(result).isNotNull().jsonEquals(message);
101-
});
102-
});
103-
104-
test('legacy; message not found', () {
105-
return FakeApiConnection.with_(zulipFeatureLevel: 119, (connection) async {
106-
final message = eg.streamMessage();
107-
final fakeResult = GetMessagesResult(
108-
anchor: message.id,
109-
foundNewest: false,
110-
foundOldest: false,
111-
foundAnchor: false,
112-
historyLimited: false,
113-
messages: [],
114-
);
115-
connection.prepare(json: fakeResult.toJson());
116-
final result = await checkGetMessageCompat(connection,
117-
expectLegacy: true,
118-
messageId: message.id,
119-
applyMarkdown: true,
120-
);
121-
check(result).isNull();
122-
});
123-
});
124-
});
125-
12618
group('getMessage', () {
12719
Future<GetMessageResult> checkGetMessage(
12820
FakeApiConnection connection, {
@@ -162,15 +54,6 @@ void main() {
16254
expected: {'apply_markdown': 'false'});
16355
});
16456
});
165-
166-
test('Throws assertion error when FL <120', () {
167-
return FakeApiConnection.with_(zulipFeatureLevel: 119, (connection) async {
168-
connection.prepare(json: fakeResult.toJson());
169-
check(() => getMessage(connection,
170-
messageId: 1,
171-
)).throws<AssertionError>();
172-
});
173-
});
17457
});
17558

17659
test('Narrow.toJson', () {

0 commit comments

Comments
 (0)