mirror of
https://github.com/zulip/zulip.git
synced 2025-10-23 04:52:12 +00:00
settings_data: Add 'user_can_create_streams' helper.
This commit adds 'user_can_create_streams' helper which is used to check whether user can create streams or not and replaces all the instances of 'page_params.can_create_streams'. This change helps us to remove the complex logic of updating 'page_params.can_create_streams' for 'realm_update' event in 'server_events_dispatch.js', as we will always get the updated values from the added helper for checking whether the users can create streams or not.
This commit is contained in:
@@ -13,7 +13,6 @@ const {
|
||||
const {make_stub} = require("../zjsunit/stub");
|
||||
const {run_test} = require("../zjsunit/test");
|
||||
const $ = require("../zjsunit/zjquery");
|
||||
const {page_params} = require("../zjsunit/zpage_params");
|
||||
|
||||
// Important note on these tests:
|
||||
|
||||
@@ -74,6 +73,7 @@ const popovers = mock_esm("../../static/js/popovers", {
|
||||
});
|
||||
const reactions = mock_esm("../../static/js/reactions");
|
||||
const search = mock_esm("../../static/js/search");
|
||||
const settings_data = mock_esm("../../static/js/settings_data");
|
||||
const stream_list = mock_esm("../../static/js/stream_list");
|
||||
const subs = mock_esm("../../static/js/subs");
|
||||
|
||||
@@ -281,13 +281,13 @@ run_test("allow normal typing when processing text", (override) => {
|
||||
});
|
||||
|
||||
run_test("streams", (override) => {
|
||||
page_params.can_create_streams = true;
|
||||
settings_data.user_can_create_streams = () => true;
|
||||
override(overlays, "streams_open", () => true);
|
||||
override(overlays, "is_active", () => true);
|
||||
assert_mapping("S", subs, "keyboard_sub");
|
||||
assert_mapping("V", subs, "view_stream");
|
||||
assert_mapping("n", subs, "open_create_stream");
|
||||
page_params.can_create_streams = false;
|
||||
settings_data.user_can_create_streams = () => false;
|
||||
assert_unmapped("n");
|
||||
});
|
||||
|
||||
|
@@ -167,3 +167,33 @@ run_test("user_can_subscribe_other_users", () => {
|
||||
isaac.date_joined = new Date(Date.now() - 20 * 86400000);
|
||||
assert.equal(can_subscribe_other_users(), true);
|
||||
});
|
||||
|
||||
run_test("user_can_create_streams", () => {
|
||||
const can_create_streams = settings_data.user_can_create_streams;
|
||||
|
||||
page_params.is_admin = true;
|
||||
page_params.realm_create_stream_policy =
|
||||
settings_config.common_policy_values.by_admins_only.code;
|
||||
assert.equal(can_create_streams(), true);
|
||||
|
||||
page_params.is_admin = false;
|
||||
assert.equal(can_create_streams(), false);
|
||||
|
||||
page_params.is_guest = true;
|
||||
page_params.realm_create_stream_policy = settings_config.common_policy_values.by_members.code;
|
||||
assert.equal(can_create_streams(), false);
|
||||
|
||||
page_params.is_guest = false;
|
||||
assert.equal(can_create_streams(), true);
|
||||
|
||||
page_params.realm_create_stream_policy =
|
||||
settings_config.common_policy_values.by_full_members.code;
|
||||
page_params.user_id = 30;
|
||||
people.add_active_user(isaac);
|
||||
isaac.date_joined = new Date(Date.now());
|
||||
page_params.realm_waiting_period_threshold = 10;
|
||||
assert.equal(can_create_streams(), false);
|
||||
|
||||
isaac.date_joined = new Date(Date.now() - 20 * 86400000);
|
||||
assert.equal(can_create_streams(), true);
|
||||
});
|
||||
|
@@ -32,6 +32,7 @@ import * as popovers from "./popovers";
|
||||
import * as reactions from "./reactions";
|
||||
import * as recent_topics from "./recent_topics";
|
||||
import * as search from "./search";
|
||||
import * as settings_data from "./settings_data";
|
||||
import * as stream_list from "./stream_list";
|
||||
import * as stream_popover from "./stream_popover";
|
||||
import * as subs from "./subs";
|
||||
@@ -724,7 +725,11 @@ export function process_hotkey(e, hotkey) {
|
||||
subs.view_stream();
|
||||
return true;
|
||||
}
|
||||
if (event_name === "n_key" && overlays.streams_open() && page_params.can_create_streams) {
|
||||
if (
|
||||
event_name === "n_key" &&
|
||||
overlays.streams_open() &&
|
||||
settings_data.user_can_create_streams()
|
||||
) {
|
||||
subs.open_create_stream();
|
||||
return true;
|
||||
}
|
||||
|
@@ -221,12 +221,6 @@ export function dispatch_normal_event(event) {
|
||||
page_params["realm_" + event.property] = event.value;
|
||||
realm_settings[event.property]();
|
||||
settings_org.sync_realm_settings(event.property);
|
||||
if (event.property === "create_stream_policy") {
|
||||
// TODO: Add waiting_period_threshold logic here.
|
||||
page_params.can_create_streams =
|
||||
page_params.is_admin ||
|
||||
page_params.realm_create_stream_policy === 1;
|
||||
}
|
||||
|
||||
if (event.property === "name" && window.electron_bridge !== undefined) {
|
||||
window.electron_bridge.send_event("realm_name", event.value);
|
||||
|
@@ -114,3 +114,7 @@ export function user_can_invite_others_to_realm() {
|
||||
export function user_can_subscribe_other_users() {
|
||||
return user_has_permission(page_params.realm_invite_to_stream_policy);
|
||||
}
|
||||
|
||||
export function user_can_create_streams() {
|
||||
return user_has_permission(page_params.realm_create_stream_policy);
|
||||
}
|
||||
|
@@ -24,6 +24,7 @@ import {page_params} from "./page_params";
|
||||
import * as people from "./people";
|
||||
import * as scroll_util from "./scroll_util";
|
||||
import * as search_util from "./search_util";
|
||||
import * as settings_data from "./settings_data";
|
||||
import * as stream_create from "./stream_create";
|
||||
import * as stream_data from "./stream_data";
|
||||
import * as stream_edit from "./stream_edit";
|
||||
@@ -626,7 +627,7 @@ export function setup_page(callback) {
|
||||
$("#subscriptions_table").empty();
|
||||
|
||||
const template_data = {
|
||||
can_create_streams: page_params.can_create_streams,
|
||||
can_create_streams: settings_data.user_can_create_streams(),
|
||||
hide_all_streams: !should_list_all_streams(),
|
||||
max_name_length: page_params.max_stream_name_length,
|
||||
max_description_length: page_params.max_stream_description_length,
|
||||
|
Reference in New Issue
Block a user