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.

refactor: Extract `excludes_muted_topics` logic.

Abhijeet Prasad Bodas9cb41c2976b8
diff --git a/frontend_tests/node_tests/message_list.js b/frontend_tests/node_tests/message_list.js
index c701e3b4d91..b62de671cf7 100644
--- a/frontend_tests/node_tests/message_list.js
+++ b/frontend_tests/node_tests/message_list.js
@@ -364,18 +364,22 @@ run_test("bookend", (override) => {
});
run_test("filter_muted_topic_messages", () => {
- const list = new MessageList({});
+ const list = new MessageList({
+ excludes_muted_topics: true,
+ });
muting.add_muted_topic(1, "muted");
const unmuted = [
{
id: 50,
+ type: "stream",
stream_id: 1,
mentioned: true, // overrides mute
topic: "muted",
},
{
id: 60,
+ type: "stream",
stream_id: 1,
mentioned: false,
topic: "whatever",
@@ -384,6 +388,7 @@ run_test("filter_muted_topic_messages", () => {
const muted = [
{
id: 70,
+ type: "stream",
stream_id: 1,
mentioned: false,
topic: "muted",
@@ -403,9 +408,6 @@ run_test("add_remove_rerender", () => {
const messages = [{id: 1}, {id: 2}, {id: 3}];
- list.data.unmuted_messages = function (msgs) {
- return msgs;
- };
list.add_messages(messages);
assert.equal(list.num_items(), 3);
diff --git a/frontend_tests/node_tests/message_list_data.js b/frontend_tests/node_tests/message_list_data.js
index 89109805ebf..2eae91634db 100644
--- a/frontend_tests/node_tests/message_list_data.js
+++ b/frontend_tests/node_tests/message_list_data.js
@@ -2,9 +2,11 @@
const {strict: assert} = require("assert");
-const {set_global, zrequire} = require("../zjsunit/namespace");
+const MessageListData = require("../../static/js/message_list_data");
+const {set_global, with_field, zrequire} = require("../zjsunit/namespace");
const {run_test} = require("../zjsunit/test");
+zrequire("muting");
zrequire("unread");
zrequire("Filter", "js/filter");
@@ -12,7 +14,6 @@ zrequire("FetchStatus", "js/fetch_status");
zrequire("MessageListData", "js/message_list_data");
set_global("page_params", {});
-set_global("muting", {});
set_global("setTimeout", (f, delay) => {
assert.equal(delay, 0);
@@ -22,6 +23,7 @@ set_global("setTimeout", (f, delay) => {
function make_msg(msg_id) {
return {
id: msg_id,
+ type: "stream",
unread: true,
topic: "whatever",
};
@@ -36,6 +38,13 @@ function assert_contents(mld, msg_ids) {
assert.deepEqual(msgs, make_msgs(msg_ids));
}
+function assert_msg_ids(messages, msg_ids) {
+ assert.deepEqual(
+ msg_ids,
+ messages.map((message) => message.id),
+ );
+}
+
run_test("basics", () => {
const mld = new MessageListData({
excludes_muted_topics: false,
@@ -103,100 +112,122 @@ run_test("basics", () => {
assert.equal(mld.first_unread_message_id(), 145);
});
-run_test("muting enabled", () => {
- const mld = new MessageListData({
+run_test("muting", () => {
+ let mld = new MessageListData({
+ excludes_muted_topics: false,
+ filter: undefined,
+ });
+
+ const msgs = [
+ {id: 1, type: "stream", stream_id: 1, topic: "muted"},
+ {id: 2, type: "stream", stream_id: 1, topic: "whatever"},
+ {id: 3, type: "stream", stream_id: 1, topic: "muted", mentioned: true}, // mentions override muting
+ ];
+
+ // `messages_filtered_for_topic_mutes` should skip filtering
+ // messages if `excludes_muted_topics` is false.
+ let is_topic_muted_calls = 0;
+
+ with_field(
+ muting,
+ "is_topic_muted",
+ () => {
+ is_topic_muted_calls = is_topic_muted_calls + 1;
+ },
+ () => {
+ const res = mld.messages_filtered_for_topic_mutes(msgs);
+ assert.equal(is_topic_muted_calls, 0);
+ assert.deepEqual(res, msgs);
+ },
+ );
+
+ // Test actual behaviour of `messages_filtered_for_topic_mutes`
+ mld.excludes_muted_topics = true;
+ muting.add_muted_topic(1, "muted");
+ const res = mld.messages_filtered_for_topic_mutes(msgs);
+ assert.deepEqual(res, [
+ {id: 2, type: "stream", stream_id: 1, topic: "whatever"},
+ {id: 3, type: "stream", stream_id: 1, topic: "muted", mentioned: true}, // mentions override muting
+ ]);
+
+ // MessageListData methods should always attempt to filter messages,
+ // and update `_all_items` when `excludes_muted_topics` is true.
+ mld = new MessageListData({
excludes_muted_topics: true,
filter: undefined,
});
+ assert.deepEqual(mld._all_items, []);
- muting.is_topic_muted = function () {
- return true;
+ let messages_filtered_for_topic_mutes_calls = 0;
+ mld.messages_filtered_for_topic_mutes = function (messages) {
+ messages_filtered_for_topic_mutes_calls = messages_filtered_for_topic_mutes_calls + 1;
+ return messages;
};
- mld.add_anywhere(make_msgs([35, 25, 15, 45]));
- assert_contents(mld, []);
- mld.get(35).mentioned = true;
- mld.update_items_for_muting();
- assert.deepEqual(mld._items, [mld.get(35)]);
+ mld.add_anywhere([{id: 10}]);
+ assert.equal(messages_filtered_for_topic_mutes_calls, 1);
+ assert_msg_ids(mld._all_items, [10]);
- mld.remove([35, 15]);
- assert_contents(mld, []);
- assert.deepEqual(mld._all_items, make_msgs([25, 45]));
+ mld.prepend([{id: 9}]);
+ assert.equal(messages_filtered_for_topic_mutes_calls, 2);
+ assert_msg_ids(mld._all_items, [9, 10]);
- const msgs = make_msgs([10, 20]);
- msgs[0].mentioned = true;
- mld.prepend(msgs);
- assert.deepEqual(mld._items, [mld.get(10)]);
- assert.deepEqual(mld._all_items, msgs.concat(make_msgs([25, 45])));
+ mld.append([{id: 11}]);
+ assert.equal(messages_filtered_for_topic_mutes_calls, 3);
+ assert_msg_ids(mld._all_items, [9, 10, 11]);
- mld.clear();
- assert.deepEqual(mld._all_items, []);
-});
+ mld.remove([9]);
+ assert_msg_ids(mld._all_items, [10, 11]);
-run_test("more muting", () => {
- muting.is_topic_muted = function (stream_id, topic) {
- return topic === "muted";
- };
+ mld.clear();
+ assert_msg_ids(mld._all_items, []);
- const mld = new MessageListData({
+ // Test `add_messages` populates the `info` dict **after**
+ // filtering the messages.
+ mld = new MessageListData({
excludes_muted_topics: true,
filter: undefined,
});
const orig_messages = [
- {id: 3, topic: "muted"},
- {id: 4, topic: "whatever"},
- {id: 7, topic: "muted"},
- {id: 8, topic: "whatever"},
+ {id: 3, type: "stream", stream_id: 1, topic: "muted"},
+ {id: 4, type: "stream", stream_id: 1, topic: "whatever"},
+ {id: 7, type: "stream", stream_id: 1, topic: "muted"},
+ {id: 8, type: "stream", stream_id: 1, topic: "whatever"},
];
const orig_info = mld.add_messages(orig_messages);
-
assert.deepEqual(orig_info, {
top_messages: [],
interior_messages: [],
bottom_messages: [
- {id: 4, topic: "whatever"},
- {id: 8, topic: "whatever"},
+ {id: 4, type: "stream", stream_id: 1, topic: "whatever"},
+ {id: 8, type: "stream", stream_id: 1, topic: "whatever"},
],
});
- assert.deepEqual(
- mld._all_items.map((message) => message.id),
- [3, 4, 7, 8],
- );
-
- assert.deepEqual(
- mld.all_messages().map((message) => message.id),
- [4, 8],
- );
+ assert_msg_ids(mld._all_items, [3, 4, 7, 8]);
+ assert_msg_ids(mld._items, [4, 8]);
const more_messages = [
- {id: 1, topic: "muted"},
- {id: 2, topic: "whatever"},
- {id: 3, topic: "muted"}, // dup
- {id: 5, topic: "muted"},
- {id: 6, topic: "whatever"},
- {id: 9, topic: "muted"},
- {id: 10, topic: "whatever"},
+ {id: 1, type: "stream", stream_id: 1, topic: "muted"},
+ {id: 2, type: "stream", stream_id: 1, topic: "whatever"},
+ {id: 3, type: "stream", stream_id: 1, topic: "muted"}, // dup
+ {id: 5, type: "stream", stream_id: 1, topic: "muted"},
+ {id: 6, type: "stream", stream_id: 1, topic: "whatever"},
+ {id: 9, type: "stream", stream_id: 1, topic: "muted"},
+ {id: 10, type: "stream", stream_id: 1, topic: "whatever"},
];
const more_info = mld.add_messages(more_messages);
- assert.deepEqual(
- mld._all_items.map((message) => message.id),
- [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
- );
-
- assert.deepEqual(
- mld.all_messages().map((message) => message.id),
- [2, 4, 6, 8, 10],
- );
+ assert_msg_ids(mld._all_items, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
+ assert_msg_ids(mld._items, [2, 4, 6, 8, 10]);
assert.deepEqual(more_info, {
- top_messages: [{id: 2, topic: "whatever"}],
- interior_messages: [{id: 6, topic: "whatever"}],
- bottom_messages: [{id: 10, topic: "whatever"}],
+ top_messages: [{id: 2, type: "stream", stream_id: 1, topic: "whatever"}],
+ interior_messages: [{id: 6, type: "stream", stream_id: 1, topic: "whatever"}],
+ bottom_messages: [{id: 10, type: "stream", stream_id: 1, topic: "whatever"}],
});
});
diff --git a/static/js/message_list_data.js b/static/js/message_list_data.js
index 83fb0f8fc72..01f00cbed20 100644
--- a/static/js/message_list_data.js
+++ b/static/js/message_list_data.js
@@ -170,11 +170,21 @@ class MessageListData {
return messages.filter((message) => predicate(message));
}
+ messages_filtered_for_topic_mutes(messages) {
+ if (!this.excludes_muted_topics) {
+ return [...messages];
+ }
+
+ return messages.filter((message) => {
+ if (message.type !== "stream") {
+ return true;
+ }
+ return !muting.is_topic_muted(message.stream_id, message.topic) || message.mentioned;
+ });
+ }
+
unmuted_messages(messages) {
- return messages.filter(
- (message) =>
- !muting.is_topic_muted(message.stream_id, message.topic) || message.mentioned,
- );
+ return this.messages_filtered_for_topic_mutes(messages);
}
update_items_for_muting() {
@@ -252,47 +262,43 @@ class MessageListData {
// This should be used internally when we have
// "interior" messages to add and can't optimize
// things by only doing prepend or only doing append.
- let viewable_messages;
+
+ const viewable_messages = this.unmuted_messages(messages);
+
if (this.excludes_muted_topics) {
this._all_items = messages.concat(this._all_items);
this._all_items.sort((a, b) => a.id - b.id);
-
- viewable_messages = this.unmuted_messages(messages);
- this._items = viewable_messages.concat(this._items);
- } else {
- viewable_messages = messages;
- this._items = messages.concat(this._items);
}
+ this._items = viewable_messages.concat(this._items);
this._items.sort((a, b) => a.id - b.id);
+
this._add_to_hash(messages);
return viewable_messages;
}
append(messages) {
// Caller should have already filtered
- let viewable_messages;
+ const viewable_messages = this.unmuted_messages(messages);
+
if (this.excludes_muted_topics) {
this._all_items = this._all_items.concat(messages);
- viewable_messages = this.unmuted_messages(messages);
- } else {
- viewable_messages = messages;
}
this._items = this._items.concat(viewable_messages);
+
this._add_to_hash(messages);
return viewable_messages;
}
prepend(messages) {
// Caller should have already filtered
- let viewable_messages;
+ const viewable_messages = this.unmuted_messages(messages);
+
if (this.excludes_muted_topics) {
this._all_items = messages.concat(this._all_items);
- viewable_messages = this.unmuted_messages(messages);
- } else {
- viewable_messages = messages;
}
this._items = viewable_messages.concat(this._items);
+
this._add_to_hash(messages);
return viewable_messages;
}