Skip to content

Commit e39e845

Browse files
committed
internal_link [nfc]: Add ApiIsNarrow.
This replaces ApiNarrowIsMentioned and ApiNarrowIsUnread with the generic ApiNarrowIs element. This does not change the internal links behavior, hence the nfc. This is also why this commit hasn't introduced new tests yet. Signed-off-by: Zixuan James Li <[email protected]>
1 parent 7b4a5df commit e39e845

File tree

7 files changed

+68
-20
lines changed

7 files changed

+68
-20
lines changed

lib/api/model/narrow.dart

Lines changed: 38 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
import 'package:json_annotation/json_annotation.dart';
2+
3+
part 'narrow.g.dart';
4+
15
typedef ApiNarrow = List<ApiNarrowElement>;
26

37
/// Resolve any [ApiNarrowDm] elements appropriately.
@@ -114,19 +118,44 @@ class ApiNarrowPmWith extends ApiNarrowDm {
114118
ApiNarrowPmWith._(super.operand, {super.negated});
115119
}
116120

117-
// TODO: generalize into ApiNarrowIs
118-
class ApiNarrowIsMentioned extends ApiNarrowElement {
121+
class ApiNarrowIs extends ApiNarrowElement {
119122
@override String get operator => 'is';
120-
@override String get operand => 'mentioned';
121123

122-
ApiNarrowIsMentioned({super.negated});
123-
}
124+
@override final IsOperand operand;
124125

125-
class ApiNarrowIsUnread extends ApiNarrowElement {
126-
@override String get operator => 'is';
127-
@override String get operand => 'unread';
126+
ApiNarrowIs(this.operand, {super.negated});
128127

129-
ApiNarrowIsUnread({super.negated});
128+
factory ApiNarrowIs.fromJson(Map<String, dynamic> json) => ApiNarrowIs(
129+
IsOperand.fromRawString(json['operand'] as String),
130+
negated: json['negated'] as bool? ?? false,
131+
);
132+
}
133+
134+
/// An operand value of "is" operator.
135+
///
136+
/// See also:
137+
/// - https://zulip.com/api/construct-narrow
138+
/// - https://zulip.com/help/search-for-messages#search-your-important-messages
139+
/// - https://zulip.com/help/search-for-messages#search-by-message-status
140+
@JsonEnum(alwaysCreate: true)
141+
enum IsOperand {
142+
dm, // TODO(server-7) new in FL 177
143+
private, // TODO(server-7) deprecated in FL 177, equivalent to [dm].
144+
alerted,
145+
mentioned,
146+
starred,
147+
followed, // TODO(server-9) new in FL 265
148+
resolved,
149+
unread,
150+
unknown;
151+
152+
static IsOperand fromRawString(String raw) => $enumDecode(
153+
_$IsOperandEnumMap, raw, unknownValue: unknown);
154+
155+
@override
156+
String toString() => _$IsOperandEnumMap[this]!;
157+
158+
String toJson() => toString();
130159
}
131160

132161
class ApiNarrowMessageId extends ApiNarrowElement {

lib/api/model/narrow.g.dart

Lines changed: 21 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/model/internal_link.dart

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,7 @@ Uri narrowLink(PerAccountStore store, Narrow narrow, {int? nearMessageId}) {
8585
fragment.write('${element.operand.join(',')}-$suffix');
8686
case ApiNarrowDm():
8787
assert(false, 'ApiNarrowDm should have been resolved');
88-
case ApiNarrowIsMentioned():
89-
fragment.write(element.operand.toString());
90-
case ApiNarrowIsUnread():
88+
case ApiNarrowIs():
9189
fragment.write(element.operand.toString());
9290
case ApiNarrowMessageId():
9391
fragment.write(element.operand.toString());
@@ -154,7 +152,7 @@ Narrow? _interpretNarrowSegments(List<String> segments, PerAccountStore store) {
154152
ApiNarrowStream? streamElement;
155153
ApiNarrowTopic? topicElement;
156154
ApiNarrowDm? dmElement;
157-
ApiNarrowIsMentioned? isMentionedElement;
155+
ApiNarrowIs? isMentionedElement;
158156

159157
for (var i = 0; i < segments.length; i += 2) {
160158
final (operator, negated) = _parseOperator(segments[i]);
@@ -185,7 +183,7 @@ Narrow? _interpretNarrowSegments(List<String> segments, PerAccountStore store) {
185183
case _NarrowOperator.is_:
186184
if (isMentionedElement != null) return null;
187185
if (operand == 'mentioned') {
188-
isMentionedElement = ApiNarrowIsMentioned();
186+
isMentionedElement = ApiNarrowIs(IsOperand.mentioned);
189187
} else {
190188
return null;
191189
}

lib/model/narrow.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,7 @@ class MentionsNarrow extends Narrow {
309309
}
310310

311311
@override
312-
ApiNarrow apiEncode() => [ApiNarrowIsMentioned()];
312+
ApiNarrow apiEncode() => [ApiNarrowIs(IsOperand.mentioned)];
313313

314314
@override
315315
bool operator ==(Object other) {

lib/widgets/actions.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ Future<void> markNarrowAsRead(
4444
// The server applies the same optimization within the (deprecated)
4545
// specialized endpoints for marking messages as read; see
4646
// `do_mark_stream_messages_as_read` in `zulip:zerver/actions/message_flags.py`.
47-
final apiNarrow = narrow.apiEncode()..add(ApiNarrowIsUnread());
47+
final apiNarrow = narrow.apiEncode()..add(ApiNarrowIs(IsOperand.unread));
4848

4949
while (true) {
5050
final result = await updateMessageFlagsForNarrow(connection,

test/widgets/actions_test.dart

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ void main() {
5454
foundOldest: true, foundNewest: true).toJson());
5555
markNarrowAsRead(context, narrow, false);
5656
await tester.pump(Duration.zero);
57-
final apiNarrow = narrow.apiEncode()..add(ApiNarrowIsUnread());
57+
final apiNarrow = narrow.apiEncode()..add(ApiNarrowIs(IsOperand.unread));
5858
check(connection.lastRequest).isA<http.Request>()
5959
..method.equals('POST')
6060
..url.path.equals('/api/v1/messages/flags/narrow')
@@ -104,7 +104,7 @@ void main() {
104104
firstProcessedId: 1, lastProcessedId: 1989,
105105
foundOldest: true, foundNewest: false).toJson());
106106
markNarrowAsRead(context, narrow, false);
107-
final apiNarrow = narrow.apiEncode()..add(ApiNarrowIsUnread());
107+
final apiNarrow = narrow.apiEncode()..add(ApiNarrowIs(IsOperand.unread));
108108
check(connection.lastRequest).isA<http.Request>()
109109
..method.equals('POST')
110110
..url.path.equals('/api/v1/messages/flags/narrow')
@@ -164,7 +164,7 @@ void main() {
164164
foundOldest: true, foundNewest: false).toJson());
165165
markNarrowAsRead(context, narrow, false);
166166
await tester.pump(Duration.zero);
167-
final apiNarrow = narrow.apiEncode()..add(ApiNarrowIsUnread());
167+
final apiNarrow = narrow.apiEncode()..add(ApiNarrowIs(IsOperand.unread));
168168
check(connection.lastRequest).isA<http.Request>()
169169
..method.equals('POST')
170170
..url.path.equals('/api/v1/messages/flags/narrow')

test/widgets/message_list_test.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -536,7 +536,7 @@ void main() {
536536
firstProcessedId: null, lastProcessedId: null,
537537
foundOldest: true, foundNewest: true).toJson());
538538
await tester.tap(find.byType(MarkAsReadWidget));
539-
final apiNarrow = narrow.apiEncode()..add(ApiNarrowIsUnread());
539+
final apiNarrow = narrow.apiEncode()..add(ApiNarrowIs(IsOperand.unread));
540540
check(connection.lastRequest).isA<http.Request>()
541541
..method.equals('POST')
542542
..url.path.equals('/api/v1/messages/flags/narrow')

0 commit comments

Comments
 (0)