compose: Fix opening compose box when viewing invalid stream.

This likely needs further refactoring to switch to using stream IDs
rather than names in this code path, but this change fixes an
exception that would be throw when opening the compose box while
viewing a narrow to an invalid stream name/ID.
This commit is contained in:
Tim Abbott
2023-05-12 17:32:22 -07:00
parent ffaccb8af2
commit 3876171df1
2 changed files with 15 additions and 2 deletions

View File

@@ -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")) {

View File

@@ -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");