Skip to content

Commit d238e43

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 4ad8754 commit d238e43

File tree

3 files changed

+9
-167
lines changed

3 files changed

+9
-167
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
@@ -693,17 +693,20 @@ Future<String?> fetchRawContentWithFeedback({
693693
// On final failure or success, auto-dismiss the snackbar.
694694
final zulipLocalizations = ZulipLocalizations.of(context);
695695
try {
696-
fetchedMessage = await getMessageCompat(PerAccountStoreWidget.of(context).connection,
696+
fetchedMessage = (await getMessage(PerAccountStoreWidget.of(context).connection,
697697
messageId: messageId,
698698
applyMarkdown: false,
699-
);
700-
if (fetchedMessage == null) {
701-
errorMessage = zulipLocalizations.errorMessageDoesNotSeemToExist;
702-
}
699+
)).message;
703700
} catch (e) {
704701
switch (e) {
705702
case ZulipApiException():
706-
errorMessage = e.message;
703+
if (e.code == 'BAD_REQUEST') {
704+
// Servers use this code when the message doesn't exist, according
705+
// to the example in the doc.
706+
errorMessage = zulipLocalizations.errorMessageDoesNotSeemToExist;
707+
} else {
708+
errorMessage = e.message;
709+
}
707710
// TODO specific messages for common errors, like network errors
708711
// (support with reusable code)
709712
default:

test/api/route/messages_test.dart

-113
Original file line numberDiff line numberDiff line change
@@ -15,110 +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-
connection.prepare(
69-
apiException: eg.apiBadRequest(message: 'Invalid message(s)'));
70-
final result = await checkGetMessageCompat(connection,
71-
expectLegacy: false,
72-
messageId: message.id,
73-
applyMarkdown: true,
74-
);
75-
check(result).isNull();
76-
});
77-
});
78-
79-
test('legacy; message found', () {
80-
return FakeApiConnection.with_(zulipFeatureLevel: 119, (connection) async {
81-
final message = eg.streamMessage();
82-
final fakeResult = GetMessagesResult(
83-
anchor: message.id,
84-
foundNewest: false,
85-
foundOldest: false,
86-
foundAnchor: true,
87-
historyLimited: false,
88-
messages: [message],
89-
);
90-
connection.prepare(json: fakeResult.toJson());
91-
final result = await checkGetMessageCompat(connection,
92-
expectLegacy: true,
93-
messageId: message.id,
94-
applyMarkdown: true,
95-
);
96-
check(result).isNotNull().jsonEquals(message);
97-
});
98-
});
99-
100-
test('legacy; message not found', () {
101-
return FakeApiConnection.with_(zulipFeatureLevel: 119, (connection) async {
102-
final message = eg.streamMessage();
103-
final fakeResult = GetMessagesResult(
104-
anchor: message.id,
105-
foundNewest: false,
106-
foundOldest: false,
107-
foundAnchor: false,
108-
historyLimited: false,
109-
messages: [],
110-
);
111-
connection.prepare(json: fakeResult.toJson());
112-
final result = await checkGetMessageCompat(connection,
113-
expectLegacy: true,
114-
messageId: message.id,
115-
applyMarkdown: true,
116-
);
117-
check(result).isNull();
118-
});
119-
});
120-
});
121-
12218
group('getMessage', () {
12319
Future<GetMessageResult> checkGetMessage(
12420
FakeApiConnection connection, {
@@ -158,15 +54,6 @@ void main() {
15854
expected: {'apply_markdown': 'false'});
15955
});
16056
});
161-
162-
test('Throws assertion error when FL <120', () {
163-
return FakeApiConnection.with_(zulipFeatureLevel: 119, (connection) async {
164-
connection.prepare(json: fakeResult.toJson());
165-
check(() => getMessage(connection,
166-
messageId: 1,
167-
)).throws<AssertionError>();
168-
});
169-
});
17057
});
17158

17259
test('ApiNarrow.toJson', () {

0 commit comments

Comments
 (0)