Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
message_fetch: Update recent view for newly discovered unreads.
Aman Agrawala4c8125c902a
diff --git a/web/src/message_events.ts b/web/src/message_events.tsindex 0ada3500ac7..40537df0e72 100644--- a/web/src/message_events.ts+++ b/web/src/message_events.ts@@ -324,7 +324,7 @@ export function insert_new_messages(opts: InsertNewMessagesOpts): Message[] {messages = local_messages;}- const any_untracked_unread_messages = unread.process_loaded_messages(messages, false);+ const untracked_unread_messages = unread.process_loaded_messages(messages, false);direct_message_group_data.process_loaded_messages(messages);let need_user_to_scroll = false;@@ -386,7 +386,7 @@ export function insert_new_messages(opts: InsertNewMessagesOpts): Message[] {});}- if (any_untracked_unread_messages) {+ if (untracked_unread_messages.length > 0) {unread_ui.update_unread_counts();}diff --git a/web/src/message_fetch.ts b/web/src/message_fetch.tsindex bce5e66ce15..2663cbeb9a1 100644--- a/web/src/message_fetch.ts+++ b/web/src/message_fetch.ts@@ -25,6 +25,7 @@ import {page_params} from "./page_params.ts";import * as popup_banners from "./popup_banners.ts";import {recent_view_messages_data} from "./recent_view_messages_data.ts";import * as recent_view_ui from "./recent_view_ui.ts";+import * as recent_view_util from "./recent_view_util.ts";import {get_retry_backoff_seconds} from "./retry_backoff.ts";import {narrow_operator_schema} from "./state_data.ts";import type {NarrowTerm} from "./state_data.ts";@@ -157,12 +158,21 @@ export function fetch_more_if_required_for_current_msg_list(}export function do_unread_count_updates(messages: Message[], expect_no_new_unreads = false): void {- const any_new_unreads = unread.process_loaded_messages(messages, expect_no_new_unreads);+ const untracked_unread_messages = unread.process_loaded_messages(+ messages,+ expect_no_new_unreads,+ );- if (any_new_unreads) {+ if (untracked_unread_messages.length > 0) {// The following operations are expensive, and thus should// only happen if we found any unread messages justifying it.unread_ui.update_unread_counts();+ const conversation_keys = new Set(+ untracked_unread_messages.map((message) =>+ recent_view_util.get_key_from_message(message),+ ),+ );+ recent_view_ui.bulk_inplace_rerender(conversation_keys);}}diff --git a/web/src/unread.ts b/web/src/unread.tsindex 6b0bc2983f8..825dba005b4 100644--- a/web/src/unread.ts+++ b/web/src/unread.ts@@ -770,17 +770,17 @@ export function update_unread_topic_name_case(export function process_loaded_messages(messages: Message[],expect_no_new_unreads = false,-): boolean {+): Message[] {// Process a set of messages that we have full copies of from the// server for whether any are unread but not tracked as such by// our data structures. This can occur due to old_unreads_missing,// changes in muting configuration, innocent races, or potentially bugs.//- // Returns whether there were any new unread messages; in that- // case, the caller will need to trigger a rerender of UI+ // Returns the messages newly discovered to be unread; if there+ // are any, the caller will need to trigger a rerender of UI// displaying unread counts.- let any_untracked_unread_messages = false;+ const untracked_unread_messages: Message[] = [];for (const message of messages) {if (message.unread) {if (unread_messages.has(message.id)) {@@ -816,11 +816,11 @@ export function process_loaded_messages(unread: true,});}- any_untracked_unread_messages = true;+ untracked_unread_messages.push(message);}}- return any_untracked_unread_messages;+ return untracked_unread_messages;}type UnreadMessageData = {diff --git a/web/tests/message_fetch.test.cjs b/web/tests/message_fetch.test.cjsindex c5d2d272335..08c0c412151 100644--- a/web/tests/message_fetch.test.cjs+++ b/web/tests/message_fetch.test.cjs@@ -2,13 +2,18 @@const assert = require("node:assert/strict");-const {zrequire} = require("./lib/namespace.cjs");+const {mock_esm, zrequire} = require("./lib/namespace.cjs");const {run_test} = require("./lib/test.cjs");const blueslip = require("./lib/zblueslip.cjs");+const recent_view_ui = mock_esm("../src/recent_view_ui");+const unread_ui = mock_esm("../src/unread_ui");+const {Filter} = zrequire("filter");const {MessageListData} = zrequire("message_list_data");const message_fetch = zrequire("message_fetch");+const {get_topic_key} = zrequire("recent_view_util");+const unread = zrequire("unread");run_test("get_parameters_for_message_fetch_api date anchor", () => {const msg_list_data = new MessageListData({@@ -38,3 +43,33 @@ run_test("get_parameters_for_message_fetch_api date anchor", () => {});assert.equal(missing_date.anchor_date, undefined);});++run_test("do_unread_count_updates", ({override}) => {+ unread.declare_bankruptcy();+ const stream_id = 5;+ const messages = [+ {id: 1, type: "stream", stream_id, topic: "Lunch", unread: true},+ {id: 2, type: "stream", stream_id, topic: "lunch", unread: true},+ {id: 3, type: "stream", stream_id, topic: "read", unread: false},+ ];++ let unread_counts_updated = false;+ override(unread_ui, "update_unread_counts", () => {+ unread_counts_updated = true;+ });+ let rerendered_conversation_keys;+ override(recent_view_ui, "bulk_inplace_rerender", (conversation_keys) => {+ rerendered_conversation_keys = conversation_keys;+ });++ message_fetch.do_unread_count_updates(messages);+ assert.ok(unread_counts_updated);+ assert.deepEqual(rerendered_conversation_keys, new Set([get_topic_key(stream_id, "lunch")]));++ // Fetching the same messages again discovers nothing new.+ unread_counts_updated = false;+ rerendered_conversation_keys = undefined;+ message_fetch.do_unread_count_updates(messages);+ assert.ok(!unread_counts_updated);+ assert.equal(rerendered_conversation_keys, undefined);+});diff --git a/web/tests/unread.test.cjs b/web/tests/unread.test.cjsindex f1355f71c99..2c181b36c46 100644--- a/web/tests/unread.test.cjs+++ b/web/tests/unread.test.cjs@@ -553,6 +553,30 @@ test("get_unread_message_conversation_key", () => {assert.equal(unread.get_unread_message_conversation_key(stream_message.id), undefined);});+test("process_loaded_messages returns newly tracked unreads", () => {+ const unread_message = {+ id: 31,+ type: "stream",+ stream_id: social.stream_id,+ topic: "lunch",+ unread: true,+ };+ const read_message = {+ id: 32,+ type: "stream",+ stream_id: social.stream_id,+ topic: "lunch",+ unread: false,+ };+ assert.deepEqual(unread.process_loaded_messages([unread_message, read_message]), [+ unread_message,+ ]);+ assert.equal(unread.num_unread_for_topic(social.stream_id, "lunch"), 1);++ // Messages already tracked as unread are not reported again.+ assert.deepEqual(unread.process_loaded_messages([unread_message, read_message]), []);+});+test("private_messages", () => {const alice = make_user({email: "[email protected]",