2019-11-02 00:06:25 +01:00
|
|
|
const render_settings_deactivation_stream_modal = require("../templates/settings/deactivation_stream_modal.hbs");
|
|
|
|
const render_stream_member_list_entry = require('../templates/stream_member_list_entry.hbs');
|
|
|
|
const render_subscription_settings = require('../templates/subscription_settings.hbs');
|
|
|
|
const render_subscription_stream_privacy_modal = require("../templates/subscription_stream_privacy_modal.hbs");
|
2019-07-09 21:24:00 +02:00
|
|
|
|
2017-10-03 00:15:42 +02:00
|
|
|
function setup_subscriptions_stream_hash(sub) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const hash = hash_util.stream_edit_uri(sub);
|
2018-12-01 20:15:50 +01:00
|
|
|
hashchange.update_browser_history(hash);
|
2017-04-24 04:11:25 +02:00
|
|
|
}
|
|
|
|
|
2019-04-06 12:55:16 +02:00
|
|
|
exports.setup_subscriptions_tab_hash = function (tab_key_value) {
|
|
|
|
if (tab_key_value === "all-streams") {
|
|
|
|
hashchange.update_browser_history('#streams/all');
|
|
|
|
} else if (tab_key_value === "subscribed") {
|
|
|
|
hashchange.update_browser_history('#streams/subscribed');
|
|
|
|
} else {
|
|
|
|
blueslip.debug("Unknown tab_key_value: " + tab_key_value);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-05-02 19:10:10 +02:00
|
|
|
exports.settings_for_sub = function (sub) {
|
2020-01-16 22:27:27 +01:00
|
|
|
return $("#subscription_overlay .subscription_settings[data-stream-id='" + sub.stream_id + "']");
|
2019-05-02 19:10:10 +02:00
|
|
|
};
|
2017-04-24 04:11:25 +02:00
|
|
|
|
2018-04-06 06:48:46 +02:00
|
|
|
exports.is_sub_settings_active = function (sub) {
|
2018-04-10 15:49:03 +02:00
|
|
|
// This function return whether the provided given sub object is
|
|
|
|
// currently being viewed/edited in the stream edit UI. This is
|
|
|
|
// used to determine whether we need to rerender the stream edit
|
|
|
|
// UI when a sub object is modified by an event.
|
2019-11-02 00:06:25 +01:00
|
|
|
const active_stream = subs.active_stream();
|
2018-04-06 06:48:46 +02:00
|
|
|
if (active_stream !== undefined && active_stream.id === sub.stream_id) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
|
2019-05-03 07:26:56 +02:00
|
|
|
exports.get_email_of_subscribers = function (subscribers) {
|
2019-12-27 13:16:22 +01:00
|
|
|
return subscribers.map(function (user_id) {
|
2020-02-05 14:30:59 +01:00
|
|
|
return people.get_by_user_id(user_id).email;
|
2017-06-17 22:29:01 +02:00
|
|
|
});
|
2017-10-11 23:45:03 +02:00
|
|
|
};
|
2017-06-17 22:29:11 +02:00
|
|
|
|
2018-12-01 21:53:09 +01:00
|
|
|
function clear_edit_panel() {
|
2017-04-24 04:11:25 +02:00
|
|
|
$(".display-type #add_new_stream_title").hide();
|
|
|
|
$(".display-type #stream_settings_title, .right .settings").show();
|
|
|
|
$(".stream-row.active").removeClass("active");
|
2018-12-01 21:53:09 +01:00
|
|
|
}
|
2017-04-24 04:11:25 +02:00
|
|
|
|
2019-04-03 07:14:10 +02:00
|
|
|
function get_stream_id(target) {
|
|
|
|
if (target.constructor !== jQuery) {
|
|
|
|
target = $(target);
|
|
|
|
}
|
2019-12-30 12:51:16 +01:00
|
|
|
const row = target.closest(".stream-row, .subscription_settings");
|
|
|
|
return parseInt(row.attr("data-stream-id"), 10);
|
2019-04-03 07:14:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function get_sub_for_target(target) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const stream_id = get_stream_id(target);
|
2019-04-03 07:14:10 +02:00
|
|
|
if (!stream_id) {
|
|
|
|
blueslip.error('Cannot find stream id for target');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const sub = stream_data.get_sub_by_id(stream_id);
|
2019-04-03 07:14:10 +02:00
|
|
|
if (!sub) {
|
|
|
|
blueslip.error('get_sub_for_target() failed id lookup: ' + stream_id);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
return sub;
|
|
|
|
}
|
|
|
|
|
2018-12-01 21:53:09 +01:00
|
|
|
exports.open_edit_panel_for_row = function (stream_row) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const sub = get_sub_for_target(stream_row);
|
2019-04-03 07:14:10 +02:00
|
|
|
|
2018-12-01 21:53:09 +01:00
|
|
|
clear_edit_panel();
|
|
|
|
subs.show_subs_pane.settings();
|
|
|
|
$(stream_row).addClass("active");
|
2019-04-03 07:14:10 +02:00
|
|
|
setup_subscriptions_stream_hash(sub);
|
2019-10-25 09:45:13 +02:00
|
|
|
exports.show_settings_for(stream_row);
|
2018-12-01 21:53:09 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
exports.open_edit_panel_empty = function () {
|
2019-11-02 00:06:25 +01:00
|
|
|
const tab_key = subs.get_active_data().tab.attr("data-tab-key");
|
2018-12-01 21:53:09 +01:00
|
|
|
clear_edit_panel();
|
|
|
|
subs.show_subs_pane.nothing_selected();
|
2019-04-06 13:30:12 +02:00
|
|
|
exports.setup_subscriptions_tab_hash(tab_key);
|
2017-04-24 04:11:25 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
function format_member_list_elem(email) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const person = people.get_by_email(email);
|
2019-07-09 21:24:00 +02:00
|
|
|
return render_stream_member_list_entry({
|
|
|
|
name: person.full_name, email: email,
|
|
|
|
displaying_for_admin: page_params.is_admin,
|
|
|
|
});
|
2017-04-24 04:11:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function get_subscriber_list(sub_row) {
|
2020-01-16 22:27:27 +01:00
|
|
|
const stream_id_str = sub_row.data("stream-id");
|
|
|
|
return $('.subscription_settings[data-stream-id="' + stream_id_str + '"] .subscriber-list');
|
2017-04-24 04:11:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
exports.update_stream_name = function (sub, new_name) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const sub_settings = exports.settings_for_sub(sub);
|
2017-04-24 04:11:25 +02:00
|
|
|
sub_settings.find(".email-address").text(sub.email_address);
|
|
|
|
sub_settings.find(".stream-name-editable").text(new_name);
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.update_stream_description = function (sub) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const stream_settings = exports.settings_for_sub(sub);
|
2017-04-24 04:11:25 +02:00
|
|
|
stream_settings.find('input.description').val(sub.description);
|
|
|
|
stream_settings.find('.stream-description-editable').html(sub.rendered_description);
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.invite_user_to_stream = function (user_email, sub, success, failure) {
|
|
|
|
// TODO: use stream_id when backend supports it
|
2019-11-02 00:06:25 +01:00
|
|
|
const stream_name = sub.name;
|
2017-04-24 04:11:25 +02:00
|
|
|
return channel.post({
|
|
|
|
url: "/json/users/me/subscriptions",
|
|
|
|
data: {subscriptions: JSON.stringify([{name: stream_name}]),
|
|
|
|
principals: JSON.stringify([user_email])},
|
|
|
|
success: success,
|
|
|
|
error: failure,
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.remove_user_from_stream = function (user_email, sub, success, failure) {
|
|
|
|
// TODO: use stream_id when backend supports it
|
2019-11-02 00:06:25 +01:00
|
|
|
const stream_name = sub.name;
|
2017-04-24 04:11:25 +02:00
|
|
|
return channel.del({
|
|
|
|
url: "/json/users/me/subscriptions",
|
|
|
|
data: {subscriptions: JSON.stringify([stream_name]),
|
|
|
|
principals: JSON.stringify([user_email])},
|
|
|
|
success: success,
|
|
|
|
error: failure,
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2018-04-10 16:54:36 +02:00
|
|
|
exports.sort_but_pin_current_user_on_top = function (emails) {
|
|
|
|
if (emails === undefined) {
|
|
|
|
blueslip.error("Undefined emails are passed to function sort_but_pin_current_user_on_top");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// Set current user top of subscription list, if subscribed.
|
|
|
|
if (emails.indexOf(people.my_current_email()) > -1) {
|
|
|
|
emails.splice(emails.indexOf(people.my_current_email()), 1);
|
|
|
|
emails.sort();
|
|
|
|
emails.unshift(people.my_current_email());
|
|
|
|
} else {
|
|
|
|
emails.sort();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-04-24 04:11:25 +02:00
|
|
|
function show_subscription_settings(sub_row) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const stream_id = sub_row.data("stream-id");
|
|
|
|
const sub = stream_data.get_sub_by_id(stream_id);
|
|
|
|
const sub_settings = exports.settings_for_sub(sub);
|
2017-04-24 04:11:25 +02:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const colorpicker = sub_settings.find('.colorpicker');
|
|
|
|
const color = stream_data.get_color(sub.name);
|
2017-04-24 04:11:25 +02:00
|
|
|
stream_color.set_colorpicker_color(colorpicker, color);
|
2019-05-03 07:59:31 +02:00
|
|
|
stream_ui_updates.update_add_subscriptions_elements(sub);
|
2017-04-24 04:11:25 +02:00
|
|
|
|
2018-03-11 15:05:59 +01:00
|
|
|
if (!sub.render_subscribers) {
|
2017-04-24 04:11:25 +02:00
|
|
|
return;
|
|
|
|
}
|
2019-05-06 14:35:57 +02:00
|
|
|
if (!sub.should_display_subscription_button) {
|
|
|
|
stream_ui_updates.initialize_cant_subscribe_popover(sub);
|
|
|
|
}
|
2017-04-24 04:11:25 +02:00
|
|
|
// fetch subscriber list from memory.
|
2019-11-02 00:06:25 +01:00
|
|
|
const list = get_subscriber_list(sub_settings);
|
2017-04-24 04:11:25 +02:00
|
|
|
list.empty();
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const emails = exports.get_email_of_subscribers(sub.subscribers);
|
2018-04-10 16:54:36 +02:00
|
|
|
exports.sort_but_pin_current_user_on_top(emails);
|
2017-04-24 04:11:25 +02:00
|
|
|
|
2018-06-20 18:32:39 +02:00
|
|
|
list_render.create(list, emails, {
|
2017-04-17 23:21:37 +02:00
|
|
|
name: "stream_subscribers/" + stream_id,
|
|
|
|
modifier: function (item) {
|
|
|
|
return format_member_list_elem(item);
|
|
|
|
},
|
|
|
|
filter: {
|
|
|
|
element: $("[data-stream-id='" + stream_id + "'] .search"),
|
2019-12-30 17:44:24 +01:00
|
|
|
predicate: function (item, value) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const person = people.get_by_email(item);
|
2017-04-17 23:21:37 +02:00
|
|
|
|
|
|
|
if (person) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const email = person.email.toLocaleLowerCase();
|
|
|
|
const full_name = person.full_name.toLowerCase();
|
2017-04-17 23:21:37 +02:00
|
|
|
|
2018-06-06 18:19:09 +02:00
|
|
|
return email.indexOf(value) > -1 || full_name.indexOf(value) > -1;
|
2017-04-17 23:21:37 +02:00
|
|
|
}
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}).init();
|
2017-04-24 04:11:25 +02:00
|
|
|
|
|
|
|
sub_settings.find('input[name="principal"]').typeahead({
|
|
|
|
source: people.get_realm_persons, // This is a function.
|
|
|
|
items: 5,
|
|
|
|
highlighter: function (item) {
|
2017-06-12 19:15:57 +02:00
|
|
|
return typeahead_helper.render_person(item);
|
2017-04-24 04:11:25 +02:00
|
|
|
},
|
|
|
|
matcher: function (item) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const query = $.trim(this.query.toLowerCase());
|
2017-04-24 04:11:25 +02:00
|
|
|
if (query === '' || query === item.email) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// Case-insensitive.
|
2019-11-02 00:06:25 +01:00
|
|
|
const item_matches = item.email.toLowerCase().indexOf(query) !== -1 ||
|
2018-06-06 18:50:09 +02:00
|
|
|
item.full_name.toLowerCase().indexOf(query) !== -1;
|
2019-11-02 00:06:25 +01:00
|
|
|
const is_subscribed = stream_data.is_user_subscribed(sub.name, item.user_id);
|
2017-06-20 21:46:43 +02:00
|
|
|
return item_matches && !is_subscribed;
|
2017-04-24 04:11:25 +02:00
|
|
|
},
|
|
|
|
sorter: function (matches) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const current_stream = compose_state.stream_name();
|
2017-04-24 04:11:25 +02:00
|
|
|
return typeahead_helper.sort_recipientbox_typeahead(
|
|
|
|
this.query, matches, current_stream);
|
|
|
|
},
|
|
|
|
updater: function (item) {
|
|
|
|
return item.email;
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-06-14 14:42:48 +02:00
|
|
|
exports.is_notification_setting = function (setting_label) {
|
|
|
|
if (setting_label.indexOf("_notifications") > -1) {
|
|
|
|
return true;
|
2019-11-26 02:37:12 +01:00
|
|
|
} else if (setting_label.indexOf("_notify") > -1) {
|
|
|
|
return true;
|
2019-06-14 14:42:48 +02:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const settings_labels = {
|
2019-06-14 14:42:48 +02:00
|
|
|
is_muted: i18n.t("Mute stream"),
|
|
|
|
desktop_notifications: i18n.t("Visual desktop notifications"),
|
|
|
|
audible_notifications: i18n.t("Audible desktop notifications"),
|
|
|
|
push_notifications: i18n.t("Mobile notifications"),
|
|
|
|
email_notifications: i18n.t("Email notifications"),
|
|
|
|
pin_to_top: i18n.t("Pin stream to top of left sidebar"),
|
2019-11-26 02:37:12 +01:00
|
|
|
wildcard_mentions_notify: i18n.t("Notifications for @all/@everyone mentions"),
|
2019-06-14 14:42:48 +02:00
|
|
|
};
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const check_realm_setting = {
|
2019-04-26 15:14:53 +02:00
|
|
|
push_notifications: !page_params.realm_push_notifications_enabled,
|
|
|
|
};
|
|
|
|
|
2019-06-14 14:42:48 +02:00
|
|
|
exports.stream_settings = function (sub) {
|
2020-01-27 13:18:04 +01:00
|
|
|
const settings = Object.keys(settings_labels).map((setting) => {
|
|
|
|
const ret = {
|
|
|
|
name: setting,
|
|
|
|
label: settings_labels[setting],
|
2020-01-27 18:54:44 +01:00
|
|
|
disabled_realm_setting: check_realm_setting[setting],
|
2020-01-27 18:50:49 +01:00
|
|
|
is_disabled: check_realm_setting[setting],
|
2020-01-27 13:18:04 +01:00
|
|
|
is_notification_setting: exports.is_notification_setting(setting),
|
|
|
|
};
|
2019-06-14 14:42:48 +02:00
|
|
|
if (exports.is_notification_setting(setting)) {
|
2020-01-27 18:42:35 +01:00
|
|
|
ret.is_checked = sub[setting + "_display"] && !check_realm_setting[setting];
|
2020-01-27 18:50:49 +01:00
|
|
|
ret.is_disabled = ret.is_disabled || sub.is_muted;
|
2020-01-27 13:18:04 +01:00
|
|
|
return ret;
|
2019-06-14 14:42:48 +02:00
|
|
|
}
|
2020-01-27 18:42:35 +01:00
|
|
|
ret.is_checked = sub[setting] && !check_realm_setting[setting];
|
2020-01-27 13:18:04 +01:00
|
|
|
return ret;
|
2019-06-14 14:42:48 +02:00
|
|
|
});
|
|
|
|
return settings;
|
|
|
|
};
|
|
|
|
|
2017-04-24 04:11:25 +02:00
|
|
|
exports.show_settings_for = function (node) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const stream_id = get_stream_id(node);
|
|
|
|
const sub = stream_data.get_sub_by_id(stream_id);
|
2017-04-24 04:11:25 +02:00
|
|
|
|
2017-05-10 20:02:21 +02:00
|
|
|
stream_data.update_calculated_fields(sub);
|
2019-11-02 00:06:25 +01:00
|
|
|
const html = render_subscription_settings({
|
2019-06-14 14:42:48 +02:00
|
|
|
sub: sub,
|
|
|
|
settings: exports.stream_settings(sub),
|
2020-02-04 21:50:55 +01:00
|
|
|
stream_post_policy_values: stream_data.stream_post_policy_values,
|
2019-06-14 14:42:48 +02:00
|
|
|
});
|
2019-03-01 01:40:05 +01:00
|
|
|
ui.get_content_element($('.subscriptions .right .settings')).html(html);
|
2017-05-10 20:02:21 +02:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const sub_settings = exports.settings_for_sub(sub);
|
2017-04-24 04:11:25 +02:00
|
|
|
|
2017-05-10 20:02:21 +02:00
|
|
|
$(".nothing-selected").hide();
|
2017-07-28 14:07:58 +02:00
|
|
|
|
2017-04-24 04:11:25 +02:00
|
|
|
sub_settings.addClass("show");
|
|
|
|
|
2017-05-10 20:02:21 +02:00
|
|
|
show_subscription_settings(sub_settings);
|
2017-04-24 04:11:25 +02:00
|
|
|
};
|
|
|
|
|
2019-05-21 10:46:42 +02:00
|
|
|
function stream_is_muted_clicked(e) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const sub = get_sub_for_target(e.target);
|
2017-04-24 04:11:25 +02:00
|
|
|
if (!sub) {
|
2019-05-21 10:46:42 +02:00
|
|
|
blueslip.error('stream_is_muted_clicked() fails');
|
2017-04-24 04:11:25 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const sub_settings = exports.settings_for_sub(sub);
|
|
|
|
const notification_checkboxes = sub_settings.find(".sub_notification_setting");
|
2017-04-24 04:11:25 +02:00
|
|
|
|
|
|
|
subs.toggle_home(sub);
|
|
|
|
|
2019-05-15 08:54:25 +02:00
|
|
|
if (!sub.is_muted) {
|
2017-04-24 04:11:25 +02:00
|
|
|
sub_settings.find(".mute-note").addClass("hide-mute-note");
|
|
|
|
notification_checkboxes.removeClass("muted-sub");
|
2017-06-30 00:57:46 +02:00
|
|
|
notification_checkboxes.find("input[type='checkbox']").prop("disabled", false);
|
2017-04-24 04:11:25 +02:00
|
|
|
} else {
|
|
|
|
sub_settings.find(".mute-note").removeClass("hide-mute-note");
|
|
|
|
notification_checkboxes.addClass("muted-sub");
|
|
|
|
notification_checkboxes.find("input[type='checkbox']").attr("disabled", true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-14 15:41:28 +02:00
|
|
|
function stream_setting_clicked(e) {
|
2020-02-04 11:46:36 +01:00
|
|
|
if (e.currentTarget.id === 'sub_is_muted_setting') {
|
|
|
|
return;
|
|
|
|
}
|
2020-02-04 13:36:10 +01:00
|
|
|
|
|
|
|
const checkbox = $(e.currentTarget).find('.sub_setting_control');
|
2019-11-02 00:06:25 +01:00
|
|
|
const sub = get_sub_for_target(e.target);
|
|
|
|
const setting = checkbox.attr('name');
|
2019-06-14 15:41:28 +02:00
|
|
|
if (!sub) {
|
|
|
|
blueslip.error('undefined sub in stream_setting_clicked()');
|
|
|
|
return;
|
|
|
|
}
|
2019-06-24 10:16:02 +02:00
|
|
|
if (checkbox.prop('disabled')) {
|
|
|
|
return false;
|
|
|
|
}
|
2019-06-14 15:41:28 +02:00
|
|
|
if (exports.is_notification_setting(setting) && sub[setting] === null) {
|
2019-11-26 02:37:12 +01:00
|
|
|
if (setting === 'wildcard_mentions_notify') {
|
|
|
|
sub[setting] = page_params[setting];
|
|
|
|
} else {
|
|
|
|
sub[setting] = page_params["enable_stream_" + setting];
|
|
|
|
}
|
2019-06-14 15:41:28 +02:00
|
|
|
}
|
|
|
|
exports.set_stream_property(sub, setting, !sub[setting]);
|
|
|
|
|
|
|
|
// A hack. Don't change the state of the checkbox if we
|
|
|
|
// clicked on the checkbox itself.
|
|
|
|
if (checkbox[0] !== e.target) {
|
|
|
|
checkbox.prop("checked", !checkbox.prop("checked"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-14 14:53:10 +01:00
|
|
|
exports.bulk_set_stream_property = function (sub_data) {
|
2017-04-24 04:11:25 +02:00
|
|
|
return channel.post({
|
2017-05-09 07:01:42 +02:00
|
|
|
url: '/json/users/me/subscriptions/properties',
|
2018-02-14 14:53:10 +01:00
|
|
|
data: {subscription_data: JSON.stringify(sub_data)},
|
2018-06-04 21:13:07 +02:00
|
|
|
timeout: 10 * 1000,
|
2017-04-24 04:11:25 +02:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2018-02-14 14:53:10 +01:00
|
|
|
exports.set_stream_property = function (sub, property, value) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const sub_data = {stream_id: sub.stream_id, property: property, value: value};
|
2018-02-14 14:53:10 +01:00
|
|
|
exports.bulk_set_stream_property([sub_data]);
|
|
|
|
};
|
|
|
|
|
2017-04-24 04:11:25 +02:00
|
|
|
function change_stream_privacy(e) {
|
|
|
|
e.stopPropagation();
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const stream_id = $(e.target).data("stream-id");
|
|
|
|
const sub = stream_data.get_sub_by_id(stream_id);
|
2017-04-24 04:11:25 +02:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const privacy_setting = $('#stream_privacy_modal input[name=privacy]:checked').val();
|
2020-02-04 21:50:55 +01:00
|
|
|
const stream_post_policy = parseInt($('#stream_privacy_modal input[name=stream-post-policy]:checked').val(), 10);
|
2018-05-03 18:52:39 +02:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
let invite_only;
|
|
|
|
let history_public_to_subscribers;
|
2018-05-03 18:52:39 +02:00
|
|
|
|
|
|
|
if (privacy_setting === 'invite-only') {
|
|
|
|
invite_only = true;
|
|
|
|
history_public_to_subscribers = false;
|
|
|
|
} else if (privacy_setting === 'invite-only-public-history') {
|
|
|
|
invite_only = true;
|
|
|
|
history_public_to_subscribers = true;
|
|
|
|
} else {
|
|
|
|
invite_only = false;
|
|
|
|
history_public_to_subscribers = true;
|
|
|
|
}
|
|
|
|
|
2018-01-03 14:24:49 +01:00
|
|
|
$(".stream_change_property_info").hide();
|
2019-11-02 00:06:25 +01:00
|
|
|
const data = {
|
2017-04-24 04:11:25 +02:00
|
|
|
stream_name: sub.name,
|
|
|
|
// toggle the privacy setting
|
2018-05-03 18:52:39 +02:00
|
|
|
is_private: JSON.stringify(invite_only),
|
2020-02-04 21:50:55 +01:00
|
|
|
stream_post_policy: JSON.stringify(stream_post_policy),
|
2018-05-03 18:52:39 +02:00
|
|
|
history_public_to_subscribers: JSON.stringify(history_public_to_subscribers),
|
2017-04-24 04:11:25 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
channel.patch({
|
|
|
|
url: "/json/streams/" + stream_id,
|
|
|
|
data: data,
|
|
|
|
success: function () {
|
|
|
|
$("#stream_privacy_modal").remove();
|
2019-05-02 19:43:27 +02:00
|
|
|
// The rest will be done by update stream event we will get.
|
2017-04-24 04:11:25 +02:00
|
|
|
},
|
|
|
|
error: function () {
|
|
|
|
$("#change-stream-privacy-button").text(i18n.t("Try again"));
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
exports.change_stream_name = function (e) {
|
|
|
|
e.preventDefault();
|
2019-11-02 00:06:25 +01:00
|
|
|
const sub_settings = $(e.target).closest('.subscription_settings');
|
|
|
|
const stream_id = $(e.target).closest(".subscription_settings").attr("data-stream-id");
|
|
|
|
const new_name_box = sub_settings.find('.stream-name-editable');
|
|
|
|
const new_name = $.trim(new_name_box.text());
|
2018-01-03 14:24:49 +01:00
|
|
|
$(".stream_change_property_info").hide();
|
2017-04-24 04:11:25 +02:00
|
|
|
|
|
|
|
channel.patch({
|
|
|
|
// Stream names might contain unsafe characters so we must encode it first.
|
|
|
|
url: "/json/streams/" + stream_id,
|
|
|
|
data: {new_name: JSON.stringify(new_name)},
|
|
|
|
success: function () {
|
|
|
|
new_name_box.val('');
|
2018-01-03 14:24:49 +01:00
|
|
|
ui_report.success(i18n.t("The stream has been renamed!"), $(".stream_change_property_info"));
|
2017-04-24 04:11:25 +02:00
|
|
|
},
|
|
|
|
error: function (xhr) {
|
2018-01-04 19:20:30 +01:00
|
|
|
new_name_box.text(stream_data.maybe_get_stream_name(stream_id));
|
2018-04-30 09:55:35 +02:00
|
|
|
ui_report.error(i18n.t("Error"), xhr, $(".stream_change_property_info"));
|
2017-04-24 04:11:25 +02:00
|
|
|
},
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2019-02-07 08:22:14 +01:00
|
|
|
exports.set_raw_description = function (target, destination) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const sub_settings = $(target).closest('.subscription_settings');
|
|
|
|
const sub = get_sub_for_target(sub_settings);
|
2019-02-07 08:22:14 +01:00
|
|
|
if (!sub) {
|
|
|
|
blueslip.error('set_raw_description() fails');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
destination.text(sub.description);
|
|
|
|
};
|
|
|
|
|
2017-04-24 04:11:25 +02:00
|
|
|
exports.change_stream_description = function (e) {
|
|
|
|
e.preventDefault();
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const sub_settings = $(e.target).closest('.subscription_settings');
|
|
|
|
const sub = get_sub_for_target(sub_settings);
|
2017-04-24 04:11:25 +02:00
|
|
|
if (!sub) {
|
|
|
|
blueslip.error('change_stream_description() fails');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const stream_id = sub.stream_id;
|
|
|
|
const description = sub_settings.find('.stream-description-editable').text().trim();
|
2018-01-03 14:24:49 +01:00
|
|
|
$(".stream_change_property_info").hide();
|
2017-04-24 04:11:25 +02:00
|
|
|
|
|
|
|
channel.patch({
|
2018-05-05 07:26:27 +02:00
|
|
|
// Description might contain unsafe characters so we must encode it first.
|
2017-04-24 04:11:25 +02:00
|
|
|
url: '/json/streams/' + stream_id,
|
|
|
|
data: {
|
|
|
|
description: JSON.stringify(description),
|
|
|
|
},
|
|
|
|
success: function () {
|
|
|
|
// The event from the server will update the rest of the UI
|
|
|
|
ui_report.success(i18n.t("The stream description has been updated!"),
|
2018-05-06 21:43:17 +02:00
|
|
|
$(".stream_change_property_info"));
|
2017-04-24 04:11:25 +02:00
|
|
|
},
|
|
|
|
error: function (xhr) {
|
2018-04-30 09:55:35 +02:00
|
|
|
sub_settings.find('.stream-description-editable').html(sub.rendered_description);
|
|
|
|
ui_report.error(i18n.t("Error"), xhr, $(".stream_change_property_info"));
|
2017-04-24 04:11:25 +02:00
|
|
|
},
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2018-04-30 14:08:10 +02:00
|
|
|
exports.delete_stream = function (stream_id, alert_element, stream_row) {
|
|
|
|
channel.del({
|
|
|
|
url: '/json/streams/' + stream_id,
|
|
|
|
error: function (xhr) {
|
|
|
|
ui_report.error(i18n.t("Failed"), xhr, alert_element);
|
|
|
|
},
|
|
|
|
success: function () {
|
|
|
|
stream_row.remove();
|
|
|
|
},
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2018-05-15 22:03:14 +02:00
|
|
|
exports.initialize = function () {
|
2018-09-28 19:23:41 +02:00
|
|
|
$("#main_div").on("click", ".stream_sub_unsub_button", function (e) {
|
2017-04-24 04:11:25 +02:00
|
|
|
e.preventDefault();
|
|
|
|
e.stopPropagation();
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const stream_name = narrow_state.stream();
|
2017-04-24 04:11:25 +02:00
|
|
|
if (stream_name === undefined) {
|
|
|
|
return;
|
|
|
|
}
|
2019-11-02 00:06:25 +01:00
|
|
|
const sub = stream_data.get_sub(stream_name);
|
2017-04-24 04:11:25 +02:00
|
|
|
subs.sub_or_unsub(sub);
|
|
|
|
});
|
|
|
|
|
|
|
|
$("#subscriptions_table").on("click", ".change-stream-privacy", function (e) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const stream_id = get_stream_id(e.target);
|
|
|
|
const stream = stream_data.get_sub_by_id(stream_id);
|
|
|
|
const template_data = {
|
2017-04-24 04:11:25 +02:00
|
|
|
stream_id: stream_id,
|
2018-05-03 18:52:39 +02:00
|
|
|
stream_name: stream.name,
|
2020-02-04 21:50:55 +01:00
|
|
|
stream_post_policy_values: stream_data.stream_post_policy_values,
|
|
|
|
stream_post_policy: stream.stream_post_policy,
|
2018-05-03 18:52:39 +02:00
|
|
|
is_public: !stream.invite_only,
|
|
|
|
is_private: stream.invite_only && !stream.history_public_to_subscribers,
|
2018-06-06 18:19:09 +02:00
|
|
|
is_private_with_public_history: stream.invite_only &&
|
2019-02-27 22:28:19 +01:00
|
|
|
stream.history_public_to_subscribers,
|
|
|
|
is_admin: page_params.is_admin,
|
2017-04-24 04:11:25 +02:00
|
|
|
};
|
2019-11-02 00:06:25 +01:00
|
|
|
const change_privacy_modal = render_subscription_stream_privacy_modal(template_data);
|
2018-05-31 19:20:23 +02:00
|
|
|
$("#stream_privacy_modal").remove();
|
2017-04-24 04:11:25 +02:00
|
|
|
$("#subscriptions_table").append(change_privacy_modal);
|
2018-05-31 19:20:23 +02:00
|
|
|
overlays.open_modal('stream_privacy_modal');
|
2019-06-06 20:45:30 +02:00
|
|
|
e.preventDefault();
|
|
|
|
e.stopPropagation();
|
2017-04-24 04:11:25 +02:00
|
|
|
});
|
|
|
|
|
2018-05-31 19:20:23 +02:00
|
|
|
$("#subscriptions_table").on('click', '#change-stream-privacy-button',
|
|
|
|
change_stream_privacy);
|
|
|
|
|
|
|
|
$("#subscriptions_table").on('click', '.close-privacy-modal', function (e) {
|
|
|
|
// This fixes a weird bug in which, subscription_settings hides
|
|
|
|
// unexpectedly by clicking the cancel button.
|
|
|
|
e.stopPropagation();
|
2017-04-24 04:11:25 +02:00
|
|
|
});
|
|
|
|
|
2019-06-14 10:24:59 +02:00
|
|
|
$("#subscriptions_table").on("click", "#sub_is_muted_setting",
|
2019-05-21 10:46:42 +02:00
|
|
|
stream_is_muted_clicked);
|
2019-06-14 15:41:28 +02:00
|
|
|
|
2020-02-04 11:46:36 +01:00
|
|
|
$("#subscriptions_table").on("click", ".sub_setting_checkbox",
|
|
|
|
stream_setting_clicked);
|
2017-04-24 04:11:25 +02:00
|
|
|
|
|
|
|
$("#subscriptions_table").on("submit", ".subscriber_list_add form", function (e) {
|
|
|
|
e.preventDefault();
|
2019-11-02 00:06:25 +01:00
|
|
|
const settings_row = $(e.target).closest('.subscription_settings');
|
|
|
|
const sub = get_sub_for_target(settings_row);
|
2017-04-24 04:11:25 +02:00
|
|
|
if (!sub) {
|
|
|
|
blueslip.error('.subscriber_list_add form submit fails');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const text_box = settings_row.find('input[name="principal"]');
|
|
|
|
const principal = $.trim(text_box.val());
|
|
|
|
const stream_subscription_info_elem = $('.stream_subscription_info').expectOne();
|
2017-04-24 04:11:25 +02:00
|
|
|
|
|
|
|
function invite_success(data) {
|
|
|
|
text_box.val('');
|
|
|
|
|
|
|
|
if (data.subscribed.hasOwnProperty(principal)) {
|
2017-12-29 21:07:37 +01:00
|
|
|
stream_subscription_info_elem.text(i18n.t("Subscribed successfully!"));
|
2018-03-14 03:29:33 +01:00
|
|
|
// The rest of the work is done via the subscription -> add event we will get
|
2017-04-24 04:11:25 +02:00
|
|
|
} else {
|
2018-01-08 19:54:19 +01:00
|
|
|
stream_subscription_info_elem.text(i18n.t("User already subscribed."));
|
2017-04-24 04:11:25 +02:00
|
|
|
}
|
2017-12-29 21:07:37 +01:00
|
|
|
stream_subscription_info_elem.addClass("text-success")
|
2018-05-06 21:43:17 +02:00
|
|
|
.removeClass("text-error");
|
2017-04-24 04:11:25 +02:00
|
|
|
}
|
|
|
|
|
2018-07-31 02:34:27 +02:00
|
|
|
function invite_failure(xhr) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const error = JSON.parse(xhr.responseText);
|
2018-07-31 02:34:27 +02:00
|
|
|
stream_subscription_info_elem.text(error.msg)
|
2018-05-06 21:43:17 +02:00
|
|
|
.addClass("text-error").removeClass("text-success");
|
2017-04-24 04:11:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
exports.invite_user_to_stream(principal, sub, invite_success, invite_failure);
|
|
|
|
});
|
|
|
|
|
|
|
|
$("#subscriptions_table").on("submit", ".subscriber_list_remove form", function (e) {
|
|
|
|
e.preventDefault();
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const list_entry = $(e.target).closest("tr");
|
|
|
|
const principal = list_entry.children(".subscriber-email").text();
|
|
|
|
const settings_row = $(e.target).closest('.subscription_settings');
|
2017-04-24 04:11:25 +02:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const sub = get_sub_for_target(settings_row);
|
2017-04-24 04:11:25 +02:00
|
|
|
if (!sub) {
|
|
|
|
blueslip.error('.subscriber_list_remove form submit fails');
|
|
|
|
return;
|
|
|
|
}
|
2019-11-02 00:06:25 +01:00
|
|
|
const stream_subscription_info_elem = $('.stream_subscription_info').expectOne();
|
2017-04-24 04:11:25 +02:00
|
|
|
|
|
|
|
function removal_success(data) {
|
|
|
|
if (data.removed.length > 0) {
|
|
|
|
// Remove the user from the subscriber list.
|
|
|
|
list_entry.remove();
|
2017-12-29 21:07:37 +01:00
|
|
|
stream_subscription_info_elem.text(i18n.t("Unsubscribed successfully!"));
|
2018-03-14 03:29:33 +01:00
|
|
|
// The rest of the work is done via the subscription -> remove event we will get
|
2017-04-24 04:11:25 +02:00
|
|
|
} else {
|
2018-01-08 19:54:19 +01:00
|
|
|
stream_subscription_info_elem.text(i18n.t("User is already not subscribed."));
|
2017-04-24 04:11:25 +02:00
|
|
|
}
|
2017-12-29 21:07:37 +01:00
|
|
|
stream_subscription_info_elem.addClass('text-success')
|
2018-05-06 21:43:17 +02:00
|
|
|
.removeClass('text-error');
|
2017-04-24 04:11:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function removal_failure() {
|
2018-01-08 19:54:19 +01:00
|
|
|
stream_subscription_info_elem.text(i18n.t("Error removing user from this stream."))
|
2018-05-06 21:43:17 +02:00
|
|
|
.addClass("text-error").removeClass("text-success");
|
2017-04-24 04:11:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
exports.remove_user_from_stream(principal, sub, removal_success,
|
|
|
|
removal_failure);
|
|
|
|
});
|
|
|
|
|
|
|
|
// This handler isn't part of the normal edit interface; it's the convenient
|
|
|
|
// checkmark in the subscriber list.
|
|
|
|
$("#subscriptions_table").on("click", ".sub_unsub_button", function (e) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const sub = get_sub_for_target(e.target);
|
|
|
|
const stream_row = $(this).parent();
|
2017-04-24 04:11:25 +02:00
|
|
|
subs.sub_or_unsub(sub);
|
|
|
|
if (!sub.subscribed) {
|
2018-12-01 21:53:09 +01:00
|
|
|
exports.open_edit_panel_for_row(stream_row);
|
2017-04-24 04:11:25 +02:00
|
|
|
}
|
2019-04-02 18:37:24 +02:00
|
|
|
stream_ui_updates.update_regular_sub_settings(sub);
|
2017-04-24 04:11:25 +02:00
|
|
|
|
|
|
|
e.preventDefault();
|
|
|
|
e.stopPropagation();
|
|
|
|
});
|
|
|
|
|
2018-02-13 11:47:17 +01:00
|
|
|
$("#subscriptions_table").on("click", ".deactivate", function (e) {
|
|
|
|
e.preventDefault();
|
|
|
|
e.stopPropagation();
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const stream_id = get_stream_id(e.target);
|
2018-02-13 11:47:17 +01:00
|
|
|
if (!stream_id) {
|
|
|
|
ui_report.message(i18n.t("Invalid stream id"), $(".stream_change_property_info"), 'alert-error');
|
|
|
|
return;
|
|
|
|
}
|
2019-11-02 00:06:25 +01:00
|
|
|
const stream_name = stream_data.maybe_get_stream_name(stream_id);
|
|
|
|
const deactivate_stream_modal = render_settings_deactivation_stream_modal({
|
2019-06-12 21:01:38 +02:00
|
|
|
stream_name: stream_name,
|
|
|
|
stream_id: stream_id,
|
|
|
|
});
|
|
|
|
$("#deactivation_stream_modal").remove();
|
|
|
|
$("#subscriptions_table").append(deactivate_stream_modal);
|
2018-02-13 11:47:17 +01:00
|
|
|
overlays.open_modal('deactivation_stream_modal');
|
|
|
|
});
|
|
|
|
|
|
|
|
$("#subscriptions_table").on("click", "#do_deactivate_stream_button", function (e) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const stream_id = $(e.target).data("stream-id");
|
2018-02-13 11:47:17 +01:00
|
|
|
overlays.close_modal('deactivation_stream_modal');
|
|
|
|
$("#deactivation_stream_modal").remove();
|
|
|
|
if (!stream_id) {
|
|
|
|
ui_report.message(i18n.t("Invalid stream id"), $(".stream_change_property_info"), 'alert-error');
|
|
|
|
return;
|
|
|
|
}
|
2019-11-02 00:06:25 +01:00
|
|
|
const row = $(".stream-row.active");
|
2018-04-30 14:08:10 +02:00
|
|
|
exports.delete_stream(stream_id, $(".stream_change_property_info"), row);
|
2018-02-13 11:47:17 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
$("#subscriptions_table").on("hide.bs.modal", "#deactivation_stream_modal", function () {
|
|
|
|
$("#deactivation_stream_modal").remove();
|
|
|
|
});
|
|
|
|
|
2017-04-24 04:11:25 +02:00
|
|
|
$("#subscriptions_table").on("click", ".stream-row", function (e) {
|
|
|
|
if ($(e.target).closest(".check, .subscription_settings").length === 0) {
|
2018-12-01 21:53:09 +01:00
|
|
|
exports.open_edit_panel_for_row(this);
|
2017-04-24 04:11:25 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2020-02-04 12:09:57 +01:00
|
|
|
$(document).on('peer_subscribe.zulip peer_unsubscribe.zulip', function (e, data) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const sub = stream_data.get_sub(data.stream_name);
|
2018-04-08 07:33:49 +02:00
|
|
|
subs.rerender_subscriptions_settings(sub);
|
2017-04-24 04:11:25 +02:00
|
|
|
});
|
2018-05-15 22:03:14 +02:00
|
|
|
};
|
2017-04-24 04:11:25 +02:00
|
|
|
|
2019-10-25 09:45:13 +02:00
|
|
|
window.stream_edit = exports;
|