typeahead: Include wildcard mentions in typeahead for small streams.

We refactor the code checking if wildcard mentions are allowed by
renaming the pre-existing function `wildcard_mention_allowed` to
`wildcard_mention_allowed_in_large_stream`, adding a new function
`is_recipient_large_stream`, then redefining `wildcard_mention_allowed`
to combine these two functions.

Fixes: #27248.
This commit is contained in:
N-Shar-ma
2023-11-15 08:25:43 +05:30
committed by Tim Abbott
parent 27ffcc6576
commit bdba280c3d
2 changed files with 32 additions and 6 deletions

View File

@@ -341,7 +341,15 @@ function check_unsubscribed_stream_for_send(stream_name, autosubscribe) {
return result; return result;
} }
export function wildcard_mention_allowed() { function is_recipient_large_stream() {
return (
compose_state.stream_id() &&
peer_data.get_subscriber_count(compose_state.stream_id()) >
wildcard_mention_large_stream_threshold
);
}
function wildcard_mention_allowed_in_large_stream() {
if ( if (
page_params.realm_wildcard_mention_policy === page_params.realm_wildcard_mention_policy ===
settings_config.wildcard_mention_policy_values.by_everyone.code settings_config.wildcard_mention_policy_values.by_everyone.code
@@ -385,6 +393,10 @@ export function wildcard_mention_allowed() {
return !page_params.is_guest; return !page_params.is_guest;
} }
export function wildcard_mention_allowed() {
return !is_recipient_large_stream() || wildcard_mention_allowed_in_large_stream();
}
export function set_wildcard_mention_large_stream_threshold(value) { export function set_wildcard_mention_large_stream_threshold(value) {
wildcard_mention_large_stream_threshold = value; wildcard_mention_large_stream_threshold = value;
} }
@@ -393,12 +405,13 @@ export function validate_stream_message_mentions(opts) {
const subscriber_count = peer_data.get_subscriber_count(opts.stream_id) || 0; const subscriber_count = peer_data.get_subscriber_count(opts.stream_id) || 0;
// If the user is attempting to do a wildcard mention in a large // If the user is attempting to do a wildcard mention in a large
// stream, check if they permission to do so. // stream, check if they permission to do so. If yes, warn them
// if they haven't acknowledged the wildcard warning yet.
if ( if (
opts.wildcard_mention !== null && opts.wildcard_mention !== null &&
subscriber_count > wildcard_mention_large_stream_threshold subscriber_count > wildcard_mention_large_stream_threshold
) { ) {
if (!wildcard_mention_allowed()) { if (!wildcard_mention_allowed_in_large_stream()) {
compose_banner.show_error_message( compose_banner.show_error_message(
$t({ $t({
defaultMessage: defaultMessage:

View File

@@ -324,9 +324,13 @@ test_ui("get_invalid_recipient_emails", ({override_rewire}) => {
assert.deepEqual(compose_validate.get_invalid_recipient_emails(), []); assert.deepEqual(compose_validate.get_invalid_recipient_emails(), []);
}); });
test_ui("test_wildcard_mention_allowed", () => { test_ui("test_wildcard_mention_allowed", ({override_rewire}) => {
page_params.user_id = me.user_id; page_params.user_id = me.user_id;
// First, check for large streams (>15 subscribers) where the wildcard mention
// policy matters.
override_rewire(peer_data, "get_subscriber_count", () => 16);
page_params.realm_wildcard_mention_policy = page_params.realm_wildcard_mention_policy =
settings_config.wildcard_mention_policy_values.by_everyone.code; settings_config.wildcard_mention_policy_values.by_everyone.code;
page_params.is_guest = true; page_params.is_guest = true;
@@ -374,6 +378,15 @@ test_ui("test_wildcard_mention_allowed", () => {
assert.ok(compose_validate.wildcard_mention_allowed()); assert.ok(compose_validate.wildcard_mention_allowed());
page_params.is_admin = false; page_params.is_admin = false;
assert.ok(!compose_validate.wildcard_mention_allowed()); assert.ok(!compose_validate.wildcard_mention_allowed());
// Now, check for small streams (<=15 subscribers) where the wildcard mention
// policy doesn't matter; everyone is allowed to use wildcard mentions.
override_rewire(peer_data, "get_subscriber_count", () => 14);
page_params.realm_wildcard_mention_policy =
settings_config.wildcard_mention_policy_values.by_admins_only.code;
page_params.is_admin = false;
page_params.is_guest = true;
assert.ok(compose_validate.wildcard_mention_allowed());
}); });
test_ui("validate_stream_message", ({override_rewire, mock_template}) => { test_ui("validate_stream_message", ({override_rewire, mock_template}) => {
@@ -407,7 +420,7 @@ test_ui("validate_stream_message", ({override_rewire, mock_template}) => {
assert.equal(data.subscriber_count, 16); assert.equal(data.subscriber_count, 16);
}); });
override_rewire(compose_validate, "wildcard_mention_allowed", () => true); override_rewire(compose_validate, "wildcard_mention_allowed_in_large_stream", () => true);
compose_state.message_content("Hey @**all**"); compose_state.message_content("Hey @**all**");
assert.ok(!compose_validate.validate()); assert.ok(!compose_validate.validate());
assert.equal($("#compose-send-button").prop("disabled"), false); assert.equal($("#compose-send-button").prop("disabled"), false);
@@ -425,7 +438,7 @@ test_ui("validate_stream_message", ({override_rewire, mock_template}) => {
); );
wildcards_not_allowed_rendered = true; wildcards_not_allowed_rendered = true;
}); });
override_rewire(compose_validate, "wildcard_mention_allowed", () => false); override_rewire(compose_validate, "wildcard_mention_allowed_in_large_stream", () => false);
assert.ok(!compose_validate.validate()); assert.ok(!compose_validate.validate());
assert.ok(wildcards_not_allowed_rendered); assert.ok(wildcards_not_allowed_rendered);
}); });