org settings: Handle floating point durations better for time limits.

Fixes: #9253.
This commit is contained in:
Shubham Dhama 2018-04-28 19:05:14 +05:30 committed by Tim Abbott
parent fb0a421b8c
commit ccd5581bcd
3 changed files with 52 additions and 8 deletions

View File

@ -466,6 +466,34 @@ function test_sync_realm_settings() {
assert.equal(waiting_period_input_parent.visible(), false);
}
function test_parse_time_limit() {
const elem = $('#id_realm_message_content_edit_limit_minutes');
const test_function = (value, expected_value = value) => {
elem.val(value);
global.page_params.realm_message_content_edit_limit_seconds =
settings_org.parse_time_limit(elem);
assert.equal(expected_value,
settings_org.get_realm_time_limits_in_minutes('realm_message_content_edit_limit_seconds'));
};
test_function('0.01', '0');
test_function('0.1');
test_function('0.122', '0.1');
test_function('0.155', '0.2');
test_function('0.150', '0.1');
test_function('0.5');
test_function('1');
test_function('1.1');
test_function('10.5');
test_function('50.3');
test_function('100');
test_function('100.1');
test_function('127.79', '127.8');
test_function('201.1');
test_function('501.15', '501.1');
test_function('501.34', '501.3');
}
(function test_set_up() {
var callbacks = {};
@ -532,6 +560,7 @@ function test_sync_realm_settings() {
test_extract_property_name();
test_change_save_button_state();
test_sync_realm_settings();
test_parse_time_limit();
settings_org.render_notifications_stream_ui = stub_render_notifications_stream_ui;
}());

View File

@ -48,9 +48,9 @@ function _setup_page() {
realm_allow_message_deleting: page_params.realm_allow_message_deleting,
realm_allow_message_editing: page_params.realm_allow_message_editing,
realm_message_content_edit_limit_minutes:
Math.ceil(page_params.realm_message_content_edit_limit_seconds / 60),
settings_org.get_realm_time_limits_in_minutes('realm_message_content_edit_limit_seconds'),
realm_message_content_delete_limit_minutes:
Math.ceil(page_params.realm_message_content_delete_limit_seconds / 60),
settings_org.get_realm_time_limits_in_minutes('realm_message_content_delete_limit_seconds'),
realm_message_retention_days: page_params.realm_message_retention_days,
realm_allow_edit_history: page_params.realm_allow_edit_history,
language_list: page_params.language_list,

View File

@ -94,12 +94,20 @@ function get_subsection_property_types(subsection) {
return;
}
exports.get_realm_time_limits_in_minutes = function (property) {
var val = (page_params[property]/ 60).toFixed(1);
if (parseFloat(val, 10) === parseInt(val, 10)) {
val = parseInt(val, 10);
}
return val.toString();
};
function get_property_value(property_name) {
var value;
if (property_name === 'realm_message_content_edit_limit_minutes') {
return Math.ceil(page_params.realm_message_content_edit_limit_seconds / 60).toString();
return exports.get_realm_time_limits_in_minutes('realm_message_content_edit_limit_seconds');
} else if (property_name === 'realm_message_content_delete_limit_minutes') {
return Math.ceil(page_params.realm_message_content_delete_limit_seconds / 60).toString();
return exports.get_realm_time_limits_in_minutes('realm_message_content_delete_limit_seconds');
} else if (property_name === 'realm_create_stream_permission') {
if (page_params.realm_create_stream_by_admins_only) {
return "by_admins_only";
@ -596,6 +604,11 @@ function _set_up() {
});
};
function parse_time_limit(elem) {
return Math.floor(parseFloat(elem.val(), 10).toFixed(1) * 60);
}
exports.parse_time_limit = parse_time_limit;
function get_complete_data_for_subsection(subsection) {
var opts = {};
if (subsection === 'msg_editing') {
@ -604,8 +617,9 @@ function _set_up() {
if (edit_limit_setting_value === 'never') {
opts.data.allow_message_editing = false;
} else if (edit_limit_setting_value === 'custom_limit') {
opts.data.allow_message_editing = true;
opts.data.message_content_edit_limit_seconds = parseInt($("#id_realm_message_content_edit_limit_minutes").val(), 10) * 60;
opts.data.message_content_edit_limit_seconds = parse_time_limit($('#id_realm_message_content_edit_limit_minutes'));
// Disable editing if the parsed time limit is 0 seconds
opts.data.allow_message_editing = !!opts.data.message_content_edit_limit_seconds;
} else {
opts.data.allow_message_editing = true;
opts.data.message_content_edit_limit_seconds =
@ -615,8 +629,9 @@ function _set_up() {
if (delete_limit_setting_value === 'never') {
opts.data.allow_message_deleting = false;
} else if (delete_limit_setting_value === 'custom_limit') {
opts.data.allow_message_deleting = true;
opts.data.message_content_delete_limit_seconds = parseInt($("#id_realm_message_content_delete_limit_minutes").val(), 10) * 60;
opts.data.message_content_delete_limit_seconds = parse_time_limit($('#id_realm_message_content_delete_limit_minutes'));
// Disable deleting if the parsed time limit is 0 seconds
opts.data.allow_message_deleting = !!opts.data.message_content_delete_limit_seconds;
} else {
opts.data.allow_message_deleting = true;
opts.data.message_content_delete_limit_seconds =