2019-11-02 00:06:25 +01:00
|
|
|
const render_invitation_failed_error = require("../templates/invitation_failed_error.hbs");
|
|
|
|
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
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const autosize = require('autosize');
|
2019-07-16 20:19:11 +02:00
|
|
|
|
2018-08-23 19:24:37 +02:00
|
|
|
function reset_error_messages() {
|
2019-02-06 20:32:06 +01:00
|
|
|
$('#invite_status').hide().text('').removeClass(common.status_classes);
|
|
|
|
$('#multiuse_invite_status').hide().text('').removeClass(common.status_classes);
|
|
|
|
|
|
|
|
$("#invitee_emails").closest('.control-group').removeClass('warning error');
|
2018-08-23 19:24:37 +02:00
|
|
|
|
|
|
|
if (page_params.development_environment) {
|
2019-02-02 08:12:38 +01: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() {
|
2019-11-02 00:06:25 +01:00
|
|
|
const invite_as = parseInt($('#invite_as').val(), 10);
|
|
|
|
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 = {
|
2019-02-01 12:20:13 +01:00
|
|
|
csrfmiddlewaretoken: $('input[name="csrfmiddlewaretoken"]').attr('value'),
|
|
|
|
invite_as: 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.
|
|
|
|
$('#submit-invitation').button('loading');
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-02-01 12:20:13 +01:00
|
|
|
function submit_invitation_form() {
|
2019-11-02 00:06:25 +01:00
|
|
|
const invite_status = $('#invite_status');
|
|
|
|
const invitee_emails = $("#invitee_emails");
|
|
|
|
const invitee_emails_group = invitee_emails.closest('.control-group');
|
|
|
|
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",
|
|
|
|
data: data,
|
2019-02-02 08:12:38 +01:00
|
|
|
beforeSend: beforeSend,
|
2018-08-23 19:24:37 +02:00
|
|
|
success: function () {
|
2019-01-17 16:55:25 +01:00
|
|
|
ui_report.success(i18n.t('User(s) invited successfully.'), invite_status);
|
|
|
|
invitee_emails_group.removeClass('warning');
|
2018-08-23 19:24:37 +02:00
|
|
|
invitee_emails.val('');
|
|
|
|
|
|
|
|
if (page_params.development_environment) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const rendered_email_msg = render_settings_dev_env_email_access();
|
2018-08-23 19:24:37 +02:00
|
|
|
$('#dev_env_msg').html(rendered_email_msg).addClass('alert-info').show();
|
|
|
|
}
|
|
|
|
|
|
|
|
},
|
|
|
|
error: function (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;
|
2019-01-17 16:55:25 +01:00
|
|
|
arr.errors.forEach(function (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;
|
|
|
|
}
|
2018-08-23 19:24:37 +02:00
|
|
|
invitee_emails_errored.push(value[0]);
|
|
|
|
});
|
|
|
|
|
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,
|
|
|
|
error_list: error_list,
|
2019-11-14 11:21:08 +01:00
|
|
|
is_admin: page_params.is_admin,
|
|
|
|
is_invitee_deactivated: is_invitee_deactivated,
|
2019-01-17 16:55:25 +01:00
|
|
|
});
|
|
|
|
ui_report.message(error_response, invite_status, "alert-warning");
|
2018-08-23 19:24:37 +02:00
|
|
|
invitee_emails_group.addClass('warning');
|
|
|
|
|
|
|
|
if (arr.sent_invitations) {
|
|
|
|
invitee_emails.val(invitee_emails_errored.join('\n'));
|
|
|
|
}
|
|
|
|
}
|
2019-02-02 08:12:38 +01:00
|
|
|
},
|
|
|
|
complete: function () {
|
|
|
|
$('#submit-invitation').button('reset');
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
2018-08-23 19:24:37 +02:00
|
|
|
|
2019-02-02 08:12:38 +01:00
|
|
|
function generate_multiuse_invite() {
|
2019-11-02 00:06:25 +01:00
|
|
|
const invite_status = $('#multiuse_invite_status');
|
|
|
|
const data = get_common_invitation_data();
|
2019-02-02 08:12:38 +01:00
|
|
|
channel.post({
|
|
|
|
url: "/json/invites/multiuse",
|
|
|
|
data: data,
|
|
|
|
beforeSend: beforeSend,
|
|
|
|
success: function (data) {
|
|
|
|
ui_report.success(i18n.t('Invitation link: <a href="__link__">__link__</a>',
|
|
|
|
{link: data.invite_link}), invite_status);
|
|
|
|
},
|
|
|
|
error: function (xhr) {
|
|
|
|
ui_report.error("", xhr, invite_status);
|
|
|
|
},
|
|
|
|
complete: function () {
|
|
|
|
$('#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);
|
2018-07-20 09:43:32 +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 () {
|
2013-01-31 17:23:34 +01:00
|
|
|
$('#submit-invitation').button();
|
2017-03-14 23:22:46 +01:00
|
|
|
prepare_form_to_be_shown();
|
2019-07-16 20:19:11 +02:00
|
|
|
autosize($("#invitee_emails").focus());
|
2012-12-05 22:48:15 +01:00
|
|
|
|
2017-05-27 15:40:54 +02:00
|
|
|
overlays.open_overlay({
|
2017-05-06 00:41:47 +02:00
|
|
|
name: 'invite',
|
|
|
|
overlay: $('#invite-user'),
|
|
|
|
on_close: function () {
|
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 () {
|
2013-04-02 20:47:18 +02:00
|
|
|
$(document).on('click', '.invite_check_all_button', function (e) {
|
2016-08-24 22:24:45 +02:00
|
|
|
$('#streams_to_add :checkbox').prop('checked', true);
|
2013-04-02 20:47:18 +02:00
|
|
|
e.preventDefault();
|
|
|
|
});
|
|
|
|
|
|
|
|
$(document).on('click', '.invite_uncheck_all_button', function (e) {
|
2016-08-24 22:24:45 +02:00
|
|
|
$('#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
|
|
|
|
2019-02-02 08:12:38 +01:00
|
|
|
$("#submit-invitation").on("click", function () {
|
2019-11-02 00:06:25 +01: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();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2019-02-06 20:31:45 +01:00
|
|
|
$("#generate_multiuse_invite_button").on("click", function () {
|
|
|
|
$("#generate_multiuse_invite_radio").prop("checked", true);
|
|
|
|
$("#multiuse_radio_section").show();
|
|
|
|
$("#invite-method-choice").hide();
|
|
|
|
$('#invitee_emails').prop('disabled', true);
|
|
|
|
$('#submit-invitation').text(i18n.t('Generate invite link'));
|
|
|
|
$('#submit-invitation').data('loading-text', i18n.t('Generating link...'));
|
|
|
|
reset_error_messages();
|
|
|
|
});
|
|
|
|
|
|
|
|
$('#invite-user').on('change', '#generate_multiuse_invite_radio', function () {
|
|
|
|
$('#invitee_emails').prop('disabled', false);
|
|
|
|
$('#submit-invitation').text(i18n.t('Invite'));
|
|
|
|
$('#submit-invitation').data('loading-text', i18n.t('Inviting...'));
|
|
|
|
$("#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;
|