From c85c333c90431288806bad0262f11c7f66e05c5d Mon Sep 17 00:00:00 2001 From: Aman Agrawal Date: Mon, 20 Nov 2023 05:02:29 +0000 Subject: [PATCH] narrow: Fix `near:ID` narrows with invalid stream name in hash. Fixes #27622 If target message ID is valid but the stream name is invalid, we fix the stream name in the URL and narrow the target message. --- web/src/narrow.js | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/web/src/narrow.js b/web/src/narrow.js index 88860c94f6..83938ff143 100644 --- a/web/src/narrow.js +++ b/web/src/narrow.js @@ -245,7 +245,29 @@ export function activate(raw_operators, opts) { // to where the message is located now. const narrow_topic = filter.operands("topic")[0]; const narrow_stream_name = filter.operands("stream")[0]; - const narrow_stream_id = stream_data.get_sub(narrow_stream_name).stream_id; + const narrow_stream_data = stream_data.get_sub(narrow_stream_name); + if (!narrow_stream_data) { + // The id of the target message is correct but the stream name is + // incorrect in the URL. We reconstruct the narrow with the correct + // stream name and narrow. + const adjusted_operators = adjusted_operators_if_moved( + raw_operators, + target_message, + ); + + if (adjusted_operators === null) { + blueslip.error("adjusted_operators impossibly null"); + return; + } + + activate(adjusted_operators, { + ...opts, + // Update the URL fragment to reflect the redirect. + change_hash: true, + }); + return; + } + const narrow_stream_id = narrow_stream_data.stream_id; const narrow_dict = {stream_id: narrow_stream_id, topic: narrow_topic}; const narrow_exists_in_edit_history =