diff --git a/web/src/narrow_state.js b/web/src/narrow_state.js index 5daec76896..a1e8bc7a1d 100644 --- a/web/src/narrow_state.js +++ b/web/src/narrow_state.js @@ -88,7 +88,13 @@ export function set_compose_defaults() { // if they are uniquely specified in the narrow view. if (single.has("stream")) { - opts.stream = stream_data.get_name(single.get("stream")); + // The raw stream name from collect_single may be an arbitrary + // unvalidated string from the URL fragment and thus not be valid. + // So we look up the resolved stream and return that if appropriate. + const sub = stream_sub(); + if (sub !== undefined) { + opts.stream = sub.name; + } } if (single.has("topic")) { diff --git a/web/tests/narrow_state.test.js b/web/tests/narrow_state.test.js index e78e37ef89..12b62920bb 100644 --- a/web/tests/narrow_state.test.js +++ b/web/tests/narrow_state.test.js @@ -174,7 +174,14 @@ test("set_compose_defaults", () => { ["topic", "Bar"], ]); - const stream_and_topic = narrow_state.set_compose_defaults(); + // First try with a stream that doesn't exist. + let stream_and_topic = narrow_state.set_compose_defaults(); + assert.equal(stream_and_topic.stream, undefined); + assert.equal(stream_and_topic.topic, "Bar"); + + const test_stream = {name: "Foo", stream_id: 72}; + stream_data.add_sub(test_stream); + stream_and_topic = narrow_state.set_compose_defaults(); assert.equal(stream_and_topic.stream, "Foo"); assert.equal(stream_and_topic.topic, "Bar");