Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
topic_generator: Add has_next_unread_topic.
Aman Agrawal1ada6b976bd9
diff --git a/web/src/topic_generator.ts b/web/src/topic_generator.tsindex 0fa66c9e8fb..fa07d98da73 100644--- a/web/src/topic_generator.ts+++ b/web/src/topic_generator.ts@@ -7,6 +7,7 @@ import * as stream_list_sort from "./stream_list_sort.ts";import * as stream_topic_history from "./stream_topic_history.ts";import * as unread from "./unread.ts";import * as user_topics from "./user_topics.ts";+import * as util from "./util.ts";// If there are any unreads in the current topic,// user likely wants to avoid reading them right now.@@ -220,6 +221,30 @@ export function get_next_topic();}+// Whether get_next_topic has a topic other than the current one to+// go to, without its side effects or sorting every channel's topics.+// A topic the user left unread counts, although get_next_topic skips+// it once.+export function has_next_unread_topic(+ curr_stream_id: number | undefined,+ curr_topic: string | undefined,+ channels_info: {channel_id: number}[],+): boolean {+ function is_current_topic(channel_id: number, topic: string): boolean {+ return (+ channel_id === curr_stream_id &&+ curr_topic !== undefined &&+ util.lower_same(topic, curr_topic)+ );+ }++ return channels_info.some(({channel_id}) =>+ get_unmuted_topics(channel_id, unread.get_topics_with_unreads(channel_id)).some(+ (topic) => !is_current_topic(channel_id, topic),+ ),+ );+}+export function get_next_unread_pm_string(curr_pm: string | undefined): string | undefined {const my_pm_strings = pm_conversations.recent.get_strings();// undefined translates to "not found".diff --git a/web/src/unread.ts b/web/src/unread.tsindex 825dba005b4..ea8605af38a 100644--- a/web/src/unread.ts+++ b/web/src/unread.ts@@ -588,6 +588,21 @@ class UnreadTopicCounter {return followed_topic_unread_mentions;}+ get_topics_with_unreads(stream_id: number): string[] {+ const per_stream_bucketer = this.bucketer.get(stream_id);+ if (!per_stream_bucketer) {+ return [];+ }++ const topics = [];+ for (const [topic, msgs] of per_stream_bucketer) {+ if (msgs.size > 0) {+ topics.push(topic);+ }+ }+ return topics;+ }+topic_has_any_unread(stream_id: number, topic: string): boolean {const per_stream_bucketer = this.bucketer.get(stream_id);@@ -1083,6 +1098,10 @@ export function topic_has_any_unread_mentions(stream_id: number, topic: string):return (unread_mention_topics.get(topic_key)?.size ?? 0) > 0;}+export function get_topics_with_unreads(stream_id: number): string[] {+ return unread_topic_counter.get_topics_with_unreads(stream_id);+}+export function topic_has_any_unread(stream_id: number, topic: string): boolean {return unread_topic_counter.topic_has_any_unread(stream_id, topic);}diff --git a/web/tests/topic_generator.test.cjs b/web/tests/topic_generator.test.cjsindex 715bc1d5695..b77e10161f2 100644--- a/web/tests/topic_generator.test.cjs+++ b/web/tests/topic_generator.test.cjs@@ -198,6 +198,7 @@ function mock_channels_and_topics(override) {mock(unread, "get_msg_ids_for_topic", (stream_id, topic) =>is_unread(stream_id, topic) ? [`${stream_id}:${topic}`] : [],);+ mock(unread, "get_topics_with_unreads", (stream_id) => unread_topics.get(stream_id) ?? []);mock(narrow_state, "stream_id", () => curr_stream_id);mock(narrow_state, "topic", () => curr_topic);mock(narrow_state, "filter", () => ({@@ -222,7 +223,7 @@ function mock_channels_and_topics(override) {};}-run_test("get_next_topic", ({override}) => {+run_test("get_next_topic and has_next_unread_topic", ({override}) => {const {set_unread_topics, set_topic_policies, set_narrow} = mock_channels_and_topics(override);function assert_next_topic(narrow, expected) {@@ -233,6 +234,10 @@ run_test("get_next_topic", ({override}) => {tg.get_next_topic(...narrow, only_followed_topics, sorted_channels_info),expected,);+ assert.equal(+ tg.has_next_unread_topic(...narrow, sorted_channels_info),+ expected !== undefined,+ );}assert_next_topic(elsewhere, undefined);@@ -289,7 +294,7 @@ run_test("get_next_topic", ({override}) => {assert_next_topic(elsewhere, undefined);});-run_test("get_next_topic skips a topic left unread once", ({override}) => {+run_test("a topic left unread is skipped once by get_next_topic, but counts", ({override}) => {const {set_unread_topics, set_narrow} = mock_channels_and_topics(override);tg.reset_topics_kept_unread_by_user();const only_followed_topics = false;@@ -302,10 +307,20 @@ run_test("get_next_topic skips a topic left unread once", ({override}) => {assert.deepEqual(get_next_topic([devel, "lunch"]), {stream_id: devel, topic: "dinner"});set_unread_topics([[devel, ["lunch"]]]);+ set_narrow([devel, "read topic"]);+ assert.ok(tg.has_next_unread_topic(devel, "read topic", sorted_channels_info));assert.equal(get_next_topic([devel, "read topic"]), undefined);assert.deepEqual(get_next_topic([devel, "read topic"]), {stream_id: devel, topic: "lunch"});});+run_test("has_next_unread_topic ignores the case of the current topic", ({override}) => {+ const {set_unread_topics, set_narrow} = mock_channels_and_topics(override);++ set_unread_topics([[devel, ["lunch"]]]);+ set_narrow([devel, "LUNCH"]);+ assert.ok(!tg.has_next_unread_topic(devel, "LUNCH", sorted_channels_info));+});+run_test("get_next_unread_pm_string", ({override}) => {override(pm_conversations.recent, "get_strings", () => ["1", "read", "2,3", "4", "unk"]);diff --git a/web/tests/unread.test.cjs b/web/tests/unread.test.cjsindex 2c181b36c46..d1ed04c57cd 100644--- a/web/tests/unread.test.cjs+++ b/web/tests/unread.test.cjs@@ -151,6 +151,8 @@ test("changing_topics", () => {assert.ok(unread.topic_has_any_unread(stream_id, "lunch"));assert.ok(!unread.topic_has_any_unread(wrong_stream_id, "lunch"));assert.ok(!unread.topic_has_any_unread(stream_id, "NOT lunch"));+ assert.deepEqual(unread.get_topics_with_unreads(stream_id), ["luNch"]);+ assert.deepEqual(unread.get_topics_with_unreads(wrong_stream_id), []);count = unread.num_unread_for_topic(stream_id, "NOT lunch");assert.equal(count, 0);@@ -185,6 +187,8 @@ test("changing_topics", () => {assert.equal(count, 1);assert.ok(unread.topic_has_any_unread(stream_id, "snack"));assert.ok(!unread.topic_has_any_unread(wrong_stream_id, "snack"));+ // The topic that no longer has unread messages is left out.+ assert.deepEqual(unread.get_topics_with_unreads(stream_id), ["dinner", "snack"]);unread.update_unread_topic_name_case(stream_id, "snack", "SnaCK");const topic_counts = unread.get_unread_topics().topic_counts.get(stream_id);