Skip to content

Commit 3e797d4

Browse files
committed
api: In getMessages, use "dm" operator where possible
This is the last part of #146. Fixes: #146
1 parent fd54070 commit 3e797d4

File tree

3 files changed

+56
-5
lines changed

3 files changed

+56
-5
lines changed

lib/api/model/narrow.dart

+43-5
Original file line numberDiff line numberDiff line change
@@ -47,15 +47,53 @@ class ApiNarrowTopic extends ApiNarrowElement {
4747
}
4848

4949
/// An [ApiNarrowElement] with the 'dm', or legacy 'pm-with', operator.
50+
///
51+
/// An instance directly of this class must not be serialized with [jsonEncode],
52+
/// and more generally its [operator] getter must not be called.
53+
/// Instead, call [resolve] and use the object it returns.
5054
class ApiNarrowDm extends ApiNarrowElement {
51-
@override String get operator => 'pm-with'; // TODO(#146): use 'dm' where possible
55+
@override String get operator {
56+
assert(false,
57+
"The [operator] getter was called on a plain [ApiNarrowDm]. "
58+
"Before passing to [jsonEncode] or otherwise getting [operator], "
59+
"the [ApiNarrowDm] must be replaced by the result of [ApiNarrowDm.resolve]."
60+
);
61+
return "dm";
62+
}
5263

5364
@override final List<int> operand;
5465

5566
ApiNarrowDm(this.operand, {super.negated});
5667

57-
factory ApiNarrowDm.fromJson(Map<String, dynamic> json) => ApiNarrowDm(
58-
(json['operand'] as List<dynamic>).map((e) => e as int).toList(),
59-
negated: json['negated'] as bool? ?? false,
60-
);
68+
factory ApiNarrowDm.fromJson(Map<String, dynamic> json) {
69+
var operand = (json['operand'] as List<dynamic>).map((e) => e as int).toList();
70+
var negated = json['negated'] as bool? ?? false;
71+
return (json['operator'] == 'pm-with')
72+
? ApiNarrowPmWith._(operand, negated: negated)
73+
: ApiNarrowDmModern._(operand, negated: negated);
74+
}
75+
76+
/// This element resolved, as either an [ApiNarrowDmModern] or an [ApiNarrowPmWith].
77+
ApiNarrowDm resolve({required bool legacy}) {
78+
return legacy ? ApiNarrowPmWith._(operand, negated: negated)
79+
: ApiNarrowDmModern._(operand, negated: negated);
80+
}
81+
}
82+
83+
/// An [ApiNarrowElement] with the 'dm' operator (and not the legacy 'pm-with').
84+
///
85+
/// To construct one of these, use [ApiNarrowDm.resolve].
86+
class ApiNarrowDmModern extends ApiNarrowDm {
87+
@override String get operator => 'dm';
88+
89+
ApiNarrowDmModern._(super.operand, {super.negated});
90+
}
91+
92+
/// An [ApiNarrowElement] with the legacy 'pm-with' operator.
93+
///
94+
/// To construct one of these, use [ApiNarrowDm.resolve].
95+
class ApiNarrowPmWith extends ApiNarrowDm {
96+
@override String get operator => 'pm-with';
97+
98+
ApiNarrowPmWith._(super.operand, {super.negated});
6199
}

lib/api/route/messages.dart

+7
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,13 @@ Future<GetMessagesResult> getMessages(ApiConnection connection, {
1717
bool? applyMarkdown,
1818
// bool? useFirstUnreadAnchor // omitted because deprecated
1919
}) {
20+
if (narrow.any((element) => element is ApiNarrowDm)) {
21+
final supportsOperatorDm = connection.zulipFeatureLevel! >= 177; // TODO(server-7)
22+
narrow = narrow.map((element) => switch (element) {
23+
ApiNarrowDm() => element.resolve(legacy: !supportsOperatorDm),
24+
_ => element,
25+
}).toList();
26+
}
2027
return connection.get('getMessages', GetMessagesResult.fromJson, 'messages', {
2128
'narrow': narrow,
2229
'anchor': switch (anchor) {

test/api/route/messages_test.dart

+6
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import 'package:zulip/api/model/narrow.dart';
77
import 'package:zulip/api/route/messages.dart';
88
import 'package:zulip/model/narrow.dart';
99

10+
import '../../example_data.dart' as eg;
1011
import '../../stdlib_checks.dart';
1112
import '../fake_api.dart';
1213
import 'route_checks.dart';
@@ -79,9 +80,14 @@ void main() {
7980
{'operator': 'topic', 'operand': 'stuff'},
8081
]));
8182

83+
await checkNarrow([ApiNarrowDm([123, 234])], jsonEncode([
84+
{'operator': 'dm', 'operand': [123, 234]},
85+
]));
86+
connection.zulipFeatureLevel = 176;
8287
await checkNarrow([ApiNarrowDm([123, 234])], jsonEncode([
8388
{'operator': 'pm-with', 'operand': [123, 234]},
8489
]));
90+
connection.zulipFeatureLevel = eg.futureZulipFeatureLevel;
8591
});
8692
});
8793

0 commit comments

Comments
 (0)