2020-07-25 02:02:35 +02:00
|
|
|
const _ = require("lodash");
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
set_global("page_params", {
|
2016-10-30 15:47:20 +01:00
|
|
|
is_admin: false,
|
2017-04-24 21:59:07 +02:00
|
|
|
realm_users: [],
|
2019-05-08 08:57:23 +02:00
|
|
|
is_guest: false,
|
2016-10-30 15:47:20 +01:00
|
|
|
});
|
|
|
|
|
2020-07-15 00:34:28 +02:00
|
|
|
set_global("$", () => {});
|
2017-06-20 04:34:04 +02:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
set_global("document", null);
|
2019-03-13 13:47:57 +01:00
|
|
|
global.stub_out_jquery();
|
2013-09-16 16:56:46 +02:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
zrequire("color_data");
|
|
|
|
zrequire("hash_util");
|
|
|
|
zrequire("stream_topic_history");
|
|
|
|
zrequire("people");
|
|
|
|
zrequire("stream_color");
|
|
|
|
zrequire("stream_data");
|
|
|
|
zrequire("FetchStatus", "js/fetch_status");
|
|
|
|
zrequire("Filter", "js/filter");
|
|
|
|
zrequire("MessageListData", "js/message_list_data");
|
|
|
|
zrequire("MessageListView", "js/message_list_view");
|
|
|
|
zrequire("message_list");
|
2020-02-21 14:26:11 +01:00
|
|
|
const settings_config = zrequire("settings_config");
|
2013-08-19 18:46:21 +02:00
|
|
|
|
2019-12-28 11:53:50 +01:00
|
|
|
const me = {
|
2020-07-15 01:29:15 +02:00
|
|
|
email: "me@zulip.com",
|
|
|
|
full_name: "Current User",
|
2020-03-21 20:19:30 +01:00
|
|
|
user_id: 100,
|
2019-12-28 11:53:50 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
// set up user data
|
2020-05-26 22:34:15 +02:00
|
|
|
people.add_active_user(me);
|
2019-12-28 11:53:50 +01:00
|
|
|
people.initialize_current_user(me.user_id);
|
|
|
|
|
|
|
|
function contains_sub(subs, sub) {
|
2020-07-02 01:39:34 +02:00
|
|
|
return subs.some((s) => s.name === sub.name);
|
2019-12-28 11:53:50 +01:00
|
|
|
}
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
run_test("basics", () => {
|
2019-11-02 00:06:25 +01:00
|
|
|
const denmark = {
|
2013-08-19 18:46:21 +02:00
|
|
|
subscribed: false,
|
2020-07-15 01:29:15 +02:00
|
|
|
color: "blue",
|
|
|
|
name: "Denmark",
|
2014-02-05 20:35:16 +01:00
|
|
|
stream_id: 1,
|
2019-05-15 08:54:25 +02:00
|
|
|
is_muted: true,
|
2013-08-19 18:46:21 +02:00
|
|
|
};
|
2019-11-02 00:06:25 +01:00
|
|
|
const social = {
|
2013-08-19 18:46:21 +02:00
|
|
|
subscribed: true,
|
2020-07-15 01:29:15 +02:00
|
|
|
color: "red",
|
|
|
|
name: "social",
|
2014-02-05 20:35:16 +01:00
|
|
|
stream_id: 2,
|
2019-05-15 08:54:25 +02:00
|
|
|
is_muted: false,
|
2016-12-03 23:17:57 +01:00
|
|
|
invite_only: true,
|
2020-02-04 21:50:55 +01:00
|
|
|
stream_post_policy: stream_data.stream_post_policy_values.admins.code,
|
2013-08-19 18:46:21 +02:00
|
|
|
};
|
2019-11-02 00:06:25 +01:00
|
|
|
const test = {
|
2014-02-20 17:57:30 +01:00
|
|
|
subscribed: true,
|
2020-07-15 01:29:15 +02:00
|
|
|
color: "yellow",
|
|
|
|
name: "test",
|
2014-02-20 17:57:30 +01:00
|
|
|
stream_id: 3,
|
2019-05-15 08:54:25 +02:00
|
|
|
is_muted: true,
|
2016-12-03 23:17:57 +01:00
|
|
|
invite_only: false,
|
2014-02-20 17:57:30 +01:00
|
|
|
};
|
2020-02-09 22:02:55 +01:00
|
|
|
stream_data.add_sub(denmark);
|
|
|
|
stream_data.add_sub(social);
|
2014-02-20 17:57:30 +01:00
|
|
|
assert(stream_data.all_subscribed_streams_are_in_home_view());
|
2020-02-09 22:02:55 +01:00
|
|
|
stream_data.add_sub(test);
|
2014-02-20 17:57:30 +01:00
|
|
|
assert(!stream_data.all_subscribed_streams_are_in_home_view());
|
2013-08-19 18:46:21 +02:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.equal(stream_data.get_sub("denmark"), denmark);
|
|
|
|
assert.equal(stream_data.get_sub("Social"), social);
|
2013-08-19 18:46:21 +02:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.deepEqual(stream_data.home_view_stream_names(), ["social"]);
|
|
|
|
assert.deepEqual(stream_data.subscribed_streams(), ["social", "test"]);
|
|
|
|
assert.deepEqual(stream_data.get_colors(), ["red", "yellow"]);
|
2020-07-25 18:20:54 +02:00
|
|
|
assert.deepEqual(stream_data.subscribed_stream_ids(), [social.stream_id, test.stream_id]);
|
2013-08-19 18:46:21 +02:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
assert(stream_data.is_subscribed("social"));
|
|
|
|
assert(stream_data.is_subscribed("Social"));
|
|
|
|
assert(!stream_data.is_subscribed("Denmark"));
|
|
|
|
assert(!stream_data.is_subscribed("Rome"));
|
2013-08-19 18:46:21 +02:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
assert(stream_data.get_invite_only("social"));
|
|
|
|
assert(!stream_data.get_invite_only("unknown"));
|
2018-05-14 12:06:56 +02:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.equal(stream_data.get_color("social"), "red");
|
|
|
|
assert.equal(stream_data.get_color("unknown"), global.stream_color.default_color);
|
2013-08-19 19:25:44 +02:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.equal(stream_data.get_name("denMARK"), "Denmark");
|
|
|
|
assert.equal(stream_data.get_name("unknown Stream"), "unknown Stream");
|
2013-08-23 05:36:49 +02:00
|
|
|
|
2019-05-21 09:33:21 +02:00
|
|
|
assert(!stream_data.is_muted(social.stream_id));
|
|
|
|
assert(stream_data.is_muted(denmark.stream_id));
|
2017-07-15 19:12:31 +02:00
|
|
|
|
|
|
|
assert.equal(stream_data.maybe_get_stream_name(), undefined);
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.equal(stream_data.maybe_get_stream_name(social.stream_id), "social");
|
2017-07-15 19:12:31 +02:00
|
|
|
assert.equal(stream_data.maybe_get_stream_name(42), undefined);
|
2017-12-03 20:40:01 +01:00
|
|
|
|
|
|
|
stream_data.set_realm_default_streams([denmark]);
|
2020-03-22 17:04:47 +01:00
|
|
|
assert(stream_data.is_default_stream_id(denmark.stream_id));
|
|
|
|
assert(!stream_data.is_default_stream_id(social.stream_id));
|
|
|
|
assert(!stream_data.is_default_stream_id(999999));
|
2020-07-03 16:37:16 +02:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.equal(stream_data.slug_to_name("2-social"), "social");
|
|
|
|
assert.equal(stream_data.slug_to_name("2-whatever"), "social");
|
|
|
|
assert.equal(stream_data.slug_to_name("2"), "social");
|
2020-07-03 16:37:16 +02:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.equal(stream_data.slug_to_name("25-or-6-to-4"), "25-or-6-to-4");
|
2018-05-15 12:40:07 +02:00
|
|
|
});
|
2013-08-23 05:29:41 +02:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
run_test("renames", () => {
|
2014-02-05 20:35:16 +01:00
|
|
|
stream_data.clear_subscriptions();
|
2019-11-02 00:06:25 +01:00
|
|
|
const id = 42;
|
|
|
|
let sub = {
|
2020-07-15 01:29:15 +02:00
|
|
|
name: "Denmark",
|
2014-02-05 20:35:16 +01:00
|
|
|
subscribed: true,
|
2020-07-15 01:29:15 +02:00
|
|
|
color: "red",
|
2016-12-03 23:17:57 +01:00
|
|
|
stream_id: id,
|
2014-02-05 20:35:16 +01:00
|
|
|
};
|
2020-02-09 22:02:55 +01:00
|
|
|
stream_data.add_sub(sub);
|
2020-07-15 01:29:15 +02:00
|
|
|
sub = stream_data.get_sub("Denmark");
|
|
|
|
assert.equal(sub.color, "red");
|
2014-02-05 20:35:16 +01:00
|
|
|
sub = stream_data.get_sub_by_id(id);
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.equal(sub.color, "red");
|
2016-10-30 17:33:23 +01:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
stream_data.rename_sub(sub, "Sweden");
|
2016-10-30 17:33:23 +01:00
|
|
|
sub = stream_data.get_sub_by_id(id);
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.equal(sub.color, "red");
|
|
|
|
assert.equal(sub.name, "Sweden");
|
2017-05-11 21:49:38 +02:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
sub = stream_data.get_sub("Denmark");
|
2017-05-11 21:49:38 +02:00
|
|
|
assert.equal(sub, undefined);
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
sub = stream_data.get_sub_by_name("Denmark");
|
|
|
|
assert.equal(sub.name, "Sweden");
|
2017-05-11 21:49:38 +02:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
const actual_id = stream_data.get_stream_id("Denmark");
|
2017-05-11 21:49:38 +02:00
|
|
|
assert.equal(actual_id, 42);
|
2018-05-15 12:40:07 +02:00
|
|
|
});
|
2014-02-05 20:35:16 +01:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
run_test("unsubscribe", () => {
|
2016-11-09 16:26:35 +01:00
|
|
|
stream_data.clear_subscriptions();
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
let sub = {name: "devel", subscribed: false, stream_id: 1};
|
2016-11-09 16:26:35 +01:00
|
|
|
|
|
|
|
// set up our subscription
|
2020-02-09 22:02:55 +01:00
|
|
|
stream_data.add_sub(sub);
|
2016-11-09 16:26:35 +01:00
|
|
|
sub.subscribed = true;
|
|
|
|
stream_data.set_subscribers(sub, [me.user_id]);
|
|
|
|
|
|
|
|
// ensure our setup is accurate
|
2020-07-15 01:29:15 +02:00
|
|
|
assert(stream_data.is_subscribed("devel"));
|
2016-11-09 16:26:35 +01:00
|
|
|
|
|
|
|
// DO THE UNSUBSCRIBE HERE
|
|
|
|
stream_data.unsubscribe_myself(sub);
|
|
|
|
assert(!sub.subscribed);
|
2020-07-15 01:29:15 +02:00
|
|
|
assert(!stream_data.is_subscribed("devel"));
|
2019-12-28 11:53:50 +01:00
|
|
|
assert(!contains_sub(stream_data.subscribed_subs(), sub));
|
|
|
|
assert(contains_sub(stream_data.unsubscribed_subs(), sub));
|
2016-11-09 16:26:35 +01:00
|
|
|
|
|
|
|
// make sure subsequent calls work
|
2020-07-15 01:29:15 +02:00
|
|
|
sub = stream_data.get_sub("devel");
|
2016-11-09 16:26:35 +01:00
|
|
|
assert(!sub.subscribed);
|
2018-05-15 12:40:07 +02:00
|
|
|
});
|
2016-11-09 16:26:35 +01:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
run_test("subscribers", () => {
|
2013-09-12 16:55:56 +02:00
|
|
|
stream_data.clear_subscriptions();
|
2020-07-15 01:29:15 +02:00
|
|
|
let sub = {name: "Rome", subscribed: true, stream_id: 1};
|
2013-09-12 16:55:56 +02:00
|
|
|
|
2020-02-09 22:02:55 +01:00
|
|
|
stream_data.add_sub(sub);
|
2013-09-16 22:26:53 +02:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const fred = {
|
2020-07-15 01:29:15 +02:00
|
|
|
email: "fred@zulip.com",
|
|
|
|
full_name: "Fred",
|
2016-12-03 23:17:57 +01:00
|
|
|
user_id: 101,
|
2016-10-30 15:47:20 +01:00
|
|
|
};
|
2019-11-02 00:06:25 +01:00
|
|
|
const not_fred = {
|
2020-07-15 01:29:15 +02:00
|
|
|
email: "not_fred@zulip.com",
|
|
|
|
full_name: "Not Fred",
|
2016-12-03 23:17:57 +01:00
|
|
|
user_id: 102,
|
2016-10-30 15:47:20 +01:00
|
|
|
};
|
2019-11-02 00:06:25 +01:00
|
|
|
const george = {
|
2020-07-15 01:29:15 +02:00
|
|
|
email: "george@zulip.com",
|
|
|
|
full_name: "George",
|
2016-12-03 23:17:57 +01:00
|
|
|
user_id: 103,
|
2016-10-30 15:47:20 +01:00
|
|
|
};
|
2020-05-26 22:34:15 +02:00
|
|
|
people.add_active_user(fred);
|
|
|
|
people.add_active_user(not_fred);
|
|
|
|
people.add_active_user(george);
|
2020-03-21 15:22:40 +01:00
|
|
|
|
|
|
|
function potential_subscriber_ids() {
|
|
|
|
const users = stream_data.potential_subscribers(sub);
|
|
|
|
return users.map((u) => u.user_id).sort();
|
|
|
|
}
|
|
|
|
|
2020-07-15 00:34:28 +02:00
|
|
|
assert.deepEqual(potential_subscriber_ids(), [
|
|
|
|
me.user_id,
|
|
|
|
fred.user_id,
|
|
|
|
not_fred.user_id,
|
|
|
|
george.user_id,
|
|
|
|
]);
|
2016-10-30 15:47:20 +01:00
|
|
|
|
2020-03-21 20:19:30 +01:00
|
|
|
stream_data.set_subscribers(sub, [me.user_id, fred.user_id, george.user_id]);
|
2018-03-19 05:52:37 +01:00
|
|
|
stream_data.update_calculated_fields(sub);
|
2020-07-08 22:28:35 +02:00
|
|
|
assert(stream_data.is_user_subscribed(sub.stream_id, me.user_id));
|
|
|
|
assert(stream_data.is_user_subscribed(sub.stream_id, fred.user_id));
|
|
|
|
assert(stream_data.is_user_subscribed(sub.stream_id, george.user_id));
|
|
|
|
assert(!stream_data.is_user_subscribed(sub.stream_id, not_fred.user_id));
|
2013-09-16 22:26:53 +02:00
|
|
|
|
2020-07-15 00:34:28 +02:00
|
|
|
assert.deepEqual(potential_subscriber_ids(), [not_fred.user_id]);
|
2020-03-21 15:22:40 +01:00
|
|
|
|
2013-09-16 22:26:53 +02:00
|
|
|
stream_data.set_subscribers(sub, []);
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const brutus = {
|
2020-07-15 01:29:15 +02:00
|
|
|
email: "brutus@zulip.com",
|
|
|
|
full_name: "Brutus",
|
2016-12-03 23:17:57 +01:00
|
|
|
user_id: 104,
|
2016-10-30 15:47:20 +01:00
|
|
|
};
|
2020-05-26 22:34:15 +02:00
|
|
|
people.add_active_user(brutus);
|
2020-07-08 22:28:35 +02:00
|
|
|
assert(!stream_data.is_user_subscribed(sub.stream_id, brutus.user_id));
|
2013-09-12 16:55:56 +02:00
|
|
|
|
|
|
|
// add
|
2020-06-20 21:35:41 +02:00
|
|
|
let ok = stream_data.add_subscriber(sub.stream_id, brutus.user_id);
|
2017-03-03 23:14:06 +01:00
|
|
|
assert(ok);
|
2020-07-08 22:28:35 +02:00
|
|
|
assert(stream_data.is_user_subscribed(sub.stream_id, brutus.user_id));
|
2020-07-15 01:29:15 +02:00
|
|
|
sub = stream_data.get_sub("Rome");
|
2016-03-14 06:38:43 +01:00
|
|
|
stream_data.update_subscribers_count(sub);
|
|
|
|
assert.equal(sub.subscriber_count, 1);
|
2019-11-02 00:06:25 +01:00
|
|
|
const sub_email = "Rome:214125235@zulipdev.com:9991";
|
2018-04-05 19:58:27 +02:00
|
|
|
stream_data.update_stream_email_address(sub, sub_email);
|
|
|
|
assert.equal(sub.email_address, sub_email);
|
2013-09-12 16:55:56 +02:00
|
|
|
|
2016-08-23 23:39:06 +02:00
|
|
|
// verify that adding an already-added subscriber is a noop
|
2020-06-20 21:35:41 +02:00
|
|
|
stream_data.add_subscriber(sub.stream_id, brutus.user_id);
|
2020-07-08 22:28:35 +02:00
|
|
|
assert(stream_data.is_user_subscribed(sub.stream_id, brutus.user_id));
|
2020-07-15 01:29:15 +02:00
|
|
|
sub = stream_data.get_sub("Rome");
|
2016-03-14 06:38:43 +01:00
|
|
|
stream_data.update_subscribers_count(sub);
|
|
|
|
assert.equal(sub.subscriber_count, 1);
|
2013-09-12 16:55:56 +02:00
|
|
|
|
|
|
|
// remove
|
2020-06-20 21:52:31 +02:00
|
|
|
ok = stream_data.remove_subscriber(sub.stream_id, brutus.user_id);
|
2017-03-03 23:14:06 +01:00
|
|
|
assert(ok);
|
2020-07-08 22:28:35 +02:00
|
|
|
assert(!stream_data.is_user_subscribed(sub.stream_id, brutus.user_id));
|
2020-07-15 01:29:15 +02:00
|
|
|
sub = stream_data.get_sub("Rome");
|
2016-03-14 06:38:43 +01:00
|
|
|
stream_data.update_subscribers_count(sub);
|
|
|
|
assert.equal(sub.subscriber_count, 0);
|
2013-09-12 16:55:56 +02:00
|
|
|
|
2018-04-06 05:22:07 +02:00
|
|
|
// verify that checking subscription with undefined user id
|
2018-07-10 11:21:19 +02:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
blueslip.expect("warn", "Undefined user_id passed to function is_user_subscribed");
|
2020-07-08 22:28:35 +02:00
|
|
|
assert.equal(stream_data.is_user_subscribed(sub.stream_id, undefined), undefined);
|
2017-12-03 18:46:30 +01:00
|
|
|
|
2017-12-03 19:50:50 +01:00
|
|
|
// Verify noop for bad stream when removing subscriber
|
2020-06-20 21:52:31 +02:00
|
|
|
const bad_stream_id = 999999;
|
2020-07-15 00:34:28 +02:00
|
|
|
blueslip.expect(
|
|
|
|
"warn",
|
|
|
|
"We got a remove_subscriber call for a non-existent stream " + bad_stream_id,
|
|
|
|
);
|
2020-06-20 21:52:31 +02:00
|
|
|
ok = stream_data.remove_subscriber(bad_stream_id, brutus.user_id);
|
2017-12-03 19:50:50 +01:00
|
|
|
assert(!ok);
|
|
|
|
|
2013-09-12 16:55:56 +02:00
|
|
|
// verify that removing an already-removed subscriber is a noop
|
2020-07-15 01:29:15 +02:00
|
|
|
blueslip.expect("warn", "We tried to remove invalid subscriber: 104");
|
2020-06-20 21:52:31 +02:00
|
|
|
ok = stream_data.remove_subscriber(sub.stream_id, brutus.user_id);
|
2017-03-03 23:14:06 +01:00
|
|
|
assert(!ok);
|
2020-07-08 22:28:35 +02:00
|
|
|
assert(!stream_data.is_user_subscribed(sub.stream_id, brutus.user_id));
|
2020-07-15 01:29:15 +02:00
|
|
|
sub = stream_data.get_sub("Rome");
|
2016-03-14 06:38:43 +01:00
|
|
|
stream_data.update_subscribers_count(sub);
|
|
|
|
assert.equal(sub.subscriber_count, 0);
|
2013-09-12 16:55:56 +02:00
|
|
|
|
2013-10-15 19:08:35 +02:00
|
|
|
// Verify defensive code in set_subscribers, where the second parameter
|
|
|
|
// can be undefined.
|
|
|
|
stream_data.set_subscribers(sub);
|
2020-02-09 22:02:55 +01:00
|
|
|
stream_data.add_sub(sub);
|
2020-06-20 21:35:41 +02:00
|
|
|
stream_data.add_subscriber(sub.stream_id, brutus.user_id);
|
2013-10-15 19:08:35 +02:00
|
|
|
sub.subscribed = true;
|
2020-07-08 22:28:35 +02:00
|
|
|
assert(stream_data.is_user_subscribed(sub.stream_id, brutus.user_id));
|
2013-10-15 19:08:35 +02:00
|
|
|
|
2016-11-08 16:06:50 +01:00
|
|
|
// Verify that we noop and don't crash when unsubscribed.
|
2013-09-12 16:55:56 +02:00
|
|
|
sub.subscribed = false;
|
2018-03-19 05:52:37 +01:00
|
|
|
stream_data.update_calculated_fields(sub);
|
2020-06-20 21:35:41 +02:00
|
|
|
ok = stream_data.add_subscriber(sub.stream_id, brutus.user_id);
|
2017-03-03 23:14:06 +01:00
|
|
|
assert(ok);
|
2020-07-08 22:28:35 +02:00
|
|
|
assert.equal(stream_data.is_user_subscribed(sub.stream_id, brutus.user_id), true);
|
2020-06-20 21:52:31 +02:00
|
|
|
stream_data.remove_subscriber(sub.stream_id, brutus.user_id);
|
2020-07-08 22:28:35 +02:00
|
|
|
assert.equal(stream_data.is_user_subscribed(sub.stream_id, brutus.user_id), false);
|
2020-06-20 21:35:41 +02:00
|
|
|
stream_data.add_subscriber(sub.stream_id, brutus.user_id);
|
2020-07-08 22:28:35 +02:00
|
|
|
assert.equal(stream_data.is_user_subscribed(sub.stream_id, brutus.user_id), true);
|
2018-03-19 05:52:37 +01:00
|
|
|
|
2020-04-03 17:18:04 +02:00
|
|
|
blueslip.expect(
|
2020-07-15 01:29:15 +02:00
|
|
|
"warn",
|
2020-07-15 00:34:28 +02:00
|
|
|
"We got a is_user_subscribed call for a non-existent or inaccessible stream.",
|
|
|
|
2,
|
|
|
|
);
|
2018-03-19 05:52:37 +01:00
|
|
|
sub.invite_only = true;
|
|
|
|
stream_data.update_calculated_fields(sub);
|
2020-07-08 22:28:35 +02:00
|
|
|
assert.equal(stream_data.is_user_subscribed(sub.stream_id, brutus.user_id), undefined);
|
2020-06-20 21:52:31 +02:00
|
|
|
stream_data.remove_subscriber(sub.stream_id, brutus.user_id);
|
2020-07-08 22:28:35 +02:00
|
|
|
assert.equal(stream_data.is_user_subscribed(sub.stream_id, brutus.user_id), undefined);
|
2013-09-12 16:55:56 +02:00
|
|
|
|
2017-03-03 23:14:06 +01:00
|
|
|
// Verify that we don't crash and return false for a bad stream.
|
2020-07-15 00:34:28 +02:00
|
|
|
blueslip.expect("warn", "We got an add_subscriber call for a non-existent stream.");
|
2020-06-20 21:35:41 +02:00
|
|
|
ok = stream_data.add_subscriber(9999999, brutus.user_id);
|
2017-03-03 23:14:06 +01:00
|
|
|
assert(!ok);
|
|
|
|
|
|
|
|
// Verify that we don't crash and return false for a bad user id.
|
2020-07-15 01:29:15 +02:00
|
|
|
blueslip.expect("error", "Unknown user_id in get_by_user_id: 9999999");
|
|
|
|
blueslip.expect("error", "We tried to add invalid subscriber: 9999999");
|
2020-06-20 21:35:41 +02:00
|
|
|
ok = stream_data.add_subscriber(sub.stream_id, 9999999);
|
2017-03-03 23:14:06 +01:00
|
|
|
assert(!ok);
|
2018-05-15 12:40:07 +02:00
|
|
|
});
|
2016-10-17 16:38:15 +02:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
run_test("is_active", () => {
|
2017-04-28 15:59:30 +02:00
|
|
|
stream_data.clear_subscriptions();
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
let sub;
|
2019-02-11 22:05:59 +01:00
|
|
|
|
2019-03-17 14:48:51 +01:00
|
|
|
page_params.demote_inactive_streams =
|
2020-02-21 14:26:11 +01:00
|
|
|
settings_config.demote_inactive_streams_values.automatic.code;
|
2019-12-27 15:59:42 +01:00
|
|
|
stream_data.set_filter_out_inactives();
|
2019-03-17 14:48:51 +01:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
sub = {name: "pets", subscribed: false, stream_id: 111};
|
2020-02-09 22:02:55 +01:00
|
|
|
stream_data.add_sub(sub);
|
2017-04-28 15:59:30 +02:00
|
|
|
|
2019-02-11 22:05:59 +01:00
|
|
|
assert(stream_data.is_active(sub));
|
|
|
|
|
2019-03-17 14:48:51 +01:00
|
|
|
stream_data.subscribe_myself(sub);
|
|
|
|
assert(stream_data.is_active(sub));
|
|
|
|
|
2019-12-28 11:53:50 +01:00
|
|
|
assert(contains_sub(stream_data.subscribed_subs(), sub));
|
|
|
|
assert(!contains_sub(stream_data.unsubscribed_subs(), sub));
|
|
|
|
|
2019-03-17 14:48:51 +01:00
|
|
|
stream_data.unsubscribe_myself(sub);
|
|
|
|
assert(stream_data.is_active(sub));
|
|
|
|
|
|
|
|
sub.pin_to_top = true;
|
|
|
|
assert(stream_data.is_active(sub));
|
|
|
|
sub.pin_to_top = false;
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const opts = {
|
2019-03-17 14:48:51 +01:00
|
|
|
stream_id: 222,
|
|
|
|
message_id: 108,
|
2020-07-15 01:29:15 +02:00
|
|
|
topic_name: "topic2",
|
2019-03-17 14:48:51 +01:00
|
|
|
};
|
2020-03-22 18:40:05 +01:00
|
|
|
stream_topic_history.add_message(opts);
|
2019-03-17 14:48:51 +01:00
|
|
|
|
|
|
|
assert(stream_data.is_active(sub));
|
|
|
|
|
|
|
|
page_params.demote_inactive_streams =
|
2020-02-21 14:26:11 +01:00
|
|
|
settings_config.demote_inactive_streams_values.always.code;
|
2019-12-27 15:59:42 +01:00
|
|
|
stream_data.set_filter_out_inactives();
|
2019-03-17 14:48:51 +01:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
sub = {name: "pets", subscribed: false, stream_id: 111};
|
2020-02-09 22:02:55 +01:00
|
|
|
stream_data.add_sub(sub);
|
2019-02-11 22:05:59 +01:00
|
|
|
|
2017-04-28 15:59:30 +02:00
|
|
|
assert(!stream_data.is_active(sub));
|
|
|
|
|
2019-03-17 14:48:51 +01:00
|
|
|
sub.pin_to_top = true;
|
|
|
|
assert(stream_data.is_active(sub));
|
|
|
|
sub.pin_to_top = false;
|
|
|
|
|
2017-04-28 15:59:30 +02:00
|
|
|
stream_data.subscribe_myself(sub);
|
|
|
|
assert(stream_data.is_active(sub));
|
|
|
|
|
|
|
|
stream_data.unsubscribe_myself(sub);
|
|
|
|
assert(!stream_data.is_active(sub));
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
sub = {name: "lunch", subscribed: false, stream_id: 222};
|
2020-02-09 22:02:55 +01:00
|
|
|
stream_data.add_sub(sub);
|
2017-04-28 15:59:30 +02:00
|
|
|
|
2019-03-17 14:48:51 +01:00
|
|
|
assert(stream_data.is_active(sub));
|
|
|
|
|
2020-03-22 18:40:05 +01:00
|
|
|
stream_topic_history.add_message(opts);
|
2019-03-17 14:48:51 +01:00
|
|
|
|
|
|
|
assert(stream_data.is_active(sub));
|
|
|
|
|
2020-07-15 00:34:28 +02:00
|
|
|
page_params.demote_inactive_streams = settings_config.demote_inactive_streams_values.never.code;
|
2019-12-27 15:59:42 +01:00
|
|
|
stream_data.set_filter_out_inactives();
|
2019-03-17 14:48:51 +01:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
sub = {name: "pets", subscribed: false, stream_id: 111};
|
2020-02-09 22:02:55 +01:00
|
|
|
stream_data.add_sub(sub);
|
2019-03-17 14:48:51 +01:00
|
|
|
|
|
|
|
assert(stream_data.is_active(sub));
|
|
|
|
|
|
|
|
stream_data.subscribe_myself(sub);
|
|
|
|
assert(stream_data.is_active(sub));
|
|
|
|
|
|
|
|
stream_data.unsubscribe_myself(sub);
|
|
|
|
assert(stream_data.is_active(sub));
|
|
|
|
|
|
|
|
sub.pin_to_top = true;
|
|
|
|
assert(stream_data.is_active(sub));
|
2017-04-28 15:59:30 +02:00
|
|
|
|
2020-03-22 18:40:05 +01:00
|
|
|
stream_topic_history.add_message(opts);
|
2017-04-28 15:59:30 +02:00
|
|
|
|
|
|
|
assert(stream_data.is_active(sub));
|
2018-05-15 12:40:07 +02:00
|
|
|
});
|
2017-04-28 15:59:30 +02:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
run_test("admin_options", () => {
|
2016-10-17 16:38:15 +02:00
|
|
|
function make_sub() {
|
2019-11-02 00:06:25 +01:00
|
|
|
const sub = {
|
2016-10-17 16:38:15 +02:00
|
|
|
subscribed: false,
|
2020-07-15 01:29:15 +02:00
|
|
|
color: "blue",
|
|
|
|
name: "stream_to_admin",
|
2016-10-17 16:38:15 +02:00
|
|
|
stream_id: 1,
|
2019-05-15 08:54:25 +02:00
|
|
|
is_muted: true,
|
2016-12-03 23:17:57 +01:00
|
|
|
invite_only: false,
|
2016-10-17 16:38:15 +02:00
|
|
|
};
|
2020-02-09 22:02:55 +01:00
|
|
|
stream_data.add_sub(sub);
|
2017-04-28 17:55:22 +02:00
|
|
|
return sub;
|
2016-10-17 16:38:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// non-admins can't do anything
|
|
|
|
global.page_params.is_admin = false;
|
2019-11-02 00:06:25 +01:00
|
|
|
let sub = make_sub();
|
2017-04-28 17:55:22 +02:00
|
|
|
stream_data.update_calculated_fields(sub);
|
2016-10-17 16:38:15 +02:00
|
|
|
assert(!sub.is_admin);
|
2018-05-22 01:14:18 +02:00
|
|
|
assert(!sub.can_change_stream_permissions);
|
2016-10-17 16:38:15 +02:00
|
|
|
|
|
|
|
// just a sanity check that we leave "normal" fields alone
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.equal(sub.color, "blue");
|
2016-10-17 16:38:15 +02:00
|
|
|
|
|
|
|
// the remaining cases are for admin users
|
|
|
|
global.page_params.is_admin = true;
|
|
|
|
|
|
|
|
// admins can make public streams become private
|
|
|
|
sub = make_sub();
|
2017-04-28 17:55:22 +02:00
|
|
|
stream_data.update_calculated_fields(sub);
|
2016-10-17 16:38:15 +02:00
|
|
|
assert(sub.is_admin);
|
2018-05-22 01:14:18 +02:00
|
|
|
assert(sub.can_change_stream_permissions);
|
2016-10-17 16:38:15 +02:00
|
|
|
|
|
|
|
// admins can only make private streams become public
|
|
|
|
// if they are subscribed
|
|
|
|
sub = make_sub();
|
|
|
|
sub.invite_only = true;
|
|
|
|
sub.subscribed = false;
|
2017-04-28 17:55:22 +02:00
|
|
|
stream_data.update_calculated_fields(sub);
|
2016-10-17 16:38:15 +02:00
|
|
|
assert(sub.is_admin);
|
2018-05-22 01:14:18 +02:00
|
|
|
assert(!sub.can_change_stream_permissions);
|
2016-10-17 16:38:15 +02:00
|
|
|
|
|
|
|
sub = make_sub();
|
|
|
|
sub.invite_only = true;
|
|
|
|
sub.subscribed = true;
|
2017-04-28 17:55:22 +02:00
|
|
|
stream_data.update_calculated_fields(sub);
|
2016-10-17 16:38:15 +02:00
|
|
|
assert(sub.is_admin);
|
2018-05-22 01:14:18 +02:00
|
|
|
assert(sub.can_change_stream_permissions);
|
2018-05-15 12:40:07 +02:00
|
|
|
});
|
2016-10-17 17:48:56 +02:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
run_test("stream_settings", () => {
|
2019-11-02 00:06:25 +01:00
|
|
|
const cinnamon = {
|
2016-10-17 17:48:56 +02:00
|
|
|
stream_id: 1,
|
2020-07-15 01:29:15 +02:00
|
|
|
name: "c",
|
|
|
|
color: "cinnamon",
|
2016-12-03 23:17:57 +01:00
|
|
|
subscribed: true,
|
2017-08-22 20:51:41 +02:00
|
|
|
invite_only: false,
|
2016-10-17 17:48:56 +02:00
|
|
|
};
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const blue = {
|
2016-10-17 17:48:56 +02:00
|
|
|
stream_id: 2,
|
2020-07-15 01:29:15 +02:00
|
|
|
name: "b",
|
|
|
|
color: "blue",
|
2016-12-03 23:17:57 +01:00
|
|
|
subscribed: false,
|
2017-08-22 20:51:41 +02:00
|
|
|
invite_only: false,
|
2016-10-17 17:48:56 +02:00
|
|
|
};
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const amber = {
|
2016-10-17 17:48:56 +02:00
|
|
|
stream_id: 3,
|
2020-07-15 01:29:15 +02:00
|
|
|
name: "a",
|
|
|
|
color: "amber",
|
2016-12-03 23:17:57 +01:00
|
|
|
subscribed: true,
|
2017-08-22 20:51:41 +02:00
|
|
|
invite_only: true,
|
2019-05-02 19:43:27 +02:00
|
|
|
history_public_to_subscribers: true,
|
2020-02-04 21:50:55 +01:00
|
|
|
stream_post_policy: stream_data.stream_post_policy_values.admins.code,
|
2020-06-15 17:00:00 +02:00
|
|
|
message_retention_days: 10,
|
2016-10-17 17:48:56 +02:00
|
|
|
};
|
|
|
|
stream_data.clear_subscriptions();
|
2020-02-09 22:02:55 +01:00
|
|
|
stream_data.add_sub(cinnamon);
|
|
|
|
stream_data.add_sub(amber);
|
|
|
|
stream_data.add_sub(blue);
|
2016-10-17 17:48:56 +02:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
let sub_rows = stream_data.get_streams_for_settings_page();
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.equal(sub_rows[0].color, "blue");
|
|
|
|
assert.equal(sub_rows[1].color, "amber");
|
|
|
|
assert.equal(sub_rows[2].color, "cinnamon");
|
2016-10-17 17:48:56 +02:00
|
|
|
|
2017-08-22 20:51:41 +02:00
|
|
|
sub_rows = stream_data.get_streams_for_admin();
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.equal(sub_rows[0].name, "a");
|
|
|
|
assert.equal(sub_rows[1].name, "b");
|
|
|
|
assert.equal(sub_rows[2].name, "c");
|
2017-08-22 20:51:41 +02:00
|
|
|
assert.equal(sub_rows[0].invite_only, true);
|
|
|
|
assert.equal(sub_rows[1].invite_only, false);
|
|
|
|
assert.equal(sub_rows[2].invite_only, false);
|
|
|
|
|
2019-05-02 19:43:27 +02:00
|
|
|
assert.equal(sub_rows[0].history_public_to_subscribers, true);
|
2020-07-15 00:34:28 +02:00
|
|
|
assert.equal(
|
|
|
|
sub_rows[0].stream_post_policy === stream_data.stream_post_policy_values.admins.code,
|
|
|
|
true,
|
|
|
|
);
|
2020-06-15 17:00:00 +02:00
|
|
|
assert.equal(sub_rows[0].message_retention_days, 10);
|
2019-05-02 19:43:27 +02:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
const sub = stream_data.get_sub("a");
|
2019-05-02 19:43:27 +02:00
|
|
|
stream_data.update_stream_privacy(sub, {
|
|
|
|
invite_only: false,
|
|
|
|
history_public_to_subscribers: false,
|
|
|
|
});
|
2020-02-04 21:50:55 +01:00
|
|
|
stream_data.update_stream_post_policy(sub, 1);
|
2020-06-15 17:00:00 +02:00
|
|
|
stream_data.update_message_retention_setting(sub, -1);
|
2019-05-02 19:43:27 +02:00
|
|
|
stream_data.update_calculated_fields(sub);
|
|
|
|
assert.equal(sub.invite_only, false);
|
|
|
|
assert.equal(sub.history_public_to_subscribers, false);
|
2020-07-15 00:34:28 +02:00
|
|
|
assert.equal(sub.stream_post_policy, stream_data.stream_post_policy_values.everyone.code);
|
2020-06-15 17:00:00 +02:00
|
|
|
assert.equal(sub.message_retention_days, -1);
|
2019-05-08 08:57:23 +02:00
|
|
|
|
|
|
|
// For guest user only retrieve subscribed streams
|
|
|
|
sub_rows = stream_data.get_updated_unsorted_subs();
|
|
|
|
assert.equal(sub_rows.length, 3);
|
|
|
|
global.page_params.is_guest = true;
|
|
|
|
sub_rows = stream_data.get_updated_unsorted_subs();
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.equal(sub_rows[0].name, "c");
|
|
|
|
assert.equal(sub_rows[1].name, "a");
|
2019-05-08 08:57:23 +02:00
|
|
|
assert.equal(sub_rows.length, 2);
|
2018-05-15 12:40:07 +02:00
|
|
|
});
|
2017-02-16 03:47:08 +01:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
run_test("default_stream_names", () => {
|
2019-11-02 00:06:25 +01:00
|
|
|
const announce = {
|
2017-08-22 20:00:09 +02:00
|
|
|
stream_id: 101,
|
2020-07-15 01:29:15 +02:00
|
|
|
name: "announce",
|
2017-08-22 20:00:09 +02:00
|
|
|
subscribed: true,
|
|
|
|
};
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const public_stream = {
|
2017-08-22 20:00:09 +02:00
|
|
|
stream_id: 102,
|
2020-07-15 01:29:15 +02:00
|
|
|
name: "public",
|
2017-08-22 20:00:09 +02:00
|
|
|
subscribed: true,
|
|
|
|
};
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const private_stream = {
|
2017-08-22 20:00:09 +02:00
|
|
|
stream_id: 103,
|
2020-07-15 01:29:15 +02:00
|
|
|
name: "private",
|
2017-08-22 20:00:09 +02:00
|
|
|
subscribed: true,
|
|
|
|
invite_only: true,
|
|
|
|
};
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const general = {
|
2018-07-22 11:30:38 +02:00
|
|
|
stream_id: 104,
|
2020-07-15 01:29:15 +02:00
|
|
|
name: "general",
|
2018-07-22 11:30:38 +02:00
|
|
|
subscribed: true,
|
|
|
|
invite_only: false,
|
|
|
|
};
|
|
|
|
|
2017-08-22 20:00:09 +02:00
|
|
|
stream_data.clear_subscriptions();
|
2018-07-22 11:30:38 +02:00
|
|
|
stream_data.set_realm_default_streams([announce, general]);
|
2020-02-09 22:02:55 +01:00
|
|
|
stream_data.add_sub(announce);
|
|
|
|
stream_data.add_sub(public_stream);
|
|
|
|
stream_data.add_sub(private_stream);
|
|
|
|
stream_data.add_sub(general);
|
2017-08-22 20:00:09 +02:00
|
|
|
|
2020-03-22 13:28:16 +01:00
|
|
|
const names = stream_data.get_non_default_stream_names();
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.deepEqual(names.sort(), ["private", "public"]);
|
2018-07-22 11:30:38 +02:00
|
|
|
|
2020-03-22 13:28:16 +01:00
|
|
|
const default_stream_ids = stream_data.get_default_stream_ids();
|
2020-07-15 00:34:28 +02:00
|
|
|
assert.deepEqual(default_stream_ids.sort(), [announce.stream_id, general.stream_id]);
|
2018-05-15 12:40:07 +02:00
|
|
|
});
|
2017-08-22 20:00:09 +02:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
run_test("delete_sub", () => {
|
2019-11-02 00:06:25 +01:00
|
|
|
const canada = {
|
2017-02-16 03:47:08 +01:00
|
|
|
stream_id: 101,
|
2020-07-15 01:29:15 +02:00
|
|
|
name: "Canada",
|
2017-02-16 03:47:08 +01:00
|
|
|
subscribed: true,
|
|
|
|
};
|
|
|
|
|
|
|
|
stream_data.clear_subscriptions();
|
2020-02-09 22:02:55 +01:00
|
|
|
stream_data.add_sub(canada);
|
2017-02-16 03:47:08 +01:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
assert(stream_data.is_subscribed("Canada"));
|
|
|
|
assert(stream_data.get_sub("Canada").stream_id, canada.stream_id);
|
|
|
|
assert(stream_data.get_sub_by_id(canada.stream_id).name, "Canada");
|
2017-02-16 03:47:08 +01:00
|
|
|
|
|
|
|
stream_data.delete_sub(canada.stream_id);
|
2020-07-15 01:29:15 +02:00
|
|
|
assert(!stream_data.is_subscribed("Canada"));
|
|
|
|
assert(!stream_data.get_sub("Canada"));
|
2017-02-16 03:47:08 +01:00
|
|
|
assert(!stream_data.get_sub_by_id(canada.stream_id));
|
2017-12-14 16:23:19 +01:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
blueslip.expect("warn", "Failed to delete stream 99999");
|
2019-12-30 12:51:16 +01:00
|
|
|
stream_data.delete_sub(99999);
|
2018-05-15 12:40:07 +02:00
|
|
|
});
|
2017-06-29 15:35:34 +02:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
run_test("get_subscriber_count", () => {
|
2019-11-02 00:06:25 +01:00
|
|
|
const india = {
|
2017-06-29 15:35:34 +02:00
|
|
|
stream_id: 102,
|
2020-07-15 01:29:15 +02:00
|
|
|
name: "India",
|
2017-06-29 15:35:34 +02:00
|
|
|
subscribed: true,
|
|
|
|
};
|
|
|
|
stream_data.clear_subscriptions();
|
2018-07-10 11:21:19 +02:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
blueslip.expect("warn", "We got a get_subscriber_count count call for a non-existent stream.");
|
2020-07-06 12:36:55 +02:00
|
|
|
assert.equal(stream_data.get_subscriber_count(india.stream_id), undefined);
|
2018-07-10 11:21:19 +02:00
|
|
|
|
2020-02-09 22:02:55 +01:00
|
|
|
stream_data.add_sub(india);
|
2020-07-06 12:36:55 +02:00
|
|
|
assert.equal(stream_data.get_subscriber_count(india.stream_id), 0);
|
2017-06-29 15:35:34 +02:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const fred = {
|
2020-07-15 01:29:15 +02:00
|
|
|
email: "fred@zulip.com",
|
|
|
|
full_name: "Fred",
|
2017-06-29 15:35:34 +02:00
|
|
|
user_id: 101,
|
|
|
|
};
|
2020-05-26 22:34:15 +02:00
|
|
|
people.add_active_user(fred);
|
2020-06-20 21:35:41 +02:00
|
|
|
stream_data.add_subscriber(india.stream_id, 102);
|
2020-07-06 12:36:55 +02:00
|
|
|
assert.equal(stream_data.get_subscriber_count(india.stream_id), 1);
|
2019-11-02 00:06:25 +01:00
|
|
|
const george = {
|
2020-07-15 01:29:15 +02:00
|
|
|
email: "george@zulip.com",
|
|
|
|
full_name: "George",
|
2017-06-29 15:35:34 +02:00
|
|
|
user_id: 103,
|
|
|
|
};
|
2020-05-26 22:34:15 +02:00
|
|
|
people.add_active_user(george);
|
2020-06-20 21:35:41 +02:00
|
|
|
stream_data.add_subscriber(india.stream_id, 103);
|
2020-07-06 12:36:55 +02:00
|
|
|
assert.equal(stream_data.get_subscriber_count(india.stream_id), 2);
|
2017-12-08 16:16:59 +01:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
const sub = stream_data.get_sub_by_name("India");
|
2017-12-08 16:16:59 +01:00
|
|
|
delete sub.subscribers;
|
2020-07-06 12:36:55 +02:00
|
|
|
assert.deepStrictEqual(stream_data.get_subscriber_count(india.stream_id), 0);
|
2018-05-15 12:40:07 +02:00
|
|
|
});
|
2017-12-03 05:30:58 +01:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
run_test("notifications", () => {
|
2019-11-02 00:06:25 +01:00
|
|
|
const india = {
|
2017-12-03 05:30:58 +01:00
|
|
|
stream_id: 102,
|
2020-07-15 01:29:15 +02:00
|
|
|
name: "India",
|
2017-12-03 05:30:58 +01:00
|
|
|
subscribed: true,
|
2020-02-27 13:57:11 +01:00
|
|
|
invite_only: false,
|
|
|
|
is_web_public: false,
|
2019-02-13 10:22:16 +01:00
|
|
|
desktop_notifications: null,
|
|
|
|
audible_notifications: null,
|
2019-12-10 03:11:12 +01:00
|
|
|
email_notifications: null,
|
|
|
|
push_notifications: null,
|
|
|
|
wildcard_mentions_notify: null,
|
2017-12-03 05:30:58 +01:00
|
|
|
};
|
|
|
|
stream_data.clear_subscriptions();
|
2020-02-09 22:02:55 +01:00
|
|
|
stream_data.add_sub(india);
|
2019-02-13 10:22:16 +01:00
|
|
|
|
2020-06-19 22:08:34 +02:00
|
|
|
assert(!stream_data.receives_notifications(india.stream_id, "desktop_notifications"));
|
|
|
|
assert(!stream_data.receives_notifications(india.stream_id, "audible_notifications"));
|
2017-12-03 05:33:29 +01:00
|
|
|
|
2019-02-13 10:22:16 +01:00
|
|
|
page_params.enable_stream_desktop_notifications = true;
|
2019-06-11 08:47:49 +02:00
|
|
|
page_params.enable_stream_audible_notifications = true;
|
2020-06-19 22:08:34 +02:00
|
|
|
assert(stream_data.receives_notifications(india.stream_id, "desktop_notifications"));
|
|
|
|
assert(stream_data.receives_notifications(india.stream_id, "audible_notifications"));
|
2019-02-13 10:22:16 +01:00
|
|
|
|
|
|
|
page_params.enable_stream_desktop_notifications = false;
|
2019-06-11 08:47:49 +02:00
|
|
|
page_params.enable_stream_audible_notifications = false;
|
2020-06-19 22:08:34 +02:00
|
|
|
assert(!stream_data.receives_notifications(india.stream_id, "desktop_notifications"));
|
|
|
|
assert(!stream_data.receives_notifications(india.stream_id, "audible_notifications"));
|
2019-02-13 10:22:16 +01:00
|
|
|
|
|
|
|
india.desktop_notifications = true;
|
|
|
|
india.audible_notifications = true;
|
2020-06-19 22:08:34 +02:00
|
|
|
assert(stream_data.receives_notifications(india.stream_id, "desktop_notifications"));
|
|
|
|
assert(stream_data.receives_notifications(india.stream_id, "audible_notifications"));
|
2019-02-13 10:22:16 +01:00
|
|
|
|
|
|
|
india.desktop_notifications = false;
|
|
|
|
india.audible_notifications = false;
|
|
|
|
page_params.enable_stream_desktop_notifications = true;
|
2019-06-11 08:47:49 +02:00
|
|
|
page_params.enable_stream_audible_notifications = true;
|
2020-06-19 22:08:34 +02:00
|
|
|
assert(!stream_data.receives_notifications(india.stream_id, "desktop_notifications"));
|
|
|
|
assert(!stream_data.receives_notifications(india.stream_id, "audible_notifications"));
|
2019-12-10 03:11:12 +01:00
|
|
|
|
|
|
|
page_params.wildcard_mentions_notify = true;
|
2020-06-19 22:08:34 +02:00
|
|
|
assert(stream_data.receives_notifications(india.stream_id, "wildcard_mentions_notify"));
|
2019-12-10 03:11:12 +01:00
|
|
|
page_params.wildcard_mentions_notify = false;
|
2020-06-19 22:08:34 +02:00
|
|
|
assert(!stream_data.receives_notifications(india.stream_id, "wildcard_mentions_notify"));
|
2019-12-10 03:11:12 +01:00
|
|
|
india.wildcard_mentions_notify = true;
|
2020-06-19 22:08:34 +02:00
|
|
|
assert(stream_data.receives_notifications(india.stream_id, "wildcard_mentions_notify"));
|
2019-12-10 03:11:12 +01:00
|
|
|
page_params.wildcard_mentions_notify = true;
|
|
|
|
india.wildcard_mentions_notify = false;
|
2020-06-19 22:08:34 +02:00
|
|
|
assert(!stream_data.receives_notifications(india.stream_id, "wildcard_mentions_notify"));
|
2019-12-10 03:11:12 +01:00
|
|
|
|
|
|
|
page_params.enable_stream_push_notifications = true;
|
2020-06-19 22:08:34 +02:00
|
|
|
assert(stream_data.receives_notifications(india.stream_id, "push_notifications"));
|
2019-12-10 03:11:12 +01:00
|
|
|
page_params.enable_stream_push_notifications = false;
|
2020-06-19 22:08:34 +02:00
|
|
|
assert(!stream_data.receives_notifications(india.stream_id, "push_notifications"));
|
2019-12-10 03:11:12 +01:00
|
|
|
india.push_notifications = true;
|
2020-06-19 22:08:34 +02:00
|
|
|
assert(stream_data.receives_notifications(india.stream_id, "push_notifications"));
|
2019-12-10 03:11:12 +01:00
|
|
|
page_params.enable_stream_push_notifications = true;
|
|
|
|
india.push_notifications = false;
|
2020-06-19 22:08:34 +02:00
|
|
|
assert(!stream_data.receives_notifications(india.stream_id, "push_notifications"));
|
2019-12-10 03:11:12 +01:00
|
|
|
|
|
|
|
page_params.enable_stream_email_notifications = true;
|
2020-06-19 22:08:34 +02:00
|
|
|
assert(stream_data.receives_notifications(india.stream_id, "email_notifications"));
|
2019-12-10 03:11:12 +01:00
|
|
|
page_params.enable_stream_email_notifications = false;
|
2020-06-19 22:08:34 +02:00
|
|
|
assert(!stream_data.receives_notifications(india.stream_id, "email_notifications"));
|
2019-12-10 03:11:12 +01:00
|
|
|
india.email_notifications = true;
|
2020-06-19 22:08:34 +02:00
|
|
|
assert(stream_data.receives_notifications(india.stream_id, "email_notifications"));
|
2019-12-10 03:11:12 +01:00
|
|
|
page_params.enable_stream_email_notifications = true;
|
|
|
|
india.email_notifications = false;
|
2020-06-19 22:08:34 +02:00
|
|
|
assert(!stream_data.receives_notifications(india.stream_id, "email_notifications"));
|
2020-02-27 13:57:11 +01:00
|
|
|
|
|
|
|
const canada = {
|
|
|
|
stream_id: 103,
|
2020-07-15 01:29:15 +02:00
|
|
|
name: "Canada",
|
2020-02-27 13:57:11 +01:00
|
|
|
subscribed: true,
|
|
|
|
invite_only: true,
|
|
|
|
is_web_public: false,
|
|
|
|
desktop_notifications: null,
|
|
|
|
audible_notifications: null,
|
|
|
|
email_notifications: null,
|
|
|
|
push_notifications: null,
|
|
|
|
wildcard_mentions_notify: null,
|
|
|
|
};
|
|
|
|
stream_data.add_sub(canada);
|
|
|
|
|
|
|
|
const antarctica = {
|
|
|
|
stream_id: 104,
|
2020-07-15 01:29:15 +02:00
|
|
|
name: "Antarctica",
|
2020-02-27 13:57:11 +01:00
|
|
|
subscribed: true,
|
|
|
|
desktop_notifications: null,
|
|
|
|
audible_notifications: null,
|
|
|
|
email_notifications: null,
|
|
|
|
push_notifications: null,
|
|
|
|
wildcard_mentions_notify: null,
|
|
|
|
};
|
|
|
|
stream_data.add_sub(antarctica);
|
|
|
|
|
|
|
|
page_params.enable_stream_desktop_notifications = true;
|
|
|
|
page_params.enable_stream_audible_notifications = true;
|
|
|
|
page_params.enable_stream_email_notifications = false;
|
|
|
|
page_params.enable_stream_push_notifications = false;
|
|
|
|
page_params.wildcard_mentions_notify = true;
|
|
|
|
|
|
|
|
india.desktop_notifications = null;
|
|
|
|
india.audible_notifications = true;
|
|
|
|
india.email_notifications = true;
|
|
|
|
india.push_notifications = true;
|
|
|
|
india.wildcard_mentions_notify = false;
|
|
|
|
|
|
|
|
canada.desktop_notifications = true;
|
|
|
|
canada.audible_notifications = false;
|
|
|
|
canada.email_notifications = true;
|
|
|
|
canada.push_notifications = null;
|
|
|
|
canada.wildcard_mentions_notify = false;
|
|
|
|
|
|
|
|
antarctica.desktop_notifications = true;
|
|
|
|
antarctica.audible_notifications = null;
|
|
|
|
antarctica.email_notifications = false;
|
|
|
|
antarctica.push_notifications = null;
|
|
|
|
antarctica.wildcard_mentions_notify = null;
|
|
|
|
|
|
|
|
const unmatched_streams = stream_data.get_unmatched_streams_for_notification_settings();
|
|
|
|
const expected_streams = [
|
|
|
|
{
|
|
|
|
desktop_notifications: true,
|
|
|
|
audible_notifications: false,
|
|
|
|
email_notifications: true,
|
|
|
|
push_notifications: false,
|
|
|
|
wildcard_mentions_notify: false,
|
|
|
|
invite_only: true,
|
|
|
|
is_web_public: false,
|
2020-07-15 01:29:15 +02:00
|
|
|
stream_name: "Canada",
|
2020-02-27 13:57:11 +01:00
|
|
|
stream_id: 103,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desktop_notifications: true,
|
|
|
|
audible_notifications: true,
|
|
|
|
email_notifications: true,
|
|
|
|
push_notifications: true,
|
|
|
|
wildcard_mentions_notify: false,
|
|
|
|
invite_only: false,
|
|
|
|
is_web_public: false,
|
2020-07-15 01:29:15 +02:00
|
|
|
stream_name: "India",
|
2020-02-27 13:57:11 +01:00
|
|
|
stream_id: 102,
|
|
|
|
},
|
|
|
|
];
|
|
|
|
|
|
|
|
assert.deepEqual(unmatched_streams, expected_streams);
|
2018-05-15 12:40:07 +02:00
|
|
|
});
|
2017-12-05 18:18:25 +01:00
|
|
|
|
2020-04-14 12:55:18 +02:00
|
|
|
const tony = {
|
|
|
|
stream_id: 999,
|
2020-07-15 01:29:15 +02:00
|
|
|
name: "tony",
|
2020-04-14 12:55:18 +02:00
|
|
|
subscribed: true,
|
|
|
|
is_muted: false,
|
|
|
|
};
|
2018-05-07 03:30:13 +02:00
|
|
|
|
2020-04-14 12:55:18 +02:00
|
|
|
const jazy = {
|
|
|
|
stream_id: 500,
|
2020-07-15 01:29:15 +02:00
|
|
|
name: "jazy",
|
2020-04-14 12:55:18 +02:00
|
|
|
subscribed: false,
|
|
|
|
is_muted: true,
|
|
|
|
};
|
2018-05-07 03:30:13 +02:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
run_test("is_muted", () => {
|
2020-02-09 22:02:55 +01:00
|
|
|
stream_data.add_sub(tony);
|
|
|
|
stream_data.add_sub(jazy);
|
2020-07-15 01:29:15 +02:00
|
|
|
assert(!stream_data.is_stream_muted_by_name("tony"));
|
|
|
|
assert(stream_data.is_stream_muted_by_name("jazy"));
|
|
|
|
assert(stream_data.is_stream_muted_by_name("EEXISTS"));
|
2018-05-15 12:40:07 +02:00
|
|
|
});
|
2017-12-05 18:46:01 +01:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
run_test("is_notifications_stream_muted", () => {
|
2020-04-14 12:55:18 +02:00
|
|
|
stream_data.add_sub(tony);
|
|
|
|
stream_data.add_sub(jazy);
|
|
|
|
|
|
|
|
page_params.realm_notifications_stream_id = tony.stream_id;
|
2019-05-21 09:33:21 +02:00
|
|
|
assert(!stream_data.is_notifications_stream_muted());
|
2017-12-05 18:46:01 +01:00
|
|
|
|
2020-04-14 12:55:18 +02:00
|
|
|
page_params.realm_notifications_stream_id = jazy.stream_id;
|
2019-05-21 09:33:21 +02:00
|
|
|
assert(stream_data.is_notifications_stream_muted());
|
2018-05-15 12:40:07 +02:00
|
|
|
});
|
2017-12-07 02:32:47 +01:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
run_test("realm_has_notifications_stream", () => {
|
2020-04-15 18:29:26 +02:00
|
|
|
page_params.realm_notifications_stream_id = 10;
|
|
|
|
assert(stream_data.realm_has_notifications_stream());
|
|
|
|
page_params.realm_notifications_stream_id = -1;
|
|
|
|
assert(!stream_data.realm_has_notifications_stream());
|
|
|
|
});
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
run_test("remove_default_stream", () => {
|
2019-11-02 00:06:25 +01:00
|
|
|
const remove_me = {
|
2017-12-07 02:32:47 +01:00
|
|
|
stream_id: 674,
|
2020-07-15 01:29:15 +02:00
|
|
|
name: "remove_me",
|
2017-12-07 02:32:47 +01:00
|
|
|
subscribed: false,
|
2019-05-15 08:54:25 +02:00
|
|
|
is_muted: true,
|
2017-12-07 02:32:47 +01:00
|
|
|
};
|
|
|
|
|
2020-02-09 22:02:55 +01:00
|
|
|
stream_data.add_sub(remove_me);
|
2017-12-07 02:32:47 +01:00
|
|
|
stream_data.set_realm_default_streams([remove_me]);
|
|
|
|
stream_data.remove_default_stream(remove_me.stream_id);
|
2020-03-22 17:04:47 +01:00
|
|
|
assert(!stream_data.is_default_stream_id(remove_me.stream_id));
|
2018-05-15 12:40:07 +02:00
|
|
|
});
|
2017-12-08 17:14:56 +01:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
run_test("canonicalized_name", () => {
|
2020-07-15 00:34:28 +02:00
|
|
|
assert.deepStrictEqual(stream_data.canonicalized_name("Stream_Bar"), "stream_bar");
|
2018-05-15 12:40:07 +02:00
|
|
|
});
|
2017-12-05 09:22:13 +01:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
run_test("create_sub", () => {
|
2017-12-05 09:22:13 +01:00
|
|
|
stream_data.clear_subscriptions();
|
2019-11-02 00:06:25 +01:00
|
|
|
const india = {
|
2017-12-05 09:22:13 +01:00
|
|
|
stream_id: 102,
|
2020-07-15 01:29:15 +02:00
|
|
|
name: "India",
|
2017-12-05 09:22:13 +01:00
|
|
|
subscribed: true,
|
|
|
|
};
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const canada = {
|
2020-07-15 01:29:15 +02:00
|
|
|
name: "Canada",
|
2017-12-05 09:22:13 +01:00
|
|
|
subscribed: true,
|
|
|
|
};
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const antarctica = {
|
2017-12-05 09:22:13 +01:00
|
|
|
stream_id: 103,
|
2020-07-15 01:29:15 +02:00
|
|
|
name: "Antarctica",
|
2017-12-05 09:22:13 +01:00
|
|
|
subscribed: true,
|
2020-07-15 01:29:15 +02:00
|
|
|
color: "#76ce90",
|
2017-12-05 09:22:13 +01:00
|
|
|
};
|
|
|
|
|
2018-11-28 22:07:14 +01:00
|
|
|
color_data.pick_color = function () {
|
2020-07-15 01:29:15 +02:00
|
|
|
return "#bd86e5";
|
2017-12-05 09:22:13 +01:00
|
|
|
};
|
|
|
|
|
2020-06-21 15:23:43 +02:00
|
|
|
const india_sub = stream_data.create_sub_from_server_data(india);
|
2017-12-05 09:22:13 +01:00
|
|
|
assert(india_sub);
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.equal(india_sub.color, "#bd86e5");
|
2020-06-21 15:23:43 +02:00
|
|
|
const new_sub = stream_data.create_sub_from_server_data(india);
|
|
|
|
// make sure sub doesn't get created twice
|
2017-12-05 09:22:13 +01:00
|
|
|
assert.equal(india_sub, new_sub);
|
|
|
|
|
2020-07-15 00:34:28 +02:00
|
|
|
assert.throws(
|
|
|
|
() => {
|
|
|
|
stream_data.create_sub_from_server_data("Canada", canada);
|
|
|
|
},
|
|
|
|
{message: "We cannot create a sub without a stream_id"},
|
|
|
|
);
|
2017-12-05 09:22:13 +01:00
|
|
|
|
2020-06-21 15:23:43 +02:00
|
|
|
const antarctica_sub = stream_data.create_sub_from_server_data(antarctica);
|
2017-12-05 09:22:13 +01:00
|
|
|
assert(antarctica_sub);
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.equal(antarctica_sub.color, "#76ce90");
|
2018-05-15 12:40:07 +02:00
|
|
|
});
|
2017-12-05 09:22:13 +01:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
run_test("initialize", () => {
|
2020-02-25 12:16:26 +01:00
|
|
|
function get_params() {
|
|
|
|
const params = {};
|
|
|
|
|
2020-07-15 00:34:28 +02:00
|
|
|
params.subscriptions = [
|
|
|
|
{
|
|
|
|
name: "subscriptions",
|
|
|
|
stream_id: 2001,
|
|
|
|
},
|
|
|
|
];
|
2017-12-14 18:00:30 +01:00
|
|
|
|
2020-07-15 00:34:28 +02:00
|
|
|
params.unsubscribed = [
|
|
|
|
{
|
|
|
|
name: "unsubscribed",
|
|
|
|
stream_id: 2002,
|
|
|
|
},
|
|
|
|
];
|
2020-02-25 12:16:26 +01:00
|
|
|
|
2020-07-15 00:34:28 +02:00
|
|
|
params.never_subscribed = [
|
|
|
|
{
|
|
|
|
name: "never_subscribed",
|
|
|
|
stream_id: 2003,
|
|
|
|
},
|
2020-03-22 17:32:31 +01:00
|
|
|
];
|
|
|
|
|
2020-07-15 00:34:28 +02:00
|
|
|
params.realm_default_streams = [];
|
|
|
|
|
2020-02-25 12:16:26 +01:00
|
|
|
return params;
|
|
|
|
}
|
|
|
|
|
|
|
|
function initialize() {
|
|
|
|
stream_data.initialize(get_params());
|
2017-12-14 18:00:30 +01:00
|
|
|
}
|
|
|
|
|
2019-03-17 14:48:51 +01:00
|
|
|
page_params.demote_inactive_streams = 1;
|
2017-12-14 18:00:30 +01:00
|
|
|
page_params.realm_notifications_stream_id = -1;
|
2020-02-25 12:16:26 +01:00
|
|
|
|
|
|
|
initialize();
|
2019-02-11 22:05:59 +01:00
|
|
|
assert(!stream_data.is_filtering_inactives());
|
2017-12-14 18:00:30 +01:00
|
|
|
|
2020-07-02 01:39:34 +02:00
|
|
|
const stream_names = stream_data.get_streams_for_admin().map((elem) => elem.name);
|
2020-07-15 01:29:15 +02:00
|
|
|
assert(stream_names.includes("subscriptions"));
|
|
|
|
assert(stream_names.includes("unsubscribed"));
|
|
|
|
assert(stream_names.includes("never_subscribed"));
|
2020-04-14 12:55:18 +02:00
|
|
|
assert.equal(stream_data.get_notifications_stream(), "");
|
2017-12-14 18:00:30 +01:00
|
|
|
|
2018-05-03 17:28:38 +02:00
|
|
|
// Simulate a private stream the user isn't subscribed to
|
|
|
|
page_params.realm_notifications_stream_id = 89;
|
2020-02-25 12:16:26 +01:00
|
|
|
initialize();
|
2020-04-14 12:55:18 +02:00
|
|
|
assert.equal(stream_data.get_notifications_stream(), "");
|
2018-05-03 17:28:38 +02:00
|
|
|
|
|
|
|
// Now actually subscribe the user to the stream
|
2017-12-14 18:00:30 +01:00
|
|
|
initialize();
|
2019-11-02 00:06:25 +01:00
|
|
|
const foo = {
|
2020-07-15 01:29:15 +02:00
|
|
|
name: "foo",
|
2017-12-14 18:00:30 +01:00
|
|
|
stream_id: 89,
|
|
|
|
};
|
|
|
|
|
2020-02-09 22:02:55 +01:00
|
|
|
stream_data.add_sub(foo);
|
2020-02-25 12:16:26 +01:00
|
|
|
initialize();
|
2020-04-14 12:55:18 +02:00
|
|
|
assert.equal(stream_data.get_notifications_stream(), "foo");
|
2018-05-15 12:40:07 +02:00
|
|
|
});
|
2017-12-22 14:59:10 +01:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
run_test("filter inactives", () => {
|
2020-02-25 12:16:26 +01:00
|
|
|
const params = {};
|
|
|
|
params.unsubscribed = [];
|
|
|
|
params.never_subscribed = [];
|
|
|
|
params.subscriptions = [];
|
2020-03-22 17:32:31 +01:00
|
|
|
params.realm_default_streams = [];
|
2019-02-11 22:05:59 +01:00
|
|
|
|
2020-02-25 12:16:26 +01:00
|
|
|
stream_data.initialize(params);
|
2019-02-11 22:05:59 +01:00
|
|
|
assert(!stream_data.is_filtering_inactives());
|
|
|
|
|
2020-07-02 01:45:54 +02:00
|
|
|
_.times(30, (i) => {
|
2020-07-15 01:29:15 +02:00
|
|
|
const name = "random" + i.toString();
|
2019-11-02 00:06:25 +01:00
|
|
|
const stream_id = 100 + i;
|
2019-02-11 22:05:59 +01:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const sub = {
|
2020-07-20 22:18:43 +02:00
|
|
|
name,
|
2019-02-11 22:05:59 +01:00
|
|
|
subscribed: true,
|
|
|
|
newly_subscribed: false,
|
2020-07-20 22:18:43 +02:00
|
|
|
stream_id,
|
2019-02-11 22:05:59 +01:00
|
|
|
};
|
2020-02-09 22:02:55 +01:00
|
|
|
stream_data.add_sub(sub);
|
2019-02-11 22:05:59 +01:00
|
|
|
});
|
2020-02-25 12:16:26 +01:00
|
|
|
stream_data.initialize(params);
|
2019-02-11 22:05:59 +01:00
|
|
|
assert(stream_data.is_filtering_inactives());
|
|
|
|
});
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
run_test("is_subscriber_subset", () => {
|
refactor: Extract is_subscriber_subset().
Extracting the function makes it a bit easier to
test and use in a generic way.
Also, I wanted this to live in stream_data, so that
it's easier to find if we change how we model
subscriber data.
Finally, I use _.every to do the subset check
instead of `_.difference`, since _.difference
is actually N-squared:
_.difference = restArguments(function(array, rest) {
rest = flatten(rest, true, true);
return _.filter(array, function(value){
return !_.contains(rest, value);
});
});
And we don't actually want to build a list only
to check that it's zero vs. nonzero length.
We now do this, which short circuits as soon
as it finds any key that is only in sub1:
return _.every(sub1.subscribers.keys(), (key) => {
return sub2_set.has(key);
});
2020-01-14 19:35:33 +01:00
|
|
|
function make_sub(user_ids) {
|
|
|
|
const sub = {};
|
|
|
|
stream_data.set_subscribers(sub, user_ids);
|
|
|
|
return sub;
|
|
|
|
}
|
|
|
|
|
|
|
|
const sub_a = make_sub([1, 2]);
|
|
|
|
const sub_b = make_sub([2, 3]);
|
|
|
|
const sub_c = make_sub([1, 2, 3]);
|
|
|
|
|
|
|
|
// The bogus case should not come up in normal
|
|
|
|
// use.
|
|
|
|
// We simply punt on any calculation if
|
|
|
|
// a stream has no subscriber info (like
|
|
|
|
// maybe Zephyr?).
|
|
|
|
const bogus = {}; // no subscribers
|
|
|
|
|
|
|
|
const matrix = [
|
|
|
|
[sub_a, sub_a, true],
|
|
|
|
[sub_a, sub_b, false],
|
|
|
|
[sub_a, sub_c, true],
|
|
|
|
[sub_b, sub_a, false],
|
|
|
|
[sub_b, sub_b, true],
|
|
|
|
[sub_b, sub_c, true],
|
|
|
|
[sub_c, sub_a, false],
|
|
|
|
[sub_c, sub_b, false],
|
|
|
|
[sub_c, sub_c, true],
|
|
|
|
[bogus, bogus, false],
|
|
|
|
];
|
|
|
|
|
js: Automatically convert _.each to for…of.
This commit was automatically generated by the following script,
followed by lint --fix and a few small manual lint-related cleanups.
import * as babelParser from "recast/parsers/babel";
import * as recast from "recast";
import * as tsParser from "recast/parsers/typescript";
import { builders as b, namedTypes as n } from "ast-types";
import { Context } from "ast-types/lib/path-visitor";
import K from "ast-types/gen/kinds";
import { NodePath } from "ast-types/lib/node-path";
import assert from "assert";
import fs from "fs";
import path from "path";
import process from "process";
const checkExpression = (node: n.Node): node is K.ExpressionKind =>
n.Expression.check(node);
const checkStatement = (node: n.Node): node is K.StatementKind =>
n.Statement.check(node);
for (const file of process.argv.slice(2)) {
console.log("Parsing", file);
const ast = recast.parse(fs.readFileSync(file, { encoding: "utf8" }), {
parser: path.extname(file) === ".ts" ? tsParser : babelParser,
});
let changed = false;
let inLoop = false;
let replaceReturn = false;
const visitLoop = (...args: string[]) =>
function(this: Context, path: NodePath) {
for (const arg of args) {
this.visit(path.get(arg));
}
const old = { inLoop };
inLoop = true;
this.visit(path.get("body"));
inLoop = old.inLoop;
return false;
};
recast.visit(ast, {
visitDoWhileStatement: visitLoop("test"),
visitExpressionStatement(path) {
const { expression, comments } = path.node;
let valueOnly;
if (
n.CallExpression.check(expression) &&
n.MemberExpression.check(expression.callee) &&
!expression.callee.computed &&
n.Identifier.check(expression.callee.object) &&
expression.callee.object.name === "_" &&
n.Identifier.check(expression.callee.property) &&
["each", "forEach"].includes(expression.callee.property.name) &&
[2, 3].includes(expression.arguments.length) &&
checkExpression(expression.arguments[0]) &&
(n.FunctionExpression.check(expression.arguments[1]) ||
n.ArrowFunctionExpression.check(expression.arguments[1])) &&
[1, 2].includes(expression.arguments[1].params.length) &&
n.Identifier.check(expression.arguments[1].params[0]) &&
((valueOnly = expression.arguments[1].params[1] === undefined) ||
n.Identifier.check(expression.arguments[1].params[1])) &&
(expression.arguments[2] === undefined ||
n.ThisExpression.check(expression.arguments[2]))
) {
const old = { inLoop, replaceReturn };
inLoop = false;
replaceReturn = true;
this.visit(
path
.get("expression")
.get("arguments")
.get(1)
.get("body")
);
inLoop = old.inLoop;
replaceReturn = old.replaceReturn;
const [right, { body, params }] = expression.arguments;
const loop = b.forOfStatement(
b.variableDeclaration("let", [
b.variableDeclarator(
valueOnly ? params[0] : b.arrayPattern([params[1], params[0]])
),
]),
valueOnly
? right
: b.callExpression(
b.memberExpression(right, b.identifier("entries")),
[]
),
checkStatement(body) ? body : b.expressionStatement(body)
);
loop.comments = comments;
path.replace(loop);
changed = true;
}
this.traverse(path);
},
visitForStatement: visitLoop("init", "test", "update"),
visitForInStatement: visitLoop("left", "right"),
visitForOfStatement: visitLoop("left", "right"),
visitFunction(path) {
this.visit(path.get("params"));
const old = { replaceReturn };
replaceReturn = false;
this.visit(path.get("body"));
replaceReturn = old.replaceReturn;
return false;
},
visitReturnStatement(path) {
if (replaceReturn) {
assert(!inLoop); // could use labeled continue if this ever fires
const { argument, comments } = path.node;
if (argument === null) {
const s = b.continueStatement();
s.comments = comments;
path.replace(s);
} else {
const s = b.expressionStatement(argument);
s.comments = comments;
path.replace(s, b.continueStatement());
}
return false;
}
this.traverse(path);
},
visitWhileStatement: visitLoop("test"),
});
if (changed) {
console.log("Writing", file);
fs.writeFileSync(file, recast.print(ast).code, { encoding: "utf8" });
}
}
Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-02-06 06:19:47 +01:00
|
|
|
for (const row of matrix) {
|
2020-07-15 00:34:28 +02:00
|
|
|
assert.equal(stream_data.is_subscriber_subset(row[0], row[1]), row[2]);
|
js: Automatically convert _.each to for…of.
This commit was automatically generated by the following script,
followed by lint --fix and a few small manual lint-related cleanups.
import * as babelParser from "recast/parsers/babel";
import * as recast from "recast";
import * as tsParser from "recast/parsers/typescript";
import { builders as b, namedTypes as n } from "ast-types";
import { Context } from "ast-types/lib/path-visitor";
import K from "ast-types/gen/kinds";
import { NodePath } from "ast-types/lib/node-path";
import assert from "assert";
import fs from "fs";
import path from "path";
import process from "process";
const checkExpression = (node: n.Node): node is K.ExpressionKind =>
n.Expression.check(node);
const checkStatement = (node: n.Node): node is K.StatementKind =>
n.Statement.check(node);
for (const file of process.argv.slice(2)) {
console.log("Parsing", file);
const ast = recast.parse(fs.readFileSync(file, { encoding: "utf8" }), {
parser: path.extname(file) === ".ts" ? tsParser : babelParser,
});
let changed = false;
let inLoop = false;
let replaceReturn = false;
const visitLoop = (...args: string[]) =>
function(this: Context, path: NodePath) {
for (const arg of args) {
this.visit(path.get(arg));
}
const old = { inLoop };
inLoop = true;
this.visit(path.get("body"));
inLoop = old.inLoop;
return false;
};
recast.visit(ast, {
visitDoWhileStatement: visitLoop("test"),
visitExpressionStatement(path) {
const { expression, comments } = path.node;
let valueOnly;
if (
n.CallExpression.check(expression) &&
n.MemberExpression.check(expression.callee) &&
!expression.callee.computed &&
n.Identifier.check(expression.callee.object) &&
expression.callee.object.name === "_" &&
n.Identifier.check(expression.callee.property) &&
["each", "forEach"].includes(expression.callee.property.name) &&
[2, 3].includes(expression.arguments.length) &&
checkExpression(expression.arguments[0]) &&
(n.FunctionExpression.check(expression.arguments[1]) ||
n.ArrowFunctionExpression.check(expression.arguments[1])) &&
[1, 2].includes(expression.arguments[1].params.length) &&
n.Identifier.check(expression.arguments[1].params[0]) &&
((valueOnly = expression.arguments[1].params[1] === undefined) ||
n.Identifier.check(expression.arguments[1].params[1])) &&
(expression.arguments[2] === undefined ||
n.ThisExpression.check(expression.arguments[2]))
) {
const old = { inLoop, replaceReturn };
inLoop = false;
replaceReturn = true;
this.visit(
path
.get("expression")
.get("arguments")
.get(1)
.get("body")
);
inLoop = old.inLoop;
replaceReturn = old.replaceReturn;
const [right, { body, params }] = expression.arguments;
const loop = b.forOfStatement(
b.variableDeclaration("let", [
b.variableDeclarator(
valueOnly ? params[0] : b.arrayPattern([params[1], params[0]])
),
]),
valueOnly
? right
: b.callExpression(
b.memberExpression(right, b.identifier("entries")),
[]
),
checkStatement(body) ? body : b.expressionStatement(body)
);
loop.comments = comments;
path.replace(loop);
changed = true;
}
this.traverse(path);
},
visitForStatement: visitLoop("init", "test", "update"),
visitForInStatement: visitLoop("left", "right"),
visitForOfStatement: visitLoop("left", "right"),
visitFunction(path) {
this.visit(path.get("params"));
const old = { replaceReturn };
replaceReturn = false;
this.visit(path.get("body"));
replaceReturn = old.replaceReturn;
return false;
},
visitReturnStatement(path) {
if (replaceReturn) {
assert(!inLoop); // could use labeled continue if this ever fires
const { argument, comments } = path.node;
if (argument === null) {
const s = b.continueStatement();
s.comments = comments;
path.replace(s);
} else {
const s = b.expressionStatement(argument);
s.comments = comments;
path.replace(s, b.continueStatement());
}
return false;
}
this.traverse(path);
},
visitWhileStatement: visitLoop("test"),
});
if (changed) {
console.log("Writing", file);
fs.writeFileSync(file, recast.print(ast).code, { encoding: "utf8" });
}
}
Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-02-06 06:19:47 +01:00
|
|
|
}
|
refactor: Extract is_subscriber_subset().
Extracting the function makes it a bit easier to
test and use in a generic way.
Also, I wanted this to live in stream_data, so that
it's easier to find if we change how we model
subscriber data.
Finally, I use _.every to do the subset check
instead of `_.difference`, since _.difference
is actually N-squared:
_.difference = restArguments(function(array, rest) {
rest = flatten(rest, true, true);
return _.filter(array, function(value){
return !_.contains(rest, value);
});
});
And we don't actually want to build a list only
to check that it's zero vs. nonzero length.
We now do this, which short circuits as soon
as it finds any key that is only in sub1:
return _.every(sub1.subscribers.keys(), (key) => {
return sub2_set.has(key);
});
2020-01-14 19:35:33 +01:00
|
|
|
});
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
run_test("edge_cases", () => {
|
2019-11-02 00:06:25 +01:00
|
|
|
const bad_stream_ids = [555555, 99999];
|
2018-07-29 15:26:45 +02:00
|
|
|
|
|
|
|
// just make sure we don't explode
|
|
|
|
stream_data.sort_for_stream_settings(bad_stream_ids);
|
|
|
|
});
|
2019-01-10 17:57:35 +01:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
run_test("get_invite_stream_data", () => {
|
2019-01-10 17:57:35 +01:00
|
|
|
// add default stream
|
2019-11-02 00:06:25 +01:00
|
|
|
const orie = {
|
2020-07-15 01:29:15 +02:00
|
|
|
name: "Orie",
|
2019-01-10 17:57:35 +01:00
|
|
|
stream_id: 320,
|
|
|
|
invite_only: false,
|
|
|
|
subscribed: true,
|
|
|
|
};
|
|
|
|
|
|
|
|
// clear all the data form stream_data, and people
|
|
|
|
stream_data.clear_subscriptions();
|
|
|
|
people.init();
|
|
|
|
|
2020-02-09 22:02:55 +01:00
|
|
|
stream_data.add_sub(orie);
|
2019-01-10 17:57:35 +01:00
|
|
|
stream_data.set_realm_default_streams([orie]);
|
|
|
|
|
2020-07-15 00:34:28 +02:00
|
|
|
const expected_list = [
|
|
|
|
{
|
|
|
|
name: "Orie",
|
|
|
|
stream_id: 320,
|
|
|
|
invite_only: false,
|
|
|
|
default_stream: true,
|
|
|
|
},
|
|
|
|
];
|
2019-01-10 17:57:35 +01:00
|
|
|
assert.deepEqual(stream_data.get_invite_stream_data(), expected_list);
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const inviter = {
|
2020-07-15 01:29:15 +02:00
|
|
|
name: "Inviter",
|
2019-01-10 17:57:35 +01:00
|
|
|
stream_id: 25,
|
|
|
|
invite_only: true,
|
|
|
|
subscribed: true,
|
|
|
|
};
|
2020-02-09 22:02:55 +01:00
|
|
|
stream_data.add_sub(inviter);
|
2019-01-10 17:57:35 +01:00
|
|
|
|
|
|
|
expected_list.push({
|
2020-07-15 01:29:15 +02:00
|
|
|
name: "Inviter",
|
2019-01-10 17:57:35 +01:00
|
|
|
stream_id: 25,
|
|
|
|
invite_only: true,
|
|
|
|
default_stream: false,
|
|
|
|
});
|
|
|
|
assert.deepEqual(stream_data.get_invite_stream_data(), expected_list);
|
|
|
|
});
|
2019-03-13 13:47:57 +01:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
run_test("all_topics_in_cache", () => {
|
2019-03-13 13:47:57 +01:00
|
|
|
// Add a new stream with first_message_id set.
|
2019-11-02 00:06:25 +01:00
|
|
|
const general = {
|
2020-07-15 01:29:15 +02:00
|
|
|
name: "general",
|
2019-03-13 13:47:57 +01:00
|
|
|
stream_id: 21,
|
|
|
|
first_message_id: null,
|
|
|
|
};
|
2019-11-02 00:06:25 +01:00
|
|
|
const messages = [
|
2019-03-13 13:47:57 +01:00
|
|
|
{id: 1, stream_id: 21},
|
|
|
|
{id: 2, stream_id: 21},
|
|
|
|
{id: 3, stream_id: 21},
|
|
|
|
];
|
2020-06-21 15:23:43 +02:00
|
|
|
const sub = stream_data.create_sub_from_server_data(general);
|
2019-03-13 13:47:57 +01:00
|
|
|
|
|
|
|
assert.equal(stream_data.all_topics_in_cache(sub), false);
|
|
|
|
|
|
|
|
message_list.all.data.add_messages(messages);
|
|
|
|
assert.equal(stream_data.all_topics_in_cache(sub), false);
|
2020-07-02 01:41:40 +02:00
|
|
|
message_list.all.data.fetch_status.has_found_newest = () => true;
|
2019-03-13 13:47:57 +01:00
|
|
|
assert.equal(stream_data.all_topics_in_cache(sub), true);
|
|
|
|
|
|
|
|
sub.first_message_id = 0;
|
|
|
|
assert.equal(stream_data.all_topics_in_cache(sub), false);
|
|
|
|
|
|
|
|
sub.first_message_id = 2;
|
|
|
|
assert.equal(stream_data.all_topics_in_cache(sub), true);
|
|
|
|
});
|