2021-02-10 17:00:58 +01:00
|
|
|
import ClipboardJS from "clipboard";
|
2022-02-10 11:52:34 +01:00
|
|
|
import {add} from "date-fns";
|
2021-03-11 05:43:45 +01:00
|
|
|
import $ from "jquery";
|
2023-09-22 10:32:06 +02:00
|
|
|
import assert from "minimalistic-assert";
|
2024-05-05 09:10:44 +02:00
|
|
|
import {z} from "zod";
|
2020-08-01 03:43:15 +02:00
|
|
|
|
2021-02-10 17:00:58 +01:00
|
|
|
import copy_invite_link from "../templates/copy_invite_link.hbs";
|
|
|
|
import render_invitation_failed_error from "../templates/invitation_failed_error.hbs";
|
2022-10-11 18:02:39 +02:00
|
|
|
import render_invite_user_modal from "../templates/invite_user_modal.hbs";
|
2023-02-02 17:29:51 +01:00
|
|
|
import render_invite_tips_banner from "../templates/modal_banner/invite_tips_banner.hbs";
|
2021-02-10 17:00:58 +01:00
|
|
|
import render_settings_dev_env_email_access from "../templates/settings/dev_env_email_access.hbs";
|
2019-07-09 21:24:00 +02:00
|
|
|
|
2021-02-28 00:39:51 +01:00
|
|
|
import * as channel from "./channel";
|
2021-02-28 00:35:35 +01:00
|
|
|
import * as common from "./common";
|
2024-04-12 13:40:37 +02:00
|
|
|
import * as components from "./components";
|
2023-02-02 17:29:51 +01:00
|
|
|
import * as compose_banner from "./compose_banner";
|
2023-10-09 16:00:30 +02:00
|
|
|
import {show_copied_confirmation} from "./copied_tooltip";
|
2023-04-08 13:16:25 +02:00
|
|
|
import {csrf_token} from "./csrf";
|
2022-10-11 18:02:39 +02:00
|
|
|
import * as dialog_widget from "./dialog_widget";
|
2024-03-23 19:36:45 +01:00
|
|
|
import * as email_pill from "./email_pill";
|
2021-04-13 06:51:54 +02:00
|
|
|
import {$t, $t_html} from "./i18n";
|
2024-03-23 19:36:45 +01:00
|
|
|
import * as input_pill from "./input_pill";
|
2024-01-24 14:03:43 +01:00
|
|
|
import * as invite_stream_picker_pill from "./invite_stream_picker_pill";
|
2021-03-25 22:35:45 +01:00
|
|
|
import {page_params} from "./page_params";
|
2021-06-14 11:49:01 +02:00
|
|
|
import * as settings_config from "./settings_config";
|
2023-03-17 20:03:47 +01:00
|
|
|
import * as settings_data from "./settings_data";
|
2024-02-13 02:08:24 +01:00
|
|
|
import {current_user, realm} from "./state_data";
|
2021-02-28 00:53:59 +01:00
|
|
|
import * as stream_data from "./stream_data";
|
2024-01-24 14:03:43 +01:00
|
|
|
import * as stream_pill from "./stream_pill";
|
2023-04-24 12:11:47 +02:00
|
|
|
import * as timerender from "./timerender";
|
2024-05-03 23:55:50 +02:00
|
|
|
import type {HTMLSelectOneElement} from "./types";
|
2021-02-28 00:58:55 +01:00
|
|
|
import * as ui_report from "./ui_report";
|
2024-08-20 07:18:02 +02:00
|
|
|
import * as util from "./util";
|
2021-02-28 00:35:35 +01:00
|
|
|
|
2022-02-11 17:54:15 +01:00
|
|
|
let custom_expiration_time_input = 10;
|
|
|
|
let custom_expiration_time_unit = "days";
|
2024-03-23 19:36:45 +01:00
|
|
|
let pills: email_pill.EmailPillWidget;
|
2024-01-24 14:03:43 +01:00
|
|
|
let stream_pill_widget: stream_pill.StreamPillWidget;
|
2022-02-11 17:54:15 +01:00
|
|
|
|
2023-09-22 10:32:06 +02:00
|
|
|
function reset_error_messages(): void {
|
2022-10-11 18:02:39 +02:00
|
|
|
$("#dialog_error").hide().text("").removeClass(common.status_classes);
|
2019-02-06 20:32:06 +01:00
|
|
|
|
2018-08-23 19:24:37 +02:00
|
|
|
if (page_params.development_environment) {
|
2020-07-15 01:29:15 +02:00
|
|
|
$("#dev_env_msg").hide().text("").removeClass(common.status_classes);
|
2018-08-23 19:24:37 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-22 10:32:06 +02:00
|
|
|
function get_common_invitation_data(): {
|
|
|
|
csrfmiddlewaretoken: string;
|
|
|
|
invite_as: number;
|
2024-04-18 19:38:47 +02:00
|
|
|
notify_referrer_on_join: boolean;
|
2023-09-22 10:32:06 +02:00
|
|
|
stream_ids: string;
|
|
|
|
invite_expires_in_minutes: string;
|
2024-03-23 19:36:45 +01:00
|
|
|
invitee_emails: string;
|
2023-05-30 13:27:00 +02:00
|
|
|
include_realm_default_subscriptions: string;
|
2023-09-22 10:32:06 +02:00
|
|
|
} {
|
|
|
|
const invite_as = Number.parseInt(
|
2024-05-03 23:55:50 +02:00
|
|
|
$<HTMLSelectOneElement>("select:not([multiple])#invite_as").val()!,
|
2023-09-22 10:32:06 +02:00
|
|
|
10,
|
|
|
|
);
|
2024-04-18 19:38:47 +02:00
|
|
|
const notify_referrer_on_join = $("#receive-invite-acceptance-notification").is(":checked");
|
2024-05-03 23:55:50 +02:00
|
|
|
const raw_expires_in = $<HTMLSelectOneElement>("select:not([multiple])#expires_in").val()!;
|
2021-12-01 12:10:58 +01:00
|
|
|
// See settings_config.expires_in_values for why we do this conversion.
|
2023-09-22 10:32:06 +02:00
|
|
|
let expires_in: number | null;
|
|
|
|
if (raw_expires_in === "null") {
|
|
|
|
expires_in = null;
|
|
|
|
} else if (raw_expires_in === "custom") {
|
|
|
|
expires_in = get_expiration_time_in_minutes();
|
2021-12-01 12:10:58 +01:00
|
|
|
} else {
|
2023-09-22 10:32:06 +02:00
|
|
|
expires_in = Number.parseFloat(raw_expires_in);
|
2021-12-01 12:10:58 +01:00
|
|
|
}
|
|
|
|
|
2024-01-24 14:03:43 +01:00
|
|
|
let stream_ids: number[] = [];
|
2023-05-30 13:27:00 +02:00
|
|
|
let include_realm_default_subscriptions = false;
|
|
|
|
if (
|
|
|
|
$("#invite_select_default_streams").prop("checked") ||
|
|
|
|
!settings_data.user_can_subscribe_other_users()
|
|
|
|
) {
|
|
|
|
include_realm_default_subscriptions = true;
|
2023-05-09 11:30:41 +02:00
|
|
|
} else {
|
2024-01-24 14:03:43 +01:00
|
|
|
stream_ids = stream_pill.get_stream_ids(stream_pill_widget);
|
2023-05-09 11:30:41 +02:00
|
|
|
}
|
|
|
|
|
2023-09-22 10:32:06 +02:00
|
|
|
assert(csrf_token !== undefined);
|
2019-11-02 00:06:25 +01:00
|
|
|
const data = {
|
2023-04-08 13:16:25 +02:00
|
|
|
csrfmiddlewaretoken: csrf_token,
|
2020-07-20 22:18:43 +02:00
|
|
|
invite_as,
|
2024-04-18 19:38:47 +02:00
|
|
|
notify_referrer_on_join,
|
2018-12-22 05:41:54 +01:00
|
|
|
stream_ids: JSON.stringify(stream_ids),
|
2023-09-22 10:32:06 +02:00
|
|
|
invite_expires_in_minutes: JSON.stringify(expires_in),
|
2024-03-23 19:36:45 +01:00
|
|
|
invitee_emails: pills
|
|
|
|
.items()
|
|
|
|
.map((pill) => email_pill.get_email_from_item(pill))
|
|
|
|
.join(","),
|
2023-05-30 13:27:00 +02:00
|
|
|
include_realm_default_subscriptions: JSON.stringify(include_realm_default_subscriptions),
|
2019-02-01 12:20:13 +01:00
|
|
|
};
|
2024-03-23 19:36:45 +01:00
|
|
|
const current_email = email_pill.get_current_email(pills);
|
|
|
|
if (current_email) {
|
|
|
|
if (pills.items().length === 0) {
|
|
|
|
data.invitee_emails = current_email;
|
|
|
|
} else {
|
|
|
|
data.invitee_emails += "," + current_email;
|
|
|
|
}
|
|
|
|
}
|
2019-02-01 12:20:13 +01:00
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
2023-09-22 10:32:06 +02:00
|
|
|
function beforeSend(): void {
|
2019-02-02 08:12:38 +01:00
|
|
|
reset_error_messages();
|
2024-05-24 05:35:03 +02:00
|
|
|
// TODO: You could alternatively parse the emails here, and return errors to
|
2019-02-02 08:12:38 +01:00
|
|
|
// the user if they don't match certain constraints (i.e. not real email addresses,
|
|
|
|
// aren't in the right domain, etc.)
|
|
|
|
//
|
|
|
|
// OR, you could just let the server do it. Probably my temptation.
|
2024-05-03 02:58:10 +02:00
|
|
|
const loading_text = $("#invite-user-modal .dialog_submit_button").attr("data-loading-text");
|
|
|
|
assert(loading_text !== undefined);
|
2022-10-11 18:02:39 +02:00
|
|
|
$("#invite-user-modal .dialog_submit_button").text(loading_text);
|
|
|
|
$("#invite-user-modal .dialog_submit_button").prop("disabled", true);
|
2019-02-02 08:12:38 +01:00
|
|
|
}
|
|
|
|
|
2023-09-22 10:32:06 +02:00
|
|
|
function submit_invitation_form(): void {
|
2024-05-03 23:55:50 +02:00
|
|
|
const $expires_in = $<HTMLSelectOneElement>("select:not([multiple])#expires_in");
|
2022-10-11 18:02:39 +02:00
|
|
|
const $invite_status = $("#dialog_error");
|
2019-11-02 00:06:25 +01:00
|
|
|
const data = get_common_invitation_data();
|
2018-08-23 19:24:37 +02:00
|
|
|
|
2023-09-22 10:32:06 +02:00
|
|
|
void channel.post({
|
2018-08-23 19:24:37 +02:00
|
|
|
url: "/json/invites",
|
2020-07-20 22:18:43 +02:00
|
|
|
data,
|
|
|
|
beforeSend,
|
|
|
|
success() {
|
2024-03-23 19:36:45 +01:00
|
|
|
const number_of_invites_sent = pills.items().length;
|
2021-04-13 05:18:25 +02:00
|
|
|
ui_report.success(
|
2023-11-13 08:23:44 +01:00
|
|
|
$t_html(
|
|
|
|
{
|
|
|
|
defaultMessage:
|
|
|
|
"{N, plural, one {User invited successfully.} other {Users invited successfully.}}",
|
|
|
|
},
|
|
|
|
{N: number_of_invites_sent},
|
|
|
|
),
|
2022-01-25 11:36:19 +01:00
|
|
|
$invite_status,
|
2021-04-13 05:18:25 +02:00
|
|
|
);
|
2024-03-23 19:36:45 +01:00
|
|
|
pills.clear();
|
2018-08-23 19:24:37 +02:00
|
|
|
|
|
|
|
if (page_params.development_environment) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const rendered_email_msg = render_settings_dev_env_email_access();
|
2020-07-15 01:29:15 +02:00
|
|
|
$("#dev_env_msg").html(rendered_email_msg).addClass("alert-info").show();
|
2018-08-23 19:24:37 +02:00
|
|
|
}
|
2022-02-11 17:54:15 +01:00
|
|
|
|
2023-09-22 10:32:06 +02:00
|
|
|
if ($expires_in.val() === "custom") {
|
2022-02-11 17:54:15 +01:00
|
|
|
// Hide the custom inputs if the custom input is set
|
|
|
|
// to one of the dropdown's standard options.
|
|
|
|
const time_in_minutes = get_expiration_time_in_minutes();
|
|
|
|
for (const option of Object.values(settings_config.expires_in_values)) {
|
|
|
|
if (option.value === time_in_minutes) {
|
|
|
|
$("#custom-invite-expiration-time").hide();
|
2023-09-22 10:32:06 +02:00
|
|
|
$expires_in.val(time_in_minutes);
|
2022-02-11 17:54:15 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-08-23 19:24:37 +02:00
|
|
|
},
|
2020-07-20 22:18:43 +02:00
|
|
|
error(xhr) {
|
2024-05-05 09:10:44 +02:00
|
|
|
const parsed = z
|
|
|
|
.object({
|
|
|
|
result: z.literal("error"),
|
|
|
|
code: z.literal("INVITATION_FAILED"),
|
|
|
|
msg: z.string(),
|
|
|
|
errors: z.array(z.tuple([z.string(), z.string(), z.boolean()])),
|
|
|
|
sent_invitations: z.boolean(),
|
|
|
|
license_limit_reached: z.boolean(),
|
|
|
|
daily_limit_reached: z.boolean(),
|
|
|
|
})
|
|
|
|
.safeParse(xhr.responseJSON);
|
|
|
|
if (!parsed.success) {
|
2018-08-23 19:24:37 +02:00
|
|
|
// There was a fatal error, no partial processing occurred.
|
2022-01-25 11:36:19 +01:00
|
|
|
ui_report.error("", xhr, $invite_status);
|
2018-08-23 19:24:37 +02:00
|
|
|
} else {
|
|
|
|
// Some users were not invited.
|
2019-11-02 00:06:25 +01:00
|
|
|
const invitee_emails_errored = [];
|
|
|
|
const error_list = [];
|
2019-11-14 11:21:08 +01:00
|
|
|
let is_invitee_deactivated = false;
|
2024-05-05 09:10:44 +02:00
|
|
|
for (const [email, error_message, deactivated] of parsed.data.errors) {
|
2019-11-14 11:21:08 +01:00
|
|
|
error_list.push(`${email}: ${error_message}`);
|
|
|
|
if (deactivated) {
|
|
|
|
is_invitee_deactivated = true;
|
|
|
|
}
|
2020-08-05 07:43:50 +02:00
|
|
|
invitee_emails_errored.push(email);
|
2021-01-22 22:29:08 +01:00
|
|
|
}
|
2018-08-23 19:24:37 +02:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const error_response = render_invitation_failed_error({
|
2024-05-05 09:10:44 +02:00
|
|
|
error_message: parsed.data.msg,
|
2020-07-20 22:18:43 +02:00
|
|
|
error_list,
|
2024-02-13 02:08:16 +01:00
|
|
|
is_admin: current_user.is_admin,
|
2020-07-20 22:18:43 +02:00
|
|
|
is_invitee_deactivated,
|
2024-05-05 09:10:44 +02:00
|
|
|
license_limit_reached: parsed.data.license_limit_reached,
|
2024-02-13 02:08:16 +01:00
|
|
|
has_billing_access: current_user.is_owner || current_user.is_billing_admin,
|
2024-05-05 09:10:44 +02:00
|
|
|
daily_limit_reached: parsed.data.daily_limit_reached,
|
2019-01-17 16:55:25 +01:00
|
|
|
});
|
2024-03-26 18:15:37 +01:00
|
|
|
ui_report.message(error_response, $invite_status, "alert-error");
|
2018-08-23 19:24:37 +02:00
|
|
|
|
2024-05-05 09:10:44 +02:00
|
|
|
if (parsed.data.sent_invitations) {
|
2024-03-23 19:36:45 +01:00
|
|
|
for (const email of invitee_emails_errored) {
|
|
|
|
pills.appendValue(email);
|
|
|
|
}
|
2018-08-23 19:24:37 +02:00
|
|
|
}
|
|
|
|
}
|
2019-02-02 08:12:38 +01:00
|
|
|
},
|
2020-07-20 22:18:43 +02:00
|
|
|
complete() {
|
2022-10-11 18:02:39 +02:00
|
|
|
$("#invite-user-modal .dialog_submit_button").text($t({defaultMessage: "Invite"}));
|
|
|
|
$("#invite-user-modal .dialog_submit_button").prop("disabled", false);
|
2023-07-25 17:58:43 +02:00
|
|
|
$("#invite-user-modal .dialog_exit_button").prop("disabled", false);
|
2024-08-20 07:18:02 +02:00
|
|
|
util.the($invite_status).scrollIntoView();
|
2019-02-02 08:12:38 +01:00
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
2018-08-23 19:24:37 +02:00
|
|
|
|
2023-09-22 10:32:06 +02:00
|
|
|
function generate_multiuse_invite(): void {
|
2022-10-11 18:02:39 +02:00
|
|
|
const $invite_status = $("#dialog_error");
|
2019-11-02 00:06:25 +01:00
|
|
|
const data = get_common_invitation_data();
|
2023-09-22 10:32:06 +02:00
|
|
|
void channel.post({
|
2019-02-02 08:12:38 +01:00
|
|
|
url: "/json/invites/multiuse",
|
2020-07-20 22:18:43 +02:00
|
|
|
data,
|
|
|
|
beforeSend,
|
|
|
|
success(data) {
|
2020-10-13 18:17:20 +02:00
|
|
|
const copy_link_html = copy_invite_link(data);
|
2022-01-25 11:36:19 +01:00
|
|
|
ui_report.success(copy_link_html, $invite_status);
|
2023-10-09 16:00:30 +02:00
|
|
|
const clipboard = new ClipboardJS("#copy_generated_invite_link");
|
|
|
|
|
|
|
|
clipboard.on("success", () => {
|
|
|
|
const tippy_timeout_in_ms = 800;
|
2024-08-16 11:59:28 +02:00
|
|
|
show_copied_confirmation(util.the($("#copy_generated_invite_link")), {
|
|
|
|
timeout_in_ms: tippy_timeout_in_ms,
|
|
|
|
});
|
2023-10-09 16:00:30 +02:00
|
|
|
});
|
2019-02-02 08:12:38 +01:00
|
|
|
},
|
2020-07-20 22:18:43 +02:00
|
|
|
error(xhr) {
|
2022-01-25 11:36:19 +01:00
|
|
|
ui_report.error("", xhr, $invite_status);
|
2019-02-02 08:12:38 +01:00
|
|
|
},
|
2020-07-20 22:18:43 +02:00
|
|
|
complete() {
|
2024-07-11 23:33:27 +02:00
|
|
|
$("#invite-user-modal .dialog_submit_button").text($t({defaultMessage: "Create link"}));
|
2022-10-11 18:02:39 +02:00
|
|
|
$("#invite-user-modal .dialog_submit_button").prop("disabled", false);
|
2023-07-25 17:58:43 +02:00
|
|
|
$("#invite-user-modal .dialog_exit_button").prop("disabled", false);
|
2024-08-20 07:18:02 +02:00
|
|
|
util.the($invite_status).scrollIntoView();
|
2018-08-23 19:24:37 +02:00
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-09-22 10:32:06 +02:00
|
|
|
function valid_to(time_valid: number): string {
|
2021-09-15 11:49:36 +02:00
|
|
|
if (!time_valid) {
|
|
|
|
return $t({defaultMessage: "Never expires"});
|
|
|
|
}
|
2023-05-01 07:02:10 +02:00
|
|
|
|
|
|
|
// The below is a duplicate of timerender.get_full_datetime, with a different base string.
|
2022-02-10 11:52:34 +01:00
|
|
|
const valid_to = add(new Date(), {minutes: time_valid});
|
2023-04-24 12:11:47 +02:00
|
|
|
const date = timerender.get_localized_date_or_time_for_format(valid_to, "dayofyear_year");
|
|
|
|
const time = timerender.get_localized_date_or_time_for_format(valid_to, "time");
|
|
|
|
|
|
|
|
return $t({defaultMessage: "Expires on {date} at {time}"}, {date, time});
|
2022-02-11 17:54:15 +01:00
|
|
|
}
|
|
|
|
|
2023-09-22 10:32:06 +02:00
|
|
|
function get_expiration_time_in_minutes(): number {
|
2022-02-11 17:54:15 +01:00
|
|
|
switch (custom_expiration_time_unit) {
|
|
|
|
case "hours":
|
|
|
|
return custom_expiration_time_input * 60;
|
|
|
|
case "days":
|
|
|
|
return custom_expiration_time_input * 24 * 60;
|
|
|
|
case "weeks":
|
|
|
|
return custom_expiration_time_input * 7 * 24 * 60;
|
|
|
|
default:
|
|
|
|
return custom_expiration_time_input;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-22 10:32:06 +02:00
|
|
|
function set_expires_on_text(): void {
|
2024-05-03 23:55:50 +02:00
|
|
|
const $expires_in = $<HTMLSelectOneElement>("select:not([multiple])#expires_in");
|
2023-09-22 10:32:06 +02:00
|
|
|
if ($expires_in.val() === "custom") {
|
2022-02-11 17:54:15 +01:00
|
|
|
$("#expires_on").hide();
|
|
|
|
$("#custom_expires_on").text(valid_to(get_expiration_time_in_minutes()));
|
|
|
|
} else {
|
|
|
|
$("#expires_on").show();
|
2023-09-22 10:32:06 +02:00
|
|
|
$("#expires_on").text(valid_to(Number.parseFloat($expires_in.val()!)));
|
2022-02-11 17:54:15 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-22 10:32:06 +02:00
|
|
|
function set_custom_time_inputs_visibility(): void {
|
2024-05-03 23:55:50 +02:00
|
|
|
const $expires_in = $<HTMLSelectOneElement>("select:not([multiple])#expires_in");
|
2023-09-22 10:32:06 +02:00
|
|
|
if ($expires_in.val() === "custom") {
|
2022-02-11 17:54:15 +01:00
|
|
|
$("#custom-expiration-time-input").val(custom_expiration_time_input);
|
2024-05-03 23:55:50 +02:00
|
|
|
$<HTMLSelectOneElement>("select:not([multiple])#custom-expiration-time-unit").val(
|
|
|
|
custom_expiration_time_unit,
|
|
|
|
);
|
2022-02-11 17:54:15 +01:00
|
|
|
$("#custom-invite-expiration-time").show();
|
|
|
|
} else {
|
|
|
|
$("#custom-invite-expiration-time").hide();
|
|
|
|
}
|
2021-09-15 11:49:36 +02:00
|
|
|
}
|
|
|
|
|
2023-09-22 10:32:06 +02:00
|
|
|
function set_streams_to_join_list_visibility(): void {
|
2024-05-23 17:07:54 +02:00
|
|
|
const realm_has_default_streams = stream_data.get_default_stream_ids().length !== 0;
|
|
|
|
const hide_streams_list =
|
|
|
|
realm_has_default_streams &&
|
2024-08-20 07:18:02 +02:00
|
|
|
util.the($<HTMLInputElement>("input#invite_select_default_streams")).checked;
|
2024-05-23 17:07:54 +02:00
|
|
|
if (hide_streams_list) {
|
2024-01-24 14:03:43 +01:00
|
|
|
$(".add_streams_container").hide();
|
2023-05-09 11:30:41 +02:00
|
|
|
} else {
|
2024-01-24 14:03:43 +01:00
|
|
|
$(".add_streams_container").show();
|
2023-05-09 11:30:41 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-02 17:29:51 +01:00
|
|
|
function generate_invite_tips_data(): Record<string, boolean> {
|
2024-02-13 02:08:24 +01:00
|
|
|
const {realm_description, realm_icon_source, custom_profile_fields} = realm;
|
2023-02-02 17:29:51 +01:00
|
|
|
|
|
|
|
return {
|
|
|
|
realm_has_description:
|
|
|
|
realm_description !== "" &&
|
|
|
|
!/^Organization imported from [A-Za-z]+[!.]$/.test(realm_description),
|
|
|
|
realm_has_user_set_icon: realm_icon_source !== "G",
|
|
|
|
realm_has_custom_profile_fields: custom_profile_fields.length > 0,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2023-09-22 10:32:06 +02:00
|
|
|
function open_invite_user_modal(e: JQuery.ClickEvent<Document, undefined>): void {
|
2022-10-11 18:02:39 +02:00
|
|
|
e.stopPropagation();
|
|
|
|
e.preventDefault();
|
|
|
|
|
2022-02-11 17:54:15 +01:00
|
|
|
const time_unit_choices = ["minutes", "hours", "days", "weeks"];
|
2022-10-11 18:02:39 +02:00
|
|
|
const html_body = render_invite_user_modal({
|
2024-02-13 02:08:16 +01:00
|
|
|
is_admin: current_user.is_admin,
|
|
|
|
is_owner: current_user.is_owner,
|
2021-06-14 11:49:01 +02:00
|
|
|
development_environment: page_params.development_environment,
|
|
|
|
invite_as_options: settings_config.user_role_values,
|
2021-09-15 11:49:36 +02:00
|
|
|
expires_in_options: settings_config.expires_in_values,
|
2022-02-11 17:54:15 +01:00
|
|
|
time_choices: time_unit_choices,
|
2023-05-09 11:30:41 +02:00
|
|
|
show_select_default_streams_option: stream_data.get_default_stream_ids().length !== 0,
|
2023-03-17 20:03:47 +01:00
|
|
|
user_has_email_set: !settings_data.user_email_not_configured(),
|
2023-04-23 17:55:22 +02:00
|
|
|
can_subscribe_other_users: settings_data.user_can_subscribe_other_users(),
|
2021-06-14 11:49:01 +02:00
|
|
|
});
|
|
|
|
|
2023-09-22 10:32:06 +02:00
|
|
|
function invite_user_modal_post_render(): void {
|
2024-05-03 23:55:50 +02:00
|
|
|
const $expires_in = $<HTMLSelectOneElement>("select:not([multiple])#expires_in");
|
2024-03-23 19:36:45 +01:00
|
|
|
const $pill_container = $("#invitee_emails_container .pill-container");
|
|
|
|
pills = input_pill.create({
|
|
|
|
$container: $pill_container,
|
|
|
|
create_item_from_text: email_pill.create_item_from_email,
|
|
|
|
get_text_from_item: email_pill.get_email_from_item,
|
2024-07-30 06:13:31 +02:00
|
|
|
get_display_value_from_item: email_pill.get_email_from_item,
|
2024-03-23 19:36:45 +01:00
|
|
|
});
|
2023-09-22 10:32:06 +02:00
|
|
|
|
2022-10-11 18:02:39 +02:00
|
|
|
$("#invite-user-modal .dialog_submit_button").prop("disabled", true);
|
2021-06-14 11:49:01 +02:00
|
|
|
|
2023-03-17 20:03:47 +01:00
|
|
|
const user_has_email_set = !settings_data.user_email_not_configured();
|
|
|
|
|
2022-10-11 18:02:39 +02:00
|
|
|
set_custom_time_inputs_visibility();
|
|
|
|
set_expires_on_text();
|
2024-05-15 05:06:09 +02:00
|
|
|
|
|
|
|
if (settings_data.user_can_subscribe_other_users()) {
|
|
|
|
set_streams_to_join_list_visibility();
|
2024-01-24 14:03:43 +01:00
|
|
|
const $stream_pill_container = $("#invite_streams_container .pill-container");
|
|
|
|
stream_pill_widget = invite_stream_picker_pill.create($stream_pill_container);
|
2024-05-15 05:06:09 +02:00
|
|
|
}
|
2018-12-22 05:41:54 +01:00
|
|
|
|
2023-02-02 17:29:51 +01:00
|
|
|
$("#invite-user-modal").on("click", ".setup-tips-container .banner_content a", () => {
|
|
|
|
dialog_widget.close();
|
|
|
|
});
|
|
|
|
|
|
|
|
$("#invite-user-modal").on("click", ".main-view-banner-close-button", (e) => {
|
|
|
|
e.preventDefault();
|
|
|
|
$(e.target).parent().remove();
|
|
|
|
});
|
|
|
|
|
2024-05-24 05:22:29 +02:00
|
|
|
function toggle_invite_submit_button(selected_tab?: string): void {
|
|
|
|
if (selected_tab === undefined) {
|
|
|
|
selected_tab = $(".invite_users_option_tabs")
|
|
|
|
.find(".selected")
|
|
|
|
.attr("data-tab-key");
|
|
|
|
}
|
2024-04-12 13:40:37 +02:00
|
|
|
const $button = $("#invite-user-modal .dialog_submit_button");
|
|
|
|
$button.prop(
|
2022-10-11 18:02:39 +02:00
|
|
|
"disabled",
|
2024-05-24 05:22:29 +02:00
|
|
|
selected_tab === "invite-email-tab" &&
|
2024-04-12 13:40:37 +02:00
|
|
|
pills.items().length === 0 &&
|
|
|
|
email_pill.get_current_email(pills) === null,
|
2022-10-11 18:02:39 +02:00
|
|
|
);
|
2024-05-24 05:22:29 +02:00
|
|
|
if (selected_tab === "invite-email-tab") {
|
2024-04-12 13:40:37 +02:00
|
|
|
$button.text($t({defaultMessage: "Invite"}));
|
2024-07-11 23:33:27 +02:00
|
|
|
$button.attr("data-loading-text", $t({defaultMessage: "Inviting…"}));
|
2024-04-12 13:40:37 +02:00
|
|
|
} else {
|
2024-07-11 23:33:27 +02:00
|
|
|
$button.text($t({defaultMessage: "Create link"}));
|
|
|
|
$button.attr("data-loading-text", $t({defaultMessage: "Creating link…"}));
|
2024-04-12 13:40:37 +02:00
|
|
|
}
|
2022-10-11 18:02:39 +02:00
|
|
|
}
|
|
|
|
|
2024-03-23 19:36:45 +01:00
|
|
|
pills.onPillCreate(toggle_invite_submit_button);
|
2024-05-24 05:22:29 +02:00
|
|
|
pills.onPillRemove(() => {
|
|
|
|
toggle_invite_submit_button();
|
|
|
|
});
|
2024-03-23 19:36:45 +01:00
|
|
|
pills.onTextInputHook(toggle_invite_submit_button);
|
|
|
|
|
2023-09-22 10:32:06 +02:00
|
|
|
$expires_in.on("change", () => {
|
2022-10-11 18:02:39 +02:00
|
|
|
set_custom_time_inputs_visibility();
|
|
|
|
set_expires_on_text();
|
|
|
|
});
|
|
|
|
|
2023-09-22 10:32:06 +02:00
|
|
|
$("#expires_on").text(valid_to(Number.parseFloat($expires_in.val()!)));
|
2022-10-11 18:02:39 +02:00
|
|
|
|
|
|
|
$("#custom-expiration-time-input").on("keydown", (e) => {
|
|
|
|
if (e.key === "Enter") {
|
|
|
|
e.preventDefault();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
$(".custom-expiration-time").on("change", () => {
|
2023-09-22 10:32:06 +02:00
|
|
|
custom_expiration_time_input = Number.parseFloat(
|
2024-03-19 19:45:03 +01:00
|
|
|
$<HTMLInputElement>("input#custom-expiration-time-input").val()!,
|
2023-09-22 10:32:06 +02:00
|
|
|
);
|
2024-05-03 23:55:50 +02:00
|
|
|
custom_expiration_time_unit = $<HTMLSelectOneElement>(
|
2023-09-22 10:32:06 +02:00
|
|
|
"select:not([multiple])#custom-expiration-time-unit",
|
|
|
|
).val()!;
|
2022-10-11 18:02:39 +02:00
|
|
|
$("#custom_expires_on").text(valid_to(get_expiration_time_in_minutes()));
|
|
|
|
});
|
|
|
|
|
|
|
|
$("#invite_check_all_button").on("click", () => {
|
2023-05-09 11:30:41 +02:00
|
|
|
$("#invite-stream-checkboxes input[type=checkbox]").prop("checked", true);
|
2022-10-11 18:02:39 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
$("#invite_uncheck_all_button").on("click", () => {
|
2023-05-09 11:30:41 +02:00
|
|
|
$("#invite-stream-checkboxes input[type=checkbox]").prop("checked", false);
|
|
|
|
});
|
|
|
|
|
|
|
|
$("#invite_select_default_streams").on("change", () => {
|
|
|
|
set_streams_to_join_list_visibility();
|
2022-10-11 18:02:39 +02:00
|
|
|
});
|
2023-03-17 20:03:47 +01:00
|
|
|
|
|
|
|
if (!user_has_email_set) {
|
2024-09-04 22:04:51 +02:00
|
|
|
$(util.the($<HTMLFormElement>("form#invite-user-form")).elements).prop(
|
|
|
|
"disabled",
|
|
|
|
true,
|
|
|
|
);
|
2023-03-17 20:03:47 +01:00
|
|
|
}
|
2023-08-03 16:06:40 +02:00
|
|
|
|
2023-02-02 17:29:51 +01:00
|
|
|
const invite_tips_data = generate_invite_tips_data();
|
|
|
|
|
|
|
|
const context = {
|
2023-11-22 21:19:51 +01:00
|
|
|
banner_type: compose_banner.INFO,
|
|
|
|
classname: "setup_tips_banner",
|
2023-02-02 17:29:51 +01:00
|
|
|
...invite_tips_data,
|
|
|
|
};
|
|
|
|
|
|
|
|
$("#invite-user-form .setup-tips-container").html(render_invite_tips_banner(context));
|
2024-04-12 13:40:37 +02:00
|
|
|
|
|
|
|
const toggler = components.toggle({
|
|
|
|
html_class: "invite_users_option_tabs large allow-overflow",
|
|
|
|
selected: 0,
|
|
|
|
child_wants_focus: true,
|
|
|
|
values: [
|
2024-07-11 23:33:27 +02:00
|
|
|
{label: $t({defaultMessage: "Email invitation"}), key: "invite-email-tab"},
|
|
|
|
{label: $t({defaultMessage: "Invitation link"}), key: "invite-link-tab"},
|
2024-04-12 13:40:37 +02:00
|
|
|
],
|
|
|
|
callback(_name, key) {
|
|
|
|
switch (key) {
|
|
|
|
case "invite-email-tab":
|
|
|
|
$("#invitee_emails_container").show();
|
2024-04-18 19:38:47 +02:00
|
|
|
$("#receive-invite-acceptance-notification-container").show();
|
2024-04-12 13:40:37 +02:00
|
|
|
break;
|
|
|
|
case "invite-link-tab":
|
|
|
|
$("#invitee_emails_container").hide();
|
2024-04-18 19:38:47 +02:00
|
|
|
$("#receive-invite-acceptance-notification-container").hide();
|
2024-04-12 13:40:37 +02:00
|
|
|
break;
|
|
|
|
}
|
2024-05-24 05:22:29 +02:00
|
|
|
toggle_invite_submit_button(key);
|
2024-04-12 13:40:37 +02:00
|
|
|
reset_error_messages();
|
|
|
|
},
|
|
|
|
});
|
|
|
|
const $container = $("#invite_users_option_tabs_container");
|
|
|
|
if (!settings_data.user_can_invite_users_by_email()) {
|
|
|
|
toggler.disable_tab("invite-email-tab");
|
|
|
|
toggler.goto("invite-link-tab");
|
|
|
|
}
|
|
|
|
if (!settings_data.user_can_create_multiuse_invite()) {
|
|
|
|
toggler.disable_tab("invite-link-tab");
|
|
|
|
}
|
|
|
|
const $elem = toggler.get();
|
|
|
|
$container.append($elem);
|
|
|
|
setTimeout(() => {
|
|
|
|
$(".invite_users_option_tabs .ind-tab.selected").trigger("focus");
|
|
|
|
}, 0);
|
2022-10-11 18:02:39 +02:00
|
|
|
}
|
|
|
|
|
2023-09-22 10:32:06 +02:00
|
|
|
function invite_users(): void {
|
2024-04-12 13:40:37 +02:00
|
|
|
const is_generate_invite_link =
|
|
|
|
$(".invite_users_option_tabs").find(".selected").attr("data-tab-key") ===
|
|
|
|
"invite-link-tab";
|
2019-02-02 08:12:38 +01:00
|
|
|
if (is_generate_invite_link) {
|
|
|
|
generate_multiuse_invite();
|
|
|
|
} else {
|
|
|
|
submit_invitation_form();
|
|
|
|
}
|
2022-10-11 18:02:39 +02:00
|
|
|
}
|
2022-02-11 17:54:15 +01:00
|
|
|
|
2022-10-11 18:02:39 +02:00
|
|
|
dialog_widget.launch({
|
2024-04-03 05:52:27 +02:00
|
|
|
html_heading: $t_html({defaultMessage: "Invite users to organization"}),
|
2022-10-11 18:02:39 +02:00
|
|
|
html_body,
|
|
|
|
html_submit_button: $t_html({defaultMessage: "Invite"}),
|
|
|
|
id: "invite-user-modal",
|
|
|
|
loading_spinner: true,
|
|
|
|
on_click: invite_users,
|
|
|
|
post_render: invite_user_modal_post_render,
|
2024-04-09 07:18:29 +02:00
|
|
|
always_visible_scrollbar: true,
|
2022-02-11 17:54:15 +01:00
|
|
|
});
|
2022-10-11 18:02:39 +02:00
|
|
|
}
|
2022-02-11 17:54:15 +01:00
|
|
|
|
2023-09-22 10:32:06 +02:00
|
|
|
export function initialize(): void {
|
2022-10-11 18:02:39 +02:00
|
|
|
$(document).on("click", ".invite-user-link", open_invite_user_modal);
|
2021-02-10 17:00:58 +01:00
|
|
|
}
|