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.
This commit is contained in:
Brijmohan Siyag 2023-05-13 03:18:00 +05:30 committed by Tim Abbott
parent ee88bb5187
commit cb00fbc42f
5 changed files with 45 additions and 25 deletions

View File

@ -246,9 +246,7 @@ export function start(msg_type, opts) {
// Show a warning if topic is resolved // Show a warning if topic is resolved
compose_validate.warn_if_topic_resolved(true); compose_validate.warn_if_topic_resolved(true);
if (msg_type === "stream") { compose_recipient.check_posting_policy_for_compose_box();
compose_recipient.check_stream_posting_policy_for_compose_box(opts.stream);
}
// Reset the `max-height` property of `compose-textarea` so that the // Reset the `max-height` property of `compose-textarea` so that the
// compose-box do not cover the last messages of the current stream // compose-box do not cover the last messages of the current stream

View File

@ -18,6 +18,7 @@ import {page_params} from "./page_params";
import * as settings_config from "./settings_config"; import * as settings_config from "./settings_config";
import * as stream_bar from "./stream_bar"; import * as stream_bar from "./stream_bar";
import * as stream_data from "./stream_data"; import * as stream_data from "./stream_data";
import * as sub_store from "./sub_store";
import * as ui_util from "./ui_util"; import * as ui_util from "./ui_util";
import * as util from "./util"; import * as util from "./util";
@ -108,25 +109,42 @@ export function update_on_recipient_change() {
update_narrow_to_recipient_visibility(); update_narrow_to_recipient_visibility();
} }
export function check_stream_posting_policy_for_compose_box(stream_name) { export function get_posting_policy_error_message() {
const stream = stream_data.get_sub_by_name(stream_name); if (selected_recipient_id === "direct") {
if (!stream) { if (
return; page_params.realm_private_message_policy ===
settings_config.private_message_policy_values.disabled.code
) {
return $t({
defaultMessage: "Direct messages are disabled in this organization.",
});
} }
const can_post_messages_in_stream = stream_data.can_post_messages_in_stream(stream); return "";
if (!can_post_messages_in_stream) { }
$(".compose_right_float_container").addClass("disabled-compose-send-button-container");
compose_banner.show_error_message( const stream = sub_store.get(selected_recipient_id);
$t({ if (stream && !stream_data.can_post_messages_in_stream(stream)) {
return $t({
defaultMessage: "You do not have permission to post in this stream.", defaultMessage: "You do not have permission to post in this stream.",
}), });
compose_banner.CLASSNAMES.no_post_permissions, }
$("#compose_banners"), return "";
); }
} else {
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_right_float_container").removeClass("disabled-compose-send-button-container");
compose_banner.clear_errors(); 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) { 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 // since it's likely the user will want to update the topic
// after updating the stream. // after updating the stream.
ui_util.place_caret_at_end($("#stream_message_recipient_topic")[0]); 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(); update_on_recipient_change();
} }

View File

@ -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_narrow_to_compose_recipients_tooltip from "../templates/narrow_to_compose_recipients_tooltip.hbs";
import render_tooltip_templates from "../templates/tooltip_templates.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 * as compose_state from "./compose_state";
import {$t} from "./i18n"; import {$t} from "./i18n";
import * as message_lists from "./message_lists"; import * as message_lists from "./message_lists";
@ -640,9 +641,7 @@ export function initialize() {
delegate("body", { delegate("body", {
target: [".disabled-compose-send-button-container"], target: [".disabled-compose-send-button-container"],
content: $t({ content: () => compose_recipient.get_posting_policy_error_message(),
defaultMessage: "You do not have permission to post in this stream.",
}),
appendTo: () => document.body, appendTo: () => document.body,
onHidden(instance) { onHidden(instance) {
instance.destroy(); instance.destroy();

View File

@ -121,7 +121,7 @@ test("start", ({override, override_rewire, mock_template}) => {
override_rewire(compose_actions, "blur_compose_inputs", () => {}); override_rewire(compose_actions, "blur_compose_inputs", () => {});
override_rewire(compose_actions, "clear_textarea", () => {}); override_rewire(compose_actions, "clear_textarea", () => {});
override_rewire(compose_recipient, "on_compose_select_recipient_update", () => {}); 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_template("inline_decorated_stream_name.hbs", false, () => {});
mock_stream_header_colorblock(); 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, "complete_starting_tasks", () => {});
override_rewire(compose_actions, "clear_textarea", () => {}); override_rewire(compose_actions, "clear_textarea", () => {});
override_rewire(compose_recipient, "on_compose_select_recipient_update", noop); 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}); override_private_message_recipient({override});
mock_template("inline_decorated_stream_name.hbs", false, () => {}); mock_template("inline_decorated_stream_name.hbs", false, () => {});
mock_stream_header_colorblock(); 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, "complete_starting_tasks", () => {});
override_rewire(compose_actions, "clear_textarea", () => {}); override_rewire(compose_actions, "clear_textarea", () => {});
override_private_message_recipient({override}); 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, () => {}); mock_template("inline_decorated_stream_name.hbs", false, () => {});
const denmark = { const denmark = {

View File

@ -164,6 +164,11 @@ test("basics", () => {
assert.equal(stream_data.slug_to_name("99-whatever"), "99-whatever"); assert.equal(stream_data.slug_to_name("99-whatever"), "99-whatever");
assert.equal(stream_data.slug_to_name("99whatever"), "99whatever"); 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", () => { test("get_subscribed_streams_for_user", () => {