-
Notifications
You must be signed in to change notification settings - Fork 310
action_sheet: Add "Mark as unread from here" button #779
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
7392ccb
msglist: Ensure markNarrowAsRead passes before acting on it
Khader-1 2a734c7
actions [nfc]: Port generic error handling to `markNarrowAsRead`
Khader-1 367e82e
actions [nfc]: Port handleAllMessagesReadSuccess to markNarrowAsRead
Khader-1 28f5c2c
actions [nfc]: Move legacy-server check inside markNarrowAsRead
Khader-1 5ed1886
actions [nfc]: Factor out `updateMessageFlagsStartingFromAnchor`
Khader-1 5bb673f
actions test [nfc]: Move most markNarrowAsRead tests to updateMessage…
Khader-1 4788a63
action_sheet: Add "Mark as unread from here" button
Khader-1 929783d
actions [nfc]: Simplify includeAnchor logic slightly in update-flags …
gnprice b077b66
actions [nfc]: Simplify name of anchor parameter to update-flags
gnprice 4e25bc8
actions [nfc]: Revise doc comment for updateMessageFlagsStartingFromA…
gnprice File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,101 +16,174 @@ import '../model/narrow.dart'; | |
import 'dialog.dart'; | ||
import 'store.dart'; | ||
|
||
Future<void> markNarrowAsRead( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: see previous commits for what summary-line prefixes are used on a given file, and match those
See https://github.com/zulip/zulip-mobile/blob/main/docs/howto/git.md for tips on reading history, including filtering to changes touching a file. |
||
BuildContext context, | ||
Narrow narrow, | ||
bool useLegacy, // TODO(server-6) | ||
) async { | ||
Future<void> markNarrowAsRead(BuildContext context, Narrow narrow) async { | ||
final store = PerAccountStoreWidget.of(context); | ||
final connection = store.connection; | ||
final zulipLocalizations = ZulipLocalizations.of(context); | ||
final useLegacy = connection.zulipFeatureLevel! < 155; // TODO(server-6) | ||
if (useLegacy) { | ||
return await _legacyMarkNarrowAsRead(context, narrow); | ||
try { | ||
await _legacyMarkNarrowAsRead(context, narrow); | ||
return; | ||
} catch (e) { | ||
if (!context.mounted) return; | ||
await showErrorDialog(context: context, | ||
title: zulipLocalizations.errorMarkAsReadFailedTitle, | ||
message: e.toString()); // TODO(#741): extract user-facing message better | ||
return; | ||
} | ||
} | ||
|
||
final didPass = await updateMessageFlagsStartingFromAnchor( | ||
context: context, | ||
// 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`. | ||
apiNarrow: narrow.apiEncode()..add(ApiNarrowIsUnread()), | ||
// Use [AnchorCode.oldest], because [AnchorCode.firstUnread] | ||
// will be the oldest non-muted unread message, which would | ||
// result in muted unreads older than the first unread not | ||
// being processed. | ||
anchor: AnchorCode.oldest, | ||
// [AnchorCode.oldest] is an anchor ID lower than any valid | ||
// message ID. | ||
includeAnchor: false, | ||
op: UpdateMessageFlagsOp.add, | ||
flag: MessageFlag.read, | ||
onCompletedMessage: zulipLocalizations.markAsReadComplete, | ||
progressMessage: zulipLocalizations.markAsReadInProgress, | ||
onFailedTitle: zulipLocalizations.errorMarkAsReadFailedTitle); | ||
|
||
if (!didPass || !context.mounted) return; | ||
if (narrow is CombinedFeedNarrow) { | ||
PerAccountStoreWidget.of(context).unreads.handleAllMessagesReadSuccess(); | ||
} | ||
} | ||
|
||
// Compare web's `mark_all_as_read` in web/src/unread_ops.js | ||
// and zulip-mobile's `markAsUnreadFromMessage` in src/action-sheets/index.js . | ||
Future<void> markNarrowAsUnreadFromMessage( | ||
BuildContext context, | ||
Message message, | ||
Narrow narrow, | ||
) async { | ||
final connection = PerAccountStoreWidget.of(context).connection; | ||
assert(connection.zulipFeatureLevel! >= 155); // TODO(server-6) | ||
final zulipLocalizations = ZulipLocalizations.of(context); | ||
final scaffoldMessenger = ScaffoldMessenger.of(context); | ||
// Use [AnchorCode.oldest], because [AnchorCode.firstUnread] | ||
// will be the oldest non-muted unread message, which would | ||
// result in muted unreads older than the first unread not | ||
// being processed. | ||
Anchor anchor = AnchorCode.oldest; | ||
int responseCount = 0; | ||
int updatedCount = 0; | ||
await updateMessageFlagsStartingFromAnchor( | ||
Khader-1 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
context: context, | ||
apiNarrow: narrow.apiEncode(), | ||
anchor: NumericAnchor(message.id), | ||
includeAnchor: true, | ||
op: UpdateMessageFlagsOp.remove, | ||
flag: MessageFlag.read, | ||
onCompletedMessage: zulipLocalizations.markAsUnreadComplete, | ||
progressMessage: zulipLocalizations.markAsUnreadInProgress, | ||
onFailedTitle: zulipLocalizations.errorMarkAsUnreadFailedTitle); | ||
} | ||
|
||
// 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()); | ||
/// Add or remove the given flag from the anchor to the end of the narrow, | ||
/// showing feedback to the user on progress or failure. | ||
/// | ||
/// This has the semantics of [updateMessageFlagsForNarrow] | ||
/// (see https://zulip.com/api/update-message-flags-for-narrow) | ||
/// with `numBefore: 0` and infinite `numAfter`. It operates by calling that | ||
/// endpoint with a finite `numAfter` as a batch size, in a loop. | ||
/// | ||
/// If the operation requires more than one batch, the user is shown progress | ||
/// feedback through [SnackBar], using [progressMessage] and [onCompletedMessage]. | ||
/// If the operation fails, the user is shown an error dialog box with title | ||
/// [onFailedTitle]. | ||
/// | ||
/// Returns true just if the operation finished successfully. | ||
Future<bool> updateMessageFlagsStartingFromAnchor({ | ||
Khader-1 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
required BuildContext context, | ||
required List<ApiNarrowElement> apiNarrow, | ||
required Anchor anchor, | ||
required bool includeAnchor, | ||
required UpdateMessageFlagsOp op, | ||
required MessageFlag flag, | ||
required String Function(int) onCompletedMessage, | ||
required String progressMessage, | ||
required String onFailedTitle, | ||
}) async { | ||
try { | ||
final store = PerAccountStoreWidget.of(context); | ||
final connection = store.connection; | ||
final scaffoldMessenger = ScaffoldMessenger.of(context); | ||
|
||
while (true) { | ||
final result = await updateMessageFlagsForNarrow(connection, | ||
anchor: anchor, | ||
// [AnchorCode.oldest] is an anchor ID lower than any valid | ||
// message ID; and follow-up requests will have already | ||
// processed the anchor ID, so we just want this to be | ||
// unconditionally false. | ||
includeAnchor: false, | ||
// There is an upper limit of 5000 messages per batch | ||
// (numBefore + numAfter <= 5000) enforced on the server. | ||
// See `update_message_flags_in_narrow` in zerver/views/message_flags.py . | ||
// zulip-mobile uses `numAfter` of 5000, but web uses 1000 | ||
// for more responsive feedback. See zulip@f0d87fcf6. | ||
numBefore: 0, | ||
numAfter: 1000, | ||
narrow: apiNarrow, | ||
op: UpdateMessageFlagsOp.add, | ||
flag: MessageFlag.read); | ||
if (!context.mounted) { | ||
scaffoldMessenger.clearSnackBars(); | ||
return; | ||
} | ||
responseCount++; | ||
updatedCount += result.updatedCount; | ||
// Compare web's `mark_all_as_read` in web/src/unread_ops.js | ||
// and zulip-mobile's `markAsUnreadFromMessage` in src/action-sheets/index.js . | ||
int responseCount = 0; | ||
int updatedCount = 0; | ||
while (true) { | ||
final result = await updateMessageFlagsForNarrow(connection, | ||
anchor: anchor, | ||
includeAnchor: includeAnchor, | ||
// There is an upper limit of 5000 messages per batch | ||
// (numBefore + numAfter <= 5000) enforced on the server. | ||
// See `update_message_flags_in_narrow` in zerver/views/message_flags.py . | ||
// zulip-mobile uses `numAfter` of 5000, but web uses 1000 | ||
// for more responsive feedback. See zulip@f0d87fcf6. | ||
numBefore: 0, | ||
numAfter: 1000, | ||
narrow: apiNarrow, | ||
op: op, | ||
flag: flag); | ||
if (!context.mounted) { | ||
scaffoldMessenger.clearSnackBars(); | ||
return false; | ||
} | ||
responseCount++; | ||
updatedCount += result.updatedCount; | ||
|
||
if (result.foundNewest) { | ||
if (responseCount > 1) { | ||
// We previously showed an in-progress [SnackBar], so say we're done. | ||
// There may be a backlog of [SnackBar]s accumulated in the queue | ||
// so be sure to clear them out here. | ||
scaffoldMessenger | ||
..clearSnackBars() | ||
..showSnackBar(SnackBar(behavior: SnackBarBehavior.floating, | ||
content: Text(zulipLocalizations.markAsReadComplete(updatedCount)))); | ||
if (result.foundNewest) { | ||
if (responseCount > 1) { | ||
// We previously showed an in-progress [SnackBar], so say we're done. | ||
// There may be a backlog of [SnackBar]s accumulated in the queue | ||
// so be sure to clear them out here. | ||
scaffoldMessenger | ||
..clearSnackBars() | ||
..showSnackBar(SnackBar(behavior: SnackBarBehavior.floating, | ||
content: Text(onCompletedMessage(updatedCount)))); | ||
} | ||
return true; | ||
} | ||
return; | ||
} | ||
|
||
if (result.lastProcessedId == null) { | ||
// No messages were in the range of the request. | ||
// This should be impossible given that `foundNewest` was false | ||
// (and that our `numAfter` was positive.) | ||
await showErrorDialog(context: context, | ||
title: zulipLocalizations.errorMarkAsReadFailedTitle, | ||
message: zulipLocalizations.errorInvalidResponse); | ||
return; | ||
} | ||
anchor = NumericAnchor(result.lastProcessedId!); | ||
if (result.lastProcessedId == null) { | ||
final zulipLocalizations = ZulipLocalizations.of(context); | ||
// No messages were in the range of the request. | ||
// This should be impossible given that `foundNewest` was false | ||
// (and that our `numAfter` was positive.) | ||
showErrorDialog(context: context, | ||
title: onFailedTitle, | ||
message: zulipLocalizations.errorInvalidResponse); | ||
return false; | ||
} | ||
anchor = NumericAnchor(result.lastProcessedId!); | ||
includeAnchor = false; | ||
|
||
// The task is taking a while, so tell the user we're working on it. | ||
// No need to say how many messages, as the [MarkAsUnread] widget | ||
// should follow along. | ||
// TODO: Ideally we'd have a progress widget here that showed up based | ||
// on actual time elapsed -- so it could appear before the first | ||
// batch returns, if that takes a while -- and that then stuck | ||
// around continuously until the task ends. For now we use a | ||
// series of [SnackBar]s, which may feel a bit janky. | ||
// There is complexity in tracking the status of each [SnackBar], | ||
// due to having no way to determine which is currently active, | ||
// or if there is an active one at all. Resetting the [SnackBar] here | ||
// results in the same message popping in and out and the user experience | ||
// is better for now if we allow them to run their timer through | ||
// and clear the backlog later. | ||
scaffoldMessenger.showSnackBar(SnackBar(behavior: SnackBarBehavior.floating, | ||
content: Text(zulipLocalizations.markAsReadInProgress))); | ||
// The task is taking a while, so tell the user we're working on it. | ||
// TODO: Ideally we'd have a progress widget here that showed up based | ||
// on actual time elapsed -- so it could appear before the first | ||
// batch returns, if that takes a while -- and that then stuck | ||
// around continuously until the task ends. For now we use a | ||
// series of [SnackBar]s, which may feel a bit janky. | ||
// There is complexity in tracking the status of each [SnackBar], | ||
// due to having no way to determine which is currently active, | ||
// or if there is an active one at all. Resetting the [SnackBar] here | ||
// results in the same message popping in and out and the user experience | ||
// is better for now if we allow them to run their timer through | ||
// and clear the backlog later. | ||
scaffoldMessenger.showSnackBar(SnackBar(behavior: SnackBarBehavior.floating, | ||
content: Text(progressMessage))); | ||
} | ||
} catch (e) { | ||
if (!context.mounted) return false; | ||
showErrorDialog(context: context, | ||
title: onFailedTitle, | ||
message: e.toString()); // TODO(#741): extract user-facing message better | ||
return false; | ||
} | ||
} | ||
|
||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.