Skip to content

Commit

Permalink
msglist: Unconditionally add ApiNarrowIsUnread to markNarrowAsRead
Browse files Browse the repository at this point in the history
Fixes: zulip#377
  • Loading branch information
sirpengi committed Nov 29, 2023
1 parent b13cf4e commit e27ae7e
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 11 deletions.
18 changes: 11 additions & 7 deletions lib/widgets/message_list.dart
Original file line number Diff line number Diff line change
Expand Up @@ -734,13 +734,17 @@ Future<void> markNarrowAsRead(BuildContext context, Narrow narrow) async {
int responseCount = 0;
int updatedCount = 0;

final apiNarrow = switch (narrow) {
// Since there's a database index on is:unread, it's a fast
// search query and thus worth using as an optimization
// when processing all messages.
AllMessagesNarrow() => [ApiNarrowIsUnread()],
_ => narrow.apiEncode(),
};
// This process can take a long time in narrows with a lot
// of history given the server ends up processing a large
// number of already read messages. Unconditionally add
// [ApiNarrowIsUnread] here to take advantage of a database
// index on `is:unread`.
// This optimization is also used server side for the
// (deprecated) specialized endpoints for marking messages
// as read. See `do_mark_stream_messages_as_read` in
// `zulip:zerver/actions/message_flags.py`.
final apiNarrow = narrow.apiEncode()..add(ApiNarrowIsUnread());

while (true) {
final result = await updateMessageFlagsForNarrow(connection,
anchor: anchor,
Expand Down
12 changes: 8 additions & 4 deletions test/widgets/message_list_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import 'package:http/http.dart' as http;
import 'package:zulip/api/model/events.dart';
import 'package:zulip/api/model/initial_snapshot.dart';
import 'package:zulip/api/model/model.dart';
import 'package:zulip/api/model/narrow.dart';
import 'package:zulip/api/route/messages.dart';
import 'package:zulip/model/localizations.dart';
import 'package:zulip/model/narrow.dart';
Expand Down Expand Up @@ -479,6 +480,7 @@ void main() {
firstProcessedId: null, lastProcessedId: null,
foundOldest: true, foundNewest: true).toJson());
await tester.tap(find.byType(MarkAsReadWidget));
final apiNarrow = narrow.apiEncode()..add(ApiNarrowIsUnread());
check(connection.lastRequest).isA<http.Request>()
..method.equals('POST')
..url.path.equals('/api/v1/messages/flags/narrow')
Expand All @@ -487,7 +489,7 @@ void main() {
'include_anchor': 'false',
'num_before': '0',
'num_after': '1000',
'narrow': jsonEncode(narrow.apiEncode()),
'narrow': jsonEncode(apiNarrow),
'op': 'add',
'flag': 'read',
});
Expand Down Expand Up @@ -537,6 +539,7 @@ void main() {
firstProcessedId: 1, lastProcessedId: 1989,
foundOldest: true, foundNewest: false).toJson());
await tester.tap(find.byType(MarkAsReadWidget));
final apiNarrow = narrow.apiEncode()..add(ApiNarrowIsUnread());
check(connection.lastRequest).isA<http.Request>()
..method.equals('POST')
..url.path.equals('/api/v1/messages/flags/narrow')
Expand All @@ -545,7 +548,7 @@ void main() {
'include_anchor': 'false',
'num_before': '0',
'num_after': '1000',
'narrow': jsonEncode(narrow.apiEncode()),
'narrow': jsonEncode(apiNarrow),
'op': 'add',
'flag': 'read',
});
Expand All @@ -564,7 +567,7 @@ void main() {
'include_anchor': 'false',
'num_before': '0',
'num_after': '1000',
'narrow': jsonEncode(narrow.apiEncode()),
'narrow': jsonEncode(apiNarrow),
'op': 'add',
'flag': 'read',
});
Expand All @@ -583,6 +586,7 @@ void main() {
firstProcessedId: null, lastProcessedId: null,
foundOldest: true, foundNewest: false).toJson());
await tester.tap(find.byType(MarkAsReadWidget));
final apiNarrow = narrow.apiEncode()..add(ApiNarrowIsUnread());
check(connection.lastRequest).isA<http.Request>()
..method.equals('POST')
..url.path.equals('/api/v1/messages/flags/narrow')
Expand All @@ -591,7 +595,7 @@ void main() {
'include_anchor': 'false',
'num_before': '0',
'num_after': '1000',
'narrow': jsonEncode(narrow.apiEncode()),
'narrow': jsonEncode(apiNarrow),
'op': 'add',
'flag': 'read',
});
Expand Down

0 comments on commit e27ae7e

Please sign in to comment.