Skip to content

Commit 53de4c2

Browse files
committed
api [nfc]: Factor out logic that supports serializing ApiNarrowDm
Also pull out narrow.toJson tests.
1 parent 4efdc3a commit 53de4c2

File tree

3 files changed

+58
-40
lines changed

3 files changed

+58
-40
lines changed

lib/api/model/narrow.dart

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,18 @@
11
typedef ApiNarrow = List<ApiNarrowElement>;
22

3+
/// Resolve any [ApiNarrowDm] elements appropriately.
4+
///
5+
/// This encapsulates a server-feature check.
6+
ApiNarrow resolveDmElements(ApiNarrow narrow, int zulipFeatureLevel) {
7+
final supportsOperatorDm = zulipFeatureLevel >= 177; // TODO(server-7)
8+
return (narrow.any((element) => element is ApiNarrowDm))
9+
? narrow.map((element) => switch (element) {
10+
ApiNarrowDm() => element.resolve(legacy: !supportsOperatorDm),
11+
_ => element,
12+
}).toList()
13+
: narrow;
14+
}
15+
316
/// An element in the list representing a narrow in the Zulip API.
417
///
518
/// Docs: <https://zulip.com/api/construct-narrow>
@@ -51,6 +64,8 @@ class ApiNarrowTopic extends ApiNarrowElement {
5164
/// An instance directly of this class must not be serialized with [jsonEncode],
5265
/// and more generally its [operator] getter must not be called.
5366
/// Instead, call [resolve] and use the object it returns.
67+
///
68+
/// If part of [ApiNarrow] use [resolveDmElements].
5469
class ApiNarrowDm extends ApiNarrowElement {
5570
@override String get operator {
5671
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': resolveDmElements(narrow, connection.zulipFeatureLevel!),
10295
'anchor': switch (anchor) {
10396
NumericAnchor(:var messageId) => messageId,
10497
AnchorCode.newest => RawParameter('newest'),

test/api/route/messages_test.dart

Lines changed: 42 additions & 32 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 = resolveDmElements(narrow, connection.zulipFeatureLevel!);
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,38 +243,20 @@ void main() {
215243
});
216244
});
217245

218-
test('narrow', () {
219-
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-
]));
245-
connection.zulipFeatureLevel = 176;
246-
await checkNarrow([ApiNarrowDm([123, 234])], jsonEncode([
247-
{'operator': 'pm-with', 'operand': [123, 234]},
248-
]));
249-
connection.zulipFeatureLevel = eg.futureZulipFeatureLevel;
246+
test('narrow uses resolveLegacyElements to encode', () {
247+
return FakeApiConnection.with_(zulipFeatureLevel: 176, (connection) async {
248+
connection.prepare(json: fakeResult.toJson());
249+
await checkGetMessages(connection,
250+
narrow: [ApiNarrowDm([123, 234])],
251+
anchor: AnchorCode.newest, numBefore: 10, numAfter: 20,
252+
expected: {
253+
'narrow': jsonEncode([
254+
{'operator': 'pm-with', 'operand': [123, 234]},
255+
]),
256+
'anchor': 'newest',
257+
'num_before': '10',
258+
'num_after': '20',
259+
});
250260
});
251261
});
252262

0 commit comments

Comments
 (0)