2021-02-10 17:00:58 +01:00
|
|
|
import autosize from "autosize";
|
|
|
|
import ClipboardJS from "clipboard";
|
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";
|
|
|
|
import render_settings_dev_env_email_access from "../templates/settings/dev_env_email_access.hbs";
|
2019-07-09 21:24:00 +02: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);
|
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),
|
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.
|
2020-07-15 01:29:15 +02:00
|
|
|
$("#submit-invitation").button("loading");
|
2019-02-02 08:12:38 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-02-01 12:20:13 +01:00
|
|
|
function submit_invitation_form() {
|
2020-07-15 01:29:15 +02:00
|
|
|
const invite_status = $("#invite_status");
|
2019-11-02 00:06:25 +01:00
|
|
|
const invitee_emails = $("#invitee_emails");
|
2020-07-15 01:29:15 +02:00
|
|
|
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() {
|
2020-07-15 01:29:15 +02:00
|
|
|
ui_report.success(i18n.t("User(s) invited successfully."), invite_status);
|
|
|
|
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.
|
2019-01-17 16:55:25 +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,
|
2019-01-17 16:55:25 +01:00
|
|
|
});
|
|
|
|
ui_report.message(error_response, invite_status, "alert-warning");
|
2020-07-15 01:29:15 +02:00
|
|
|
invitee_emails_group.addClass("warning");
|
2018-08-23 19:24:37 +02:00
|
|
|
|
|
|
|
if (arr.sent_invitations) {
|
2020-07-15 01:29:15 +02: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() {
|
2020-09-28 13:56:37 +02:00
|
|
|
$("#submit-invitation").text(i18n.t("Invite"));
|
|
|
|
$("#submit-invitation").prop("disabled", false);
|
2020-08-10 15:34:35 +02:00
|
|
|
$("#invitee_emails").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() {
|
2020-07-15 01:29:15 +02: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);
|
|
|
|
ui_report.success(copy_link_html, invite_status);
|
|
|
|
new ClipboardJS("#copy_generated_invite_link");
|
2019-02-02 08:12:38 +01:00
|
|
|
},
|
2020-07-20 22:18:43 +02:00
|
|
|
error(xhr) {
|
2019-02-02 08:12:38 +01:00
|
|
|
ui_report.error("", xhr, invite_status);
|
|
|
|
},
|
2020-07-20 22:18:43 +02:00
|
|
|
complete() {
|
2020-09-28 13:56:37 +02:00
|
|
|
$("#submit-invitation").text(i18n.t("Generate invite link"));
|
|
|
|
$("#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();
|
|
|
|
|
|
|
|
function compare_streams(a, b) {
|
|
|
|
return a.name.localeCompare(b.name);
|
|
|
|
}
|
|
|
|
streams.sort(compare_streams);
|
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",
|
|
|
|
overlay: $("#invite-user"),
|
2020-07-20 22:18:43 +02:00
|
|
|
on_close() {
|
2017-05-27 18:55:41 +02:00
|
|
|
hashchange.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
|
|
|
|
2021-02-10 17:00:58 +01:00
|
|
|
export function initialize() {
|
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);
|
|
|
|
$("#submit-invitation").text(i18n.t("Generate invite link"));
|
|
|
|
$("#submit-invitation").data("loading-text", i18n.t("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);
|
|
|
|
$("#submit-invitation").text(i18n.t("Invite"));
|
|
|
|
$("#submit-invitation").data("loading-text", i18n.t("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-02-10 17:00:58 +01:00
|
|
|
}
|