2021-02-10 17:00:58 +01:00
|
|
|
import autosize from "autosize";
|
|
|
|
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";
|
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";
|
|
|
|
import render_invite_subscription from "../templates/invite_subscription.hbs";
|
2021-06-14 11:49:01 +02:00
|
|
|
import render_invite_user from "../templates/invite_user.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-03-22 16:09:12 +01:00
|
|
|
import * as browser_history from "./browser_history";
|
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";
|
2021-04-13 06:51:54 +02:00
|
|
|
import {$t, $t_html} from "./i18n";
|
2021-02-28 01:03:46 +01:00
|
|
|
import * as overlays from "./overlays";
|
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";
|
2021-02-28 00:53:59 +01:00
|
|
|
import * as stream_data from "./stream_data";
|
2021-02-28 21:33:10 +01:00
|
|
|
import * as ui from "./ui";
|
2021-02-28 00:58:55 +01:00
|
|
|
import * as ui_report from "./ui_report";
|
2021-06-12 12:02:59 +02:00
|
|
|
import * as util from "./util";
|
2021-02-28 00:35:35 +01:00
|
|
|
|
2018-08-23 19:24:37 +02:00
|
|
|
function reset_error_messages() {
|
2020-07-15 01:29:15 +02:00
|
|
|
$("#invite_status").hide().text("").removeClass(common.status_classes);
|
|
|
|
$("#multiuse_invite_status").hide().text("").removeClass(common.status_classes);
|
2019-02-06 20:32:06 +01:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
$("#invitee_emails").closest(".control-group").removeClass("warning error");
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-01 12:20:13 +01:00
|
|
|
function get_common_invitation_data() {
|
2020-10-07 09:17:30 +02:00
|
|
|
const invite_as = Number.parseInt($("#invite_as").val(), 10);
|
2021-12-01 12:10:58 +01:00
|
|
|
let expires_in = $("#expires_in").val();
|
|
|
|
|
|
|
|
// See settings_config.expires_in_values for why we do this conversion.
|
|
|
|
if (expires_in === "null") {
|
|
|
|
expires_in = JSON.stringify(null);
|
|
|
|
} else {
|
|
|
|
expires_in = Number.parseFloat($("#expires_in").val());
|
|
|
|
}
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const stream_ids = [];
|
2019-02-01 12:20:13 +01:00
|
|
|
$("#invite-stream-checkboxes input:checked").each(function () {
|
2020-10-07 09:17:30 +02:00
|
|
|
const stream_id = Number.parseInt($(this).val(), 10);
|
2018-12-22 05:41:54 +01:00
|
|
|
stream_ids.push(stream_id);
|
2018-08-23 19:24:37 +02:00
|
|
|
});
|
2019-11-02 00:06:25 +01:00
|
|
|
const data = {
|
2020-07-15 01:29:15 +02:00
|
|
|
csrfmiddlewaretoken: $('input[name="csrfmiddlewaretoken"]').attr("value"),
|
2020-07-20 22:18:43 +02:00
|
|
|
invite_as,
|
2018-12-22 05:41:54 +01:00
|
|
|
stream_ids: JSON.stringify(stream_ids),
|
2022-02-10 11:52:34 +01:00
|
|
|
invite_expires_in_minutes: expires_in,
|
2019-02-01 12:20:13 +01:00
|
|
|
};
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
2019-02-02 08:12:38 +01:00
|
|
|
function beforeSend() {
|
|
|
|
reset_error_messages();
|
|
|
|
// TODO: You could alternatively parse the textarea here, and return errors to
|
|
|
|
// 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.
|
2021-04-04 21:28:26 +02:00
|
|
|
const loading_text = $("#submit-invitation").data("loading-text");
|
|
|
|
$("#submit-invitation").text(loading_text);
|
|
|
|
$("#submit-invitation").prop("disabled", true);
|
2019-02-02 08:12:38 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-02-01 12:20:13 +01:00
|
|
|
function submit_invitation_form() {
|
2022-01-25 11:36:19 +01:00
|
|
|
const $invite_status = $("#invite_status");
|
|
|
|
const $invitee_emails = $("#invitee_emails");
|
|
|
|
const $invitee_emails_group = $invitee_emails.closest(".control-group");
|
2019-11-02 00:06:25 +01:00
|
|
|
const data = get_common_invitation_data();
|
2019-02-01 12:20:13 +01:00
|
|
|
data.invitee_emails = $("#invitee_emails").val();
|
2018-08-23 19:24:37 +02:00
|
|
|
|
|
|
|
channel.post({
|
|
|
|
url: "/json/invites",
|
2020-07-20 22:18:43 +02:00
|
|
|
data,
|
|
|
|
beforeSend,
|
|
|
|
success() {
|
2021-04-13 05:18:25 +02:00
|
|
|
ui_report.success(
|
|
|
|
$t_html({defaultMessage: "User(s) invited successfully."}),
|
2022-01-25 11:36:19 +01:00
|
|
|
$invite_status,
|
2021-04-13 05:18:25 +02:00
|
|
|
);
|
2022-01-25 11:36:19 +01:00
|
|
|
$invitee_emails_group.removeClass("warning");
|
|
|
|
$invitee_emails.val("");
|
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
|
|
|
}
|
|
|
|
},
|
2020-07-20 22:18:43 +02:00
|
|
|
error(xhr) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const arr = JSON.parse(xhr.responseText);
|
2018-08-23 19:24:37 +02:00
|
|
|
if (arr.errors === undefined) {
|
|
|
|
// 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;
|
2021-01-22 22:29:08 +01:00
|
|
|
for (const value of arr.errors) {
|
2019-11-14 11:21:08 +01:00
|
|
|
const [email, error_message, deactivated] = value;
|
|
|
|
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({
|
2019-01-17 16:55:25 +01:00
|
|
|
error_message: arr.msg,
|
2020-07-20 22:18:43 +02:00
|
|
|
error_list,
|
2019-11-14 11:21:08 +01:00
|
|
|
is_admin: page_params.is_admin,
|
2020-07-20 22:18:43 +02:00
|
|
|
is_invitee_deactivated,
|
2021-05-28 15:57:08 +02:00
|
|
|
license_limit_reached: arr.license_limit_reached,
|
|
|
|
has_billing_access: page_params.is_owner || page_params.is_billing_admin,
|
2021-09-21 18:46:48 +02:00
|
|
|
daily_limit_reached: arr.daily_limit_reached,
|
2019-01-17 16:55:25 +01:00
|
|
|
});
|
2022-01-25 11:36:19 +01:00
|
|
|
ui_report.message(error_response, $invite_status, "alert-warning");
|
|
|
|
$invitee_emails_group.addClass("warning");
|
2018-08-23 19:24:37 +02:00
|
|
|
|
|
|
|
if (arr.sent_invitations) {
|
2022-01-25 11:36:19 +01:00
|
|
|
$invitee_emails.val(invitee_emails_errored.join("\n"));
|
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() {
|
2021-04-13 06:51:54 +02:00
|
|
|
$("#submit-invitation").text($t({defaultMessage: "Invite"}));
|
2020-09-28 13:56:37 +02:00
|
|
|
$("#submit-invitation").prop("disabled", false);
|
2022-03-16 22:19:20 +01:00
|
|
|
$("#invitee_emails").trigger("focus");
|
2020-08-11 16:04:16 +02:00
|
|
|
ui.get_scroll_element($("#invite_user_form .modal-body"))[0].scrollTop = 0;
|
2019-02-02 08:12:38 +01:00
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
2018-08-23 19:24:37 +02:00
|
|
|
|
2019-02-02 08:12:38 +01:00
|
|
|
function generate_multiuse_invite() {
|
2022-01-25 11:36:19 +01:00
|
|
|
const $invite_status = $("#multiuse_invite_status");
|
2019-11-02 00:06:25 +01:00
|
|
|
const data = get_common_invitation_data();
|
2019-02-02 08:12:38 +01:00
|
|
|
channel.post({
|
|
|
|
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);
|
2020-10-13 18:17:20 +02:00
|
|
|
new ClipboardJS("#copy_generated_invite_link");
|
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() {
|
2021-04-13 06:51:54 +02:00
|
|
|
$("#submit-invitation").text($t({defaultMessage: "Generate invite link"}));
|
2020-09-28 13:56:37 +02:00
|
|
|
$("#submit-invitation").prop("disabled", false);
|
2018-08-23 19:24:37 +02:00
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-02-10 17:00:58 +01:00
|
|
|
export function get_invite_streams() {
|
2020-01-18 17:54:25 +01:00
|
|
|
const streams = stream_data.get_invite_stream_data();
|
2021-06-12 12:02:59 +02:00
|
|
|
streams.sort((a, b) => util.strcmp(a.name, b.name));
|
2018-07-20 09:43:32 +02:00
|
|
|
return streams;
|
2021-02-10 17:00:58 +01:00
|
|
|
}
|
2018-07-20 09:43:32 +02:00
|
|
|
|
|
|
|
function update_subscription_checkboxes() {
|
2020-01-18 17:54:25 +01:00
|
|
|
const data = {
|
2021-02-10 17:00:58 +01:00
|
|
|
streams: get_invite_streams(),
|
2020-04-14 12:55:18 +02:00
|
|
|
notifications_stream: stream_data.get_notifications_stream(),
|
2020-01-18 17:54:25 +01:00
|
|
|
};
|
2019-11-02 00:06:25 +01:00
|
|
|
const html = render_invite_subscription(data);
|
2020-07-15 01:29:15 +02:00
|
|
|
$("#streams_to_add").html(html);
|
2012-12-05 22:48:15 +01:00
|
|
|
}
|
|
|
|
|
2013-01-31 17:09:18 +01:00
|
|
|
function prepare_form_to_be_shown() {
|
|
|
|
update_subscription_checkboxes();
|
|
|
|
reset_error_messages();
|
|
|
|
}
|
|
|
|
|
2021-02-10 17:00:58 +01:00
|
|
|
export function launch() {
|
2020-07-15 01:29:15 +02:00
|
|
|
$("#submit-invitation").button();
|
2017-03-14 23:22:46 +01:00
|
|
|
prepare_form_to_be_shown();
|
2012-12-05 22:48:15 +01:00
|
|
|
|
2017-05-27 15:40:54 +02:00
|
|
|
overlays.open_overlay({
|
2020-07-15 01:29:15 +02:00
|
|
|
name: "invite",
|
2022-01-25 11:36:19 +01:00
|
|
|
$overlay: $("#invite-user"),
|
2020-07-20 22:18:43 +02:00
|
|
|
on_close() {
|
2021-03-22 16:09:12 +01:00
|
|
|
browser_history.exit_overlay();
|
2017-05-06 00:41:47 +02:00
|
|
|
},
|
|
|
|
});
|
2020-08-09 09:21:31 +02:00
|
|
|
|
|
|
|
autosize($("#invitee_emails").trigger("focus"));
|
2020-08-09 12:09:30 +02:00
|
|
|
|
|
|
|
// Ctrl + Enter key to submit form
|
|
|
|
$("#invite-user").on("keydown", (e) => {
|
|
|
|
if (e.key === "Enter" && e.ctrlKey) {
|
|
|
|
submit_invitation_form();
|
|
|
|
}
|
|
|
|
});
|
2021-02-10 17:00:58 +01:00
|
|
|
}
|
2017-03-14 23:22:46 +01:00
|
|
|
|
2022-02-14 09:11:25 +01:00
|
|
|
function valid_to(expires_in) {
|
|
|
|
const time_valid = Number.parseFloat(expires_in);
|
2021-09-15 11:49:36 +02:00
|
|
|
if (!time_valid) {
|
|
|
|
return $t({defaultMessage: "Never expires"});
|
|
|
|
}
|
2022-02-10 11:52:34 +01:00
|
|
|
const valid_to = add(new Date(), {minutes: time_valid});
|
2021-09-15 11:49:36 +02:00
|
|
|
return $t({defaultMessage: "Expires on {date}"}, {date: valid_to.toLocaleDateString()});
|
|
|
|
}
|
|
|
|
|
2021-02-10 17:00:58 +01:00
|
|
|
export function initialize() {
|
2021-06-14 11:49:01 +02:00
|
|
|
const rendered = render_invite_user({
|
|
|
|
is_admin: page_params.is_admin,
|
|
|
|
is_owner: page_params.is_owner,
|
|
|
|
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,
|
2021-06-14 11:49:01 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
$(".app").append(rendered);
|
|
|
|
|
2020-08-11 16:04:16 +02:00
|
|
|
$(document).on("click", "#invite_check_all_button", () => {
|
2020-07-15 01:29:15 +02:00
|
|
|
$("#streams_to_add :checkbox").prop("checked", true);
|
2013-04-02 20:47:18 +02:00
|
|
|
});
|
|
|
|
|
2020-08-11 16:04:16 +02:00
|
|
|
$(document).on("click", "#invite_uncheck_all_button", () => {
|
2020-07-15 01:29:15 +02:00
|
|
|
$("#streams_to_add :checkbox").prop("checked", false);
|
2013-04-02 20:47:18 +02:00
|
|
|
});
|
2018-12-22 05:41:54 +01:00
|
|
|
|
2020-07-02 01:45:54 +02:00
|
|
|
$("#submit-invitation").on("click", () => {
|
2020-07-15 01:29:15 +02:00
|
|
|
const is_generate_invite_link = $("#generate_multiuse_invite_radio").prop("checked");
|
2019-02-02 08:12:38 +01:00
|
|
|
if (is_generate_invite_link) {
|
|
|
|
generate_multiuse_invite();
|
|
|
|
} else {
|
|
|
|
submit_invitation_form();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2020-07-02 01:45:54 +02:00
|
|
|
$("#generate_multiuse_invite_button").on("click", () => {
|
2019-02-06 20:31:45 +01:00
|
|
|
$("#generate_multiuse_invite_radio").prop("checked", true);
|
|
|
|
$("#multiuse_radio_section").show();
|
|
|
|
$("#invite-method-choice").hide();
|
2020-07-15 01:29:15 +02:00
|
|
|
$("#invitee_emails").prop("disabled", true);
|
2021-04-13 06:51:54 +02:00
|
|
|
$("#submit-invitation").text($t({defaultMessage: "Generate invite link"}));
|
|
|
|
$("#submit-invitation").data("loading-text", $t({defaultMessage: "Generating link..."}));
|
2019-02-06 20:31:45 +01:00
|
|
|
reset_error_messages();
|
|
|
|
});
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
$("#invite-user").on("change", "#generate_multiuse_invite_radio", () => {
|
|
|
|
$("#invitee_emails").prop("disabled", false);
|
2021-04-13 06:51:54 +02:00
|
|
|
$("#submit-invitation").text($t({defaultMessage: "Invite"}));
|
|
|
|
$("#submit-invitation").data("loading-text", $t({defaultMessage: "Inviting..."}));
|
2019-02-06 20:31:45 +01:00
|
|
|
$("#multiuse_radio_section").hide();
|
|
|
|
$("#invite-method-choice").show();
|
|
|
|
reset_error_messages();
|
2019-02-02 08:12:38 +01:00
|
|
|
});
|
2021-09-15 11:49:36 +02:00
|
|
|
|
2022-02-14 09:11:25 +01:00
|
|
|
$("#expires_on").text(valid_to($("#expires_in").val()));
|
2021-09-15 11:49:36 +02:00
|
|
|
$("#expires_in").on("change", () => {
|
2022-02-14 09:11:25 +01:00
|
|
|
$("#expires_on").text(valid_to($("#expires_in").val()));
|
2021-09-15 11:49:36 +02:00
|
|
|
});
|
2021-02-10 17:00:58 +01:00
|
|
|
}
|