Skip to content

armgit / zulip

Public Imported from GitHub
0

Zulip server and web application. Open-source team chat that helps teams stay productive and focused.

stream_topic_history: Update max msg id of stream post msg deletion.

Aman Agrawal1b05f8e6c98b
diff --git a/frontend_tests/node_tests/stream_topic_history.js b/frontend_tests/node_tests/stream_topic_history.js
index f28fa5a26cb..593ff8001a9 100644
--- a/frontend_tests/node_tests/stream_topic_history.js
+++ b/frontend_tests/node_tests/stream_topic_history.js
@@ -53,6 +53,7 @@ run_test("basics", () => {
set_global("message_util", {
get_messages_in_topic: () => [{id: 101}, {id: 102}],
+ get_max_message_id_in_stream: () => 103,
});
// Removing the last msg in topic1 changes the order
stream_topic_history.remove_messages({
@@ -63,11 +64,13 @@ run_test("basics", () => {
});
history = stream_topic_history.get_recent_topic_names(stream_id);
assert.deepEqual(history, ["topic2", "Topic1"]);
+ // check if stream's max_message_id is updated.
max_message_id = stream_topic_history.get_max_message_id(stream_id);
- assert.deepEqual(max_message_id, 104);
+ assert.deepEqual(max_message_id, 103);
set_global("message_util", {
get_messages_in_topic: () => [{id: 102}],
+ get_max_message_id_in_stream: () => 103,
});
// Removing first topic1 message has no effect.
stream_topic_history.remove_messages({
@@ -78,7 +81,6 @@ run_test("basics", () => {
});
history = stream_topic_history.get_recent_topic_names(stream_id);
assert.deepEqual(history, ["topic2", "Topic1"]);
- // Removing a topic message shouldn't effect the max_message_id.
max_message_id = stream_topic_history.get_max_message_id(stream_id);
assert.deepEqual(max_message_id, 103);
diff --git a/static/js/message_util.js b/static/js/message_util.js
index 6ec9d85b4a7..fa2761c032e 100644
--- a/static/js/message_util.js
+++ b/static/js/message_util.js
@@ -47,10 +47,6 @@ exports.add_new_messages = function (messages, msg_list) {
};
exports.get_messages_in_topic = function (stream_id, topic) {
- // This function is very expensive since it searches
- // all the messages. Please only use it in case of
- // very rare events like topic edits. Its primary
- // use case is the new experimental Recent Topics UI.
return message_list.all
.all_messages()
.filter(
@@ -61,4 +57,16 @@ exports.get_messages_in_topic = function (stream_id, topic) {
);
};
+exports.get_max_message_id_in_stream = function (stream_id) {
+ let max_message_id = 0;
+ for (const msg of message_list.all.all_messages()) {
+ if (msg.type === "stream" && msg.stream_id === stream_id) {
+ if (msg.id > max_message_id) {
+ max_message_id = msg.id;
+ }
+ }
+ }
+ return max_message_id;
+};
+
window.message_util = exports;
diff --git a/static/js/stream_topic_history.js b/static/js/stream_topic_history.js
index b444f4234db..222fb647e4c 100644
--- a/static/js/stream_topic_history.js
+++ b/static/js/stream_topic_history.js
@@ -231,6 +231,11 @@ exports.remove_messages = function (opts) {
}
existing_topic.message_id = max_message_id;
}
+
+ // Update max_message_id in stream
+ if (history.max_message_id <= max_removed_msg_id) {
+ history.max_message_id = message_util.get_max_message_id_in_stream(stream_id);
+ }
};
exports.find_or_create = function (stream_id) {