Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
compose: Modify validate_stream_message to check invalid stream name early.
sahil83966dc712830f2
diff --git a/frontend_tests/node_tests/compose.js b/frontend_tests/node_tests/compose.jsindex d86de9186b9..79d01c396c2 100644--- a/frontend_tests/node_tests/compose.js+++ b/frontend_tests/node_tests/compose.js@@ -137,8 +137,6 @@ run_test('validate_stream_message_address_info', () => {assert(compose.validate_stream_message_address_info('social'));$('#stream_message_recipient_stream').select(noop);- assert(!compose.validate_stream_message_address_info('foobar'));- assert.equal($('#compose-error-msg').html(), "translated: <p>The stream <b>foobar</b> does not exist.</p><p>Manage your subscriptions <a href='#streams/all'>on your Streams page</a>.</p>");sub.subscribed = false;stream_data.add_sub(sub);@@ -321,8 +319,8 @@ run_test('validate_stream_message', () => {assert(!$("#compose-all-everyone").visible());assert(!$("#compose-send-status").visible());- stream_data.get_subscriber_count = function (stream_name) {- assert.equal(stream_name, 'social');+ stream_data.get_subscriber_count = function (stream_id) {+ assert.equal(stream_id, 101);return 16;};global.stub_templates((template_name, data) => {@@ -346,7 +344,7 @@ run_test('test_validate_stream_message_post_policy', () => {// This test is in continuation with test_validate but it has been separated out// for better readability. Their relative position of execution should not be changed.// Although the position with respect to test_validate_stream_message does not matter- // as `get_stream_post_policy` is reset at the end.+ // as different stream is used for this test.page_params.is_admin = false;const sub = {stream_id: 102,@@ -354,19 +352,16 @@ run_test('test_validate_stream_message_post_policy', () => {subscribed: true,stream_post_policy: stream_data.stream_post_policy_values.admins.code,};- stream_data.get_stream_post_policy = function () {- return 2;- };+compose_state.topic('subject102');+ compose_state.stream_name('stream102');stream_data.add_sub(sub);assert(!compose.validate());assert.equal($('#compose-error-msg').html(), i18n.t("Only organization admins are allowed to post to this stream."));- // reset `get_stream_post_policy` so that any tests occurung after this+ // reset compose_state.stream_name to 'social' again so that any tests occurung after this// do not reproduce this error.- stream_data.get_stream_post_policy = function () {- return stream_data.stream_post_policy_values.everyone.code;- };+ compose_state.stream_name('social');});run_test('markdown_rtl', () => {diff --git a/frontend_tests/node_tests/stream_data.js b/frontend_tests/node_tests/stream_data.jsindex c376e107b56..a34f634f822 100644--- a/frontend_tests/node_tests/stream_data.js+++ b/frontend_tests/node_tests/stream_data.js@@ -83,8 +83,6 @@ run_test('basics', () => {assert(stream_data.get_invite_only('social'));assert(!stream_data.get_invite_only('unknown'));- assert(stream_data.get_stream_post_policy('social'));- assert(!stream_data.get_stream_post_policy('unknown'));assert.equal(stream_data.get_color('social'), 'red');assert.equal(stream_data.get_color('unknown'), global.stream_color.default_color);@@ -608,10 +606,10 @@ run_test('get_subscriber_count', () => {stream_data.clear_subscriptions();blueslip.expect('warn', 'We got a get_subscriber_count count call for a non-existent stream.');- assert.equal(stream_data.get_subscriber_count('India'), undefined);+ assert.equal(stream_data.get_subscriber_count(india.stream_id), undefined);stream_data.add_sub(india);- assert.equal(stream_data.get_subscriber_count('India'), 0);+ assert.equal(stream_data.get_subscriber_count(india.stream_id), 0);const fred = {email: '[email protected]',@@ -620,7 +618,7 @@ run_test('get_subscriber_count', () => {};people.add_active_user(fred);stream_data.add_subscriber(india.stream_id, 102);- assert.equal(stream_data.get_subscriber_count('India'), 1);+ assert.equal(stream_data.get_subscriber_count(india.stream_id), 1);const george = {email: '[email protected]',full_name: 'George',@@ -628,11 +626,11 @@ run_test('get_subscriber_count', () => {};people.add_active_user(george);stream_data.add_subscriber(india.stream_id, 103);- assert.equal(stream_data.get_subscriber_count('India'), 2);+ assert.equal(stream_data.get_subscriber_count(india.stream_id), 2);const sub = stream_data.get_sub_by_name('India');delete sub.subscribers;- assert.deepStrictEqual(stream_data.get_subscriber_count('India'), 0);+ assert.deepStrictEqual(stream_data.get_subscriber_count(india.stream_id), 0);});run_test('notifications', () => {diff --git a/static/js/compose.js b/static/js/compose.jsindex 953ccbdcad7..0e0a5a35795 100644--- a/static/js/compose.js+++ b/static/js/compose.js@@ -32,8 +32,8 @@ function make_uploads_relative(content) {return content.replace(exports.uploads_re, "]($1)");}-function show_all_everyone_warnings() {- const stream_count = stream_data.get_subscriber_count(compose_state.stream_name()) || 0;+function show_all_everyone_warnings(stream_id) {+ const stream_count = stream_data.get_subscriber_count(stream_id) || 0;const all_everyone_template = render_compose_all_everyone({count: stream_count,mention: wildcard_mention});@@ -79,8 +79,8 @@ function show_sending_indicator(whats_happening) {$("#sending-indicator").show();}-function show_announce_warnings() {- const stream_count = stream_data.get_subscriber_count(compose_state.stream_name()) || 0;+function show_announce_warnings(stream_id) {+ const stream_count = stream_data.get_subscriber_count(stream_id) || 0;const announce_template = render_compose_announce({count: stream_count});const error_area_announce = $("#compose-announce");@@ -430,11 +430,7 @@ exports.get_invalid_recipient_emails = function () {};function check_unsubscribed_stream_for_send(stream_name, autosubscribe) {- const stream_obj = stream_data.get_sub(stream_name);let result;- if (!stream_obj) {- return "does-not-exist";- }if (!autosubscribe) {return "not-subscribed";}@@ -465,8 +461,8 @@ function check_unsubscribed_stream_for_send(stream_name, autosubscribe) {return result;}-function validate_stream_message_mentions(stream_name) {- const stream_count = stream_data.get_subscriber_count(stream_name) || 0;+function validate_stream_message_mentions(stream_id) {+ const stream_count = stream_data.get_subscriber_count(stream_id) || 0;wildcard_mention = util.find_wildcard_mentions(compose_state.message_content());// check if wildcard_mention has any mention and henceforth execute the warning message.@@ -474,7 +470,7 @@ function validate_stream_message_mentions(stream_name) {if (user_acknowledged_all_everyone === undefined ||user_acknowledged_all_everyone === false) {// user has not seen a warning message yet if undefined- show_all_everyone_warnings();+ show_all_everyone_warnings(stream_id);$("#compose-send-button").prop('disabled', false);$("#sending-indicator").hide();@@ -490,15 +486,15 @@ function validate_stream_message_mentions(stream_name) {return true;}-function validate_stream_message_announce(stream_name) {- const stream_count = stream_data.get_subscriber_count(stream_name) || 0;+function validate_stream_message_announce(sub) {+ const stream_count = stream_data.get_subscriber_count(sub.stream_id) || 0;- if (stream_name === "announce" &&+ if (sub.name === "announce" &&stream_count > exports.announce_warn_threshold) {if (user_acknowledged_announce === undefined ||user_acknowledged_announce === false) {// user has not seen a warning message yet if undefined- show_announce_warnings();+ show_announce_warnings(sub.stream_id);$("#compose-send-button").prop('disabled', false);$("#sending-indicator").hide();@@ -513,13 +509,13 @@ function validate_stream_message_announce(stream_name) {return true;}-function validate_stream_message_post_policy(stream_name) {+function validate_stream_message_post_policy(sub) {if (page_params.is_admin) {return true;}const stream_post_permission_type = stream_data.stream_post_policy_values;- const stream_post_policy = stream_data.get_stream_post_policy(stream_name);+ const stream_post_policy = sub.stream_post_policy;if (stream_post_policy === stream_post_permission_type.admins.code) {compose_error(i18n.t("Only organization admins are allowed to post to this stream."));@@ -590,7 +586,12 @@ function validate_stream_message() {}}- if (!validate_stream_message_post_policy(stream_name)) {+ const sub = stream_data.get_sub(stream_name);+ if (!sub) {+ return exports.validation_error("does-not-exist", stream_name);+ }++ if (!validate_stream_message_post_policy(sub)) {return false;}@@ -598,14 +599,14 @@ function validate_stream_message() {// for `@all`. Users shouldn't have to hit "yes" more than once.if (wildcard_mention !== null && stream_name === "announce") {if (!exports.validate_stream_message_address_info(stream_name) ||- !validate_stream_message_mentions(stream_name)) {+ !validate_stream_message_mentions(sub.stream_id)) {return false;}// If either criteria isn't met, just do the normal validation.} else {if (!exports.validate_stream_message_address_info(stream_name) ||- !validate_stream_message_mentions(stream_name) ||- !validate_stream_message_announce(stream_name)) {+ !validate_stream_message_mentions(sub.stream_id) ||+ !validate_stream_message_announce(sub)) {return false;}}diff --git a/static/js/stream_data.js b/static/js/stream_data.jsindex fe9111ef0c6..5358332a827 100644--- a/static/js/stream_data.js+++ b/static/js/stream_data.js@@ -437,8 +437,8 @@ exports.update_stream_email_address = function (sub, email) {sub.email_address = email;};-exports.get_subscriber_count = function (stream_name) {- const sub = exports.get_sub_by_name(stream_name);+exports.get_subscriber_count = function (stream_id) {+ const sub = exports.get_sub_by_id(stream_id);if (sub === undefined) {blueslip.warn('We got a get_subscriber_count count call for a non-existent stream.');return;@@ -569,14 +569,6 @@ exports.get_invite_only = function (stream_name) {return sub.invite_only;};-exports.get_stream_post_policy = function (stream_name) {- const sub = exports.get_sub(stream_name);- if (sub === undefined) {- return false;- }- return sub.stream_post_policy;-};-exports.all_topics_in_cache = function (sub) {// Checks whether this browser's cache of contiguous messages// (used to locally render narrows) in message_list.all has all