Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
topic_list: Move and reorganize couple of functions to `topic_list.ts`.
Prakhar Pratyush7d55ad3c130b
diff --git a/web/src/stream_topic_history.ts b/web/src/stream_topic_history.tsindex 32aeefe406a..2520d2b5444 100644--- a/web/src/stream_topic_history.ts+++ b/web/src/stream_topic_history.ts@@ -1,11 +1,9 @@import assert from "minimalistic-assert";-import {all_messages_data} from "./all_messages_data.ts";import * as echo_state from "./echo_state.ts";import {FoldDict} from "./fold_dict.ts";import * as message_util from "./message_util.ts";import * as sub_store from "./sub_store.ts";-import type {StreamSubscription} from "./sub_store.ts";import * as unread from "./unread.ts";// stream_id -> PerStreamHistory object@@ -22,60 +20,6 @@ export function set_update_topic_last_message_id(update_topic_last_message_id = f;}-export function all_topics_in_cache(sub: StreamSubscription): boolean {- // Checks whether this browser's cache of contiguous messages- // (used to locally render narrows) in all_messages_data has all- // messages from a given stream. Because all_messages_data is a range,- // we just need to compare it to the range of history on the stream.-- // If the cache isn't initialized, it's a clear false.- if (all_messages_data === undefined || all_messages_data.empty()) {- return false;- }-- // If the cache doesn't have the latest messages, we can't be sure- // we have all topics.- if (!all_messages_data.fetch_status.has_found_newest()) {- return false;- }-- if (sub.first_message_id === null) {- // If the stream has no message history, we have it all- // vacuously. This should be a very rare condition, since- // stream creation sends a message.- return true;- }-- // Now, we can just compare the first cached message to the first- // message ID in the stream; if it's older, we're good, otherwise,- // we might be missing the oldest topics in this stream in our- // cache.- const first_cached_message = all_messages_data.first_including_muted();- return first_cached_message!.id <= sub.first_message_id;-}--export function is_complete_for_stream_id(stream_id: number): boolean {- if (fetched_stream_ids.has(stream_id)) {- return true;- }-- const sub = sub_store.get(stream_id);- const in_cache = sub !== undefined && all_topics_in_cache(sub);-- if (in_cache) {- /*- If the stream is cached, we can add it to- fetched_stream_ids. Note that for the opposite- scenario, we don't delete from- fetched_stream_ids, because we may just be- waiting for the initial message fetch.- */- fetched_stream_ids.add(stream_id);- }-- return in_cache;-}-export function stream_has_topics(stream_id: number): boolean {if (!stream_dict.has(stream_id)) {return false;@@ -356,6 +300,10 @@ export function has_history_for(stream_id: number): boolean {return fetched_stream_ids.has(stream_id);}+export function mark_history_fetched_for(stream_id: number): void {+ fetched_stream_ids.add(stream_id);+}+export let get_recent_topic_names = (stream_id: number): string[] => {const history = find_or_create(stream_id);diff --git a/web/src/topic_list.ts b/web/src/topic_list.tsindex 31d649e8dbb..20ca4ec1d54 100644--- a/web/src/topic_list.ts+++ b/web/src/topic_list.ts@@ -6,6 +6,7 @@ import render_more_topics from "../templates/more_topics.hbs";import render_more_topics_spinner from "../templates/more_topics_spinner.hbs";import render_topic_list_item from "../templates/topic_list_item.hbs";+import {all_messages_data} from "./all_messages_data.ts";import * as blueslip from "./blueslip.ts";import * as popover_menus from "./popover_menus.ts";import * as popovers from "./popovers.ts";@@ -13,6 +14,8 @@ import * as scroll_util from "./scroll_util.ts";import * as sidebar_ui from "./sidebar_ui.ts";import * as stream_topic_history from "./stream_topic_history.ts";import * as stream_topic_history_util from "./stream_topic_history_util.ts";+import type {StreamSubscription} from "./sub_store.ts";+import * as sub_store from "./sub_store.ts";import * as topic_list_data from "./topic_list_data.ts";import type {TopicInfo} from "./topic_list_data.ts";import * as vdom from "./vdom.ts";@@ -149,6 +152,60 @@ export function spinner_li(): ListInfoNode {};}+export function is_full_topic_history_available(stream_id: number): boolean {+ if (stream_topic_history.has_history_for(stream_id)) {+ return true;+ }++ function all_topics_in_cache(sub: StreamSubscription): boolean {+ // Checks whether this browser's cache of contiguous messages+ // (used to locally render narrows) in all_messages_data has all+ // messages from a given stream. Because all_messages_data is a range,+ // we just need to compare it to the range of history on the stream.++ // If the cache isn't initialized, it's a clear false.+ if (all_messages_data === undefined || all_messages_data.empty()) {+ return false;+ }++ // If the cache doesn't have the latest messages, we can't be sure+ // we have all topics.+ if (!all_messages_data.fetch_status.has_found_newest()) {+ return false;+ }++ if (sub.first_message_id === null) {+ // If the stream has no message history, we have it all+ // vacuously. This should be a very rare condition, since+ // stream creation sends a message.+ return true;+ }++ // Now, we can just compare the first cached message to the first+ // message ID in the stream; if it's older, we're good, otherwise,+ // we might be missing the oldest topics in this stream in our+ // cache.+ const first_cached_message = all_messages_data.first_including_muted();+ return first_cached_message!.id <= sub.first_message_id;+ }++ const sub = sub_store.get(stream_id);+ const in_cache = sub !== undefined && all_topics_in_cache(sub);++ if (in_cache) {+ /*+ If the stream is cached, we can add it to+ fetched_stream_ids. Note that for the opposite+ scenario, we don't delete from+ fetched_stream_ids, because we may just be+ waiting for the initial message fetch.+ */+ stream_topic_history.mark_history_fetched_for(stream_id);+ }++ return in_cache;+}+export class TopicListWidget {topic_list_class_name = "topic-list";prior_dom: vdom.Tag<ListInfoNodeOptions> | undefined = undefined;@@ -184,7 +241,7 @@ export class TopicListWidget {const is_showing_all_possible_topics =list_info.items.length === num_possible_topics &&- stream_topic_history.is_complete_for_stream_id(this.my_stream_id);+ is_full_topic_history_available(this.my_stream_id);const topic_list_classes: [string] = [this.topic_list_class_name];diff --git a/web/tests/stream_topic_history.test.cjs b/web/tests/stream_topic_history.test.cjsindex cb79ac6e160..1fde5ffe8d5 100644--- a/web/tests/stream_topic_history.test.cjs+++ b/web/tests/stream_topic_history.test.cjs@@ -11,8 +11,8 @@ mock_esm("../src/people.ts", {maybe_get_user_by_id: noop,});-const all_messages_data = zrequire("all_messages_data");const echo_state = zrequire("echo_state");+const topic_list = zrequire("topic_list");const unread = zrequire("unread");const message_store = zrequire("message_store");const {set_realm} = zrequire("state_data");@@ -131,40 +131,6 @@ test("basics", () => {});});-test("is_complete_for_stream_id", ({override_rewire}) => {- const sub = {- name: "devel",- stream_id: 444,- first_message_id: 1000,- };- stream_data.add_sub(sub);-- override_rewire(all_messages_data, "all_messages_data", {- empty: () => false,- fetch_status: {- has_found_newest: () => true,- },- first_including_muted: () => ({id: 5}),- });-- assert.equal(stream_topic_history.is_complete_for_stream_id(sub.stream_id), true);-- // Now simulate a more recent message id.- all_messages_data.all_messages_data.first_including_muted = () => ({- id: sub.first_message_id + 1,- });-- // Note that we'll return `true` here due to- // fetched_stream_ids having the stream_id now.- assert.equal(stream_topic_history.is_complete_for_stream_id(sub.stream_id), true);-- // But now clear the data to see what we'd have without- // the previous call.- stream_topic_history.reset();-- assert.equal(stream_topic_history.is_complete_for_stream_id(sub.stream_id), false);-});-test("server_history", () => {const sub = {name: "devel",@@ -173,7 +139,7 @@ test("server_history", () => {const stream_id = sub.stream_id;stream_data.add_sub(sub);- assert.equal(stream_topic_history.is_complete_for_stream_id(stream_id), false);+ assert.equal(topic_list.is_full_topic_history_available(stream_id), false);stream_topic_history.add_message({stream_id,@@ -192,8 +158,8 @@ test("server_history", () => {add_server_history();// Since we added history, now subsequent calls- // to is_complete_for_stream_id will return true.- assert.equal(stream_topic_history.is_complete_for_stream_id(stream_id), true);+ // to is_full_topic_history_available will return true.+ assert.equal(topic_list.is_full_topic_history_available(stream_id), true);let history = stream_topic_history.get_recent_topic_names(stream_id);assert.deepEqual(history, ["local", "hist2", "hist1"]);@@ -382,44 +348,6 @@ test("server_history_end_to_end", () => {assert.ok(on_success_called);});-test("all_topics_in_cache", ({override}) => {- // Add a new stream with first_message_id set.- const general = {- name: "general",- stream_id: 21,- first_message_id: null,- };- const messages = [- {id: 1, stream_id: 21},- {id: 2, stream_id: 21},- {id: 3, stream_id: 21},- ];- const sub = stream_data.create_sub_from_server_data(general);-- assert.equal(stream_topic_history.all_topics_in_cache(sub), false);-- all_messages_data.all_messages_data.clear();- all_messages_data.all_messages_data.add_messages(messages, true);-- let has_found_newest = false;-- override(- all_messages_data.all_messages_data.fetch_status,- "has_found_newest",- () => has_found_newest,- );-- assert.equal(stream_topic_history.all_topics_in_cache(sub), false);- has_found_newest = true;- assert.equal(stream_topic_history.all_topics_in_cache(sub), true);-- sub.first_message_id = 0;- assert.equal(stream_topic_history.all_topics_in_cache(sub), false);-- sub.first_message_id = 2;- assert.equal(stream_topic_history.all_topics_in_cache(sub), true);-});-test("ask_server_for_latest_topic_data", () => {stream_topic_history.set_update_topic_last_message_id((stream_id, topic_name) => {stream_topic_history_util.update_topic_last_message_id(stream_id, topic_name, noop);diff --git a/web/tests/topic_list.test.cjs b/web/tests/topic_list.test.cjsnew file mode 100644index 00000000000..9c6931f546b--- /dev/null+++ b/web/tests/topic_list.test.cjs@@ -0,0 +1,72 @@+"use strict";++const assert = require("node:assert/strict");++const {mock_esm, zrequire} = require("./lib/namespace.cjs");+const {run_test, noop} = require("./lib/test.cjs");++mock_esm("../src/people.ts", {+ maybe_get_user_by_id: noop,+});++const all_messages_data = zrequire("all_messages_data");+const {set_realm} = zrequire("state_data");+const stream_data = zrequire("stream_data");+const stream_topic_history = zrequire("stream_topic_history");+const topic_list = zrequire("topic_list");++set_realm({});++function test(label, f) {+ run_test(label, (helpers) => {+ f(helpers);+ });+}++test("is_full_topic_history_available", ({override}) => {+ const stream_id = 21;+ const general = {+ name: "general",+ stream_id,+ first_message_id: null,+ };+ const messages = [+ {id: 1, stream_id},+ {id: 2, stream_id},+ {id: 3, stream_id},+ ];+ const sub = stream_data.create_sub_from_server_data(general);++ // Currently, all_messages_data is empty.+ assert.equal(topic_list.is_full_topic_history_available(stream_id), false);++ all_messages_data.all_messages_data.clear();+ all_messages_data.all_messages_data.add_messages(messages, true);++ let has_found_newest = false;++ override(+ all_messages_data.all_messages_data.fetch_status,+ "has_found_newest",+ () => has_found_newest,+ );++ assert.equal(topic_list.is_full_topic_history_available(stream_id), false);+ has_found_newest = true;+ // sub.first_message_id === null+ assert.equal(topic_list.is_full_topic_history_available(stream_id), true);++ // Note that we'll return `true` here due to+ // fetched_stream_ids having the stream_id now.+ assert.equal(topic_list.is_full_topic_history_available(stream_id), true);++ // Clear the data, otherwise `is_full_topic_history_available`+ // will always return true due to stream_id in fetched_stream_ids.+ stream_topic_history.reset();++ sub.first_message_id = 0;+ assert.equal(topic_list.is_full_topic_history_available(stream_id), false);++ sub.first_message_id = 2;+ assert.equal(topic_list.is_full_topic_history_available(stream_id), true);+});