2021-03-25 22:35:45 +01:00
|
|
|
import {page_params} from "./page_params";
|
2021-02-10 17:14:20 +01:00
|
|
|
import * as settings_config from "./settings_config";
|
2024-02-13 02:08:24 +01:00
|
|
|
import {current_user, realm} from "./state_data";
|
2023-08-03 16:06:40 +02:00
|
|
|
import * as user_groups from "./user_groups";
|
2021-07-28 16:00:58 +02:00
|
|
|
import {user_settings} from "./user_settings";
|
2020-02-25 12:46:14 +01:00
|
|
|
|
2021-07-28 18:55:53 +02:00
|
|
|
let user_join_date: Date;
|
|
|
|
export function initialize(current_user_join_date: Date): void {
|
2021-07-22 08:00:06 +02:00
|
|
|
// We keep the `user_join_date` as the present day's date if the user is a spectator
|
2021-07-19 19:26:17 +02:00
|
|
|
user_join_date = current_user_join_date;
|
|
|
|
}
|
|
|
|
|
2020-02-25 12:46:14 +01:00
|
|
|
/*
|
|
|
|
This is a close cousin of settings_config,
|
|
|
|
but this has a bit more logic, and we
|
|
|
|
ensure 100% line coverage on it.
|
|
|
|
|
|
|
|
Our main goal with this code is to isolate
|
|
|
|
some key modules from having to know
|
|
|
|
about page_params and settings_config details.
|
|
|
|
*/
|
|
|
|
|
2021-07-28 18:55:53 +02:00
|
|
|
export function user_can_change_name(): boolean {
|
2024-02-13 02:08:16 +01:00
|
|
|
if (current_user.is_admin) {
|
2021-03-21 16:46:13 +01:00
|
|
|
return true;
|
|
|
|
}
|
2024-02-13 02:08:24 +01:00
|
|
|
if (realm.realm_name_changes_disabled || realm.server_name_changes_disabled) {
|
2021-03-21 16:46:13 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-07-28 18:55:53 +02:00
|
|
|
export function user_can_change_avatar(): boolean {
|
2024-02-13 02:08:16 +01:00
|
|
|
if (current_user.is_admin) {
|
2021-03-21 16:46:13 +01:00
|
|
|
return true;
|
|
|
|
}
|
2024-02-13 02:08:24 +01:00
|
|
|
if (realm.realm_avatar_changes_disabled || realm.server_avatar_changes_disabled) {
|
2021-03-21 16:46:13 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-12-09 17:34:00 +01:00
|
|
|
export function user_can_change_email(): boolean {
|
2024-02-13 02:08:16 +01:00
|
|
|
if (current_user.is_admin) {
|
2022-12-09 17:34:00 +01:00
|
|
|
return true;
|
|
|
|
}
|
2024-02-13 02:08:24 +01:00
|
|
|
if (realm.realm_email_changes_disabled) {
|
2022-12-09 17:34:00 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-07-28 18:55:53 +02:00
|
|
|
export function user_can_change_logo(): boolean {
|
2024-02-13 02:08:24 +01:00
|
|
|
return current_user.is_admin && realm.zulip_plan_is_not_limited;
|
2021-02-10 17:14:20 +01:00
|
|
|
}
|
2021-04-28 20:54:03 +02:00
|
|
|
|
2021-07-28 18:55:53 +02:00
|
|
|
function user_has_permission(policy_value: number): boolean {
|
2024-07-26 05:42:14 +02:00
|
|
|
/* At present, nobody is not present in common_policy_values,
|
|
|
|
* but we include a check for it here, so that code using
|
|
|
|
* email_invite_to_realm_policy_values or other supersets can
|
|
|
|
* use this function. */
|
|
|
|
if (policy_value === settings_config.email_invite_to_realm_policy_values.nobody.code) {
|
2021-11-24 08:38:20 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2024-02-13 02:08:16 +01:00
|
|
|
if (current_user.is_admin) {
|
2021-04-28 20:54:03 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-07-22 08:00:06 +02:00
|
|
|
if (page_params.is_spectator) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-06-23 12:53:38 +02:00
|
|
|
/* At present, by_everyone is not present in common_policy_values,
|
|
|
|
* but we include a check for it here, so that code using
|
|
|
|
* common_message_policy_values or other supersets can use this function. */
|
|
|
|
if (policy_value === settings_config.common_message_policy_values.by_everyone.code) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2024-02-13 02:08:16 +01:00
|
|
|
if (current_user.is_guest) {
|
2021-04-28 20:54:03 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (policy_value === settings_config.common_policy_values.by_admins_only.code) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2024-02-13 02:08:16 +01:00
|
|
|
if (current_user.is_moderator) {
|
2021-04-21 21:43:27 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (policy_value === settings_config.common_policy_values.by_moderators_only.code) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-04-28 20:54:03 +02:00
|
|
|
if (policy_value === settings_config.common_policy_values.by_members.code) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-07-19 19:26:17 +02:00
|
|
|
const current_datetime = new Date();
|
|
|
|
const person_date_joined = new Date(user_join_date);
|
2021-07-28 18:55:53 +02:00
|
|
|
const user_join_days =
|
|
|
|
(current_datetime.getTime() - person_date_joined.getTime()) / 1000 / 86400;
|
2024-02-13 02:08:24 +01:00
|
|
|
return user_join_days >= realm.realm_waiting_period_threshold;
|
2021-04-28 20:54:03 +02:00
|
|
|
}
|
|
|
|
|
2023-06-26 23:38:08 +02:00
|
|
|
export function user_can_invite_users_by_email(): boolean {
|
2024-02-13 02:08:24 +01:00
|
|
|
return user_has_permission(realm.realm_invite_to_realm_policy);
|
2021-04-28 20:54:03 +02:00
|
|
|
}
|
2021-04-28 21:29:12 +02:00
|
|
|
|
2023-08-03 16:06:40 +02:00
|
|
|
export function user_can_create_multiuse_invite(): boolean {
|
2024-06-12 12:04:08 +02:00
|
|
|
if (page_params.is_spectator) {
|
2023-08-03 16:06:40 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return user_groups.is_user_in_group(
|
2024-02-13 02:08:24 +01:00
|
|
|
realm.realm_create_multiuse_invite_group,
|
2024-02-13 02:08:16 +01:00
|
|
|
current_user.user_id,
|
2023-08-03 16:06:40 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-07-28 18:55:53 +02:00
|
|
|
export function user_can_subscribe_other_users(): boolean {
|
2024-02-13 02:08:24 +01:00
|
|
|
return user_has_permission(realm.realm_invite_to_stream_policy);
|
2021-04-28 21:29:12 +02:00
|
|
|
}
|
2021-04-28 21:58:33 +02:00
|
|
|
|
2021-03-27 05:48:37 +01:00
|
|
|
export function user_can_create_private_streams(): boolean {
|
2024-06-17 16:06:49 +02:00
|
|
|
if (page_params.is_spectator) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return user_groups.is_user_in_group(
|
|
|
|
realm.realm_can_create_private_channel_group,
|
|
|
|
current_user.user_id,
|
|
|
|
);
|
2021-03-27 05:48:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
export function user_can_create_public_streams(): boolean {
|
2024-06-12 12:04:08 +02:00
|
|
|
if (page_params.is_spectator) {
|
2024-05-31 09:49:10 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return user_groups.is_user_in_group(
|
|
|
|
realm.realm_can_create_public_channel_group,
|
|
|
|
current_user.user_id,
|
|
|
|
);
|
2021-04-28 21:58:33 +02:00
|
|
|
}
|
2021-04-30 09:35:20 +02:00
|
|
|
|
2021-11-19 17:36:00 +01:00
|
|
|
export function user_can_create_web_public_streams(): boolean {
|
2024-02-13 02:08:24 +01:00
|
|
|
if (!realm.server_web_public_streams_enabled || !realm.realm_enable_spectator_access) {
|
2021-11-19 17:36:00 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2024-07-26 05:42:14 +02:00
|
|
|
if (page_params.is_spectator) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return user_groups.is_user_in_group(
|
|
|
|
realm.realm_can_create_web_public_channel_group,
|
|
|
|
current_user.user_id,
|
|
|
|
);
|
2021-11-19 17:36:00 +01:00
|
|
|
}
|
|
|
|
|
2021-07-28 18:55:53 +02:00
|
|
|
export function user_can_move_messages_between_streams(): boolean {
|
2024-02-13 02:08:24 +01:00
|
|
|
return user_has_permission(realm.realm_move_messages_between_streams_policy);
|
2021-04-30 09:35:20 +02:00
|
|
|
}
|
2021-05-26 21:20:11 +02:00
|
|
|
|
2021-07-28 18:55:53 +02:00
|
|
|
export function user_can_edit_user_groups(): boolean {
|
2024-02-13 02:08:24 +01:00
|
|
|
return user_has_permission(realm.realm_user_group_edit_policy);
|
2021-06-03 15:47:03 +02:00
|
|
|
}
|
|
|
|
|
2023-09-22 19:32:14 +02:00
|
|
|
export function can_edit_user_group(group_id: number): boolean {
|
2024-06-12 12:04:08 +02:00
|
|
|
if (page_params.is_spectator) {
|
2023-09-22 19:32:14 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!user_can_edit_user_groups()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Admins and moderators are allowed to edit user groups even if they
|
|
|
|
// are not a member of that user group. Members can edit user groups
|
|
|
|
// only if they belong to that group.
|
2024-02-13 02:08:16 +01:00
|
|
|
if (current_user.is_admin || current_user.is_moderator) {
|
2023-09-22 19:32:14 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2024-02-13 02:08:16 +01:00
|
|
|
return user_groups.is_direct_member_of(current_user.user_id, group_id);
|
2023-09-22 19:32:14 +02:00
|
|
|
}
|
|
|
|
|
2023-08-08 16:15:49 +02:00
|
|
|
export function user_can_create_user_groups(): boolean {
|
|
|
|
return user_has_permission(realm.realm_user_group_edit_policy);
|
|
|
|
}
|
|
|
|
|
2021-07-28 18:55:53 +02:00
|
|
|
export function user_can_add_custom_emoji(): boolean {
|
2024-02-13 02:08:24 +01:00
|
|
|
return user_has_permission(realm.realm_add_custom_emoji_policy);
|
2021-05-17 19:32:35 +02:00
|
|
|
}
|
|
|
|
|
2022-10-18 13:48:30 +02:00
|
|
|
export function user_can_move_messages_to_another_topic(): boolean {
|
2024-02-13 02:08:24 +01:00
|
|
|
return user_has_permission(realm.realm_edit_topic_policy);
|
2021-05-26 21:20:11 +02:00
|
|
|
}
|
2021-07-08 13:14:17 +02:00
|
|
|
|
2024-08-15 07:29:15 +02:00
|
|
|
export function user_can_delete_any_message(): boolean {
|
|
|
|
if (page_params.is_spectator) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return user_groups.is_user_in_group(
|
|
|
|
realm.realm_can_delete_any_message_group,
|
|
|
|
current_user.user_id,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-06-23 12:53:38 +02:00
|
|
|
export function user_can_delete_own_message(): boolean {
|
2024-02-13 02:08:24 +01:00
|
|
|
return user_has_permission(realm.realm_delete_own_message_policy);
|
2021-06-23 12:53:38 +02:00
|
|
|
}
|
|
|
|
|
2023-04-25 13:37:16 +02:00
|
|
|
export function should_mask_unread_count(sub_muted: boolean): boolean {
|
|
|
|
if (
|
|
|
|
user_settings.web_stream_unreads_count_display_policy ===
|
|
|
|
settings_config.web_stream_unreads_count_display_policy_values.no_streams.code
|
|
|
|
) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (
|
|
|
|
user_settings.web_stream_unreads_count_display_policy ===
|
|
|
|
settings_config.web_stream_unreads_count_display_policy_values.unmuted_streams.code
|
|
|
|
) {
|
|
|
|
return sub_muted;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-07-28 18:55:53 +02:00
|
|
|
export function using_dark_theme(): boolean {
|
2024-06-19 23:30:39 +02:00
|
|
|
if (user_settings.color_scheme === settings_config.color_scheme_values.dark.code) {
|
2021-07-08 13:14:17 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (
|
2021-07-28 16:00:58 +02:00
|
|
|
user_settings.color_scheme === settings_config.color_scheme_values.automatic.code &&
|
2021-07-08 13:14:17 +02:00
|
|
|
window.matchMedia &&
|
|
|
|
window.matchMedia("(prefers-color-scheme: dark)").matches
|
|
|
|
) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2023-02-13 19:42:58 +01:00
|
|
|
|
|
|
|
export function user_email_not_configured(): boolean {
|
|
|
|
// The following should also be true in the only circumstance
|
|
|
|
// under which we expect this condition to be possible:
|
2024-02-13 02:08:24 +01:00
|
|
|
// realm.demo_organization_scheduled_deletion_date
|
2024-02-13 02:08:16 +01:00
|
|
|
return current_user.is_owner && current_user.delivery_email === "";
|
2023-02-13 19:42:58 +01:00
|
|
|
}
|
2023-09-25 10:15:19 +02:00
|
|
|
|
2023-09-26 05:52:27 +02:00
|
|
|
export function bot_type_id_to_string(type_id: number): string | undefined {
|
2023-09-25 10:15:19 +02:00
|
|
|
const bot_type = page_params.bot_types.find((bot_type) => bot_type.type_id === type_id);
|
|
|
|
|
|
|
|
if (bot_type === undefined) {
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
|
|
|
|
return bot_type.name;
|
|
|
|
}
|
2023-10-04 03:28:40 +02:00
|
|
|
|
2023-11-28 18:24:43 +01:00
|
|
|
export function user_can_access_all_other_users(): boolean {
|
2024-06-12 12:04:08 +02:00
|
|
|
if (page_params.is_spectator) {
|
2023-11-28 18:24:43 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return user_groups.is_user_in_group(
|
2024-02-13 02:08:24 +01:00
|
|
|
realm.realm_can_access_all_users_group,
|
2024-02-13 02:08:16 +01:00
|
|
|
current_user.user_id,
|
2023-11-28 18:24:43 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-10-04 03:28:40 +02:00
|
|
|
/* istanbul ignore next */
|
|
|
|
export function get_request_data_for_stream_privacy(selected_val: string): {
|
|
|
|
is_private: boolean;
|
|
|
|
history_public_to_subscribers: boolean;
|
|
|
|
is_web_public: boolean;
|
|
|
|
} {
|
|
|
|
switch (selected_val) {
|
|
|
|
case settings_config.stream_privacy_policy_values.public.code: {
|
|
|
|
return {
|
|
|
|
is_private: false,
|
|
|
|
history_public_to_subscribers: true,
|
|
|
|
is_web_public: false,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
case settings_config.stream_privacy_policy_values.private.code: {
|
|
|
|
return {
|
|
|
|
is_private: true,
|
|
|
|
history_public_to_subscribers: false,
|
|
|
|
is_web_public: false,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
case settings_config.stream_privacy_policy_values.web_public.code: {
|
|
|
|
return {
|
|
|
|
is_private: false,
|
|
|
|
history_public_to_subscribers: true,
|
|
|
|
is_web_public: true,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
default: {
|
|
|
|
return {
|
|
|
|
is_private: true,
|
|
|
|
history_public_to_subscribers: true,
|
|
|
|
is_web_public: false,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|