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 reads of messages never fetched here.

Aman Agrawalfe42fd5822f8
diff --git a/web/src/recent_view_ui.ts b/web/src/recent_view_ui.ts
index 82cf698c285..1a500318366 100644
--- a/web/src/recent_view_ui.ts
+++ b/web/src/recent_view_ui.ts
@@ -1232,11 +1232,6 @@ export function update_topic_visibility_policy(stream_id: number, topic: string)
return true;
}
-export function update_topic_unread_count(message: Message): void {
- const topic_key = recent_view_util.get_key_from_message(message);
- inplace_rerender(topic_key);
-}
-
export function set_filter(filter: string): void {
// This function updates the `filters` variable
// after user clicks on one of the filter buttons
diff --git a/web/src/unread.ts b/web/src/unread.ts
index 98b9740c464..6b0bc2983f8 100644
--- a/web/src/unread.ts
+++ b/web/src/unread.ts
@@ -721,6 +721,16 @@ export function get_unread_messages(messages: Message[]): Message[] {
return messages.filter((message) => unread_messages.has(message.id));
}
+// The key recent view uses for the message's conversation, known here
+// even for a message the client never fetched.
+export function get_unread_message_conversation_key(message_id: number): string | undefined {
+ const stream_topic = unread_topic_counter.reverse_lookup.get(message_id);
+ if (stream_topic !== undefined) {
+ return recent_view_util.get_topic_key(stream_topic.stream_id, stream_topic.topic);
+ }
+ return unread_direct_message_counter.reverse_lookup.get(message_id);
+}
+
export function get_unread_message_count(): number {
return unread_messages.size;
}
diff --git a/web/src/unread_ops.ts b/web/src/unread_ops.ts
index b8c5877fde2..00e28b51a20 100644
--- a/web/src/unread_ops.ts
+++ b/web/src/unread_ops.ts
@@ -23,6 +23,7 @@ import * as modals from "./modals.ts";
import * as people from "./people.ts";
import * as popup_banners from "./popup_banners.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 type {MessageDetails} from "./server_event_types.ts";
import type {NarrowTerm} from "./state_data.ts";
@@ -393,7 +394,6 @@ function process_newly_read_message(
msg_list.view.show_message_as_read(message, options);
}
desktop_notifications.close_notification(message.id);
- recent_view_ui.update_topic_unread_count(message);
}
export function mark_as_unread_from_here(message_id: number): void {
@@ -669,18 +669,23 @@ export function process_read_messages_event(message_ids: number[]): void {
return;
}
+ const conversation_keys = new Set<string>();
for (const message_id of message_ids) {
+ // The unread data forgets a message's conversation once it is read.
+ const conversation_key = unread.get_unread_message_conversation_key(message_id);
+ if (conversation_key !== undefined) {
+ conversation_keys.add(conversation_key);
+ }
unread.mark_as_read(message_id);
const message = message_store.get(message_id);
-
- // TODO: This ends up doing one in-place rerender operation on
- // recent conversations per message, not a single global
- // rerender or one per conversation.
if (message) {
process_newly_read_message(message, options);
}
}
+ if (conversation_keys.size > 0) {
+ recent_view_ui.bulk_inplace_rerender(conversation_keys);
+ }
if (message_lists.current !== undefined && !message_lists.current.has_unread_messages()) {
unread_ui.hide_unread_banner();
@@ -788,10 +793,13 @@ export function notify_server_messages_read(
message_flags.send_read(messages);
+ const conversation_keys = new Set<string>();
for (const message of messages) {
unread.mark_as_read(message.id);
process_newly_read_message(message, options);
+ conversation_keys.add(recent_view_util.get_key_from_message(message));
}
+ recent_view_ui.bulk_inplace_rerender(conversation_keys);
unread_ui.update_unread_counts();
}
diff --git a/web/tests/recent_view.test.cjs b/web/tests/recent_view.test.cjs
index 33680e0af88..3b95e5e6714 100644
--- a/web/tests/recent_view.test.cjs
+++ b/web/tests/recent_view.test.cjs
@@ -919,17 +919,6 @@ test("test_filter_participated", ({mock_template}) => {
rt.process_messages([messages[4]]);
});
-test("test_update_unread_count", () => {
- recent_view_util.set_visible(false);
- rt.clear_for_tests();
- stub_out_filter_buttons();
- rt.process_messages(messages);
-
- // update a message
- generate_topic_data([[1, "topic-7", 1, all_visibility_policies.INHERIT]]);
- rt.update_topic_unread_count(messages[9]);
-});
-
test("bulk_inplace_rerender updates the requested rows in list order", ({override}) => {
show_recent_view_with_messages();
const [with_row, not_requested, without_row] = [topic1, topic2, topic3].map((topic) =>
diff --git a/web/tests/unread.test.cjs b/web/tests/unread.test.cjs
index 211df05d847..f1355f71c99 100644
--- a/web/tests/unread.test.cjs
+++ b/web/tests/unread.test.cjs
@@ -14,6 +14,7 @@ const {FoldDict} = zrequire("fold_dict");
const message_store = zrequire("message_store");
const user_topics = zrequire("user_topics");
const people = zrequire("people");
+const recent_view_util = zrequire("recent_view_util");
const stream_data = zrequire("stream_data");
const sub_store = zrequire("sub_store");
const unread = zrequire("unread");
@@ -497,6 +498,61 @@ test("private_messages", () => {
test_notifiable_count(counts.home_unread_messages, 0);
});
+test("get_unread_message_conversation_key", () => {
+ const stream_message = {
+ id: 21,
+ type: "stream",
+ stream_id: social.stream_id,
+ topic: "Lunch",
+ unread: true,
+ };
+ const dm_message = {
+ id: 22,
+ type: "private",
+ display_recipient: [{id: anybody.user_id}, {id: me.user_id}],
+ unread: true,
+ };
+ unread.process_loaded_messages([stream_message, dm_message]);
+
+ // The keys match recent view's conversation keys, and come from the
+ // unread data rather than message_store, so they are available for
+ // messages this client never fetched.
+ assert.equal(
+ unread.get_unread_message_conversation_key(stream_message.id),
+ recent_view_util.get_topic_key(stream_message.stream_id, stream_message.topic),
+ );
+ assert.equal(
+ unread.get_unread_message_conversation_key(dm_message.id),
+ anybody.user_id.toString(),
+ );
+
+ // The server's unread data names a group DM by all its participants,
+ // and recent view's key leaves out the current user.
+ const other_user_id = 101;
+ unread.initialize({
+ unread_msgs: {
+ pms: [],
+ huddles: [
+ {
+ user_ids_string: `${me.user_id},${other_user_id},${anybody.user_id}`,
+ unread_message_ids: [24],
+ },
+ ],
+ streams: [],
+ mentions: [],
+ old_unreads_missing: false,
+ },
+ });
+ assert.equal(
+ unread.get_unread_message_conversation_key(24),
+ `${other_user_id},${anybody.user_id}`,
+ );
+ assert.equal(unread.get_unread_message_conversation_key(23), undefined);
+
+ unread.mark_as_read(stream_message.id);
+ assert.equal(unread.get_unread_message_conversation_key(stream_message.id), undefined);
+});
+
test("private_messages", () => {
const alice = make_user({
diff --git a/web/tests/unread_ops.test.cjs b/web/tests/unread_ops.test.cjs
index 8b332122213..a630b62b807 100644
--- a/web/tests/unread_ops.test.cjs
+++ b/web/tests/unread_ops.test.cjs
@@ -2,9 +2,19 @@
const assert = require("node:assert/strict");
-const {zrequire} = require("./lib/namespace.cjs");
-const {run_test} = require("./lib/test.cjs");
+const {mock_esm, zrequire} = require("./lib/namespace.cjs");
+const {run_test, noop} = require("./lib/test.cjs");
+mock_esm("../src/desktop_notifications", {close_notification: noop});
+mock_esm("../src/message_flags", {send_read: noop});
+mock_esm("../src/unread_ui", {
+ hide_unread_banner: noop,
+ update_unread_counts: noop,
+});
+const recent_view_ui = mock_esm("../src/recent_view_ui");
+
+const message_store = zrequire("message_store");
+const {get_topic_key} = zrequire("recent_view_util");
const unread = zrequire("unread");
const unread_ops = zrequire("unread_ops");
@@ -29,3 +39,107 @@ run_test("get_message_count_text", () => {
"translated: 1 message will be marked as read.",
);
});
+
+const stream_id = 7;
+const dm_user_ids_string = "5";
+
+function track_unread_stream_message(id, topic) {
+ unread.process_unread_message({
+ id,
+ type: "stream",
+ stream_id,
+ topic,
+ unread: true,
+ mentioned: false,
+ mentioned_me_directly: false,
+ });
+}
+
+function track_unread_direct_message(id) {
+ unread.process_unread_message({
+ id,
+ type: "private",
+ user_ids_string: dm_user_ids_string,
+ unread: true,
+ mentioned: false,
+ mentioned_me_directly: false,
+ });
+}
+
+run_test("process_read_messages_event", ({override}) => {
+ unread.declare_bankruptcy();
+ // Only the first message was fetched; the others are known to the
+ // unread data alone, as with reads on another client of messages
+ // older than what this client has loaded.
+ const fetched_message = {id: 101, type: "stream", stream_id, topic: "Lunch", unread: true};
+ message_store.set_messages_for_tests([{message: fetched_message}]);
+ track_unread_stream_message(101, "Lunch");
+ track_unread_stream_message(102, "lunch");
+ track_unread_stream_message(103, "dinner");
+ track_unread_direct_message(104);
+
+ let rerendered_conversation_keys;
+ override(recent_view_ui, "bulk_inplace_rerender", (conversation_keys) => {
+ rerendered_conversation_keys = conversation_keys;
+ });
+
+ // Recent view rerenders each affected conversation once, whether or
+ // not its messages were fetched. Message 105 is not unread.
+ unread_ops.process_read_messages_event([101, 102, 103, 104, 105]);
+ assert.deepEqual(
+ rerendered_conversation_keys,
+ new Set([
+ get_topic_key(stream_id, "lunch"),
+ get_topic_key(stream_id, "dinner"),
+ dm_user_ids_string,
+ ]),
+ );
+ assert.equal(fetched_message.unread, false);
+ assert.equal(unread.get_unread_message_count(), 0);
+
+ // Nothing happens for messages that are already read.
+ rerendered_conversation_keys = undefined;
+ unread_ops.process_read_messages_event([101, 102]);
+ assert.equal(rerendered_conversation_keys, undefined);
+
+ // A message tracked without a conversation, as a direct message with
+ // no recipients would be, leaves recent view alone.
+ unread.process_unread_message({
+ id: 106,
+ type: "private",
+ user_ids_string: "",
+ unread: true,
+ mentioned: false,
+ mentioned_me_directly: false,
+ });
+ unread_ops.process_read_messages_event([106]);
+ assert.equal(rerendered_conversation_keys, undefined);
+ assert.equal(unread.get_unread_message_count(), 0);
+});
+
+run_test("notify_server_messages_read", ({override}) => {
+ // Recent view rerenders each conversation once: the first two messages
+ // share a topic, and the last one is already read.
+ unread.declare_bankruptcy();
+ const messages = [
+ {id: 201, type: "stream", stream_id, topic: "Lunch", unread: true},
+ {id: 202, type: "stream", stream_id, topic: "lunch", unread: true},
+ {id: 203, type: "private", to_user_ids: dm_user_ids_string, unread: true},
+ {id: 204, type: "stream", stream_id, topic: "already read", unread: false},
+ ];
+ track_unread_stream_message(201, "Lunch");
+ track_unread_stream_message(202, "lunch");
+ track_unread_direct_message(203);
+
+ let rerendered_conversation_keys;
+ override(recent_view_ui, "bulk_inplace_rerender", (conversation_keys) => {
+ rerendered_conversation_keys = conversation_keys;
+ });
+
+ unread_ops.notify_server_messages_read(messages);
+ assert.deepEqual(
+ rerendered_conversation_keys,
+ new Set([get_topic_key(stream_id, "lunch"), dm_user_ids_string]),
+ );
+ assert.equal(unread.get_unread_message_count(), 0);
+});