Skip to content

armgit / zulip

Public Imported from GitHub
0

Zulip server and web application. Open-source team chat that helps teams stay productive and focused.

compose: Remove stale "recipient not subscribed" banners.

Aman Agrawal9d5fb07d38ff
diff --git a/web/src/compose_setup.ts b/web/src/compose_setup.ts
index 737fb01d8c0..01d17f444d1 100644
--- a/web/src/compose_setup.ts
+++ b/web/src/compose_setup.ts
@@ -97,6 +97,9 @@ export function initialize(): void {
if (recipient_widget_hidden) {
compose_validate.warn_if_topic_resolved(false);
}
+ compose_validate.maybe_clear_stale_recipient_not_subscribed_warnings(
+ $<HTMLTextAreaElement>("textarea#compose-textarea").expectOne(),
+ );
const compose_text_length = compose_validate.check_overflow_text(
$("#send_message_form"),
);
diff --git a/web/src/compose_validate.ts b/web/src/compose_validate.ts
index 47ef61eaaf6..08e845ee5be 100644
--- a/web/src/compose_validate.ts
+++ b/web/src/compose_validate.ts
@@ -371,6 +371,53 @@ export async function warn_if_mentioning_unsubscribed_user(
}
}
+// Called on every compose input event to remove any "recipient not
+// subscribed" banners whose mention is no longer in the compose text.
+// We check for the three mention syntaxes the markdown parser accepts
+// (@**Name**, @**Name|id**, @**|id**) rather than re-parsing markdown
+// on every input event.
+export function maybe_clear_stale_recipient_not_subscribed_warnings(
+ $textarea: JQuery<HTMLTextAreaElement>,
+): void {
+ const $banner_container = compose_banner.get_compose_banner_container($textarea);
+ const $existing_banners = $banner_container.find(
+ `.${CSS.escape(compose_banner.CLASSNAMES.recipient_not_subscribed)}`,
+ );
+ if ($existing_banners.length === 0) {
+ return;
+ }
+
+ const compose_text = $textarea.val() ?? "";
+ for (const banner of $existing_banners) {
+ const user_id = Number($(banner).attr("data-user-id"));
+ if (!user_id) {
+ $(banner).remove();
+ continue;
+ }
+
+ const user = people.maybe_get_user_by_id(user_id, true);
+ if (user === undefined) {
+ $(banner).remove();
+ continue;
+ }
+
+ // get_mention_syntax produces the canonical @**Name** form (or
+ // @**Name|id** for duplicate names), matching what the typeahead
+ // inserts.
+ const mention_syntax = people.get_mention_syntax(user.full_name, user_id, false);
+ const name_and_id_syntax = `@**${user.full_name}|${user_id}**`;
+ const id_only_syntax = `@**|${user_id}**`;
+
+ if (
+ !compose_text.includes(mention_syntax) &&
+ !compose_text.includes(name_and_id_syntax) &&
+ !compose_text.includes(id_only_syntax)
+ ) {
+ $(banner).remove();
+ }
+ }
+}
+
export async function warn_if_mentioning_unsubscribed_group(
mentioned_group: UserGroup,
$textarea: JQuery<HTMLTextAreaElement>,
diff --git a/web/tests/compose_validate.test.cjs b/web/tests/compose_validate.test.cjs
index 3f87254a205..12c306641a3 100644
--- a/web/tests/compose_validate.test.cjs
+++ b/web/tests/compose_validate.test.cjs
@@ -720,6 +720,117 @@ test_ui("warn_if_mentioning_unsubscribed_user", async ({override, mock_template}
assert.ok(!new_banner_rendered);
});
+test_ui("maybe_clear_stale_recipient_not_subscribed_warnings", () => {
+ const $textarea = $("<textarea>").attr("id", "compose-textarea");
+ stub_message_row($textarea);
+ const $banner_container = $("#compose_banners");
+
+ // Using a shared factory so the removal callback has a single
+ // source location — coverage is satisfied as long as any banner
+ // is actually removed across all test cases.
+ const removed_banners = new Set();
+ function make_banner(name, user_id_str) {
+ removed_banners.delete(name);
+ const $banner = $.create(name);
+ if (user_id_str !== undefined) {
+ $banner.attr("data-user-id", user_id_str);
+ }
+ $banner[0].remove = () => {
+ removed_banners.add(name);
+ };
+ return $banner;
+ }
+
+ // No banners present: function is a no-op.
+ $banner_container.set_find_results(".recipient_not_subscribed", []);
+ $textarea.val("text without any mention");
+ compose_validate.maybe_clear_stale_recipient_not_subscribed_warnings($textarea);
+
+ // Banner preserved when canonical @**Name** mention is present.
+ {
+ const $banner = make_banner("canonical", String(alice.user_id));
+ const mention = people.get_mention_syntax(alice.full_name, alice.user_id, false);
+ $banner_container.set_find_results(".recipient_not_subscribed", $banner);
+ $textarea.val(`Hello ${mention} here.`);
+ compose_validate.maybe_clear_stale_recipient_not_subscribed_warnings($textarea);
+ assert.ok(!removed_banners.has("canonical"));
+ }
+
+ // Banner removed when mention is deleted from compose text.
+ {
+ const $banner = make_banner("deleted", String(alice.user_id));
+ $banner_container.set_find_results(".recipient_not_subscribed", $banner);
+ $textarea.val("Hello, how are you?");
+ compose_validate.maybe_clear_stale_recipient_not_subscribed_warnings($textarea);
+ assert.ok(removed_banners.has("deleted"));
+ }
+
+ // Banner preserved for @**|user_id** form.
+ {
+ const $banner = make_banner("id-only", String(alice.user_id));
+ $banner_container.set_find_results(".recipient_not_subscribed", $banner);
+ $textarea.val(`Hello @**|${alice.user_id}** how are you?`);
+ compose_validate.maybe_clear_stale_recipient_not_subscribed_warnings($textarea);
+ assert.ok(!removed_banners.has("id-only"));
+ }
+
+ // Banner preserved for @**Name|user_id** form.
+ {
+ const $banner = make_banner("name-and-id", String(alice.user_id));
+ $banner_container.set_find_results(".recipient_not_subscribed", $banner);
+ $textarea.val(`Hello @**${alice.full_name}|${alice.user_id}** how are you?`);
+ compose_validate.maybe_clear_stale_recipient_not_subscribed_warnings($textarea);
+ assert.ok(!removed_banners.has("name-and-id"));
+ }
+
+ // Banner removed when only a silent mention is present, since
+ // the banner is not displayed for silent mentions.
+ {
+ const silent_mention = people.get_mention_syntax(alice.full_name, alice.user_id, true);
+ const $banner = make_banner("silent-only", String(alice.user_id));
+ $banner_container.set_find_results(".recipient_not_subscribed", $banner);
+ $textarea.val(`Hello ${silent_mention} here.`);
+ compose_validate.maybe_clear_stale_recipient_not_subscribed_warnings($textarea);
+ assert.ok(removed_banners.has("silent-only"));
+ }
+
+ // Banner removed when compose text is empty.
+ {
+ const $banner = make_banner("empty-text", String(alice.user_id));
+ $banner_container.set_find_results(".recipient_not_subscribed", $banner);
+ $textarea.val("");
+ compose_validate.maybe_clear_stale_recipient_not_subscribed_warnings($textarea);
+ assert.ok(removed_banners.has("empty-text"));
+ }
+
+ // Banner removed when mention syntax is incomplete.
+ {
+ const $banner = make_banner("incomplete", String(alice.user_id));
+ $banner_container.set_find_results(".recipient_not_subscribed", $banner);
+ $textarea.val(`Hello @**${alice.full_name} here.`);
+ compose_validate.maybe_clear_stale_recipient_not_subscribed_warnings($textarea);
+ assert.ok(removed_banners.has("incomplete"));
+ }
+
+ // Banner removed for unknown user_id.
+ {
+ const $banner = make_banner("unknown-user", "99999");
+ $banner_container.set_find_results(".recipient_not_subscribed", $banner);
+ $textarea.val("No mention here.");
+ compose_validate.maybe_clear_stale_recipient_not_subscribed_warnings($textarea);
+ assert.ok(removed_banners.has("unknown-user"));
+ }
+
+ // Banner removed when data-user-id attribute is missing.
+ {
+ const $banner = make_banner("no-user-id", undefined);
+ $banner_container.set_find_results(".recipient_not_subscribed", $banner);
+ $textarea.val("Some text.");
+ compose_validate.maybe_clear_stale_recipient_not_subscribed_warnings($textarea);
+ assert.ok(removed_banners.has("no-user-id"));
+ }
+});
+
test_ui("test warn_if_topic_resolved", ({override, mock_template}) => {
mock_banners();
$.reset_selector("#compose_banners .topic_resolved");