Skip to content

armgit / zulip

Public Imported from GitHub
0

Zulip server and web application. Open-source team chat that helps teams stay productive and focused.

compose-validate: Refactor how we check posting permissions.

Sahil Batra769a8f628f5a
diff --git a/web/src/compose_validate.ts b/web/src/compose_validate.ts
index 3e2c39190a6..ff70d654e5b 100644
--- a/web/src/compose_validate.ts
+++ b/web/src/compose_validate.ts
@@ -841,7 +841,7 @@ export function validate_stream_message_mentions(opts: StreamWildcardOptions): b
return true;
}
-export function validate_stream_message_address_info(sub: StreamSubscription): boolean {
+function validate_permission_to_post_messages_in_stream(sub: StreamSubscription): boolean {
if (sub.is_archived) {
compose_banner.show_stream_does_not_exist_error(sub.name);
if (is_validating_compose_box) {
@@ -849,14 +849,30 @@ export function validate_stream_message_address_info(sub: StreamSubscription): b
}
return false;
}
- if (sub.subscribed) {
- return true;
+
+ if (!sub.subscribed) {
+ compose_banner.show_stream_not_subscribed_error(sub, UNSUBSCRIBED_CHANNEL_ERROR_MESSAGE);
+ if (is_validating_compose_box) {
+ disabled_send_tooltip_message_html = UNSUBSCRIBED_CHANNEL_ERROR_MESSAGE;
+ }
+ return false;
}
- compose_banner.show_stream_not_subscribed_error(sub, UNSUBSCRIBED_CHANNEL_ERROR_MESSAGE);
- if (is_validating_compose_box) {
- disabled_send_tooltip_message_html = UNSUBSCRIBED_CHANNEL_ERROR_MESSAGE;
+
+ if (!stream_data.can_post_messages_in_stream(sub)) {
+ compose_banner.show_error_message(
+ NO_PERMISSION_TO_POST_IN_CHANNEL_ERROR_MESSAGE,
+ compose_banner.CLASSNAMES.no_post_permissions,
+ $("#compose_banners"),
+ );
+
+ if (is_validating_compose_box) {
+ disabled_send_tooltip_message_html = NO_PERMISSION_TO_POST_IN_CHANNEL_ERROR_MESSAGE;
+ posting_policy_error_message = NO_PERMISSION_TO_POST_IN_CHANNEL_ERROR_MESSAGE;
+ }
+ return false;
}
- return false;
+
+ return true;
}
function validate_stream_message(scheduling_message: boolean, show_banner = true): boolean {
@@ -902,17 +918,7 @@ function validate_stream_message(scheduling_message: boolean, show_banner = true
return false;
}
- if (!stream_data.can_post_messages_in_stream(sub)) {
- compose_banner.show_error_message(
- NO_PERMISSION_TO_POST_IN_CHANNEL_ERROR_MESSAGE,
- compose_banner.CLASSNAMES.no_post_permissions,
- $banner_container,
- );
-
- if (is_validating_compose_box) {
- disabled_send_tooltip_message_html = NO_PERMISSION_TO_POST_IN_CHANNEL_ERROR_MESSAGE;
- posting_policy_error_message = NO_PERMISSION_TO_POST_IN_CHANNEL_ERROR_MESSAGE;
- }
+ if (!validate_permission_to_post_messages_in_stream(sub)) {
return false;
}
@@ -937,7 +943,6 @@ function validate_stream_message(scheduling_message: boolean, show_banner = true
);
if (
- !validate_stream_message_address_info(sub) ||
!validate_stream_message_mentions({
stream_id: sub.stream_id,
$banner_container,
diff --git a/web/tests/compose_validate.test.cjs b/web/tests/compose_validate.test.cjs
index a2d883f4a63..e2928a29bbe 100644
--- a/web/tests/compose_validate.test.cjs
+++ b/web/tests/compose_validate.test.cjs
@@ -185,46 +185,6 @@ function initialize_pm_pill(mock_template) {
mock_banners();
}
-test_ui("validate_stream_message_address_info", ({mock_template, override}) => {
- // For this test we basically only use FakeComposeBox
- // to set up the DOM environment. We don't assert about
- // any side effects on the DOM, since the scope of this
- // test is mostly to make sure the template gets rendered.
- new FakeComposeBox();
-
- override(realm, "realm_can_access_all_users_group", everyone.id);
-
- const party_sub = {
- stream_id: 101,
- name: "party",
- subscribed: true,
- can_add_subscribers_group: nobody.id,
- can_subscribe_group: nobody.id,
- };
- stream_data.add_sub_for_tests(party_sub);
- assert.ok(compose_validate.validate_stream_message_address_info(party_sub));
-
- party_sub.subscribed = false;
- stream_data.add_sub_for_tests(party_sub);
- let user_not_subscribed_rendered = false;
- mock_template("compose_banner/compose_banner.hbs", true, (data, html) => {
- assert.equal(data.classname, compose_banner.CLASSNAMES.user_not_subscribed);
- user_not_subscribed_rendered = true;
- return html;
- });
- assert.ok(!compose_validate.validate_stream_message_address_info(party_sub));
- assert.ok(user_not_subscribed_rendered);
-
- party_sub.name = "Frontend";
- party_sub.stream_id = 102;
- stream_data.add_sub_for_tests(party_sub);
- user_not_subscribed_rendered = false;
-
- assert.ok(!compose_validate.validate_stream_message_address_info(party_sub));
-
- assert.ok(user_not_subscribed_rendered);
-});
-
test_ui("validate", ({mock_template, override}) => {
function add_content_to_compose_box() {
$("textarea#compose-textarea").val("foobarfoobar");
@@ -446,12 +406,32 @@ test_ui("test_stream_posting_permission", ({mock_template, override}) => {
subscribed: true,
can_send_message_group: admin.id,
can_create_topic_group: everyone.id,
+ can_add_subscribers_group: everyone.id,
};
stream_data.add_sub_for_tests(sub_stream_102);
compose_state.topic("topic102");
compose_state.set_stream_id(sub_stream_102.stream_id);
+ sub_stream_102.subscribed = false;
+ let user_not_subscribed_rendered = false;
+ mock_template("compose_banner/compose_banner.hbs", false, (data) => {
+ assert.equal(data.classname, compose_banner.CLASSNAMES.user_not_subscribed);
+ assert.equal(
+ data.banner_text,
+ $t({
+ defaultMessage:
+ "You're not subscribed to this channel. You will not be notified if other users reply to your message.",
+ }),
+ );
+ user_not_subscribed_rendered = true;
+ return "<banner-stub>";
+ });
+ assert.ok(!compose_validate.validate());
+ assert.ok(user_not_subscribed_rendered);
+
+ sub_stream_102.subscribed = true;
+
let banner_rendered = false;
mock_template("compose_banner/compose_banner.hbs", false, (data) => {
assert.equal(data.classname, compose_banner.CLASSNAMES.no_post_permissions);