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.

recent_view: Update rows for changes to the users they show.

Aman Agrawale48c84204e27
diff --git a/web/src/recent_view_ui.ts b/web/src/recent_view_ui.ts
index b02efcef20c..cdcdaf27a7e 100644
--- a/web/src/recent_view_ui.ts
+++ b/web/src/recent_view_ui.ts
@@ -1223,6 +1223,43 @@ export function rewire_inplace_rerender(value: typeof inplace_rerender): void {
inplace_rerender = value;
}
+function conversation_shows_user(conversation: ConversationData, user_id: number): boolean {
+ const message = message_store.get(conversation.last_msg_id);
+ assert(message !== undefined);
+ if (message.type === "private") {
+ return people.get_participants_from_user_ids_string(message.to_user_ids).has(user_id);
+ }
+ const message_ids = recent_senders.get_topic_message_ids_for_sender(
+ message.stream_id,
+ message.topic,
+ user_id,
+ );
+ return message_ids.size > 0;
+}
+
+export function rerender_conversations_with_user(user_id: number): void {
+ if (!recent_view_util.is_visible()) {
+ return;
+ }
+ const is_searching = get_search_keyword() !== "";
+ const conversation_keys = new Set<string>();
+ for (const [key, conversation] of recent_view_data.conversations) {
+ if (!conversation_shows_user(conversation, user_id)) {
+ continue;
+ }
+ const has_row = get_conversation_row(key).length > 0;
+ // The search matches direct message conversations by their
+ // participants' names and emails.
+ const search_may_now_list_it = is_searching && conversation.type === "private";
+ if (has_row || search_may_now_list_it) {
+ conversation_keys.add(key);
+ }
+ }
+ if (conversation_keys.size > 0) {
+ bulk_inplace_rerender(conversation_keys);
+ }
+}
+
export function update_topic_visibility_policy(stream_id: number, topic: string): boolean {
const key = recent_view_util.get_topic_key(stream_id, topic);
if (!recent_view_data.conversations.has(key)) {
diff --git a/web/src/user_events.ts b/web/src/user_events.ts
index 298931011b7..32985a469da 100644
--- a/web/src/user_events.ts
+++ b/web/src/user_events.ts
@@ -18,6 +18,7 @@ import * as navbar_alerts from "./navbar_alerts.ts";
import * as people from "./people.ts";
import * as pm_list from "./pm_list.ts";
import * as reactions from "./reactions.ts";
+import * as recent_view_ui from "./recent_view_ui.ts";
import * as settings from "./settings.ts";
import * as settings_account from "./settings_account.ts";
import * as settings_bots from "./settings_bots.ts";
@@ -211,6 +212,16 @@ export const update_person = function update(event: UserUpdate): void {
user_profile.update_profile_modal_ui(user, event);
}
+ if (
+ "full_name" in event ||
+ "new_email" in event ||
+ "delivery_email" in event ||
+ "role" in event ||
+ "avatar_url" in event
+ ) {
+ recent_view_ui.rerender_conversations_with_user(event.user_id);
+ }
+
if ("custom_profile_field" in event) {
people.set_custom_profile_field_data(event.user_id, event.custom_profile_field);
user_profile.update_user_custom_profile_fields(user);
diff --git a/web/tests/recent_view.test.cjs b/web/tests/recent_view.test.cjs
index 3b95e5e6714..5fb99776367 100644
--- a/web/tests/recent_view.test.cjs
+++ b/web/tests/recent_view.test.cjs
@@ -153,8 +153,9 @@ mock_esm("../src/pm_list", {
update_private_messages: noop,
handle_message_view_deactivated: noop,
});
-mock_esm("../src/recent_senders", {
+const recent_senders = mock_esm("../src/recent_senders", {
get_topic_recent_senders: () => [2, 1],
+ get_topic_message_ids_for_sender: () => new Set(),
get_pm_recent_senders(user_ids_string) {
return {
participants: user_ids_string.split(",").map((user_id) => Number.parseInt(user_id, 10)),
@@ -979,6 +980,63 @@ test("inplace_rerender updates one row the way a bulk rerender does", ({override
assert.equal(updates.length, 2);
});
+test("rerender_conversations_with_user rerenders the rows that show the user", ({override}) => {
+ // A row shows the senders of its topic, or the participants of its
+ // direct message conversation. sender2 sent to topics 2 to 6 and is
+ // in the direct message conversations "2,3" and "2,4".
+ show_recent_view_with_messages();
+ for (const message of private_messages) {
+ rt_data.process_message(message);
+ }
+ override(recent_senders, "get_topic_message_ids_for_sender", (stream_id, topic, sender_id) => {
+ const messages_sent = messages.filter(
+ (message) =>
+ message.stream_id === stream_id &&
+ message.topic === topic &&
+ message.sender_id === sender_id,
+ );
+ return new Set(messages_sent.map((message) => message.id));
+ });
+ const rerendered_conversations = new Set();
+ override(ListWidget, "render_item", (conversation) => {
+ rerendered_conversations.add(conversation);
+ });
+ const inserted_conversations = new Set();
+ override(ListWidget, "insert_rendered_row", (conversation) => {
+ inserted_conversations.add(conversation);
+ });
+ override(ListWidget, "get_current_list", () => rt_data.get_conversations().values().toArray());
+ function rerender_conversations_with(user_id) {
+ rerendered_conversations.clear();
+ inserted_conversations.clear();
+ expected_data_to_replace_in_list_widget = rt_data.get_conversations().values().toArray();
+ rt.rerender_conversations_with_user(user_id);
+ }
+
+ stub_no_row_for(get_topic_key(stream1, topic6));
+ stub_no_row_for("2,4");
+ const rows_showing_sender2 = new Set([
+ ...[topic2, topic3, topic4, topic5].map((topic) => conversation_for(topic)),
+ rt_data.conversations.get("2,3"),
+ ]);
+ rerender_conversations_with(sender2);
+ assert.deepEqual(rerendered_conversations, rows_showing_sender2);
+ assert.equal(inserted_conversations.size, 0);
+
+ // A search matches direct message conversations by their
+ // participants, so it may now list one that has no row.
+ $("#recent_view_search").val("fred");
+ rerender_conversations_with(sender2);
+ assert.deepEqual(rerendered_conversations, rows_showing_sender2);
+ assert.deepEqual(inserted_conversations, new Set([rt_data.conversations.get("2,4")]));
+ $("#recent_view_search").val("");
+
+ // The mocked widget rejects the list update that a rerender would
+ // start with, as none is expected for a user in no conversation.
+ const user_id_in_no_conversation = 5;
+ rt.rerender_conversations_with_user(user_id_in_no_conversation);
+});
+
test("rerender renders more rows until they reach the bottom of the viewport", ({
override,
override_rewire,
diff --git a/web/tests/user_events.test.cjs b/web/tests/user_events.test.cjs
index 6325b04f998..4833308f403 100644
--- a/web/tests/user_events.test.cjs
+++ b/web/tests/user_events.test.cjs
@@ -59,6 +59,9 @@ mock_esm("../src/compose_recipient", {
const pm_list = mock_esm("../src/pm_list", {
update_private_messages() {},
});
+const recent_view_ui = mock_esm("../src/recent_view_ui", {
+ rerender_conversations_with_user() {},
+});
const buddy_data = new buddy_list.BuddyList();
buddy_list.buddy_list = buddy_data;
@@ -433,3 +436,49 @@ run_test("updates", ({override}) => {
assert.equal($navbar_title.text(), "placeholder");
message_lists.set_current(undefined);
});
+
+run_test("update_person rerenders the user's recent view rows", ({override}) => {
+ // Recent view rerenders from the user's data, so each change has to
+ // be in that data by the time recent view is called.
+ const users_at_rerender = [];
+ override(recent_view_ui, "rerender_conversations_with_user", (user_id) => {
+ users_at_rerender.push({...people.get_by_user_id(user_id)});
+ });
+ override(message_live_update, "update_user_full_name", noop);
+ override(message_live_update, "update_avatar", noop);
+ const marie = make_user({
+ email: "[email protected]",
+ delivery_email: null,
+ user_id: 36,
+ full_name: "Marie Curie",
+ });
+ people.add_active_user(marie);
+
+ const changes_recent_view_shows = [
+ {event: {full_name: "Maria Sklodowska"}, field: "full_name", value: "Maria Sklodowska"},
+ {event: {new_email: "[email protected]"}, field: "email", value: "[email protected]"},
+ {
+ event: {delivery_email: "[email protected]"},
+ field: "delivery_email",
+ value: "[email protected]",
+ },
+ {
+ event: {role: settings_config.user_role_values.guest.code},
+ field: "is_guest",
+ value: true,
+ },
+ {
+ event: {avatar_url: "http://gravatar.com/maria"},
+ field: "avatar_url",
+ value: "http://gravatar.com/maria",
+ },
+ ];
+ for (const [index, {event, field, value}] of changes_recent_view_shows.entries()) {
+ user_events.update_person({user_id: marie.user_id, ...event});
+ assert.equal(users_at_rerender.length, index + 1);
+ assert.equal(users_at_rerender[index][field], value);
+ }
+
+ user_events.update_person({user_id: marie.user_id, timezone: "UTC"});
+ assert.equal(users_at_rerender.length, changes_recent_view_shows.length);
+});