Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
markdown: Handle mention pill behavior for inaccessible users.
Sahil Batrace6dc40a42a7
diff --git a/web/src/rendered_markdown.js b/web/src/rendered_markdown.jsindex 0923345c792..67f8d0beae2 100644--- a/web/src/rendered_markdown.js+++ b/web/src/rendered_markdown.js@@ -134,16 +134,27 @@ export const update_elements = ($content) => {}if (user_id && user_id !== "*" && !$(this).find(".highlight").length) {- // If it's a mention of a specific user, edit the- // mention text to show the user's current name,- // assuming that you're not searching for text- // inside the highlight.+ // If it's a mention of a specific user, edit the mention+ // text to show the user's current name, assuming that+ // you're not searching for text inside the highlight.const person = people.maybe_get_user_by_id(user_id, true);- if (person !== undefined) {+ if (person === undefined || person.is_inaccessible_user) {// Note that person might be undefined in some- // unpleasant corner cases involving data import.- set_name_in_mention_element(this, person.full_name, user_id);+ // unpleasant corner cases involving data import+ // or when guest users cannot access all users in+ // the organization.+ //+ // In these cases, the best we can do is leave the+ // existing name in the existing mention pill+ // HTML. Clicking on the pill will show the the+ // "Unknown user" popover.+ if (person === undefined) {+ people.add_inaccessible_user(user_id);+ }+ return;}++ set_name_in_mention_element(this, person.full_name, user_id);}});diff --git a/web/src/user_card_popover.js b/web/src/user_card_popover.jsindex 353976728cf..24d15ca616b 100644--- a/web/src/user_card_popover.js+++ b/web/src/user_card_popover.js@@ -664,6 +664,15 @@ function register_click_handlers() {user = people.get_by_user_id(user_id);} else {user = people.get_by_email(email);+ if (user === undefined) {+ // There can be a case when user is undefined if+ // the user is an inaccessible user as we do not+ // create the fake user objects for it because+ // we do not have user ID. It is fine to not+ // open popover for this case as such cases+ // without user ID are rare and old.+ return;+ }}toggle_user_card_popover_for_message(this, user, message);});diff --git a/web/tests/rendered_markdown.test.js b/web/tests/rendered_markdown.test.jsindex b694f79aa1d..bb1a791d0ca 100644--- a/web/tests/rendered_markdown.test.js+++ b/web/tests/rendered_markdown.test.js@@ -49,6 +49,8 @@ const polonius = {full_name: "Polonius",is_guest: true,};+const inaccessible_user_id = 33;+const inaccessible_user = people.add_inaccessible_user(inaccessible_user_id);people.init();people.add_active_user(iago);people.add_active_user(cordelia);@@ -230,6 +232,29 @@ run_test("user-mention without guest indicator", () => {assert.equal($polonius.text(), `@${polonius.full_name}`);});+run_test("user-mention of inaccessible users", () => {+ const $content = get_content_element();+ const $othello = $.create("user-mention(othello)");+ $othello.set_find_results(".highlight", false);+ $othello.attr("data-user-id", inaccessible_user_id);+ $othello.text("@Othello");+ $content.set_find_results(".user-mention", $array([$othello]));++ rm.update_elements($content);+ assert.equal($othello.text(), "@Othello");+ assert.notEqual($othello.text(), `@${inaccessible_user.full_name}`);++ // Test inaccessible user id with no user object.+ const $cordelia = $.create("user-mention(cordelia)");+ $cordelia.set_find_results(".highlight", false);+ $cordelia.attr("data-user-id", 40);+ $cordelia.text("@Cordelia");+ $content.set_find_results(".user-mention", $array([$cordelia]));++ rm.update_elements($content);+ assert.equal($cordelia.text(), "@Cordelia");+});+run_test("user-mention (stream wildcard)", () => {// Setupconst $content = get_content_element();