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.

starred messages: Fix "unstar all in topic" is incomplete.

Abhijeet Prasad Bodas7f9c3bb4d08d
diff --git a/frontend_tests/node_tests/message_flags.js b/frontend_tests/node_tests/message_flags.js
index 09819e98b73..2805c0d0bb7 100644
--- a/frontend_tests/node_tests/message_flags.js
+++ b/frontend_tests/node_tests/message_flags.js
@@ -11,7 +11,6 @@ const ui = mock_esm("../../static/js/ui");
mock_esm("../../static/js/starred_messages", {
add: () => {},
get_starred_msg_ids: () => [1, 2, 3, 4, 5],
- get_starred_message_ids_in_topic: () => [2, 4, 5],
remove: () => {},
});
@@ -107,18 +106,40 @@ run_test("unstar_all", (override) => {
run_test("unstar_all_in_topic", (override) => {
// Way to capture posted info in every request
- let posted_data;
+ let channel_post_opts;
+ let channel_get_opts;
+
+ override(channel, "get", (opts) => {
+ assert.equal(opts.url, "/json/messages");
+ channel_get_opts = opts;
+ opts.success({
+ messages: [{id: 2}, {id: 3}, {id: 5}],
+ });
+ });
+
override(channel, "post", (opts) => {
assert.equal(opts.url, "/json/messages/flags");
- posted_data = opts.data;
+ channel_post_opts = opts;
});
- // we've set get_starred_message_ids_in_topic to return [2, 4, 5]
- const expected_data = {messages: "[2,4,5]", flag: "starred", op: "remove"};
-
message_flags.unstar_all_messages_in_topic(20, "topic");
- assert.deepEqual(posted_data, expected_data);
+ assert.deepEqual(channel_get_opts.data, {
+ anchor: "newest",
+ num_before: 1000,
+ num_after: 0,
+ narrow: JSON.stringify([
+ {operator: "stream", operand: 20},
+ {operator: "topic", operand: "topic"},
+ {operator: "is", operand: "starred"},
+ ]),
+ });
+
+ assert.deepEqual(channel_post_opts.data, {
+ messages: "[2,3,5]",
+ flag: "starred",
+ op: "remove",
+ });
});
run_test("read", (override) => {
diff --git a/frontend_tests/node_tests/starred_messages.js b/frontend_tests/node_tests/starred_messages.js
index 1673b3ae693..add222f76d5 100644
--- a/frontend_tests/node_tests/starred_messages.js
+++ b/frontend_tests/node_tests/starred_messages.js
@@ -43,11 +43,8 @@ run_test("get starred ids in topic", () => {
starred_messages.starred_ids.add(id);
}
- assert.deepEqual(
- starred_messages.get_starred_message_ids_in_topic(undefined, "topic name"),
- [],
- );
- assert.deepEqual(starred_messages.get_starred_message_ids_in_topic(3, undefined), []);
+ assert.deepEqual(starred_messages.get_count_in_topic(undefined, "topic name"), 0);
+ assert.deepEqual(starred_messages.get_count_in_topic(3, undefined), 0);
// id: 1 isn't inserted, to test handling the case
// when message_store.get() returns undefined
@@ -77,7 +74,7 @@ run_test("get starred ids in topic", () => {
topic: "topic",
});
- assert.deepEqual(starred_messages.get_starred_message_ids_in_topic(20, "topic"), [5]);
+ assert.deepEqual(starred_messages.get_count_in_topic(20, "topic"), 1);
});
run_test("initialize", (override) => {
diff --git a/static/js/message_flags.js b/static/js/message_flags.js
index e1fac76cc91..9331db5b46d 100644
--- a/static/js/message_flags.js
+++ b/static/js/message_flags.js
@@ -122,6 +122,29 @@ export function unstar_all_messages() {
}
export function unstar_all_messages_in_topic(stream_id, topic) {
- const starred_message_ids = starred_messages.get_starred_message_ids_in_topic(stream_id, topic);
- send_flag_update_for_messages(starred_message_ids, "starred", "remove");
+ const data = {
+ anchor: "newest",
+ // In the unlikely event the user has >1000 starred messages
+ // in a topic, this won't find them all. This is probably an
+ // acceptable bug; one can do it multiple times, and we avoid
+ // creating an API endpoint just for this very minor feature.
+ num_before: 1000,
+ num_after: 0,
+ narrow: JSON.stringify([
+ {operator: "stream", operand: stream_id},
+ {operator: "topic", operand: topic},
+ {operator: "is", operand: "starred"},
+ ]),
+ };
+
+ channel.get({
+ url: "/json/messages",
+ data,
+ idempotent: true,
+ success(data) {
+ const messages = data.messages;
+ const starred_message_ids = messages.map((message) => message.id);
+ send_flag_update_for_messages(starred_message_ids, "starred", "remove");
+ },
+ });
}
diff --git a/static/js/starred_messages.js b/static/js/starred_messages.js
index e13915be3de..5d9fa1dca54 100644
--- a/static/js/starred_messages.js
+++ b/static/js/starred_messages.js
@@ -39,12 +39,12 @@ export function get_starred_msg_ids() {
return Array.from(starred_ids);
}
-export function get_starred_message_ids_in_topic(stream_id, topic) {
+export function get_count_in_topic(stream_id, topic) {
if (stream_id === undefined || topic === undefined) {
- return [];
+ return 0;
}
- return Array.from(starred_ids).filter((id) => {
+ const messages = Array.from(starred_ids).filter((id) => {
const message = message_store.get(id);
if (message === undefined) {
@@ -52,8 +52,12 @@ export function get_starred_message_ids_in_topic(stream_id, topic) {
// but the message itself hasn't been fetched from the server yet.
// We ignore this message, since we can't check if it belongs to
// the topic, but it could, meaning this implementation isn't
- // completely correct. The right way to do this would be to ask the
- // backend for starred IDs from the topic.
+ // completely correct.
+ // Since this function is used only when opening the topic popover,
+ // and not for actually unstarring messages, this discrepancy is
+ // probably acceptable. The worst it could manifest as is the topic
+ // popover not having the "Unstar all messages in topic" option, when
+ // it should have had.
return false;
}
@@ -63,6 +67,8 @@ export function get_starred_message_ids_in_topic(stream_id, topic) {
message.topic.toLowerCase() === topic.toLowerCase()
);
});
+
+ return messages.length;
}
export function rerender_ui() {
diff --git a/static/js/stream_popover.js b/static/js/stream_popover.js
index 8ff210e0b2e..0227f3c969d 100644
--- a/static/js/stream_popover.js
+++ b/static/js/stream_popover.js
@@ -237,8 +237,7 @@ function build_topic_popover(opts) {
const is_muted = muting.is_topic_muted(sub.stream_id, topic_name);
const can_mute_topic = !is_muted;
const can_unmute_topic = is_muted;
- const has_starred_messages =
- starred_messages.get_starred_message_ids_in_topic(sub.stream_id, topic_name).length > 0;
+ const has_starred_messages = starred_messages.get_count_in_topic(sub.stream_id, topic_name) > 0;
const content = render_topic_sidebar_actions({
stream_name: sub.name,