2020-08-01 03:43:15 +02:00
|
|
|
"use strict";
|
|
|
|
|
2020-07-24 06:02:07 +02:00
|
|
|
const autosize = require("autosize");
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const render_invitation_failed_error = require("../templates/invitation_failed_error.hbs");
|
2020-07-15 01:29:15 +02:00
|
|
|
const render_invite_subscription = require("../templates/invite_subscription.hbs");
|
|
|
|
const render_settings_dev_env_email_access = require("../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-07-15 01:29:15 +02:00
|
|
|
const invite_as = 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 () {
|
2019-11-02 00:06:25 +01:00
|
|
|
const stream_id = 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;
|
2020-07-02 01:45:54 +02:00
|
|
|
arr.errors.forEach((value) => {
|
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);
|
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-07-15 01:29:15 +02:00
|
|
|
$("#submit-invitation").button("reset");
|
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-07-15 00:34:28 +02:00
|
|
|
ui_report.success(
|
|
|
|
i18n.t('Invitation link: <a href="__link__">__link__</a>', {
|
|
|
|
link: data.invite_link,
|
|
|
|
}),
|
|
|
|
invite_status,
|
|
|
|
);
|
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-07-15 01:29:15 +02:00
|
|
|
$("#submit-invitation").button("reset");
|
2018-08-23 19:24:37 +02:00
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-07-20 09:43:32 +02:00
|
|
|
exports.get_invite_streams = function () {
|
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;
|
|
|
|
};
|
|
|
|
|
|
|
|
function update_subscription_checkboxes() {
|
2020-01-18 17:54:25 +01:00
|
|
|
const data = {
|
|
|
|
streams: exports.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();
|
|
|
|
}
|
|
|
|
|
2018-05-15 21:24:26 +02:00
|
|
|
exports.launch = function () {
|
2020-07-15 01:29:15 +02:00
|
|
|
$("#submit-invitation").button();
|
2017-03-14 23:22:46 +01:00
|
|
|
prepare_form_to_be_shown();
|
2020-07-20 21:24:26 +02:00
|
|
|
autosize($("#invitee_emails").trigger("focus"));
|
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
|
|
|
},
|
|
|
|
});
|
2017-03-14 23:22:46 +01:00
|
|
|
};
|
|
|
|
|
2018-05-15 22:03:14 +02:00
|
|
|
exports.initialize = function () {
|
2020-07-15 01:29:15 +02:00
|
|
|
$(document).on("click", ".invite_check_all_button", (e) => {
|
|
|
|
$("#streams_to_add :checkbox").prop("checked", true);
|
2013-04-02 20:47:18 +02:00
|
|
|
e.preventDefault();
|
|
|
|
});
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
$(document).on("click", ".invite_uncheck_all_button", (e) => {
|
|
|
|
$("#streams_to_add :checkbox").prop("checked", false);
|
2013-04-02 20:47:18 +02:00
|
|
|
e.preventDefault();
|
|
|
|
});
|
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
|
|
|
});
|
2018-05-15 22:03:14 +02:00
|
|
|
};
|
2012-12-05 22:48:15 +01:00
|
|
|
|
2019-10-25 09:45:13 +02:00
|
|
|
window.invite = exports;
|