mirror of https://github.com/zulip/zulip.git
610 lines
24 KiB
JavaScript
610 lines
24 KiB
JavaScript
var settings = (function () {
|
|
|
|
var exports = {};
|
|
|
|
var streams = [];
|
|
|
|
function add_bot_row(name, email, avatar_url, api_key) {
|
|
var info = {
|
|
name: name,
|
|
email: email,
|
|
avatar_url: avatar_url,
|
|
api_key: api_key
|
|
};
|
|
|
|
var row = $(templates.render('bot_avatar_row', info));
|
|
$('#bots_list').append(row);
|
|
$('#bots_list').show();
|
|
}
|
|
|
|
function is_local_part(value, element) {
|
|
// Adapted from Django's EmailValidator
|
|
return this.optional(element) || /^[\-!#$%&'*+\/=?\^_`{}|~0-9A-Z]+(\.[\-!#$%&'*+\/=?\^_`{}|~0-9A-Z]+)*$/i.test(value);
|
|
}
|
|
|
|
function build_stream_list($select) {
|
|
$select.empty();
|
|
_.each(_.pluck(streams, 'name'), function (name) {
|
|
$select.append($('<option>')
|
|
.attr('value', name)
|
|
.text(name)
|
|
);
|
|
});
|
|
}
|
|
|
|
// Choose avatar stamp fairly randomly, to help get old avatars out of cache.
|
|
exports.avatar_stamp = Math.floor(Math.random()*100);
|
|
|
|
exports.setup_page = function () {
|
|
var settings_tab = templates.render('settings_tab', {page_params: page_params});
|
|
$("#settings").html(settings_tab);
|
|
$("#settings-status").hide();
|
|
$("#notify-settings-status").hide();
|
|
$("#ui-settings-status").hide();
|
|
|
|
alert_words_ui.set_up_alert_words();
|
|
|
|
$("#api_key_value").text("");
|
|
$("#get_api_key_box").hide();
|
|
$("#show_api_key_box").hide();
|
|
$("#api_key_button_box").show();
|
|
|
|
function clear_password_change() {
|
|
// Clear the password boxes so that passwords don't linger in the DOM
|
|
// for an XSS attacker to find.
|
|
$('#old_password, #new_password, #confirm_password').val('');
|
|
}
|
|
|
|
clear_password_change();
|
|
|
|
channel.get({
|
|
url: '/json/streams',
|
|
success: function (data) {
|
|
streams = data.streams;
|
|
build_stream_list($('#create_bot_default_sending_stream'));
|
|
build_stream_list($('#create_bot_default_events_register_stream'));
|
|
}
|
|
});
|
|
|
|
$('#api_key_button').click(function (e) {
|
|
if (page_params.password_auth_enabled !== false) {
|
|
$("#get_api_key_box").show();
|
|
} else {
|
|
// Skip the password prompt step
|
|
$("#get_api_key_box form").submit();
|
|
}
|
|
$("#api_key_button_box").hide();
|
|
});
|
|
|
|
$('#pw_change_link').on('click', function (e) {
|
|
e.preventDefault();
|
|
$('#pw_change_link').hide();
|
|
$('#pw_change_controls').show();
|
|
});
|
|
|
|
$('#new_password').on('change keyup', function () {
|
|
password_quality($('#new_password').val(), $('#pw_strength .bar'));
|
|
});
|
|
|
|
if (feature_flags.dont_show_digest_email_setting) {
|
|
$("#digest_container").hide();
|
|
}
|
|
if (!feature_flags.new_bot_ui) {
|
|
$('.new-bot-ui').hide();
|
|
}
|
|
|
|
|
|
function settings_change_error(message) {
|
|
// Scroll to the top so the error message is visible.
|
|
// We would scroll anyway if we end up submitting the form.
|
|
viewport.scrollTop(0);
|
|
var settings_status = $('#settings-status').expectOne();
|
|
settings_status.removeClass(status_classes)
|
|
.addClass('alert-error')
|
|
.text(message).stop(true).fadeTo(0,1);
|
|
}
|
|
|
|
$("form.your-account-settings").ajaxForm({
|
|
dataType: 'json', // This seems to be ignored. We still get back an xhr.
|
|
beforeSubmit: function (arr, form, options) {
|
|
if (page_params.password_auth_enabled !== false) {
|
|
// FIXME: Check that the two password fields match
|
|
// FIXME: Use the same jQuery validation plugin as the signup form?
|
|
var new_pw = $('#new_password').val();
|
|
if (new_pw !== '') {
|
|
var password_ok = password_quality(new_pw);
|
|
if (password_ok === undefined) {
|
|
// zxcvbn.js didn't load, for whatever reason.
|
|
settings_change_error(
|
|
'An internal error occurred; try reloading the page. ' +
|
|
'Sorry for the trouble!');
|
|
return false;
|
|
} else if (!password_ok) {
|
|
settings_change_error('New password is too weak');
|
|
return false;
|
|
}
|
|
}
|
|
}
|
|
return true;
|
|
},
|
|
success: function (resp, statusText, xhr, form) {
|
|
var message = "Updated settings!";
|
|
var result = $.parseJSON(xhr.responseText);
|
|
var settings_status = $('#settings-status').expectOne();
|
|
|
|
settings_status.removeClass(status_classes)
|
|
.addClass('alert-success')
|
|
.text(message).stop(true).fadeTo(0,1);
|
|
},
|
|
error: function (xhr, error_type, xhn) {
|
|
var response = "Error changing settings";
|
|
if (xhr.status.toString().charAt(0) === "4") {
|
|
// Only display the error response for 4XX, where we've crafted
|
|
// a nice response.
|
|
response += ": " + $.parseJSON(xhr.responseText).msg;
|
|
}
|
|
settings_change_error(response);
|
|
},
|
|
complete: function (xhr, statusText) {
|
|
// Whether successful or not, clear the password boxes.
|
|
// TODO: Clear these earlier, while the request is still pending.
|
|
clear_password_change();
|
|
}
|
|
});
|
|
|
|
function update_notification_settings_success(resp, statusText, xhr, form) {
|
|
var message = "Updated notification settings!";
|
|
var result = $.parseJSON(xhr.responseText);
|
|
var notify_settings_status = $('#notify-settings-status').expectOne();
|
|
|
|
// Stream notification settings.
|
|
|
|
if (result.enable_stream_desktop_notifications !== undefined) {
|
|
page_params.stream_desktop_notifications_enabled = result.enable_stream_desktop_notifications;
|
|
}
|
|
if (result.enable_stream_sounds !== undefined) {
|
|
page_params.stream_sounds_enabled = result.enable_stream_sounds;
|
|
}
|
|
|
|
// PM and @-mention notification settings.
|
|
|
|
if (result.enable_desktop_notifications !== undefined) {
|
|
page_params.desktop_notifications_enabled = result.enable_desktop_notifications;
|
|
}
|
|
if (result.enable_sounds !== undefined) {
|
|
page_params.sounds_enabled = result.enable_sounds;
|
|
}
|
|
|
|
if (result.enable_offline_email_notifications !== undefined) {
|
|
page_params.enable_offline_email_notifications = result.enable_offline_email_notifications;
|
|
}
|
|
|
|
if (result.enable_offline_push_notifications !== undefined) {
|
|
page_params.enable_offline_push_notifications = result.enable_offline_push_notifications;
|
|
}
|
|
|
|
// Other notification settings.
|
|
|
|
if (result.enable_digest_emails !== undefined) {
|
|
page_params.enable_digest_emails = result.enable_digest_emails;
|
|
}
|
|
|
|
notify_settings_status.removeClass(status_classes)
|
|
.addClass('alert-success')
|
|
.text(message).stop(true).fadeTo(0,1);
|
|
}
|
|
|
|
function update_notification_settings_error(xhr, error_type, xhn) {
|
|
var response = "Error changing settings";
|
|
var notify_settings_status = $('#notify-settings-status').expectOne();
|
|
|
|
if (xhr.status.toString().charAt(0) === "4") {
|
|
// Only display the error response for 4XX, where we've crafted
|
|
// a nice response.
|
|
response += ": " + $.parseJSON(xhr.responseText).msg;
|
|
}
|
|
|
|
notify_settings_status.removeClass(status_classes)
|
|
.addClass('alert-error')
|
|
.text(response).stop(true).fadeTo(0,1);
|
|
}
|
|
|
|
function post_notify_settings_changes(notification_changes, success_func,
|
|
error_func) {
|
|
return channel.post({
|
|
url: "/json/notify_settings/change",
|
|
data: notification_changes,
|
|
success: success_func,
|
|
error: error_func
|
|
});
|
|
}
|
|
|
|
$("#change_notification_settings").on("click", function (e) {
|
|
var updated_settings = {};
|
|
_.each(["enable_stream_desktop_notifications", "enable_stream_sounds",
|
|
"enable_desktop_notifications", "enable_sounds",
|
|
"enable_offline_email_notifications",
|
|
"enable_offline_push_notifications", "enable_digest_emails"],
|
|
function (setting) {
|
|
updated_settings[setting] = $("#" + setting).is(":checked");
|
|
});
|
|
post_notify_settings_changes(updated_settings,
|
|
update_notification_settings_success,
|
|
update_notification_settings_error);
|
|
});
|
|
|
|
function update_global_stream_setting(notification_type, new_setting) {
|
|
var data = {};
|
|
data[notification_type] = new_setting;
|
|
channel.post({
|
|
url: "/json/notify_settings/change",
|
|
data: data,
|
|
success: update_notification_settings_success,
|
|
error: update_notification_settings_error
|
|
});
|
|
}
|
|
|
|
function update_desktop_notification_setting(new_setting) {
|
|
update_global_stream_setting("enable_stream_desktop_notifications", new_setting);
|
|
subs.set_all_stream_desktop_notifications_to(new_setting);
|
|
}
|
|
|
|
function update_audible_notification_setting(new_setting) {
|
|
update_global_stream_setting("enable_stream_sounds", new_setting);
|
|
subs.set_all_stream_audible_notifications_to(new_setting);
|
|
}
|
|
|
|
function maybe_bulk_update_stream_notification_setting(notification_checkbox,
|
|
propagate_setting_function) {
|
|
var html = templates.render("propagate_notification_change");
|
|
var control_group = notification_checkbox.closest(".control-group");
|
|
var checkbox_status = notification_checkbox.is(":checked");
|
|
control_group.find(".propagate_stream_notifications_change").html(html);
|
|
control_group.find(".yes_propagate_notifications").on("click", function (e) {
|
|
propagate_setting_function(checkbox_status);
|
|
control_group.find(".propagate_stream_notifications_change").empty();
|
|
});
|
|
control_group.find(".no_propagate_notifications").on("click", function (e) {
|
|
control_group.find(".propagate_stream_notifications_change").empty();
|
|
});
|
|
}
|
|
|
|
$("#enable_stream_desktop_notifications").on("click", function (e) {
|
|
var notification_checkbox = $("#enable_stream_desktop_notifications");
|
|
maybe_bulk_update_stream_notification_setting(notification_checkbox,
|
|
update_desktop_notification_setting);
|
|
});
|
|
|
|
$("#enable_stream_sounds").on("click", function (e) {
|
|
var notification_checkbox = $("#enable_stream_sounds");
|
|
maybe_bulk_update_stream_notification_setting(notification_checkbox,
|
|
update_audible_notification_setting);
|
|
});
|
|
|
|
$("#get_api_key_box").hide();
|
|
$("#show_api_key_box").hide();
|
|
$("#get_api_key_box form").ajaxForm({
|
|
dataType: 'json', // This seems to be ignored. We still get back an xhr.
|
|
success: function (resp, statusText, xhr, form) {
|
|
var message = "Updated settings!";
|
|
var result = $.parseJSON(xhr.responseText);
|
|
var settings_status = $('#settings-status').expectOne();
|
|
|
|
$("#get_api_key_password").val("");
|
|
$("#api_key_value").text(result.api_key);
|
|
$("#show_api_key_box").show();
|
|
$("#get_api_key_box").hide();
|
|
settings_status.hide();
|
|
},
|
|
error: function (xhr, error_type, xhn) {
|
|
var response = "Error getting API key";
|
|
var settings_status = $('#settings-status').expectOne();
|
|
|
|
if (xhr.status.toString().charAt(0) === "4") {
|
|
// Only display the error response for 4XX, where we've crafted
|
|
// a nice response.
|
|
response += ": " + $.parseJSON(xhr.responseText).msg;
|
|
}
|
|
settings_status.removeClass(status_classes)
|
|
.addClass('alert-error')
|
|
.text(response).stop(true).fadeTo(0,1);
|
|
$("#show_api_key_box").hide();
|
|
$("#get_api_key_box").show();
|
|
}
|
|
});
|
|
|
|
function upload_avatar(file_input) {
|
|
var form_data = new FormData();
|
|
|
|
form_data.append('csrfmiddlewaretoken', csrf_token);
|
|
jQuery.each(file_input[0].files, function (i, file) {
|
|
form_data.append('file-'+i, file);
|
|
});
|
|
|
|
var spinner = $("#upload_avatar_spinner").expectOne();
|
|
util.make_loading_indicator(spinner, {text: 'Uploading avatar.'});
|
|
|
|
channel.post({
|
|
url: '/json/set_avatar',
|
|
data: form_data,
|
|
cache: false,
|
|
processData: false,
|
|
contentType: false,
|
|
success: function (data) {
|
|
util.destroy_loading_indicator($("#upload_avatar_spinner"));
|
|
var url = data.avatar_url + '&stamp=' + exports.avatar_stamp;
|
|
$("#user-settings-avatar").expectOne().attr("src", url);
|
|
exports.avatar_stamp += 1;
|
|
}
|
|
});
|
|
|
|
}
|
|
|
|
avatar.build_user_avatar_widget(upload_avatar);
|
|
|
|
if (page_params.domain === "users.customer4.invalid" ||
|
|
page_params.name_changes_disabled) {
|
|
// At the request of the facilitators, CUSTOMER4 users
|
|
// can't change their names, so don't show that as a settings
|
|
// option. This is also disabled through the JSON UI. Once we
|
|
// have the infrastructure for administrative policies, we can
|
|
// handle this more gracefully.
|
|
//
|
|
// Additionally, if this install has disabled name changes, hide the
|
|
// container
|
|
$("#name_change_container").hide();
|
|
}
|
|
|
|
channel.get({
|
|
url: '/json/bots',
|
|
idempotent: true,
|
|
success: function (data) {
|
|
$('#bot_table_error').hide();
|
|
|
|
_.each(data.bots, function (elem) {
|
|
add_bot_row(elem.full_name, elem.username, elem.avatar_url, elem.api_key);
|
|
});
|
|
},
|
|
error: function (xhr, error_type, xhn) {
|
|
$('#bot_table_error').text("Could not fetch bots list").show();
|
|
}
|
|
});
|
|
|
|
$.validator.addMethod("bot_local_part",
|
|
function (value, element) {
|
|
return is_local_part.call(this, value + "-bot", element);
|
|
},
|
|
"Please only use characters that are valid in an email address");
|
|
|
|
|
|
var create_avatar_widget = avatar.build_bot_create_widget();
|
|
|
|
$('#create_bot_form').validate({
|
|
errorClass: 'text-error',
|
|
success: function () {
|
|
$('#bot_table_error').hide();
|
|
},
|
|
submitHandler: function () {
|
|
var full_name = $('#create_bot_name').val();
|
|
var short_name = $('#create_bot_short_name').val();
|
|
var default_sending_stream = $('#create_bot_default_sending_stream').val();
|
|
var default_events_register_stream = $('#create_bot_default_events_register_stream').val();
|
|
var formData = new FormData();
|
|
formData.append('csrfmiddlewaretoken', csrf_token);
|
|
formData.append('full_name', full_name);
|
|
formData.append('short_name', short_name);
|
|
|
|
if (feature_flags.new_bot_ui) {
|
|
formData.append('default_sending_stream', default_sending_stream);
|
|
formData.append('default_events_register_stream', default_events_register_stream);
|
|
}
|
|
|
|
jQuery.each($('#bot_avatar_file_input')[0].files, function (i, file) {
|
|
formData.append('file-'+i, file);
|
|
});
|
|
$('#create_bot_button').val('Adding bot...').prop('disabled', true);
|
|
channel.post({
|
|
url: '/json/bots',
|
|
data: formData,
|
|
cache: false,
|
|
processData: false,
|
|
contentType: false,
|
|
success: function (data) {
|
|
$('#bot_table_error').hide();
|
|
$('#create_bot_name').val('');
|
|
$('#create_bot_short_name').val('');
|
|
$('#create_bot_button').show();
|
|
create_avatar_widget.clear();
|
|
|
|
add_bot_row(
|
|
full_name,
|
|
short_name + "-bot@" + page_params.domain,
|
|
data.avatar_url,
|
|
data.api_key
|
|
);
|
|
},
|
|
error: function (xhr, error_type, exn) {
|
|
$('#bot_table_error').text(JSON.parse(xhr.responseText).msg).show();
|
|
},
|
|
complete: function (xhr, status) {
|
|
$('#create_bot_button').val('Create bot').prop('disabled', false);
|
|
}
|
|
});
|
|
}
|
|
});
|
|
|
|
$("#bots_list").on("click", "button.delete_bot", function (e) {
|
|
var email = $(e.currentTarget).data('email');
|
|
channel.del({
|
|
url: '/json/bots/' + encodeURIComponent(email),
|
|
success: function () {
|
|
var row = $(e.currentTarget).closest("li");
|
|
row.hide('slow', function () { row.remove(); });
|
|
},
|
|
error: function (xhr) {
|
|
$('#bot_delete_error').text(JSON.parse(xhr.responseText).msg).show();
|
|
}
|
|
});
|
|
});
|
|
|
|
$("#bots_list").on("click", "button.regenerate_bot_api_key", function (e) {
|
|
var email = $(e.currentTarget).data('email');
|
|
channel.post({
|
|
url: '/json/bots/' + encodeURIComponent(email) + '/api_key/regenerate',
|
|
idempotent: true,
|
|
success: function (data) {
|
|
var row = $(e.currentTarget).closest("li");
|
|
row.find(".api_key").find(".value").text(data.api_key);
|
|
row.find("api_key_error").hide();
|
|
},
|
|
error: function (xhr) {
|
|
var row = $(e.currentTarget).closest("li");
|
|
row.find(".api_key_error").text(JSON.parse(xhr.responseText).msg).show();
|
|
}
|
|
});
|
|
});
|
|
|
|
var image_version = 0;
|
|
|
|
$("#bots_list").on("click", "button.open_edit_bot_form", function (e) {
|
|
var li = $(e.currentTarget).closest('li');
|
|
var edit_div = li.find('div.edit_bot');
|
|
var form = li.find('.edit_bot_form');
|
|
var image = li.find(".image");
|
|
var bot_info = li.find(".bot_info");
|
|
var reset_edit_bot = li.find(".reset_edit_bot");
|
|
|
|
var old_full_name = bot_info.find(".name").text();
|
|
form.find(".edit_bot_name").attr('value', old_full_name);
|
|
|
|
image.hide();
|
|
bot_info.hide();
|
|
edit_div.show();
|
|
|
|
var avatar_widget = avatar.build_bot_edit_widget(li);
|
|
|
|
function show_row_again() {
|
|
image.show();
|
|
bot_info.show();
|
|
edit_div.hide();
|
|
avatar_widget.close();
|
|
}
|
|
|
|
reset_edit_bot.click(function (event) {
|
|
show_row_again();
|
|
$(this).off(event);
|
|
});
|
|
|
|
var errors = form.find('.bot_edit_errors');
|
|
|
|
form.validate({
|
|
errorClass: 'text-error',
|
|
success: function (label) {
|
|
errors.hide();
|
|
},
|
|
submitHandler: function () {
|
|
var email = form.data('email');
|
|
var full_name = form.find('.edit_bot_name').val();
|
|
var file_input = li.find('.edit_bot_avatar_file_input');
|
|
var spinner = form.find('.edit_bot_spinner');
|
|
var edit_button = form.find('.edit_bot_button');
|
|
var formData = new FormData();
|
|
formData.append('full_name', full_name);
|
|
formData.append('csrfmiddlewaretoken', csrf_token);
|
|
jQuery.each(file_input[0].files, function (i, file) {
|
|
formData.append('file-'+i, file);
|
|
});
|
|
util.make_loading_indicator(spinner, {text: 'Editing bot'});
|
|
edit_button.hide();
|
|
channel.patch({
|
|
url: '/json/bots/' + encodeURIComponent(email),
|
|
data: formData,
|
|
cache: false,
|
|
processData: false,
|
|
contentType: false,
|
|
success: function (data) {
|
|
util.destroy_loading_indicator(spinner);
|
|
errors.hide();
|
|
edit_button.show();
|
|
show_row_again();
|
|
bot_info.find('.name').text(full_name);
|
|
if (data.avatar_url) {
|
|
// Note that the avatar_url won't actually change on the back end
|
|
// when the user had a previous uploaded avatar. Only the content
|
|
// changes, so we version it to get an uncached copy.
|
|
image_version += 1;
|
|
image.find('img').attr('src', data.avatar_url+'&v='+image_version.toString());
|
|
}
|
|
},
|
|
error: function (xhr, error_type, exn) {
|
|
util.destroy_loading_indicator(spinner);
|
|
edit_button.show();
|
|
errors.text(JSON.parse(xhr.responseText).msg).show();
|
|
}
|
|
});
|
|
}
|
|
});
|
|
|
|
|
|
});
|
|
|
|
$("#show_api_key_box").on("click", "button.regenerate_api_key", function (e) {
|
|
channel.post({
|
|
url: '/json/users/me/api_key/regenerate',
|
|
idempotent: true,
|
|
success: function (data) {
|
|
$('#api_key_value').text(data.api_key);
|
|
},
|
|
error: function (xhr) {
|
|
$('#user_api_key_error').text(JSON.parse(xhr.responseText).msg).show();
|
|
}
|
|
});
|
|
});
|
|
|
|
$("#ui-settings").on("click", "input[name='change_settings']", function (e) {
|
|
var labs_updates = {};
|
|
_.each(["autoscroll_forever", "default_desktop_notifications"],
|
|
function (setting) {
|
|
labs_updates[setting] = $("#" + setting).is(":checked");
|
|
});
|
|
|
|
channel.post({
|
|
url: '/json/ui_settings/change',
|
|
data: labs_updates,
|
|
success: function (resp, statusText, xhr, form) {
|
|
var message = "Updated Zulip Labs settings!";
|
|
var result = $.parseJSON(xhr.responseText);
|
|
var ui_settings_status = feature_flags.show_autoscroll_forever_option &&
|
|
$('#ui-settings-status').expectOne();
|
|
|
|
if (result.autoscroll_forever !== undefined) {
|
|
page_params.autoscroll_forever = result.autoscroll_forever;
|
|
ui.resize_page_components();
|
|
}
|
|
|
|
ui_settings_status.removeClass(status_classes)
|
|
.addClass('alert-success')
|
|
.text(message).stop(true).fadeTo(0,1);
|
|
},
|
|
error: function (xhr, error_type, xhn) {
|
|
var response = "Error changing settings";
|
|
var ui_settings_status = feature_flags.show_autoscroll_forever_option &&
|
|
$('#ui-settings-status').expectOne();
|
|
|
|
if (xhr.status.toString().charAt(0) === "4") {
|
|
// Only display the error response for 4XX, where we've crafted
|
|
// a nice response.
|
|
response += ": " + $.parseJSON(xhr.responseText).msg;
|
|
}
|
|
|
|
ui_settings_status.removeClass(status_classes)
|
|
.addClass('alert-error')
|
|
.text(response).stop(true).fadeTo(0,1);
|
|
}
|
|
});
|
|
});
|
|
};
|
|
|
|
return exports;
|
|
}());
|