Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
topic_generator: Check topics in current channel before next channel.
Aman Agrawal04161a0cfd83
diff --git a/web/src/message_view.ts b/web/src/message_view.tsindex 871ddc38ecb..ebce99ae080 100644--- a/web/src/message_view.ts+++ b/web/src/message_view.ts@@ -272,6 +272,17 @@ function create_and_update_message_list(opts.show_more_topics = browser_history.get_current_state_show_more_topics() ?? false;}+ // To keep the behaviour of `n` key consistent and the memory of+ // `topics_kept_unread_by_user` as recent as possible, we clear it.+ if (+ !opts.trigger ||+ !["next_topic_unread_hotkey", "old_unreads_missing", "retarget message location"].includes(+ opts.trigger,+ )+ ) {+ topic_generator.reset_topics_kept_unread_by_user();+ }+// Show the new set of messages. It is important to set message_lists.current to// the view right as it's being shown, because we rely on message_lists.current// being shown for deciding when to condense messages.diff --git a/web/src/topic_generator.ts b/web/src/topic_generator.tsindex 3f3af3ddd6c..68363ceee50 100644--- a/web/src/topic_generator.ts+++ b/web/src/topic_generator.ts@@ -8,6 +8,23 @@ import * as stream_topic_history from "./stream_topic_history.ts";import * as unread from "./unread.ts";import * as user_topics from "./user_topics.ts";+// If there are any unreads in the current topic,+// user likely wants to avoid reading them right now.+// So, we add them to `topics_kept_unread_by_user`+// which we will navigate to later.+//+// Why first unread message ID?+// User wants to start reading from that message later,+// and can follow `moved` breadcrumbs to read further if+// the later messages in the topic were moved.+//+// Contains the first unread message in the topic.+let topics_kept_unread_by_user = new Set<number>();++export function reset_topics_kept_unread_by_user(): void {+ topics_kept_unread_by_user.clear();+}+export function next_topic(sorted_channels_info: {channel_id: number;@@ -22,21 +39,29 @@ export function next_topic(? sorted_channels_info.findIndex(({channel_id}) => channel_id === curr_stream_id): -1;- // 1. Find any unreads in the current channel after the current topic.if (curr_stream_index >= 0) {const {channel_id} = sorted_channels_info[curr_stream_index]!;const topics = get_topics(channel_id);const curr_topic_index = curr_topic !== undefined ? topics.indexOf(curr_topic) : -1; // -1 if not found+ // 1. Find any unreads in the current channel after the current topic.for (let i = curr_topic_index + 1; i < topics.length; i += 1) {const topic = topics[i]!;if (has_unread_messages(channel_id, topic)) {return {stream_id: channel_id, topic};}}++ // 2. Find any unreads in the current channel before the current topic.+ for (let i = 0; i < curr_topic_index; i += 1) {+ const topic = topics[i]!;+ if (has_unread_messages(channel_id, topic)) {+ return {stream_id: channel_id, topic};+ }+ }}- // 2. Find any unreads after the current channel in uncollapsed folders.+ // 3. Find any unreads after the current channel in uncollapsed folders.for (let i = curr_stream_index + 1; i < sorted_channels_info.length; i += 1) {const channel_info = sorted_channels_info[i]!;if (channel_info.is_collapsed) {@@ -51,8 +76,8 @@ export function next_topic(// `sorted_channels_info`: First has uncollapsed channels,// then collapsed ones.- // 3. Find any unreads before the current channel:topic.- // 4. Find any unreads in collapsed channels.+ // 4. Find any unreads before the current channel:topic.+ // 5. Find any unreads in collapsed channels.let reached_current_narrow_state = false;for (const channel_info of sorted_channels_info) {if (reached_current_narrow_state && !channel_info.is_collapsed) {@@ -82,7 +107,7 @@ export function next_topic(}}- // 5. No unread topic found.+ // 6. No unread topic found.return undefined;}@@ -147,11 +172,41 @@ export function get_next_topic(return topics;}+ // Remember that user chose to keep some messages unread in the current topic.+ if (+ narrow_state.narrowed_by_topic_reply() &&+ curr_stream_id !== undefined &&+ curr_topic !== undefined+ ) {+ const topic_unread_msg_ids = unread.get_msg_ids_for_topic(curr_stream_id, curr_topic);+ if (topic_unread_msg_ids.length > 0) {+ topics_kept_unread_by_user.add(topic_unread_msg_ids[0]!);+ }+ }++ function has_unread_messages(channel_id: number, topic: string): boolean {+ const topic_unread_msg_ids = unread.get_msg_ids_for_topic(channel_id, topic);+ if (topic_unread_msg_ids.length > 0) {+ // If user chose to keep some messages unread in this topic,+ // skip it for now and come back to it later.+ if (topic_unread_msg_ids.some((msg_id) => topics_kept_unread_by_user.has(msg_id))) {+ // Remove these msg_ids from the set so that+ // we don't skip this topic next time.+ topics_kept_unread_by_user = topics_kept_unread_by_user.difference(+ new Set(topic_unread_msg_ids),+ );+ return false;+ }+ return true;+ }+ return false;+ }+if (only_followed_topics) {return next_topic(sorted_channels_info,get_followed_topics,- unread.topic_has_any_unread,+ has_unread_messages,curr_stream_id,curr_topic,);@@ -160,7 +215,7 @@ export function get_next_topic(return next_topic(sorted_channels_info,get_unmuted_topics,- unread.topic_has_any_unread,+ has_unread_messages,curr_stream_id,curr_topic,);diff --git a/web/tests/topic_generator.test.cjs b/web/tests/topic_generator.test.cjsindex 5a8b0989bc9..c05cc73cbce 100644--- a/web/tests/topic_generator.test.cjs+++ b/web/tests/topic_generator.test.cjs@@ -39,12 +39,21 @@ run_test("streams", ({override}) => {run_test("topics", () => {const {next_topic} = tg;- function make_next_topic(sorted_channels_info, topics_by_stream, unread_topic_names) {+ function make_next_topic(+ sorted_channels_info,+ topics_by_stream,+ unread_topic_names,+ topics_kept_unread_by_user = new Set(),+ ) {function get_topics(stream_id) {return topics_by_stream.get(stream_id) ?? [];}function has_unread_messages(_stream_id, topic) {+ if (topics_kept_unread_by_user.has(topic)) {+ topics_kept_unread_by_user.delete(topic);+ return false;+ }return unread_topic_names.has(topic);}@@ -91,13 +100,27 @@ run_test("topics", () => {[2, ["b1"]],]);const unread_topic_names = new Set(["a1", "a2", "b1"]);-- const next = make_next_topic(sorted_channels_info, topics_by_stream, unread_topic_names);-- assert.deepEqual(next(1, "a1"), {stream_id: 1, topic: "a2"});- assert.deepEqual(next(1, "a2"), {stream_id: 2, topic: "b1"});- assert.deepEqual(next(2, "b1"), {stream_id: 1, topic: "a1"});- assert.deepEqual(next(undefined, undefined), {stream_id: 1, topic: "a1"});+ const topics_kept_unread_by_user = new Set();++ const next = make_next_topic(+ sorted_channels_info,+ topics_by_stream,+ unread_topic_names,+ topics_kept_unread_by_user,+ );++ // User starts with 2nd topic but doesn't mark it as read.+ topics_kept_unread_by_user.add("a2");+ // `next` navigates to a1 since it is still unread.+ assert.deepEqual(next(1, "a2"), {stream_id: 1, topic: "a1"});+ // User marks a1 as read.+ unread_topic_names.delete("a1");+ // `a2` is unread but we skip it since user wants to keep it unread.+ assert.deepEqual(next(1, "a1"), {stream_id: 2, topic: "b1"});+ // Wrap around to read any topics left.+ assert.deepEqual(next(2, "b1"), {stream_id: 1, topic: "a2"});++ assert.deepEqual(next(undefined, undefined), {stream_id: 1, topic: "a2"});}// Case 3: collapsed channels