From 0c4653d4f4d8b98f4b12da0bd38d87a24eff30c9 Mon Sep 17 00:00:00 2001 From: Shu Chen Date: Wed, 29 Nov 2023 15:58:57 +0000 Subject: [PATCH] msglist: Unconditionally add ApiNarrowIsUnread in markNarrowAsRead Fixes: #377 --- lib/widgets/message_list.dart | 14 +++++++------- test/widgets/message_list_test.dart | 12 ++++++++---- 2 files changed, 15 insertions(+), 11 deletions(-) diff --git a/lib/widgets/message_list.dart b/lib/widgets/message_list.dart index 32d34ca147..9afc9aa425 100644 --- a/lib/widgets/message_list.dart +++ b/lib/widgets/message_list.dart @@ -824,13 +824,13 @@ Future 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(), - }; + // Include `is:unread` in the narrow. That has a database index, so + // this can be an important optimization in narrows with a lot of history. + // The server applies the same optimization within 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, diff --git a/test/widgets/message_list_test.dart b/test/widgets/message_list_test.dart index 048233c24d..389ed27ea5 100644 --- a/test/widgets/message_list_test.dart +++ b/test/widgets/message_list_test.dart @@ -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'; @@ -589,6 +590,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() ..method.equals('POST') ..url.path.equals('/api/v1/messages/flags/narrow') @@ -597,7 +599,7 @@ void main() { 'include_anchor': 'false', 'num_before': '0', 'num_after': '1000', - 'narrow': jsonEncode(narrow.apiEncode()), + 'narrow': jsonEncode(apiNarrow), 'op': 'add', 'flag': 'read', }); @@ -647,6 +649,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() ..method.equals('POST') ..url.path.equals('/api/v1/messages/flags/narrow') @@ -655,7 +658,7 @@ void main() { 'include_anchor': 'false', 'num_before': '0', 'num_after': '1000', - 'narrow': jsonEncode(narrow.apiEncode()), + 'narrow': jsonEncode(apiNarrow), 'op': 'add', 'flag': 'read', }); @@ -674,7 +677,7 @@ void main() { 'include_anchor': 'false', 'num_before': '0', 'num_after': '1000', - 'narrow': jsonEncode(narrow.apiEncode()), + 'narrow': jsonEncode(apiNarrow), 'op': 'add', 'flag': 'read', }); @@ -693,6 +696,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() ..method.equals('POST') ..url.path.equals('/api/v1/messages/flags/narrow') @@ -701,7 +705,7 @@ void main() { 'include_anchor': 'false', 'num_before': '0', 'num_after': '1000', - 'narrow': jsonEncode(narrow.apiEncode()), + 'narrow': jsonEncode(apiNarrow), 'op': 'add', 'flag': 'read', });