Skip to content

Commit e27ae7e

Browse files
committed
msglist: Unconditionally add ApiNarrowIsUnread to markNarrowAsRead
Fixes: zulip#377
1 parent b13cf4e commit e27ae7e

File tree

2 files changed

+19
-11
lines changed

2 files changed

+19
-11
lines changed

lib/widgets/message_list.dart

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -734,13 +734,17 @@ Future<void> markNarrowAsRead(BuildContext context, Narrow narrow) async {
734734
int responseCount = 0;
735735
int updatedCount = 0;
736736

737-
final apiNarrow = switch (narrow) {
738-
// Since there's a database index on is:unread, it's a fast
739-
// search query and thus worth using as an optimization
740-
// when processing all messages.
741-
AllMessagesNarrow() => [ApiNarrowIsUnread()],
742-
_ => narrow.apiEncode(),
743-
};
737+
// This process can take a long time in narrows with a lot
738+
// of history given the server ends up processing a large
739+
// number of already read messages. Unconditionally add
740+
// [ApiNarrowIsUnread] here to take advantage of a database
741+
// index on `is:unread`.
742+
// This optimization is also used server side for the
743+
// (deprecated) specialized endpoints for marking messages
744+
// as read. See `do_mark_stream_messages_as_read` in
745+
// `zulip:zerver/actions/message_flags.py`.
746+
final apiNarrow = narrow.apiEncode()..add(ApiNarrowIsUnread());
747+
744748
while (true) {
745749
final result = await updateMessageFlagsForNarrow(connection,
746750
anchor: anchor,

test/widgets/message_list_test.dart

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import 'package:http/http.dart' as http;
99
import 'package:zulip/api/model/events.dart';
1010
import 'package:zulip/api/model/initial_snapshot.dart';
1111
import 'package:zulip/api/model/model.dart';
12+
import 'package:zulip/api/model/narrow.dart';
1213
import 'package:zulip/api/route/messages.dart';
1314
import 'package:zulip/model/localizations.dart';
1415
import 'package:zulip/model/narrow.dart';
@@ -479,6 +480,7 @@ void main() {
479480
firstProcessedId: null, lastProcessedId: null,
480481
foundOldest: true, foundNewest: true).toJson());
481482
await tester.tap(find.byType(MarkAsReadWidget));
483+
final apiNarrow = narrow.apiEncode()..add(ApiNarrowIsUnread());
482484
check(connection.lastRequest).isA<http.Request>()
483485
..method.equals('POST')
484486
..url.path.equals('/api/v1/messages/flags/narrow')
@@ -487,7 +489,7 @@ void main() {
487489
'include_anchor': 'false',
488490
'num_before': '0',
489491
'num_after': '1000',
490-
'narrow': jsonEncode(narrow.apiEncode()),
492+
'narrow': jsonEncode(apiNarrow),
491493
'op': 'add',
492494
'flag': 'read',
493495
});
@@ -537,6 +539,7 @@ void main() {
537539
firstProcessedId: 1, lastProcessedId: 1989,
538540
foundOldest: true, foundNewest: false).toJson());
539541
await tester.tap(find.byType(MarkAsReadWidget));
542+
final apiNarrow = narrow.apiEncode()..add(ApiNarrowIsUnread());
540543
check(connection.lastRequest).isA<http.Request>()
541544
..method.equals('POST')
542545
..url.path.equals('/api/v1/messages/flags/narrow')
@@ -545,7 +548,7 @@ void main() {
545548
'include_anchor': 'false',
546549
'num_before': '0',
547550
'num_after': '1000',
548-
'narrow': jsonEncode(narrow.apiEncode()),
551+
'narrow': jsonEncode(apiNarrow),
549552
'op': 'add',
550553
'flag': 'read',
551554
});
@@ -564,7 +567,7 @@ void main() {
564567
'include_anchor': 'false',
565568
'num_before': '0',
566569
'num_after': '1000',
567-
'narrow': jsonEncode(narrow.apiEncode()),
570+
'narrow': jsonEncode(apiNarrow),
568571
'op': 'add',
569572
'flag': 'read',
570573
});
@@ -583,6 +586,7 @@ void main() {
583586
firstProcessedId: null, lastProcessedId: null,
584587
foundOldest: true, foundNewest: false).toJson());
585588
await tester.tap(find.byType(MarkAsReadWidget));
589+
final apiNarrow = narrow.apiEncode()..add(ApiNarrowIsUnread());
586590
check(connection.lastRequest).isA<http.Request>()
587591
..method.equals('POST')
588592
..url.path.equals('/api/v1/messages/flags/narrow')
@@ -591,7 +595,7 @@ void main() {
591595
'include_anchor': 'false',
592596
'num_before': '0',
593597
'num_after': '1000',
594-
'narrow': jsonEncode(narrow.apiEncode()),
598+
'narrow': jsonEncode(apiNarrow),
595599
'op': 'add',
596600
'flag': 'read',
597601
});

0 commit comments

Comments
 (0)