Skip to content

Commit ba5e3dd

Browse files
committed
api [nfc]: Refactor out logic that supports serializing legacy DmNarrow
Also pull out narrow.toJson tests.
1 parent b6badf5 commit ba5e3dd

File tree

3 files changed

+59
-37
lines changed

3 files changed

+59
-37
lines changed

lib/api/model/narrow.dart

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,20 @@
1+
import '../core.dart';
2+
13
typedef ApiNarrow = List<ApiNarrowElement>;
24

5+
/// Convenience function to resolve legacy elements of a narrow.
6+
///
7+
/// This encapsulates a server-feature check.
8+
///
9+
/// See [ApiNarrowDm], an element with a legacy variant.
10+
ApiNarrow resolveLegacyElements(ApiNarrow narrow, ApiConnection connection) {
11+
final supportsOperatorDm = connection.zulipFeatureLevel! >= 177; // TODO(server-7)
12+
return narrow.map((element) => switch (element) {
13+
ApiNarrowDm() => element.resolve(legacy: !supportsOperatorDm),
14+
_ => element,
15+
}).toList();
16+
}
17+
318
/// An element in the list representing a narrow in the Zulip API.
419
///
520
/// Docs: <https://zulip.com/api/construct-narrow>
@@ -51,6 +66,8 @@ class ApiNarrowTopic extends ApiNarrowElement {
5166
/// An instance directly of this class must not be serialized with [jsonEncode],
5267
/// and more generally its [operator] getter must not be called.
5368
/// Instead, call [resolve] and use the object it returns.
69+
///
70+
/// If part of [ApiNarrow] use [resolveLegacyElements].
5471
class ApiNarrowDm extends ApiNarrowElement {
5572
@override String get operator {
5673
assert(false,

lib/api/route/messages.dart

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -90,15 +90,8 @@ Future<GetMessagesResult> getMessages(ApiConnection connection, {
9090
bool? applyMarkdown,
9191
// bool? useFirstUnreadAnchor // omitted because deprecated
9292
}) {
93-
if (narrow.any((element) => element is ApiNarrowDm)) {
94-
final supportsOperatorDm = connection.zulipFeatureLevel! >= 177; // TODO(server-7)
95-
narrow = narrow.map((element) => switch (element) {
96-
ApiNarrowDm() => element.resolve(legacy: !supportsOperatorDm),
97-
_ => element,
98-
}).toList();
99-
}
10093
return connection.get('getMessages', GetMessagesResult.fromJson, 'messages', {
101-
'narrow': narrow,
94+
'narrow': resolveLegacyElements(narrow, connection),
10295
'anchor': switch (anchor) {
10396
NumericAnchor(:var messageId) => messageId,
10497
AnchorCode.newest => RawParameter('newest'),

test/api/route/messages_test.dart

Lines changed: 41 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,34 @@ void main() {
172172
});
173173
});
174174

175+
test('Narrow.toJson', () {
176+
return FakeApiConnection.with_((connection) async {
177+
void checkNarrow(ApiNarrow narrow, String expected) {
178+
narrow = resolveLegacyElements(narrow, connection);
179+
check(jsonEncode(narrow)).equals(expected);
180+
}
181+
182+
checkNarrow(const AllMessagesNarrow().apiEncode(), jsonEncode([]));
183+
checkNarrow(const StreamNarrow(12).apiEncode(), jsonEncode([
184+
{'operator': 'stream', 'operand': 12},
185+
]));
186+
checkNarrow(const TopicNarrow(12, 'stuff').apiEncode(), jsonEncode([
187+
{'operator': 'stream', 'operand': 12},
188+
{'operator': 'topic', 'operand': 'stuff'},
189+
]));
190+
191+
checkNarrow([ApiNarrowDm([123, 234])], jsonEncode([
192+
{'operator': 'dm', 'operand': [123, 234]},
193+
]));
194+
195+
connection.zulipFeatureLevel = 176;
196+
checkNarrow([ApiNarrowDm([123, 234])], jsonEncode([
197+
{'operator': 'pm-with', 'operand': [123, 234]},
198+
]));
199+
connection.zulipFeatureLevel = eg.futureZulipFeatureLevel;
200+
});
201+
});
202+
175203
group('getMessages', () {
176204
Future<GetMessagesResult> checkGetMessages(
177205
FakeApiConnection connection, {
@@ -215,37 +243,21 @@ void main() {
215243
});
216244
});
217245

218-
test('narrow', () {
246+
test('narrow uses resolveLegacyElements to encode', () {
219247
return FakeApiConnection.with_((connection) async {
220-
Future<void> checkNarrow(ApiNarrow narrow, String expected) async {
221-
connection.prepare(json: fakeResult.toJson());
222-
await checkGetMessages(connection,
223-
narrow: narrow,
224-
anchor: AnchorCode.newest, numBefore: 10, numAfter: 20,
225-
expected: {
226-
'narrow': expected,
227-
'anchor': 'newest',
228-
'num_before': '10',
229-
'num_after': '20',
230-
});
231-
}
232-
233-
await checkNarrow(const AllMessagesNarrow().apiEncode(), jsonEncode([]));
234-
await checkNarrow(const StreamNarrow(12).apiEncode(), jsonEncode([
235-
{'operator': 'stream', 'operand': 12},
236-
]));
237-
await checkNarrow(const TopicNarrow(12, 'stuff').apiEncode(), jsonEncode([
238-
{'operator': 'stream', 'operand': 12},
239-
{'operator': 'topic', 'operand': 'stuff'},
240-
]));
241-
242-
await checkNarrow([ApiNarrowDm([123, 234])], jsonEncode([
243-
{'operator': 'dm', 'operand': [123, 234]},
244-
]));
245248
connection.zulipFeatureLevel = 176;
246-
await checkNarrow([ApiNarrowDm([123, 234])], jsonEncode([
247-
{'operator': 'pm-with', 'operand': [123, 234]},
248-
]));
249+
connection.prepare(json: fakeResult.toJson());
250+
await checkGetMessages(connection,
251+
narrow: [ApiNarrowDm([123, 234])],
252+
anchor: AnchorCode.newest, numBefore: 10, numAfter: 20,
253+
expected: {
254+
'narrow': jsonEncode([
255+
{'operator': 'pm-with', 'operand': [123, 234]},
256+
]),
257+
'anchor': 'newest',
258+
'num_before': '10',
259+
'num_after': '20',
260+
});
249261
connection.zulipFeatureLevel = eg.futureZulipFeatureLevel;
250262
});
251263
});

0 commit comments

Comments
 (0)