stream_edit: Send values of changed settings only to backend.

This commit changes change_stream_privacy function to only
send the values of changed settings to backend.
We also avoid sending PATCH request if none of the settings
in stream privacy modal are changed.

This change also fixes the bug in changing stream permissions
for realms with limited plans.

Fixes #16024.
This commit is contained in:
sahil839 2020-08-04 20:10:46 +05:30 committed by Tim Abbott
parent 15752067dc
commit 004b6f2e62
1 changed files with 43 additions and 18 deletions

View File

@ -459,11 +459,22 @@ exports.set_stream_property = function (sub, property, value, status_element) {
exports.bulk_set_stream_property([sub_data], status_element); exports.bulk_set_stream_property([sub_data], status_element);
}; };
function get_message_retention_days_from_sub(sub) {
if (sub.message_retention_days === null) {
return "realm_default";
}
if (sub.message_retention_days === -1) {
return "forever";
}
return sub.message_retention_days;
}
function change_stream_privacy(e) { function change_stream_privacy(e) {
e.stopPropagation(); e.stopPropagation();
const stream_id = $(e.target).data("stream-id"); const stream_id = $(e.target).data("stream-id");
const sub = stream_data.get_sub_by_id(stream_id); const sub = stream_data.get_sub_by_id(stream_id);
const data = {};
const privacy_setting = $("#stream_privacy_modal input[name=privacy]:checked").val(); const privacy_setting = $("#stream_privacy_modal input[name=privacy]:checked").val();
const stream_post_policy = parseInt( const stream_post_policy = parseInt(
@ -471,6 +482,10 @@ function change_stream_privacy(e) {
10, 10,
); );
if (sub.stream_post_policy !== stream_post_policy) {
data.stream_post_policy = JSON.stringify(stream_post_policy);
}
let invite_only; let invite_only;
let history_public_to_subscribers; let history_public_to_subscribers;
@ -485,16 +500,14 @@ function change_stream_privacy(e) {
history_public_to_subscribers = true; history_public_to_subscribers = true;
} }
$(".stream_change_property_info").hide(); if (
const data = { sub.invite_only !== invite_only ||
stream_name: sub.name, sub.history_public_to_subscribers !== history_public_to_subscribers
// toggle the privacy setting ) {
is_private: JSON.stringify(invite_only), data.is_private = JSON.stringify(invite_only);
stream_post_policy: JSON.stringify(stream_post_policy), data.history_public_to_subscribers = JSON.stringify(history_public_to_subscribers);
history_public_to_subscribers: JSON.stringify(history_public_to_subscribers), }
};
if (page_params.is_owner) {
let message_retention_days = $( let message_retention_days = $(
"#stream_privacy_modal select[name=stream_message_retention_setting]", "#stream_privacy_modal select[name=stream_message_retention_setting]",
).val(); ).val();
@ -504,9 +517,21 @@ function change_stream_privacy(e) {
10, 10,
); );
} }
const message_retention_days_from_sub = get_message_retention_days_from_sub(sub);
if (message_retention_days_from_sub !== message_retention_days) {
data.message_retention_days = JSON.stringify(message_retention_days); data.message_retention_days = JSON.stringify(message_retention_days);
} }
$(".stream_change_property_info").hide();
if (Object.keys(data).length === 0) {
overlays.close_modal("#stream_privacy_modal");
$("#stream_privacy_modal").remove();
return;
}
channel.patch({ channel.patch({
url: "/json/streams/" + stream_id, url: "/json/streams/" + stream_id,
data, data,