Skip to content

Commit b8b74ae

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
1 parent c8588da commit b8b74ae

File tree

4 files changed

+7
-179
lines changed

4 files changed

+7
-179
lines changed

lib/api/core.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import 'exception.dart';
1414
///
1515
/// When updating this, also update [kMinSupportedZulipFeatureLevel]
1616
/// and the README.
17-
// TODO(#268) address all TODO(server-5), TODO(server-6), and TODO(server-7)
17+
// TODO(#992) address all TODO(server-6), and TODO(server-7)
1818
const kMinSupportedZulipVersion = '7.0';
1919

2020
/// The Zulip feature level reserved for the [kMinSupportedZulipVersion] release.

lib/api/route/messages.dart

Lines changed: 0 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,69 +1,18 @@
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-
required bool allowEmptyTopicName,
20-
}) async {
21-
final useLegacyApi = connection.zulipFeatureLevel! < 120;
22-
if (useLegacyApi) {
23-
final response = await getMessages(connection,
24-
narrow: [ApiNarrowMessageId(messageId)],
25-
anchor: NumericAnchor(messageId),
26-
numBefore: 0,
27-
numAfter: 0,
28-
applyMarkdown: applyMarkdown,
29-
allowEmptyTopicName: allowEmptyTopicName,
30-
31-
// Hard-code this param to `true`, as the new single-message API
32-
// effectively does:
33-
// https://chat.zulip.org/#narrow/stream/378-api-design/topic/.60client_gravatar.60.20in.20.60messages.2F.7Bmessage_id.7D.60/near/1418337
34-
clientGravatar: true,
35-
);
36-
return response.messages.firstOrNull;
37-
} else {
38-
try {
39-
final response = await getMessage(connection,
40-
messageId: messageId,
41-
applyMarkdown: applyMarkdown,
42-
allowEmptyTopicName: allowEmptyTopicName,
43-
);
44-
return response.message;
45-
} on ZulipApiException catch (e) {
46-
if (e.code == 'BAD_REQUEST') {
47-
// Servers use this code when the message doesn't exist, according to
48-
// the example in the doc.
49-
return null;
50-
}
51-
rethrow;
52-
}
53-
}
54-
}
55-
569
/// https://zulip.com/api/get-message
57-
///
58-
/// This binding only supports feature levels 120+.
59-
// TODO(server-5) remove FL 120+ mention in doc, and the related `assert`
6010
Future<GetMessageResult> getMessage(ApiConnection connection, {
6111
required int messageId,
6212
bool? applyMarkdown,
6313
required bool allowEmptyTopicName,
6414
}) {
6515
assert(allowEmptyTopicName, '`allowEmptyTopicName` should only be true');
66-
assert(connection.zulipFeatureLevel! >= 120);
6716
return connection.get('getMessage', GetMessageResult.fromJson, 'messages/$messageId', {
6817
if (applyMarkdown != null) 'apply_markdown': applyMarkdown,
6918
'allow_empty_topic_name': allowEmptyTopicName,

lib/widgets/actions.dart

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -206,16 +206,17 @@ abstract final class ZulipAction {
206206
// On final failure or success, auto-dismiss the snackbar.
207207
final zulipLocalizations = ZulipLocalizations.of(context);
208208
try {
209-
fetchedMessage = await getMessageCompat(PerAccountStoreWidget.of(context).connection,
209+
fetchedMessage = (await getMessage(PerAccountStoreWidget.of(context).connection,
210210
messageId: messageId,
211211
applyMarkdown: false,
212212
allowEmptyTopicName: true,
213-
);
214-
if (fetchedMessage == null) {
215-
errorMessage = zulipLocalizations.errorMessageDoesNotSeemToExist;
216-
}
213+
)).message;
217214
} catch (e) {
218215
switch (e) {
216+
case ZulipApiException(code: 'BAD_REQUEST'):
217+
// Servers use this code when the message doesn't exist, according to
218+
// the example in the doc: https://zulip.com/api/get-message
219+
errorMessage = zulipLocalizations.errorMessageDoesNotSeemToExist;
219220
case ZulipApiException():
220221
errorMessage = e.message;
221222
// TODO specific messages for common errors, like network errors

test/api/route/messages_test.dart

Lines changed: 0 additions & 122 deletions
Original file line numberDiff line numberDiff line change
@@ -15,118 +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-
required bool allowEmptyTopicName,
24-
}) async {
25-
final result = await getMessageCompat(connection,
26-
messageId: messageId,
27-
applyMarkdown: applyMarkdown,
28-
allowEmptyTopicName: allowEmptyTopicName,
29-
);
30-
if (expectLegacy) {
31-
check(connection.lastRequest).isA<http.Request>()
32-
..method.equals('GET')
33-
..url.path.equals('/api/v1/messages')
34-
..url.queryParameters.deepEquals({
35-
'narrow': jsonEncode([ApiNarrowMessageId(messageId)]),
36-
'anchor': messageId.toString(),
37-
'num_before': '0',
38-
'num_after': '0',
39-
if (applyMarkdown != null) 'apply_markdown': applyMarkdown.toString(),
40-
'allow_empty_topic_name': allowEmptyTopicName.toString(),
41-
'client_gravatar': 'true',
42-
});
43-
} else {
44-
check(connection.lastRequest).isA<http.Request>()
45-
..method.equals('GET')
46-
..url.path.equals('/api/v1/messages/$messageId')
47-
..url.queryParameters.deepEquals({
48-
if (applyMarkdown != null) 'apply_markdown': applyMarkdown.toString(),
49-
'allow_empty_topic_name': allowEmptyTopicName.toString(),
50-
});
51-
}
52-
return result;
53-
}
54-
55-
test('modern; message found', () {
56-
return FakeApiConnection.with_((connection) async {
57-
final message = eg.streamMessage();
58-
final fakeResult = GetMessageResult(message: message);
59-
connection.prepare(json: fakeResult.toJson());
60-
final result = await checkGetMessageCompat(connection,
61-
expectLegacy: false,
62-
messageId: message.id,
63-
applyMarkdown: true,
64-
allowEmptyTopicName: true,
65-
);
66-
check(result).isNotNull().jsonEquals(message);
67-
});
68-
});
69-
70-
test('modern; message not found', () {
71-
return FakeApiConnection.with_((connection) async {
72-
final message = eg.streamMessage();
73-
connection.prepare(
74-
apiException: eg.apiBadRequest(message: 'Invalid message(s)'));
75-
final result = await checkGetMessageCompat(connection,
76-
expectLegacy: false,
77-
messageId: message.id,
78-
applyMarkdown: true,
79-
allowEmptyTopicName: true,
80-
);
81-
check(result).isNull();
82-
});
83-
});
84-
85-
test('legacy; message found', () {
86-
return FakeApiConnection.with_(zulipFeatureLevel: 119, (connection) async {
87-
final message = eg.streamMessage();
88-
final fakeResult = GetMessagesResult(
89-
anchor: message.id,
90-
foundNewest: false,
91-
foundOldest: false,
92-
foundAnchor: true,
93-
historyLimited: false,
94-
messages: [message],
95-
);
96-
connection.prepare(json: fakeResult.toJson());
97-
final result = await checkGetMessageCompat(connection,
98-
expectLegacy: true,
99-
messageId: message.id,
100-
applyMarkdown: true,
101-
allowEmptyTopicName: true,
102-
);
103-
check(result).isNotNull().jsonEquals(message);
104-
});
105-
});
106-
107-
test('legacy; message not found', () {
108-
return FakeApiConnection.with_(zulipFeatureLevel: 119, (connection) async {
109-
final message = eg.streamMessage();
110-
final fakeResult = GetMessagesResult(
111-
anchor: message.id,
112-
foundNewest: false,
113-
foundOldest: false,
114-
foundAnchor: false,
115-
historyLimited: false,
116-
messages: [],
117-
);
118-
connection.prepare(json: fakeResult.toJson());
119-
final result = await checkGetMessageCompat(connection,
120-
expectLegacy: true,
121-
messageId: message.id,
122-
applyMarkdown: true,
123-
allowEmptyTopicName: true,
124-
);
125-
check(result).isNull();
126-
});
127-
});
128-
});
129-
13018
group('getMessage', () {
13119
Future<GetMessageResult> checkGetMessage(
13220
FakeApiConnection connection, {
@@ -186,16 +74,6 @@ void main() {
18674
expected: {'allow_empty_topic_name': 'true'});
18775
});
18876
});
189-
190-
test('Throws assertion error when FL <120', () {
191-
return FakeApiConnection.with_(zulipFeatureLevel: 119, (connection) async {
192-
connection.prepare(json: fakeResult.toJson());
193-
check(() => getMessage(connection,
194-
messageId: 1,
195-
allowEmptyTopicName: true,
196-
)).throws<AssertionError>();
197-
});
198-
});
19977
});
20078

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

0 commit comments

Comments
 (0)