2021-02-28 05:27:15 +01:00
|
|
|
"use strict";
|
|
|
|
|
2024-10-09 00:25:41 +02:00
|
|
|
const assert = require("node:assert/strict");
|
2021-02-28 05:27:15 +01:00
|
|
|
|
2023-02-22 23:04:10 +01:00
|
|
|
const {mock_esm, with_overrides, zrequire} = require("./lib/namespace");
|
|
|
|
const {make_stub} = require("./lib/stub");
|
|
|
|
const {run_test} = require("./lib/test");
|
2021-02-28 05:27:15 +01:00
|
|
|
|
2023-06-15 17:07:10 +02:00
|
|
|
const left_sidebar_navigation_area = mock_esm("../src/left_sidebar_navigation_area", {
|
2022-07-10 01:06:33 +02:00
|
|
|
update_starred_count() {},
|
|
|
|
});
|
2021-03-08 09:25:25 +01:00
|
|
|
const message_store = zrequire("message_store");
|
2021-02-28 05:27:15 +01:00
|
|
|
const starred_messages = zrequire("starred_messages");
|
2023-06-01 02:40:51 +02:00
|
|
|
const starred_messages_ui = zrequire("starred_messages_ui");
|
2024-10-09 08:44:21 +02:00
|
|
|
const {initialize_user_settings} = zrequire("user_settings");
|
|
|
|
|
|
|
|
const user_settings = {};
|
|
|
|
initialize_user_settings({user_settings});
|
2021-02-28 05:27:15 +01:00
|
|
|
|
2022-07-10 01:06:33 +02:00
|
|
|
run_test("add starred", () => {
|
2021-02-28 05:27:15 +01:00
|
|
|
starred_messages.starred_ids.clear();
|
|
|
|
assert.deepEqual(starred_messages.get_starred_msg_ids(), []);
|
|
|
|
assert.equal(starred_messages.get_count(), 0);
|
|
|
|
|
|
|
|
starred_messages.add([1, 2]);
|
|
|
|
assert.deepEqual(starred_messages.get_starred_msg_ids(), [1, 2]);
|
|
|
|
assert.equal(starred_messages.get_count(), 2);
|
|
|
|
});
|
|
|
|
|
2022-07-10 01:06:33 +02:00
|
|
|
run_test("remove starred", () => {
|
2021-02-28 05:27:15 +01:00
|
|
|
starred_messages.starred_ids.clear();
|
|
|
|
assert.deepEqual(starred_messages.get_starred_msg_ids(), []);
|
|
|
|
|
|
|
|
for (const id of [1, 2, 3]) {
|
|
|
|
starred_messages.starred_ids.add(id);
|
|
|
|
}
|
|
|
|
assert.deepEqual(starred_messages.get_starred_msg_ids(), [1, 2, 3]);
|
|
|
|
|
|
|
|
starred_messages.remove([2, 3]);
|
|
|
|
assert.deepEqual(starred_messages.get_starred_msg_ids(), [1]);
|
|
|
|
assert.equal(starred_messages.get_count(), 1);
|
|
|
|
});
|
|
|
|
|
2021-03-08 09:25:25 +01:00
|
|
|
run_test("get starred ids in topic", () => {
|
|
|
|
for (const id of [1, 2, 3, 4, 5]) {
|
|
|
|
starred_messages.starred_ids.add(id);
|
|
|
|
}
|
|
|
|
|
starred messages: Fix "unstar all in topic" is incomplete.
Currently, when there are some old starred messages
in a topic, it is possible that some of them won't be
unstarred on doing "Unstar all messages in topic".
This happens when those messages haven't been fetched
yet from the server, and we have no way to verify if
they actually belong to the topic.
This commit fixes that by changing this mechanism to
first fetch all starred messages in the topic from
the server, and then send their IDs back to the backend
to unstar them.
While doing this, we assume that the user does not
have more than 1000 starred messages in that topic.
Note that, we still depend on the local data to
decide whether or not the "Unstar all messages in
topic" option should be shown in the topic popover.
A method similar to the above cannot be used here, because
making server requests before opening the popover
could visually slow down the popover opening.
Using local data for the topic popover would probably
not be a big problem, because users would want to
unstar all messages in a topic probably after noticing
that there are a lot of them, meaning there was at least
one starred message from that topic which was already
fetched, which is sufficient for us to conclude that
we need to show the option in the topic popover.
Fixes #17790
2021-04-02 08:10:11 +02:00
|
|
|
assert.deepEqual(starred_messages.get_count_in_topic(undefined, "topic name"), 0);
|
|
|
|
assert.deepEqual(starred_messages.get_count_in_topic(3, undefined), 0);
|
2021-03-08 09:25:25 +01:00
|
|
|
|
|
|
|
// id: 1 isn't inserted, to test handling the case
|
|
|
|
// when message_store.get() returns undefined
|
2021-03-28 17:57:53 +02:00
|
|
|
message_store.update_message_cache({
|
2021-03-08 09:25:25 +01:00
|
|
|
id: 2,
|
|
|
|
type: "private",
|
|
|
|
});
|
2021-03-28 17:57:53 +02:00
|
|
|
message_store.update_message_cache({
|
2021-03-08 09:25:25 +01:00
|
|
|
// Different stream
|
|
|
|
id: 3,
|
|
|
|
type: "stream",
|
|
|
|
stream_id: 19,
|
|
|
|
topic: "topic",
|
|
|
|
});
|
2021-03-28 17:57:53 +02:00
|
|
|
message_store.update_message_cache({
|
2021-03-08 09:25:25 +01:00
|
|
|
// Different topic
|
|
|
|
id: 4,
|
|
|
|
type: "stream",
|
|
|
|
stream_id: 20,
|
|
|
|
topic: "some other topic",
|
|
|
|
});
|
2021-03-28 17:57:53 +02:00
|
|
|
message_store.update_message_cache({
|
2021-03-08 09:25:25 +01:00
|
|
|
// Correct match
|
|
|
|
id: 5,
|
|
|
|
type: "stream",
|
|
|
|
stream_id: 20,
|
|
|
|
topic: "topic",
|
|
|
|
});
|
|
|
|
|
starred messages: Fix "unstar all in topic" is incomplete.
Currently, when there are some old starred messages
in a topic, it is possible that some of them won't be
unstarred on doing "Unstar all messages in topic".
This happens when those messages haven't been fetched
yet from the server, and we have no way to verify if
they actually belong to the topic.
This commit fixes that by changing this mechanism to
first fetch all starred messages in the topic from
the server, and then send their IDs back to the backend
to unstar them.
While doing this, we assume that the user does not
have more than 1000 starred messages in that topic.
Note that, we still depend on the local data to
decide whether or not the "Unstar all messages in
topic" option should be shown in the topic popover.
A method similar to the above cannot be used here, because
making server requests before opening the popover
could visually slow down the popover opening.
Using local data for the topic popover would probably
not be a big problem, because users would want to
unstar all messages in a topic probably after noticing
that there are a lot of them, meaning there was at least
one starred message from that topic which was already
fetched, which is sufficient for us to conclude that
we need to show the option in the topic popover.
Fixes #17790
2021-04-02 08:10:11 +02:00
|
|
|
assert.deepEqual(starred_messages.get_count_in_topic(20, "topic"), 1);
|
2021-03-08 09:25:25 +01:00
|
|
|
});
|
|
|
|
|
2022-07-10 01:06:33 +02:00
|
|
|
run_test("initialize", () => {
|
2021-02-28 05:27:15 +01:00
|
|
|
starred_messages.starred_ids.clear();
|
|
|
|
for (const id of [1, 2, 3]) {
|
|
|
|
starred_messages.starred_ids.add(id);
|
|
|
|
}
|
|
|
|
|
2023-06-08 18:41:41 +02:00
|
|
|
const starred_messages_params = {
|
|
|
|
starred_messages: [4, 5, 6],
|
|
|
|
};
|
|
|
|
starred_messages.initialize(starred_messages_params);
|
2021-02-28 05:27:15 +01:00
|
|
|
assert.deepEqual(starred_messages.get_starred_msg_ids(), [4, 5, 6]);
|
|
|
|
});
|
|
|
|
|
2024-10-09 21:10:22 +02:00
|
|
|
run_test("rerender_ui", ({override}) => {
|
2021-02-28 05:27:15 +01:00
|
|
|
starred_messages.starred_ids.clear();
|
|
|
|
for (const id of [1, 2, 3]) {
|
|
|
|
starred_messages.starred_ids.add(id);
|
|
|
|
}
|
|
|
|
|
2024-10-09 21:10:22 +02:00
|
|
|
override(user_settings, "starred_message_counts", true);
|
2022-07-10 01:06:33 +02:00
|
|
|
with_overrides(({override}) => {
|
2021-02-28 05:27:15 +01:00
|
|
|
const stub = make_stub();
|
2023-06-15 17:07:10 +02:00
|
|
|
override(left_sidebar_navigation_area, "update_starred_count", stub.f);
|
2023-06-01 02:40:51 +02:00
|
|
|
starred_messages_ui.rerender_ui();
|
2021-02-28 05:27:15 +01:00
|
|
|
assert.equal(stub.num_calls, 1);
|
2024-08-23 22:44:10 +02:00
|
|
|
const args = stub.get_args("count", "hidden");
|
2021-02-28 05:27:15 +01:00
|
|
|
assert.equal(args.count, 3);
|
2024-08-23 22:44:10 +02:00
|
|
|
assert.equal(args.hidden, false);
|
2021-02-28 05:27:15 +01:00
|
|
|
});
|
|
|
|
|
2024-10-09 21:10:22 +02:00
|
|
|
override(user_settings, "starred_message_counts", false);
|
2022-07-10 01:06:33 +02:00
|
|
|
with_overrides(({override}) => {
|
2021-02-28 05:27:15 +01:00
|
|
|
const stub = make_stub();
|
2023-06-15 17:07:10 +02:00
|
|
|
override(left_sidebar_navigation_area, "update_starred_count", stub.f);
|
2023-06-01 02:40:51 +02:00
|
|
|
starred_messages_ui.rerender_ui();
|
2021-02-28 05:27:15 +01:00
|
|
|
assert.equal(stub.num_calls, 1);
|
2024-08-23 22:44:10 +02:00
|
|
|
const args = stub.get_args("count", "hidden");
|
|
|
|
assert.equal(args.count, 3);
|
|
|
|
assert.equal(args.hidden, true);
|
2021-02-28 05:27:15 +01:00
|
|
|
});
|
|
|
|
});
|