From cb00fbc42f9fd2f2219c9f3fdcab3d5bd3501f7c Mon Sep 17 00:00:00 2001 From: Brijmohan Siyag Date: Sat, 13 May 2023 03:18:00 +0530 Subject: [PATCH] compose: Check posting policy for direct messages. Prior this commit, changing the message type from a stream (where posting was not allowed) to a direct message using the compose box dropdown, did not changed the state of the send button from disabled to enabled even though direct messages were allowed in the organization. This was happening because `check_stream_posting_policy_for_compose_box` was only for streams. Now, function is updated to check for both streams and direct messages, as it checks if direct messages are allowed or not, and depending on that, it updates the send button's state, tooltip and displays a relevant banner. --- web/src/compose_actions.js | 4 +-- web/src/compose_recipient.js | 50 +++++++++++++++++++++---------- web/src/tippyjs.js | 5 ++-- web/tests/compose_actions.test.js | 6 ++-- web/tests/stream_data.test.js | 5 ++++ 5 files changed, 45 insertions(+), 25 deletions(-) diff --git a/web/src/compose_actions.js b/web/src/compose_actions.js index 16fd68300e..484db7f140 100644 --- a/web/src/compose_actions.js +++ b/web/src/compose_actions.js @@ -246,9 +246,7 @@ export function start(msg_type, opts) { // Show a warning if topic is resolved compose_validate.warn_if_topic_resolved(true); - if (msg_type === "stream") { - compose_recipient.check_stream_posting_policy_for_compose_box(opts.stream); - } + compose_recipient.check_posting_policy_for_compose_box(); // Reset the `max-height` property of `compose-textarea` so that the // compose-box do not cover the last messages of the current stream diff --git a/web/src/compose_recipient.js b/web/src/compose_recipient.js index f0b23b1996..30559ad27e 100644 --- a/web/src/compose_recipient.js +++ b/web/src/compose_recipient.js @@ -18,6 +18,7 @@ import {page_params} from "./page_params"; import * as settings_config from "./settings_config"; import * as stream_bar from "./stream_bar"; import * as stream_data from "./stream_data"; +import * as sub_store from "./sub_store"; import * as ui_util from "./ui_util"; import * as util from "./util"; @@ -108,25 +109,42 @@ export function update_on_recipient_change() { update_narrow_to_recipient_visibility(); } -export function check_stream_posting_policy_for_compose_box(stream_name) { - const stream = stream_data.get_sub_by_name(stream_name); - if (!stream) { - return; +export function get_posting_policy_error_message() { + if (selected_recipient_id === "direct") { + if ( + page_params.realm_private_message_policy === + settings_config.private_message_policy_values.disabled.code + ) { + return $t({ + defaultMessage: "Direct messages are disabled in this organization.", + }); + } + return ""; } - const can_post_messages_in_stream = stream_data.can_post_messages_in_stream(stream); - if (!can_post_messages_in_stream) { - $(".compose_right_float_container").addClass("disabled-compose-send-button-container"); - compose_banner.show_error_message( - $t({ - defaultMessage: "You do not have permission to post in this stream.", - }), - compose_banner.CLASSNAMES.no_post_permissions, - $("#compose_banners"), - ); - } else { + + const stream = sub_store.get(selected_recipient_id); + if (stream && !stream_data.can_post_messages_in_stream(stream)) { + return $t({ + defaultMessage: "You do not have permission to post in this stream.", + }); + } + return ""; +} + +export function check_posting_policy_for_compose_box() { + const banner_text = get_posting_policy_error_message(); + if (banner_text === "") { $(".compose_right_float_container").removeClass("disabled-compose-send-button-container"); compose_banner.clear_errors(); + return; } + + let banner_classname = compose_banner.CLASSNAMES.no_post_permissions; + if (selected_recipient_id === "direct") { + banner_classname = compose_banner.CLASSNAMES.private_messages_disabled; + } + $(".compose_right_float_container").addClass("disabled-compose-send-button-container"); + compose_banner.show_error_message(banner_text, banner_classname, $("#compose_banners")); } function switch_message_type(message_type) { @@ -205,8 +223,8 @@ export function on_compose_select_recipient_update() { // since it's likely the user will want to update the topic // after updating the stream. ui_util.place_caret_at_end($("#stream_message_recipient_topic")[0]); - check_stream_posting_policy_for_compose_box(stream_name); } + check_posting_policy_for_compose_box(); update_on_recipient_change(); } diff --git a/web/src/tippyjs.js b/web/src/tippyjs.js index a591d23de1..bba280f67d 100644 --- a/web/src/tippyjs.js +++ b/web/src/tippyjs.js @@ -6,6 +6,7 @@ import render_message_inline_image_tooltip from "../templates/message_inline_ima import render_narrow_to_compose_recipients_tooltip from "../templates/narrow_to_compose_recipients_tooltip.hbs"; import render_tooltip_templates from "../templates/tooltip_templates.hbs"; +import * as compose_recipient from "./compose_recipient"; import * as compose_state from "./compose_state"; import {$t} from "./i18n"; import * as message_lists from "./message_lists"; @@ -640,9 +641,7 @@ export function initialize() { delegate("body", { target: [".disabled-compose-send-button-container"], - content: $t({ - defaultMessage: "You do not have permission to post in this stream.", - }), + content: () => compose_recipient.get_posting_policy_error_message(), appendTo: () => document.body, onHidden(instance) { instance.destroy(); diff --git a/web/tests/compose_actions.test.js b/web/tests/compose_actions.test.js index 8b7ac9a210..98177c421d 100644 --- a/web/tests/compose_actions.test.js +++ b/web/tests/compose_actions.test.js @@ -121,7 +121,7 @@ test("start", ({override, override_rewire, mock_template}) => { override_rewire(compose_actions, "blur_compose_inputs", () => {}); override_rewire(compose_actions, "clear_textarea", () => {}); override_rewire(compose_recipient, "on_compose_select_recipient_update", () => {}); - override_rewire(compose_recipient, "check_stream_posting_policy_for_compose_box", () => {}); + override_rewire(compose_recipient, "check_posting_policy_for_compose_box", () => {}); mock_template("inline_decorated_stream_name.hbs", false, () => {}); mock_stream_header_colorblock(); @@ -245,7 +245,7 @@ test("respond_to_message", ({override, override_rewire, mock_template}) => { override_rewire(compose_actions, "complete_starting_tasks", () => {}); override_rewire(compose_actions, "clear_textarea", () => {}); override_rewire(compose_recipient, "on_compose_select_recipient_update", noop); - override_rewire(compose_recipient, "check_stream_posting_policy_for_compose_box", noop); + override_rewire(compose_recipient, "check_posting_policy_for_compose_box", noop); override_private_message_recipient({override}); mock_template("inline_decorated_stream_name.hbs", false, () => {}); mock_stream_header_colorblock(); @@ -299,7 +299,7 @@ test("reply_with_mention", ({override, override_rewire, mock_template}) => { override_rewire(compose_actions, "complete_starting_tasks", () => {}); override_rewire(compose_actions, "clear_textarea", () => {}); override_private_message_recipient({override}); - override_rewire(compose_recipient, "check_stream_posting_policy_for_compose_box", noop); + override_rewire(compose_recipient, "check_posting_policy_for_compose_box", noop); mock_template("inline_decorated_stream_name.hbs", false, () => {}); const denmark = { diff --git a/web/tests/stream_data.test.js b/web/tests/stream_data.test.js index 8516a2df06..fc96771bd0 100644 --- a/web/tests/stream_data.test.js +++ b/web/tests/stream_data.test.js @@ -164,6 +164,11 @@ test("basics", () => { assert.equal(stream_data.slug_to_name("99-whatever"), "99-whatever"); assert.equal(stream_data.slug_to_name("99whatever"), "99whatever"); + + // sub_store + assert.equal(sub_store.get(-3), undefined); + assert.equal(sub_store.get(undefined), undefined); + assert.equal(sub_store.get(1), denmark); }); test("get_subscribed_streams_for_user", () => {