Skip to content

Commit 7ea7424

Browse files
committed
api [nfc]: Rename resolveDmElements to resolveApiNarrowForServer
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 26073e8 commit 7ea7424

File tree

4 files changed

+23
-13
lines changed

4 files changed

+23
-13
lines changed

lib/api/model/narrow.dart

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,24 @@ part 'narrow.g.dart';
66

77
typedef ApiNarrow = List<ApiNarrowElement>;
88

9-
/// Resolve any [ApiNarrowDm] elements appropriately.
9+
/// Adapt the given narrow to be sent to the given Zulip server version.
1010
///
11-
/// This encapsulates a server-feature check.
12-
ApiNarrow resolveDmElements(ApiNarrow narrow, int zulipFeatureLevel) {
13-
if (!narrow.any((element) => element is ApiNarrowDm)) {
14-
return narrow;
15-
}
11+
/// Any elements that take a different name on old vs. new servers
12+
/// will be resolved to the specific name to use.
13+
/// Any elements that are unknown to old servers and can
14+
/// reasonably be omitted will be omitted.
15+
ApiNarrow resolveApiNarrowForServer(ApiNarrow narrow, int zulipFeatureLevel) {
1616
final supportsOperatorDm = zulipFeatureLevel >= 177; // TODO(server-7)
17+
18+
bool hasDmElement = false;
19+
for (final element in narrow) {
20+
switch (element) {
21+
case ApiNarrowDm(): hasDmElement = true;
22+
default:
23+
}
24+
}
25+
if (!hasDmElement) return narrow;
26+
1727
return narrow.map((element) => switch (element) {
1828
ApiNarrowDm() => element.resolve(legacy: !supportsOperatorDm),
1929
_ => element,
@@ -102,7 +112,7 @@ class ApiNarrowTopic extends ApiNarrowElement {
102112
/// and more generally its [operator] getter must not be called.
103113
/// Instead, call [resolve] and use the object it returns.
104114
///
105-
/// If part of [ApiNarrow] use [resolveDmElements].
115+
/// If part of [ApiNarrow] use [resolveApiNarrowForServer].
106116
class ApiNarrowDm extends ApiNarrowElement {
107117
@override String get operator {
108118
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': resolveApiNarrowForServer(narrow, connection.zulipFeatureLevel!),
9595
'anchor': RawParameter(anchor.toJson()),
9696
if (includeAnchor != null) 'include_anchor': includeAnchor,
9797
'num_before': numBefore,
@@ -400,7 +400,7 @@ Future<UpdateMessageFlagsForNarrowResult> updateMessageFlagsForNarrow(ApiConnect
400400
if (includeAnchor != null) 'include_anchor': includeAnchor,
401401
'num_before': numBefore,
402402
'num_after': numAfter,
403-
'narrow': resolveDmElements(narrow, connection.zulipFeatureLevel!),
403+
'narrow': resolveApiNarrowForServer(narrow, connection.zulipFeatureLevel!),
404404
'op': RawParameter(op.toJson()),
405405
'flag': RawParameter(flag.toJson()),
406406
});

lib/model/internal_link.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ String? decodeHashComponent(String str) {
5959
// you do so by passing the `anchor` param.
6060
Uri narrowLink(PerAccountStore store, Narrow narrow, {int? nearMessageId}) {
6161
// TODO(server-7)
62-
final apiNarrow = resolveDmElements(
62+
final apiNarrow = resolveApiNarrowForServer(
6363
narrow.apiEncode(), store.connection.zulipFeatureLevel!);
6464
final fragment = StringBuffer('narrow');
6565
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
@@ -172,7 +172,7 @@ void main() {
172172
test('Narrow.toJson', () {
173173
return FakeApiConnection.with_((connection) async {
174174
void checkNarrow(ApiNarrow narrow, String expected) {
175-
narrow = resolveDmElements(narrow, connection.zulipFeatureLevel!);
175+
narrow = resolveApiNarrowForServer(narrow, connection.zulipFeatureLevel!);
176176
check(jsonEncode(narrow)).equals(expected);
177177
}
178178

@@ -259,7 +259,7 @@ void main() {
259259
});
260260
});
261261

262-
test('narrow uses resolveDmElements to encode', () {
262+
test('narrow uses resolveApiNarrowForServer to encode', () {
263263
return FakeApiConnection.with_(zulipFeatureLevel: 176, (connection) async {
264264
connection.prepare(json: fakeResult.toJson());
265265
await checkGetMessages(connection,
@@ -707,7 +707,7 @@ void main() {
707707
});
708708
});
709709

710-
test('narrow uses resolveDmElements to encode', () {
710+
test('narrow uses resolveApiNarrowForServer to encode', () {
711711
return FakeApiConnection.with_(zulipFeatureLevel: 176, (connection) async {
712712
connection.prepare(json: mkResult(foundOldest: true).toJson());
713713
await checkUpdateMessageFlagsForNarrow(connection,

0 commit comments

Comments
 (0)