Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
stream_topic_history: Fix wrong topic order when moving messages.
Aman Agrawald2d479ba618f
diff --git a/web/src/stream_topic_history.ts b/web/src/stream_topic_history.tsindex bef99247b97..eb700ef484f 100644--- a/web/src/stream_topic_history.ts+++ b/web/src/stream_topic_history.ts@@ -12,6 +12,15 @@ const stream_dict = new Map<number, PerStreamHistory>();const fetched_stream_ids = new Set<number>();const request_pending_stream_ids = new Set<number>();+// This is stream_topic_history_util.get_server_history.+// We have to indirectly set it to avoid a circular dependency.+let update_topic_last_message_id: (stream_id: number, topic_name: string) => void;+export function set_update_topic_last_message_id(+ f: (stream_id: number, topic_name: string) => void,+): void {+ 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@@ -95,21 +104,9 @@ export class PerStreamHistory {we just sort on the fly every time we are called.Attributes for a topic are:- * message_id: The latest message_id in the topic. Only usable- for imprecise applications like sorting. The message_id- cannot be fully accurate given message editing and deleting- (as we don't have a way to handle the latest message in a- stream having its stream edited or deleted).-- TODO: We can probably fix this limitation by doing a- single-message `GET /messages` query with anchor="latest",- num_before=0, num_after=0, to update this field when its- value becomes ambiguous. Or probably better to avoid a- thundering herd (of a fast query), having the server send- the data needed to do this update in stream/topic-edit and- delete events (just the new max_message_id for the relevant- topic would likely suffice, though we need to think about- private stream corner cases).+ * message_id: The latest message_id in the topic. This is to+ the best of our knowledge, and may not be accurate if+ we have not seen all the messages in the topic.* pretty_name: The topic_name, with original case.* count: Number of known messages in the topic. Used to detectwhen the last messages in a topic were moved to other topics or@@ -136,6 +133,9 @@ export class PerStreamHistory {}add_or_update(topic_name: string, message_id: number): void {+ // The `message_id` provided here can easily be far from the latest+ // message in the topic, but it is more important for us to cache the topic+ // for autocomplete purposes than to have an accurate max message ID.this.update_stream_max_message_id(message_id);const existing = this.topics.get(topic_name);@@ -160,13 +160,18 @@ export class PerStreamHistory {maybe_remove(topic_name: string, num_messages: number): void {const existing = this.topics.get(topic_name);- if (!existing) {+ if (!existing || existing.count === 0) {return;}if (existing.count <= num_messages) {this.topics.delete(topic_name);- return;+ if (!is_complete_for_stream_id(this.stream_id)) {+ // Request server for latest message in topic if we+ // cannot be sure that we have all messages in the topic.+ update_topic_last_message_id(this.stream_id, topic_name);+ return;+ }}existing.count -= num_messages;@@ -243,9 +248,13 @@ export function remove_messages(opts: {return;}- // This is the normal case of an incoming message.+ // Adjust our local data structures to account for the+ // removal of messages from a topic. We can also remove+ // the topic if it has no messages left or if we cannot+ // locally determine the current state of the topic.+ // So, it is important that we return below if we don't have+ // the topic cached.history.maybe_remove(topic_name, num_messages);-const existing_topic = history.topics.get(topic_name);if (!existing_topic) {return;diff --git a/web/src/stream_topic_history_util.ts b/web/src/stream_topic_history_util.tsindex b0fba5ee00f..23a3278ba37 100644--- a/web/src/stream_topic_history_util.ts+++ b/web/src/stream_topic_history_util.ts@@ -1,3 +1,4 @@+import assert from "minimalistic-assert";import {z} from "zod";import * as channel from "./channel";@@ -39,3 +40,50 @@ export function get_server_history(stream_id: number, on_success: () => void): v},});}++export function update_topic_last_message_id(+ stream_id: number,+ topic_name: string,+ update_dom_on_success: () => void,+): void {+ void channel.get({+ url: "/json/messages",+ data: {+ narrow: JSON.stringify([+ {operator: "stream", operand: stream_id},+ {operator: "topic", operand: topic_name},+ ]),+ anchor: "newest",+ num_before: 1,+ num_after: 0,+ },+ success(data) {+ const {messages} = z+ .object({+ messages: z.array(+ z.object({+ id: z.number(),+ }),+ ),+ })+ .parse(data);+ if (messages.length !== 1) {+ return;+ }++ const last_message = messages[0];+ assert(last_message !== undefined);+ stream_topic_history.add_history(stream_id, [+ {+ name: topic_name,+ max_id: last_message.id,+ },+ ]);+ update_dom_on_success();+ },+ error() {+ // Ideally we would retry since we should always be able to get a success response+ // from the server for this request, but for now we just ignore the error.+ },+ });+}diff --git a/web/src/ui_init.js b/web/src/ui_init.jsindex aae92a83137..965864f9b9d 100644--- a/web/src/ui_init.js+++ b/web/src/ui_init.js@@ -124,6 +124,8 @@ import * as stream_list from "./stream_list";import * as stream_list_sort from "./stream_list_sort";import * as stream_popover from "./stream_popover";import * as stream_settings_ui from "./stream_settings_ui";+import * as stream_topic_history from "./stream_topic_history";+import * as stream_topic_history_util from "./stream_topic_history_util";import * as sub_store from "./sub_store";import * as timerender from "./timerender";import * as tippyjs from "./tippyjs";@@ -487,6 +489,15 @@ export function initialize_everything(state_data) {initialize_navbar();initialize_message_feed_errors();+ // Needs to be set before we fetch any messages.+ 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,+ stream_list.update_streams_sidebar,+ );+ });+realm_logo.initialize();message_lists.initialize();recent_view_ui.initialize({diff --git a/web/tests/stream_topic_history.test.js b/web/tests/stream_topic_history.test.jsindex 61e749f5649..a2c0c429454 100644--- a/web/tests/stream_topic_history.test.js+++ b/web/tests/stream_topic_history.test.js@@ -15,6 +15,8 @@ const stream_data = zrequire("stream_data");const stream_topic_history = zrequire("stream_topic_history");const stream_topic_history_util = zrequire("stream_topic_history_util");+stream_topic_history.set_update_topic_last_message_id(noop);+function test(label, f) {run_test(label, (helpers) => {unread.declare_bankruptcy();@@ -392,3 +394,70 @@ test("all_topics_in_cache", ({override}) => {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);+ });+ const stream_id = 1080;++ let success_callback;+ let get_message_request_triggered = false;+ channel.get = (opts) => {+ get_message_request_triggered = true;+ assert.equal(opts.url, "/json/messages");+ assert.deepEqual(opts.data, {+ anchor: "newest",+ narrow: '[{"operator":"stream","operand":1080},{"operator":"topic","operand":"Topic1"}]',+ num_after: 0,+ num_before: 1,+ });+ success_callback = opts.success;+ };++ stream_topic_history.add_message({+ stream_id,+ message_id: 101,+ topic_name: "topic1",+ });++ let history = stream_topic_history.get_recent_topic_names(stream_id);+ let max_message_id = stream_topic_history.get_max_message_id(stream_id);+ assert.deepEqual(history, ["topic1"]);+ assert.deepEqual(max_message_id, 101);++ // Remove all cached messages from the topic. This sends a request to the server+ // to check for the latest message id in the topic.+ stream_topic_history.remove_messages({+ stream_id,+ topic_name: "Topic1",+ num_messages: 1,+ max_removed_msg_id: 104,+ });+ assert.equal(get_message_request_triggered, true);+ get_message_request_triggered = false;++ // Until we process the response from the server,+ // the topic is not available.+ history = stream_topic_history.get_recent_topic_names(stream_id);+ assert.deepEqual(history, []);++ // Simulate the server responses.+ // Topic is empty.+ success_callback({+ messages: [],+ });+ history = stream_topic_history.get_recent_topic_names(stream_id);+ assert.deepEqual(history, []);++ // Topic has a different max_message_id.+ success_callback({+ messages: [{id: 102}],+ });++ // The topic is now available.+ history = stream_topic_history.get_recent_topic_names(stream_id);+ max_message_id = stream_topic_history.get_max_message_id(stream_id);+ assert.deepEqual(history, ["Topic1"]);+ assert.deepEqual(max_message_id, 102);+});