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";
|
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
|
|
|
function user_can_access_delivery_email(): boolean {
|
2021-05-21 18:07:30 +02:00
|
|
|
// This function checks whether the current user should expect to
|
|
|
|
// see .delivery_email fields on user objects that it can access.
|
|
|
|
//
|
|
|
|
// If false, either everyone has access to emails (and there is no
|
|
|
|
// delivery_email field for anyone) or this user does not have
|
|
|
|
// access to emails (and this client will never receive a user
|
|
|
|
// object with a delivery_email field).
|
2020-07-15 00:34:28 +02:00
|
|
|
if (
|
|
|
|
page_params.realm_email_address_visibility ===
|
2021-05-21 18:07:30 +02:00
|
|
|
settings_config.email_address_visibility_values.admins_only.code
|
2020-07-15 00:34:28 +02:00
|
|
|
) {
|
2021-05-21 18:07:30 +02:00
|
|
|
return page_params.is_admin;
|
2020-02-25 12:46:14 +01:00
|
|
|
}
|
2021-05-23 23:26:45 +02:00
|
|
|
|
2020-07-15 00:34:28 +02:00
|
|
|
if (
|
|
|
|
page_params.realm_email_address_visibility ===
|
2021-05-21 18:07:30 +02:00
|
|
|
settings_config.email_address_visibility_values.moderators.code
|
2020-07-15 00:34:28 +02:00
|
|
|
) {
|
2021-05-21 18:07:30 +02:00
|
|
|
return page_params.is_admin || page_params.is_moderator;
|
2020-02-25 12:46:14 +01:00
|
|
|
}
|
2021-05-23 23:26:45 +02:00
|
|
|
|
|
|
|
return false;
|
2021-02-10 17:14:20 +01:00
|
|
|
}
|
2020-02-25 12:46:14 +01:00
|
|
|
|
2021-07-28 18:55:53 +02:00
|
|
|
export function show_email(): boolean {
|
2021-05-21 18:07:30 +02:00
|
|
|
if (
|
|
|
|
page_params.realm_email_address_visibility ===
|
|
|
|
settings_config.email_address_visibility_values.everyone.code
|
|
|
|
) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return user_can_access_delivery_email();
|
|
|
|
}
|
|
|
|
|
2021-07-28 18:55:53 +02:00
|
|
|
// TODO: Move this to people when converting it
|
|
|
|
// to TypeScript.
|
|
|
|
interface Person {
|
|
|
|
delivery_email: string;
|
|
|
|
email: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function email_for_user_settings(person: Person): string | undefined {
|
2021-02-10 17:14:20 +01:00
|
|
|
if (!show_email()) {
|
2020-09-24 07:50:36 +02:00
|
|
|
return undefined;
|
2020-02-25 12:46:14 +01:00
|
|
|
}
|
|
|
|
|
2021-05-21 18:07:30 +02:00
|
|
|
if (person.delivery_email && user_can_access_delivery_email()) {
|
2020-02-25 12:46:14 +01:00
|
|
|
return person.delivery_email;
|
|
|
|
}
|
|
|
|
|
|
|
|
return person.email;
|
2021-02-10 17:14:20 +01:00
|
|
|
}
|
2020-02-25 12:46:14 +01:00
|
|
|
|
2021-07-28 18:55:53 +02:00
|
|
|
export function get_time_preferences(user_timezone: string): {
|
|
|
|
timezone: string;
|
|
|
|
format: string;
|
|
|
|
} {
|
2021-07-28 16:00:58 +02:00
|
|
|
if (user_settings.twenty_four_hour_time) {
|
2020-02-25 12:46:14 +01:00
|
|
|
return {
|
|
|
|
timezone: user_timezone,
|
|
|
|
format: "H:mm",
|
|
|
|
};
|
|
|
|
}
|
|
|
|
return {
|
|
|
|
timezone: user_timezone,
|
2020-09-29 22:20:46 +02:00
|
|
|
format: "h:mm a",
|
2020-02-25 12:46:14 +01:00
|
|
|
};
|
2021-02-10 17:14:20 +01:00
|
|
|
}
|
2020-06-17 07:43:53 +02:00
|
|
|
|
2021-07-28 18:55:53 +02:00
|
|
|
export function user_can_change_name(): boolean {
|
2021-03-21 16:46:13 +01:00
|
|
|
if (page_params.is_admin) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (page_params.realm_name_changes_disabled || page_params.server_name_changes_disabled) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-07-28 18:55:53 +02:00
|
|
|
export function user_can_change_avatar(): boolean {
|
2021-03-21 16:46:13 +01:00
|
|
|
if (page_params.is_admin) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (page_params.realm_avatar_changes_disabled || page_params.server_avatar_changes_disabled) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-07-28 18:55:53 +02:00
|
|
|
export function user_can_change_logo(): boolean {
|
2020-06-17 07:43:53 +02:00
|
|
|
return page_params.is_admin && page_params.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 {
|
2021-11-24 08:38:20 +01:00
|
|
|
/* At present, nobody and by_owners_only is not present in
|
|
|
|
* common_policy_values, but we include a check for it here,
|
|
|
|
* so that code using create_web_public_stream_policy_values
|
|
|
|
* or other supersets can use this function. */
|
|
|
|
if (policy_value === settings_config.create_web_public_stream_policy_values.nobody.code) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (page_params.is_owner) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (
|
|
|
|
policy_value === settings_config.create_web_public_stream_policy_values.by_owners_only.code
|
|
|
|
) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-04-28 20:54:03 +02:00
|
|
|
if (page_params.is_admin) {
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2021-04-28 20:54:03 +02:00
|
|
|
if (page_params.is_guest) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (policy_value === settings_config.common_policy_values.by_admins_only.code) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-04-21 21:43:27 +02:00
|
|
|
if (page_params.is_moderator) {
|
|
|
|
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;
|
2021-07-19 19:26:17 +02:00
|
|
|
return user_join_days >= page_params.realm_waiting_period_threshold;
|
2021-04-28 20:54:03 +02:00
|
|
|
}
|
|
|
|
|
2021-07-28 18:55:53 +02:00
|
|
|
export function user_can_invite_others_to_realm(): boolean {
|
2021-07-18 18:18:28 +02:00
|
|
|
if (
|
|
|
|
page_params.realm_invite_to_realm_policy ===
|
|
|
|
settings_config.invite_to_realm_policy_values.nobody.code
|
|
|
|
) {
|
|
|
|
return false;
|
|
|
|
}
|
2021-04-28 20:54:03 +02:00
|
|
|
return user_has_permission(page_params.realm_invite_to_realm_policy);
|
|
|
|
}
|
2021-04-28 21:29:12 +02:00
|
|
|
|
2021-07-28 18:55:53 +02:00
|
|
|
export function user_can_subscribe_other_users(): boolean {
|
2021-04-28 21:29:12 +02:00
|
|
|
return user_has_permission(page_params.realm_invite_to_stream_policy);
|
|
|
|
}
|
2021-04-28 21:58:33 +02:00
|
|
|
|
2021-07-28 18:55:53 +02:00
|
|
|
export function user_can_unsubscribe_other_users(): boolean {
|
2021-06-27 10:00:11 +02:00
|
|
|
return page_params.is_admin;
|
|
|
|
}
|
|
|
|
|
2021-03-27 05:48:37 +01:00
|
|
|
export function user_can_create_private_streams(): boolean {
|
|
|
|
return user_has_permission(page_params.realm_create_private_stream_policy);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function user_can_create_public_streams(): boolean {
|
|
|
|
return user_has_permission(page_params.realm_create_public_stream_policy);
|
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 {
|
|
|
|
if (
|
|
|
|
!page_params.server_web_public_streams_enabled ||
|
|
|
|
!page_params.realm_enable_spectator_access
|
|
|
|
) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return user_has_permission(page_params.realm_create_web_public_stream_policy);
|
|
|
|
}
|
|
|
|
|
2021-07-28 18:55:53 +02:00
|
|
|
export function user_can_move_messages_between_streams(): boolean {
|
2021-04-30 09:35:20 +02:00
|
|
|
return user_has_permission(page_params.realm_move_messages_between_streams_policy);
|
|
|
|
}
|
2021-05-26 21:20:11 +02:00
|
|
|
|
2021-07-28 18:55:53 +02:00
|
|
|
export function user_can_edit_user_groups(): boolean {
|
2021-06-03 15:47:03 +02:00
|
|
|
return user_has_permission(page_params.realm_user_group_edit_policy);
|
|
|
|
}
|
|
|
|
|
2021-07-28 18:55:53 +02:00
|
|
|
export function user_can_add_custom_emoji(): boolean {
|
2021-05-17 19:32:35 +02:00
|
|
|
return user_has_permission(page_params.realm_add_custom_emoji_policy);
|
|
|
|
}
|
|
|
|
|
2021-07-28 18:55:53 +02:00
|
|
|
export function user_can_edit_topic_of_any_message(): boolean {
|
2021-05-26 21:20:11 +02:00
|
|
|
return user_has_permission(page_params.realm_edit_topic_policy);
|
|
|
|
}
|
2021-07-08 13:14:17 +02:00
|
|
|
|
2021-06-23 12:53:38 +02:00
|
|
|
export function user_can_delete_own_message(): boolean {
|
|
|
|
return user_has_permission(page_params.realm_delete_own_message_policy);
|
|
|
|
}
|
|
|
|
|
2021-07-28 18:55:53 +02:00
|
|
|
export function using_dark_theme(): boolean {
|
2021-07-28 16:00:58 +02:00
|
|
|
if (user_settings.color_scheme === settings_config.color_scheme_values.night.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;
|
|
|
|
}
|