Skip to content

Commit

Permalink
compose: Check posting policy for direct messages.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
brijsiyag committed May 12, 2023
1 parent ddb0bb5 commit 7516b71
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 17 deletions.
4 changes: 3 additions & 1 deletion web/src/compose_actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,9 @@ export function start(msg_type, opts) {
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(opts.stream);
} else if (msg_type === "private") {
compose_recipient.check_posting_policy_for_compose_box();
}

// Reset the `max-height` property of `compose-textarea` so that the
Expand Down
31 changes: 21 additions & 10 deletions web/src/compose_recipient.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,18 +108,28 @@ 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 check_posting_policy_for_compose_box(stream_name) {
let can_post_messages;
// If stream_name is not passed, than it should be from a direct message.
if (stream_name === undefined) {
can_post_messages =
page_params.realm_private_message_policy !==
settings_config.private_message_policy_values.disabled.code;
} else {
const stream = stream_data.get_sub_by_name(stream_name);
if (!stream) {
return;
}
can_post_messages = stream_data.can_post_messages_in_stream(stream);
}
const can_post_messages_in_stream = stream_data.can_post_messages_in_stream(stream);
if (!can_post_messages_in_stream) {
if (!can_post_messages) {
let msg = $t({defaultMessage: "You do not have permission to post in this stream."});
if (stream_name === undefined) {
msg = $t({defaultMessage: "Direct messages are disabled in this organization."});
}
$(".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.",
}),
msg,
compose_banner.CLASSNAMES.no_post_permissions,
$("#compose_banners"),
);
Expand Down Expand Up @@ -194,6 +204,7 @@ export function on_compose_select_recipient_update() {
if (compose_state.private_message_recipient().length === 0) {
$("#private_message_recipient").trigger("focus").trigger("select");
}
check_posting_policy_for_compose_box();
} else {
const $stream_header_colorblock = $(
"#compose_recipient_selection_dropdown .stream_header_colorblock",
Expand All @@ -205,7 +216,7 @@ 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(stream_name);
}
update_on_recipient_change();
}
Expand Down
13 changes: 10 additions & 3 deletions web/src/tippyjs.js
Original file line number Diff line number Diff line change
Expand Up @@ -639,9 +639,16 @@ 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() {
if (compose_state.get_message_type() === "private") {
return $t({
defaultMessage: "Direct messages are disabled in this organization.",
});
}
return $t({
defaultMessage: "You do not have permission to post in this stream.",
});
},
appendTo: () => document.body,
onHidden(instance) {
instance.destroy();
Expand Down
6 changes: 3 additions & 3 deletions web/tests/compose_actions.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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 = {
Expand Down

0 comments on commit 7516b71

Please sign in to comment.