Skip to content

Commit dd4cdde

Browse files
committed
api [nfc]: Rename resolveDmElements to resolveApiNarrowElements
And refactor the implementation to prepare for a new [ApiNarrow] feature. We're about to add ApiNarrowWith, which new in Zulip Server 9. This function seems like a good place to resolve ApiNarrows into the expected form for servers before 9 (without "with") and 9 or later (with "with"). First we have to make its name and dartdoc more generic, as done here.
1 parent 03dfdf1 commit dd4cdde

File tree

4 files changed

+19
-11
lines changed

4 files changed

+19
-11
lines changed

lib/api/model/narrow.dart

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,22 @@ part 'narrow.g.dart';
44

55
typedef ApiNarrow = List<ApiNarrowElement>;
66

7-
/// Resolve any [ApiNarrowDm] elements appropriately.
7+
/// Resolve any [ApiNarrowElement]s, such as [ApiNarrowDm]s, appropriately.
88
///
99
/// This encapsulates a server-feature check.
10-
ApiNarrow resolveDmElements(ApiNarrow narrow, int zulipFeatureLevel) {
11-
if (!narrow.any((element) => element is ApiNarrowDm)) {
12-
return narrow;
10+
// TODO(server-7) remove [ApiNarrowDm] reference in dartdoc
11+
ApiNarrow resolveApiNarrowElements(ApiNarrow narrow, int zulipFeatureLevel) {
12+
bool hasDmElement = false;
13+
for (final element in narrow) {
14+
switch (element) {
15+
case ApiNarrowDm(): hasDmElement = true;
16+
default:
17+
}
1318
}
19+
if (!hasDmElement) return narrow;
20+
1421
final supportsOperatorDm = zulipFeatureLevel >= 177; // TODO(server-7)
22+
1523
return narrow.map((element) => switch (element) {
1624
ApiNarrowDm() => element.resolve(legacy: !supportsOperatorDm),
1725
_ => element,
@@ -70,7 +78,7 @@ class ApiNarrowTopic extends ApiNarrowElement {
7078
/// and more generally its [operator] getter must not be called.
7179
/// Instead, call [resolve] and use the object it returns.
7280
///
73-
/// If part of [ApiNarrow] use [resolveDmElements].
81+
/// If part of [ApiNarrow] use [resolveApiNarrowElements].
7482
class ApiNarrowDm extends ApiNarrowElement {
7583
@override String get operator {
7684
assert(false,

lib/api/route/messages.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ Future<GetMessagesResult> getMessages(ApiConnection connection, {
9191
// bool? useFirstUnreadAnchor // omitted because deprecated
9292
}) {
9393
return connection.get('getMessages', GetMessagesResult.fromJson, 'messages', {
94-
'narrow': resolveDmElements(narrow, connection.zulipFeatureLevel!),
94+
'narrow': resolveApiNarrowElements(narrow, connection.zulipFeatureLevel!),
9595
'anchor': RawParameter(anchor.toJson()),
9696
if (includeAnchor != null) 'include_anchor': includeAnchor,
9797
'num_before': numBefore,
@@ -367,7 +367,7 @@ Future<UpdateMessageFlagsForNarrowResult> updateMessageFlagsForNarrow(ApiConnect
367367
if (includeAnchor != null) 'include_anchor': includeAnchor,
368368
'num_before': numBefore,
369369
'num_after': numAfter,
370-
'narrow': resolveDmElements(narrow, connection.zulipFeatureLevel!),
370+
'narrow': resolveApiNarrowElements(narrow, connection.zulipFeatureLevel!),
371371
'op': RawParameter(op.toJson()),
372372
'flag': RawParameter(flag.toJson()),
373373
});

lib/model/internal_link.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ String? decodeHashComponent(String str) {
5858
// you do so by passing the `anchor` param.
5959
Uri narrowLink(PerAccountStore store, Narrow narrow, {int? nearMessageId}) {
6060
// TODO(server-7)
61-
final apiNarrow = resolveDmElements(
61+
final apiNarrow = resolveApiNarrowElements(
6262
narrow.apiEncode(), store.connection.zulipFeatureLevel!);
6363
final fragment = StringBuffer('narrow');
6464
for (ApiNarrowElement element in apiNarrow) {

test/api/route/messages_test.dart

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ void main() {
176176
test('Narrow.toJson', () {
177177
return FakeApiConnection.with_((connection) async {
178178
void checkNarrow(ApiNarrow narrow, String expected) {
179-
narrow = resolveDmElements(narrow, connection.zulipFeatureLevel!);
179+
narrow = resolveApiNarrowElements(narrow, connection.zulipFeatureLevel!);
180180
check(jsonEncode(narrow)).equals(expected);
181181
}
182182

@@ -263,7 +263,7 @@ void main() {
263263
});
264264
});
265265

266-
test('narrow uses resolveDmElements to encode', () {
266+
test('narrow uses resolveApiNarrowElements to encode', () {
267267
return FakeApiConnection.with_(zulipFeatureLevel: 176, (connection) async {
268268
connection.prepare(json: fakeResult.toJson());
269269
await checkGetMessages(connection,
@@ -650,7 +650,7 @@ void main() {
650650
});
651651
});
652652

653-
test('narrow uses resolveDmElements to encode', () {
653+
test('narrow uses resolveApiNarrowElements to encode', () {
654654
return FakeApiConnection.with_(zulipFeatureLevel: 176, (connection) async {
655655
connection.prepare(json: mkResult(foundOldest: true).toJson());
656656
await checkUpdateMessageFlagsForNarrow(connection,

0 commit comments

Comments
 (0)