Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
user_events: Refresh DM pill and compose placeholder on user rename.
Shubham Padiaeaed7127eba0
diff --git a/web/src/compose_pm_pill.ts b/web/src/compose_pm_pill.tsindex 6a9f600ab27..6afc4d67679 100644--- a/web/src/compose_pm_pill.ts+++ b/web/src/compose_pm_pill.ts@@ -127,6 +127,13 @@ export function update_user_pill_active_status(user: User, is_active: boolean):widget.updatePill(pill.$element[0]!, updated_pill);}+export function update_user_pill_full_name(user_id: number, full_name: string): void {+ if (!widget) {+ return;+ }+ user_pill.update_pill_full_name(widget, user_id, full_name);+}+export function rewire_widget(value: UserPillWidget): void {widget = value;}diff --git a/web/src/compose_recipient.ts b/web/src/compose_recipient.tsindex 545cde94ef5..573f1d94acd 100644--- a/web/src/compose_recipient.ts+++ b/web/src/compose_recipient.ts@@ -307,6 +307,19 @@ export function possibly_update_stream_name_in_compose(stream_id: number): void}}+export function update_user_name_in_compose(user_id: number, full_name: string): void {+ // Only a current DM recipient's rename affects this compose box, so+ // ignore renames of anyone else.+ if (!compose_pm_pill.get_user_ids().includes(user_id)) {+ return;+ }+ // DM pills carry their own captured full_name and aren't rebuilt by+ // on_compose_select_recipient_update, so we update the pill in place+ // and refresh the textarea placeholder text.+ compose_pm_pill.update_user_pill_full_name(user_id, full_name);+ update_compose_area_placeholder_text();+}+function item_click_callback(event: JQuery.ClickEvent, dropdown: tippy.Instance): void {const recipient_id_str = $(event.currentTarget).attr("data-unique-id");assert(recipient_id_str !== undefined);diff --git a/web/src/user_events.ts b/web/src/user_events.tsindex 8cb80f24224..01bf04d2ac9 100644--- a/web/src/user_events.ts+++ b/web/src/user_events.ts@@ -11,6 +11,7 @@ import * as blueslip from "./blueslip.ts";import * as bot_data from "./bot_data.ts";import {buddy_list} from "./buddy_list.ts";import * as compose_pm_pill from "./compose_pm_pill.ts";+import * as compose_recipient from "./compose_recipient.ts";import * as message_live_update from "./message_live_update.ts";import * as message_view_header from "./message_view_header.ts";import * as navbar_alerts from "./navbar_alerts.ts";@@ -107,6 +108,7 @@ export const update_person = function update(event: UserUpdate): void {reactions.update_user_full_name(event.user_id);pm_list.update_private_messages();message_view_header.maybe_update_navbar_title_for_user(event.user_id);+ compose_recipient.update_user_name_in_compose(event.user_id, event.full_name);user_profile.update_profile_modal_ui(user, event);if (people.is_my_user_id(event.user_id)) {current_user.full_name = event.full_name;diff --git a/web/src/user_pill.ts b/web/src/user_pill.tsindex 1f3dcf4c201..b654686fc7f 100644--- a/web/src/user_pill.ts+++ b/web/src/user_pill.ts@@ -186,6 +186,18 @@ export function get_display_value_from_item(item: UserPill): string {return item.full_name ?? item.email;}+export function update_pill_full_name(+ pill_widget: UserPillWidget,+ user_id: number,+ full_name: string,+): void {+ const pill = pill_widget.getPillByPredicate((item) => item.user_id === user_id);+ if (!pill) {+ return;+ }+ pill_widget.updatePill(pill.$element[0]!, {...pill.item, full_name});+}+export function generate_pill_html(item: UserPill, show_user_status_emoji = false): string {let status_emoji_info;let has_status;diff --git a/web/tests/compose_pm_pill.test.cjs b/web/tests/compose_pm_pill.test.cjsindex 971974b8fb1..a13880adf2a 100644--- a/web/tests/compose_pm_pill.test.cjs+++ b/web/tests/compose_pm_pill.test.cjs@@ -276,3 +276,25 @@ run_test("update_user_pill_active_status", ({override_rewire}) => {assert.ok(!pill_updated);});++run_test("update_user_pill_full_name", ({override_rewire}) => {+ // The pill-update logic lives in user_pill.update_pill_full_name and+ // is tested there; here we just verify the wrapper delegates when+ // the widget is set, and is a no-op otherwise.+ let user_pill_function_called = false;+ const widget = {+ getPillByPredicate() {+ user_pill_function_called = true;+ return undefined;+ },+ updatePill() {},+ };++ override_rewire(compose_pm_pill, "widget", undefined);+ compose_pm_pill.update_user_pill_full_name(1, "Othello the Moor");+ assert.ok(!user_pill_function_called);++ override_rewire(compose_pm_pill, "widget", widget);+ compose_pm_pill.update_user_pill_full_name(1, "Othello the Moor");+ assert.ok(user_pill_function_called);+});diff --git a/web/tests/example4.test.cjs b/web/tests/example4.test.cjsindex 15693b18c6e..d372c85b535 100644--- a/web/tests/example4.test.cjs+++ b/web/tests/example4.test.cjs@@ -63,6 +63,9 @@ const pm_list = mock_esm("../src/pm_list");const settings_bots = mock_esm("../src/settings_bots");const settings_users = mock_esm("../src/settings_users");const user_profile = mock_esm("../src/user_profile");+mock_esm("../src/compose_recipient", {+ update_user_name_in_compose() {},+});mock_esm("../src/message_view_header", {maybe_update_navbar_title_for_user() {},});diff --git a/web/tests/user_events.test.cjs b/web/tests/user_events.test.cjsindex c12d609e851..f166fe5401e 100644--- a/web/tests/user_events.test.cjs+++ b/web/tests/user_events.test.cjs@@ -49,6 +49,9 @@ const buddy_list = mock_esm("../src/buddy_list", {const compose_pm_pill = mock_esm("../src/compose_pm_pill", {update_user_pill_active_status() {},});+mock_esm("../src/compose_recipient", {+ update_user_name_in_compose() {},+});const pm_list = mock_esm("../src/pm_list", {update_private_messages() {},});diff --git a/web/tests/user_pill.test.cjs b/web/tests/user_pill.test.cjsindex 3ad1be4f7ab..9a88ea527fa 100644--- a/web/tests/user_pill.test.cjs+++ b/web/tests/user_pill.test.cjs@@ -212,3 +212,23 @@ test("typeahead", () => {const result = user_pill.typeahead_source(pill_widget);assert.deepEqual(result, [{type: "user", user: alice}]);});++test("update_pill_full_name", () => {+ const pill = {item: {...isaac_item}, $element: {0: {}}};+ let pill_updated = false;+ pill_widget.getPillByPredicate = (predicate) => (predicate(pill.item) ? pill : undefined);+ pill_widget.updatePill = (_element, item) => {+ pill.item = item;+ pill_updated = true;+ };++ assert.equal(pill.item.full_name, "Isaac Newton");+ user_pill.update_pill_full_name(pill_widget, isaac.user_id, "Sir Isaac Newton");+ assert.equal(pill.item.full_name, "Sir Isaac Newton");+ assert.ok(pill_updated);++ // No-op when no matching pill.+ pill_updated = false;+ user_pill.update_pill_full_name(pill_widget, 999, "Nobody");+ assert.ok(!pill_updated);+});