Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
compose: Use compose_state.stream_id when name isn't needed.
evykassirer71165264929f
diff --git a/web/src/compose.js b/web/src/compose.jsindex a4a84bf23e4..d57bd6e0fe5 100644--- a/web/src/compose.js+++ b/web/src/compose.js@@ -517,11 +517,11 @@ export function initialize() {(event) => {event.preventDefault();- const stream_name = compose_state.stream_name();- if (stream_name === "") {+ const stream_id = compose_state.stream_id();+ if (stream_id === "") {return;}- const sub = stream_data.get_sub(stream_name);+ const sub = stream_data.get_sub_by_id(stream_id);stream_settings_ui.sub_or_unsub(sub);$(user_not_subscribed_selector).remove();},diff --git a/web/src/compose_recipient.js b/web/src/compose_recipient.jsindex bbc3979e7e5..a75e4bf8338 100644--- a/web/src/compose_recipient.js+++ b/web/src/compose_recipient.js@@ -63,8 +63,7 @@ function composing_to_current_private_message_narrow() {export function update_narrow_to_recipient_visibility() {const message_type = compose_state.get_message_type();if (message_type === "stream") {- const stream_name = compose_state.stream_name();- const stream_exists = Boolean(stream_data.get_stream_id(stream_name));+ const stream_exists = Boolean(compose_state.stream_id());if (stream_exists &&diff --git a/web/src/compose_state.js b/web/src/compose_state.jsindex b22befb1f1c..7e258847be4 100644--- a/web/src/compose_state.js+++ b/web/src/compose_state.js@@ -72,6 +72,14 @@ function get_or_set(fieldname, keep_leading_whitespace, no_trim) {// NOTE: See `selected_recipient_id` in compose_recipient to for// documentation on the variable and how it is used.+export function stream_id() {+ const stream_id = compose_recipient.selected_recipient_id;+ if (typeof stream_id === "number") {+ return stream_id;+ }+ return "";+}+export function stream_name() {const stream_id = compose_recipient.selected_recipient_id;if (typeof stream_id === "number") {diff --git a/web/src/compose_validate.js b/web/src/compose_validate.jsindex 3aa6e5a6150..61ec72161c5 100644--- a/web/src/compose_validate.js+++ b/web/src/compose_validate.js@@ -79,13 +79,7 @@ function get_stream_id_for_textarea($textarea) {return Number.parseInt(stream_id_str, 10);}- const stream_name = compose_state.stream_name();-- if (!stream_name) {- return undefined;- }-- return stream_data.get_sub(stream_name).stream_id;+ return compose_state.stream_id() || undefined;}export function warn_if_private_stream_is_linked(linked_stream, $textarea) {@@ -216,9 +210,9 @@ export function warn_if_topic_resolved(topic_changed) {return;}- const stream_name = compose_state.stream_name();+ const stream_id = compose_state.stream_id();const message_content = compose_state.message_content();- const sub = stream_data.get_sub(stream_name);+ const sub = stream_data.get_sub_by_id(stream_id);if (sub && message_content !== "" && resolved_topic.is_resolved(topic_name)) {if (compose_state.has_recipient_viewed_topic_resolved_banner()) {@@ -484,9 +478,9 @@ export function validate_stream_message_address_info(stream_name) {}function validate_stream_message(scheduling_message) {- const stream_name = compose_state.stream_name();+ const stream_id = compose_state.stream_id();const $banner_container = $("#compose_banners");- if (stream_name === "") {+ if (stream_id === "") {compose_banner.show_error_message($t({defaultMessage: "Please specify a stream."}),compose_banner.CLASSNAMES.missing_stream,@@ -511,9 +505,9 @@ function validate_stream_message(scheduling_message) {}}- const sub = stream_data.get_sub(stream_name);+ const sub = stream_data.get_sub_by_id(stream_id);if (!sub) {- return validation_error("does-not-exist", stream_name);+ return validation_error("does-not-exist", stream_id);}if (!stream_data.can_post_messages_in_stream(sub)) {@@ -530,7 +524,7 @@ function validate_stream_message(scheduling_message) {const wildcard_mention = util.find_wildcard_mentions(compose_state.message_content());if (- !validate_stream_message_address_info(stream_name) ||+ !validate_stream_message_address_info(sub.name) ||!validate_stream_message_mentions({stream_id: sub.stream_id,$banner_container,diff --git a/web/tests/compose_validate.test.js b/web/tests/compose_validate.test.jsindex 15467b71401..b7970ed43e4 100644--- a/web/tests/compose_validate.test.js+++ b/web/tests/compose_validate.test.js@@ -631,7 +631,9 @@ test_ui("needs_subscribe_warning", () => {assert.equal(compose_validate.needs_subscribe_warning(bob.user_id, sub.stream_id), true);});-test_ui("warn_if_private_stream_is_linked", ({mock_template}) => {+test_ui("warn_if_private_stream_is_linked", ({mock_template, override_rewire}) => {+ override_rewire(compose_recipient, "on_compose_select_recipient_update", () => {});+const $textarea = $("<textarea>").attr("id", "compose-textarea");stub_message_row($textarea);const test_sub = {@@ -642,7 +644,7 @@ test_ui("warn_if_private_stream_is_linked", ({mock_template}) => {stream_data.add_sub(test_sub);peer_data.set_subscribers(test_sub.stream_id, [1, 2]);- let denmark = {+ const denmark = {stream_id: 100,name: "Denmark",};@@ -674,14 +676,17 @@ test_ui("warn_if_private_stream_is_linked", ({mock_template}) => {$("#compose_private").hide();compose_state.set_message_type("stream");- denmark = {+ // Not everyone is subscribed to secret_stream in denmark, so the+ // warning is rendered.+ compose_recipient.set_selected_recipient_id(denmark.stream_id);+ const secret_stream = {invite_only: true,name: "Denmark",stream_id: 22,};- stream_data.add_sub(denmark);+ stream_data.add_sub(secret_stream);banner_rendered = false;- compose_validate.warn_if_private_stream_is_linked(denmark, $textarea);+ compose_validate.warn_if_private_stream_is_linked(secret_stream, $textarea);assert.ok(banner_rendered);});