compose: Show error for wildcard messages according to settings.

This commit changes UI to show /error for sending message with
wildcard mention according to wildcard mention policy of organization.

Fixes #16211.
This commit is contained in:
sahil839
2020-09-11 19:37:06 +05:30
committed by Tim Abbott
parent 1d5aa2e514
commit 465122b0df
2 changed files with 110 additions and 1 deletions

View File

@@ -85,6 +85,7 @@ rewiremock.proxy(() => zrequire("compose"), {
}); });
zrequire("upload"); zrequire("upload");
zrequire("server_events_dispatch"); zrequire("server_events_dispatch");
const settings_config = zrequire("settings_config");
people.small_avatar_url_for_person = function () { people.small_avatar_url_for_person = function () {
return "http://example.com/example.png"; return "http://example.com/example.png";
@@ -324,6 +325,48 @@ run_test("get_invalid_recipient_emails", () => {
assert.deepEqual(compose.get_invalid_recipient_emails(), []); assert.deepEqual(compose.get_invalid_recipient_emails(), []);
}); });
run_test("test_wildcard_mention_allowed", () => {
page_params.realm_wildcard_mention_policy =
settings_config.wildcard_mention_policy_values.by_everyone.code;
page_params.is_guest = true;
page_params.is_admin = false;
assert(compose.wildcard_mention_allowed());
page_params.realm_wildcard_mention_policy =
settings_config.wildcard_mention_policy_values.nobody.code;
page_params.is_admin = true;
assert(!compose.wildcard_mention_allowed());
page_params.realm_wildcard_mention_policy =
settings_config.wildcard_mention_policy_values.by_members.code;
page_params.is_guest = true;
page_params.is_admin = false;
assert(!compose.wildcard_mention_allowed());
page_params.is_guest = false;
assert(compose.wildcard_mention_allowed());
page_params.realm_wildcard_mention_policy =
settings_config.wildcard_mention_policy_values.by_stream_admins_only.code;
page_params.is_admin = false;
assert(!compose.wildcard_mention_allowed());
// TODO: Add a by_admins_only case when we implement stream-level administrators.
page_params.is_admin = true;
assert(compose.wildcard_mention_allowed());
page_params.realm_wildcard_mention_policy =
settings_config.wildcard_mention_policy_values.by_full_members.code;
const person = people.get_by_user_id(page_params.user_id);
person.date_joined = new Date(Date.now());
page_params.realm_waiting_period_threshold = 10;
assert(compose.wildcard_mention_allowed());
page_params.is_admin = false;
assert(!compose.wildcard_mention_allowed());
});
run_test("validate_stream_message", () => { run_test("validate_stream_message", () => {
// This test is in kind of continuation to test_validate but since it is // This test is in kind of continuation to test_validate but since it is
// primarily used to get coverage over functions called from validate() // primarily used to get coverage over functions called from validate()
@@ -354,12 +397,25 @@ run_test("validate_stream_message", () => {
$("#compose-all-everyone").append = function (data) { $("#compose-all-everyone").append = function (data) {
compose_content = data; compose_content = data;
}; };
compose.wildcard_mention_allowed = function () {
return true;
};
compose_state.message_content("Hey @**all**"); compose_state.message_content("Hey @**all**");
assert(!compose.validate()); assert(!compose.validate());
assert.equal($("#compose-send-button").prop("disabled"), false); assert.equal($("#compose-send-button").prop("disabled"), false);
assert(!$("#compose-send-status").visible()); assert(!$("#compose-send-status").visible());
assert.equal(compose_content, "compose_all_everyone_stub"); assert.equal(compose_content, "compose_all_everyone_stub");
assert($("#compose-all-everyone").visible()); assert($("#compose-all-everyone").visible());
compose.wildcard_mention_allowed = function () {
return false;
};
assert(!compose.validate());
assert.equal(
$("#compose-error-msg").html(),
i18n.t("You do not have permission to use wildcard mentions in this stream."),
);
}); });
run_test("test_validate_stream_message_post_policy_admin_only", () => { run_test("test_validate_stream_message_post_policy_admin_only", () => {

View File

@@ -10,6 +10,7 @@ const render_compose_private_stream_alert = require("../templates/compose_privat
const people = require("./people"); const people = require("./people");
const rendered_markdown = require("./rendered_markdown"); const rendered_markdown = require("./rendered_markdown");
const settings_config = require("./settings_config");
const util = require("./util"); const util = require("./util");
// Docs: https://zulip.readthedocs.io/en/latest/subsystems/sending-messages.html // Docs: https://zulip.readthedocs.io/en/latest/subsystems/sending-messages.html
@@ -481,14 +482,66 @@ function check_unsubscribed_stream_for_send(stream_name, autosubscribe) {
return result; return result;
} }
exports.wildcard_mention_allowed = function () {
if (
page_params.realm_wildcard_mention_policy ===
settings_config.wildcard_mention_policy_values.by_everyone.code
) {
return true;
}
if (
page_params.realm_wildcard_mention_policy ===
settings_config.wildcard_mention_policy_values.nobody.code
) {
return false;
}
if (
page_params.realm_wildcard_mention_policy ===
settings_config.wildcard_mention_policy_values.by_stream_admins_only.code
) {
// TODO: Check the user's stream-level role once stream-level admins exist.
return page_params.is_admin;
}
// TODO: Uncomment when we add support for stream-level administrators.
// if (
// page_params.realm_wildcard_mention_policy ===
// settings_config.wildcard_mention_policy_values.by_admins_only.code
// ) {
// return page_params.is_admin;
// }
if (
page_params.realm_wildcard_mention_policy ===
settings_config.wildcard_mention_policy_values.by_full_members.code
) {
if (page_params.is_admin) {
return true;
}
const person = people.get_by_user_id(page_params.user_id);
const current_datetime = new Date(Date.now());
const person_date_joined = new Date(person.date_joined);
const days = (current_datetime - person_date_joined) / 1000 / 86400;
return days >= page_params.realm_waiting_period_threshold && !page_params.is_guest;
}
return !page_params.is_guest;
};
function validate_stream_message_mentions(stream_id) { function validate_stream_message_mentions(stream_id) {
const stream_count = stream_data.get_subscriber_count(stream_id) || 0; const stream_count = stream_data.get_subscriber_count(stream_id) || 0;
// check if wildcard_mention has any mention and henceforth execute the warning message. // If the user is attempting to do a wildcard mention in a large
// stream, check if they permission to do so.
if ( if (
wildcard_mention !== null && wildcard_mention !== null &&
stream_count > exports.wildcard_mention_large_stream_threshold stream_count > exports.wildcard_mention_large_stream_threshold
) { ) {
if (!exports.wildcard_mention_allowed()) {
compose_error(
i18n.t("You do not have permission to use wildcard mentions in this stream."),
);
return false;
}
if ( if (
user_acknowledged_all_everyone === undefined || user_acknowledged_all_everyone === undefined ||
user_acknowledged_all_everyone === false user_acknowledged_all_everyone === false