Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
message_util: Add a helper to fetch a narrow's last message id.
Aadyot1381418facf60
diff --git a/web/src/message_util.ts b/web/src/message_util.tsindex 3bb2c304270..7e31f3ee83a 100644--- a/web/src/message_util.ts+++ b/web/src/message_util.ts@@ -1,3 +1,7 @@+import assert from "minimalistic-assert";+import * as z from "zod/mini";++import * as channel from "./channel.ts";import * as message_lists from "./message_lists.ts";import * as message_store from "./message_store.ts";import type {Message} from "./message_store.ts";@@ -14,6 +18,50 @@ type DirectMessagePermissionHints = {is_local_echo_safe: boolean;};+const last_message_id_response_schema = z.object({+ messages: z.array(+ z.object({+ id: z.number(),+ }),+ ),+});++export function get_last_message_id_in_narrow(+ narrow: {operator: string; operand: unknown}[],+ on_success: (last_message_id: number) => void,+ extra_request_data: Record<string, unknown> = {},+): void {+ // Fetches the newest message matching the narrow and, if one exists,+ // invokes `on_success` with its id. Both stream_topic_history_util and+ // pm_conversations_util use this to ask the server whether a topic or DM+ // conversation still has any messages after a local deletion emptied it.+ void channel.get({+ url: "/json/messages",+ data: {+ narrow: JSON.stringify(narrow),+ anchor: "newest",+ num_before: 1,+ num_after: 0,+ ...extra_request_data,+ },+ success(raw_data) {+ const {messages} = last_message_id_response_schema.parse(raw_data);+ if (messages.length !== 1) {+ return;+ }++ const last_message = messages[0];+ assert(last_message !== undefined);+ on_success(last_message.id);+ },+ 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.+ },+ });+}+export function do_unread_count_updates(messages: Message[], expect_no_new_unreads = false): void {const any_new_unreads = unread.process_loaded_messages(messages, expect_no_new_unreads);diff --git a/web/src/stream_topic_history_util.ts b/web/src/stream_topic_history_util.tsindex a229a39155b..66ffa9f1446 100644--- a/web/src/stream_topic_history_util.ts+++ b/web/src/stream_topic_history_util.ts@@ -1,7 +1,7 @@-import assert from "minimalistic-assert";import * as z from "zod/mini";import * as channel from "./channel.ts";+import * as message_util from "./message_util.ts";import {get_retry_backoff_seconds} from "./retry_backoff.ts";import * as stream_topic_history from "./stream_topic_history.ts";@@ -71,45 +71,20 @@ export function update_topic_last_message_id(topic_name: string,update_dom_on_success: () => void,): void {- void channel.get({- url: "/json/messages",- data: {- narrow: JSON.stringify([- {operator: "channel", operand: stream_id},- {operator: "topic", operand: topic_name},- ]),- anchor: "newest",- num_before: 1,- num_after: 0,- allow_empty_topic_name: true,- },- 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);+ message_util.get_last_message_id_in_narrow(+ [+ {operator: "channel", operand: stream_id},+ {operator: "topic", operand: topic_name},+ ],+ (last_message_id) => {stream_topic_history.add_history(stream_id, [{name: topic_name,- max_id: last_message.id,+ 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.- },- });+ {allow_empty_topic_name: true},+ );}diff --git a/web/tests/message_util.test.cjs b/web/tests/message_util.test.cjsnew file mode 100644index 00000000000..1e450e3b651--- /dev/null+++ b/web/tests/message_util.test.cjs@@ -0,0 +1,84 @@+"use strict";++const assert = require("node:assert/strict");++const {mock_esm, zrequire} = require("./lib/namespace.cjs");+const {run_test, noop} = require("./lib/test.cjs");++const channel = mock_esm("../src/channel");++const message_util = zrequire("message_util");++let reported_ids = [];+function record_last_message_id(id) {+ reported_ids.push(id);+}++function test(label, f) {+ run_test(label, (helpers) => {+ reported_ids = [];+ f(helpers);+ });+}++test("get_last_message_id_in_narrow builds the request from the narrow", () => {+ let requested_opts;+ channel.get = (opts) => {+ requested_opts = opts;+ };++ const narrow = [+ {operator: "channel", operand: 5},+ {operator: "topic", operand: "topic"},+ ];+ message_util.get_last_message_id_in_narrow(narrow, noop, {allow_empty_topic_name: true});++ assert.equal(requested_opts.url, "/json/messages");+ assert.deepEqual(requested_opts.data, {+ narrow: JSON.stringify(narrow),+ anchor: "newest",+ num_before: 1,+ num_after: 0,+ allow_empty_topic_name: true,+ });+});++test("get_last_message_id_in_narrow reports the last message id on success", () => {+ channel.get = (opts) => {+ opts.success({messages: [{id: 987}]});+ };++ message_util.get_last_message_id_in_narrow(+ [{operator: "dm", operand: [4]}],+ record_last_message_id,+ );++ assert.deepEqual(reported_ids, [987]);+});++test("get_last_message_id_in_narrow ignores an empty result", () => {+ channel.get = (opts) => {+ opts.success({messages: []});+ };++ message_util.get_last_message_id_in_narrow(+ [{operator: "dm", operand: [4]}],+ record_last_message_id,+ );++ assert.deepEqual(reported_ids, []);+});++test("get_last_message_id_in_narrow ignores errors", () => {+ channel.get = (opts) => {+ opts.error();+ };++ // Should not throw or report a message id.+ message_util.get_last_message_id_in_narrow(+ [{operator: "dm", operand: [4]}],+ record_last_message_id,+ );++ assert.deepEqual(reported_ids, []);+});diff --git a/web/tests/stream_topic_history.test.cjs b/web/tests/stream_topic_history.test.cjsindex e420b5bc9e5..0a6f4aa3098 100644--- a/web/tests/stream_topic_history.test.cjs+++ b/web/tests/stream_topic_history.test.cjs@@ -451,19 +451,16 @@ test("ask_server_for_latest_topic_data", () => {});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":"channel","operand":1080},{"operator":"topic","operand":"Topic1"}]',- num_after: 0,- num_before: 1,- allow_empty_topic_name: true,- });- success_callback = opts.success;+ let report_last_message_id;+ let last_message_request_triggered = false;+ message_util.get_last_message_id_in_narrow = (narrow, on_success, extra_request_data) => {+ last_message_request_triggered = true;+ assert.deepEqual(narrow, [+ {operator: "channel", operand: 1080},+ {operator: "topic", operand: "Topic1"},+ ]);+ assert.deepEqual(extra_request_data, {allow_empty_topic_name: true});+ report_last_message_id = on_success;};stream_topic_history.add_message({@@ -477,36 +474,24 @@ test("ask_server_for_latest_topic_data", () => {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.+ // Remove all cached messages from the topic. This asks the server 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;+ assert.equal(last_message_request_triggered, true);// 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.+ // The server reports a remaining message, so the topic becomes available+ // again with that message's id.+ report_last_message_id(102);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"]);