Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

compose: Check posting policy for direct messages. #25572

Merged
merged 2 commits into from
May 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 1 addition & 3 deletions web/src/compose_actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
50 changes: 34 additions & 16 deletions web/src/compose_recipient.js
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand Down Expand Up @@ -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() {
timabbott marked this conversation as resolved.
Show resolved Hide resolved
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;
timabbott marked this conversation as resolved.
Show resolved Hide resolved
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) {
Expand Down Expand Up @@ -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();
}

Expand Down
6 changes: 3 additions & 3 deletions web/src/tippyjs.js
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -640,9 +641,8 @@ export function initialize() {

delegate("body", {
target: [".disabled-compose-send-button-container"],
content: $t({
defaultMessage: "You do not have permission to post in this stream.",
}),
maxWidth: 350,
content: () => compose_recipient.get_posting_policy_error_message(),
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
5 changes: 5 additions & 0 deletions web/tests/stream_data.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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", () => {
Expand Down