2017-04-24 04:11:25 +02:00
|
|
|
var stream_edit = (function () {
|
|
|
|
|
|
|
|
var exports = {};
|
|
|
|
|
2017-10-03 00:15:42 +02:00
|
|
|
function setup_subscriptions_stream_hash(sub) {
|
|
|
|
var id = sub.stream_id;
|
2017-04-24 04:11:25 +02:00
|
|
|
subs.change_state.prevent_once();
|
2017-10-03 00:15:42 +02:00
|
|
|
|
|
|
|
window.location.hash = "#streams" + "/" + id + "/" + hash_util.encodeHashComponent(sub.name);
|
2017-04-24 04:11:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function settings_for_sub(sub) {
|
|
|
|
var id = parseInt(sub.stream_id, 10);
|
|
|
|
return $("#subscription_overlay .subscription_settings[data-stream-id='" + id + "']");
|
|
|
|
}
|
|
|
|
|
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.
|
2018-04-06 06:48:46 +02:00
|
|
|
var active_stream = subs.active_stream();
|
|
|
|
if (active_stream !== undefined && active_stream.id === sub.stream_id) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
|
2017-06-17 22:29:01 +02:00
|
|
|
function get_email_of_subscribers(subscribers) {
|
|
|
|
var emails = [];
|
|
|
|
subscribers.each(function (o, i) {
|
|
|
|
var email = people.get_person_from_user_id(i).email;
|
|
|
|
emails.push(email);
|
|
|
|
});
|
|
|
|
return emails;
|
|
|
|
}
|
|
|
|
|
2017-10-11 23:45:03 +02:00
|
|
|
exports.rerender_subscribers_list = function (sub) {
|
2018-02-14 17:59:01 +01:00
|
|
|
if (!sub.can_access_subscribers) {
|
2018-03-11 15:05:59 +01:00
|
|
|
$(".subscriber_list_settings_container").hide();
|
2018-02-09 20:05:56 +01:00
|
|
|
} else {
|
|
|
|
var emails = get_email_of_subscribers(sub.subscribers);
|
|
|
|
var subscribers_list = list_render.get("stream_subscribers/" + sub.stream_id);
|
|
|
|
|
|
|
|
// Changing the data clears the rendered list and the list needs to be re-rendered.
|
|
|
|
// Perform re-rendering only when the stream settings form of the corresponding
|
|
|
|
// stream is open.
|
|
|
|
if (subscribers_list) {
|
2018-04-10 16:54:36 +02:00
|
|
|
exports.sort_but_pin_current_user_on_top(emails);
|
2018-02-09 20:05:56 +01:00
|
|
|
subscribers_list.data(emails);
|
|
|
|
subscribers_list.render();
|
|
|
|
ui.update_scrollbar($(".subscriber_list_container"));
|
|
|
|
}
|
2018-03-11 15:05:59 +01:00
|
|
|
$(".subscriber_list_settings_container").show();
|
2017-06-27 22:02:11 +02:00
|
|
|
}
|
2017-10-11 23:45:03 +02:00
|
|
|
};
|
2017-06-17 22:29:11 +02:00
|
|
|
|
2017-10-03 23:33:15 +02:00
|
|
|
exports.hide_sub_settings = function (sub) {
|
2017-10-03 00:16:36 +02:00
|
|
|
var $settings = $(".subscription_settings[data-stream-id='" + sub.stream_id + "']");
|
|
|
|
$settings.find(".regular_subscription_settings").removeClass('in');
|
2018-04-07 06:05:52 +02:00
|
|
|
// Clear email address widget
|
|
|
|
$settings.find(".email-address").html("");
|
2018-08-04 23:12:13 +02:00
|
|
|
if (!sub.can_change_stream_permissions) {
|
|
|
|
$settings.find(".change-stream-privacy").hide();
|
|
|
|
}
|
2017-04-24 04:11:25 +02:00
|
|
|
};
|
|
|
|
|
2017-10-03 23:33:15 +02:00
|
|
|
exports.show_sub_settings = function (sub) {
|
2018-04-08 07:06:42 +02:00
|
|
|
if (!exports.is_sub_settings_active(sub)) {
|
|
|
|
return;
|
|
|
|
}
|
2017-10-03 00:16:36 +02:00
|
|
|
var $settings = $(".subscription_settings[data-stream-id='" + sub.stream_id + "']");
|
2018-04-05 19:58:27 +02:00
|
|
|
if ($settings.find(".email-address").val().length === 0) {
|
|
|
|
// Rerender stream email address, if not.
|
2018-03-22 22:04:24 +01:00
|
|
|
$settings.find(".email-address").text(sub.email_address);
|
2018-04-05 19:58:27 +02:00
|
|
|
$settings.find(".stream-email-box").show();
|
|
|
|
}
|
2017-10-03 00:16:36 +02:00
|
|
|
$settings.find(".regular_subscription_settings").addClass('in');
|
2017-04-24 04:11:25 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
exports.show_stream_row = function (node, show_settings) {
|
|
|
|
$(".display-type #add_new_stream_title").hide();
|
|
|
|
$(".display-type #stream_settings_title, .right .settings").show();
|
|
|
|
$(".stream-row.active").removeClass("active");
|
|
|
|
if (show_settings) {
|
|
|
|
subs.show_subs_pane.settings();
|
|
|
|
|
|
|
|
$(node).addClass("active");
|
|
|
|
stream_edit.show_settings_for(node);
|
|
|
|
} else {
|
|
|
|
subs.show_subs_pane.nothing_selected();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
function format_member_list_elem(email) {
|
|
|
|
var person = people.get_by_email(email);
|
|
|
|
return templates.render('stream_member_list_entry',
|
|
|
|
{name: person.full_name, email: email,
|
|
|
|
displaying_for_admin: page_params.is_admin});
|
|
|
|
}
|
|
|
|
|
|
|
|
function get_subscriber_list(sub_row) {
|
|
|
|
var id = sub_row.data("stream-id");
|
|
|
|
return $('.subscription_settings[data-stream-id="' + id + '"] .subscriber-list');
|
|
|
|
}
|
|
|
|
|
|
|
|
exports.update_stream_name = function (sub, new_name) {
|
|
|
|
var sub_settings = settings_for_sub(sub);
|
|
|
|
sub_settings.find(".email-address").text(sub.email_address);
|
|
|
|
sub_settings.find(".stream-name-editable").text(new_name);
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.update_stream_description = function (sub) {
|
|
|
|
var stream_settings = settings_for_sub(sub);
|
|
|
|
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
|
|
|
|
var stream_name = sub.name;
|
|
|
|
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
|
|
|
|
var stream_name = sub.name;
|
|
|
|
return channel.del({
|
|
|
|
url: "/json/users/me/subscriptions",
|
|
|
|
data: {subscriptions: JSON.stringify([stream_name]),
|
|
|
|
principals: JSON.stringify([user_email])},
|
|
|
|
success: success,
|
|
|
|
error: failure,
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
function get_stream_id(target) {
|
|
|
|
if (target.constructor !== jQuery) {
|
|
|
|
target = $(target);
|
|
|
|
}
|
|
|
|
return target.closest(".stream-row, .subscription_settings").attr("data-stream-id");
|
|
|
|
}
|
|
|
|
|
|
|
|
function get_sub_for_target(target) {
|
|
|
|
var stream_id = get_stream_id(target);
|
|
|
|
if (!stream_id) {
|
|
|
|
blueslip.error('Cannot find stream id for target');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var sub = stream_data.get_sub_by_id(stream_id);
|
|
|
|
if (!sub) {
|
|
|
|
blueslip.error('get_sub_for_target() failed id lookup: ' + stream_id);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
return sub;
|
|
|
|
}
|
|
|
|
|
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) {
|
|
|
|
var stream_id = sub_row.data("stream-id");
|
|
|
|
var sub = stream_data.get_sub_by_id(stream_id);
|
|
|
|
var sub_settings = settings_for_sub(sub);
|
|
|
|
|
|
|
|
var colorpicker = sub_settings.find('.colorpicker');
|
|
|
|
var color = stream_data.get_color(sub.name);
|
|
|
|
stream_color.set_colorpicker_color(colorpicker, color);
|
|
|
|
|
2018-03-11 15:05:59 +01:00
|
|
|
if (!sub.render_subscribers) {
|
2017-04-24 04:11:25 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
// fetch subscriber list from memory.
|
|
|
|
var list = get_subscriber_list(sub_settings);
|
|
|
|
list.empty();
|
|
|
|
|
2017-06-17 22:29:01 +02:00
|
|
|
var emails = 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"),
|
|
|
|
callback: function (item, value) {
|
|
|
|
var person = people.get_by_email(item);
|
|
|
|
|
|
|
|
if (person) {
|
|
|
|
var email = person.email.toLocaleLowerCase();
|
|
|
|
var full_name = person.full_name.toLowerCase();
|
|
|
|
|
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
|
|
|
|
2017-08-28 16:57:46 +02:00
|
|
|
ui.set_up_scrollbar($(".subscriber_list_container"));
|
|
|
|
|
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) {
|
|
|
|
var query = $.trim(this.query.toLowerCase());
|
|
|
|
if (query === '' || query === item.email) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// Case-insensitive.
|
2018-06-06 18:50:09 +02:00
|
|
|
var item_matches = item.email.toLowerCase().indexOf(query) !== -1 ||
|
|
|
|
item.full_name.toLowerCase().indexOf(query) !== -1;
|
2018-04-06 05:22:07 +02:00
|
|
|
var 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) {
|
|
|
|
var current_stream = compose_state.stream_name();
|
|
|
|
return typeahead_helper.sort_recipientbox_typeahead(
|
|
|
|
this.query, matches, current_stream);
|
|
|
|
},
|
|
|
|
updater: function (item) {
|
|
|
|
return item.email;
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
exports.show_settings_for = function (node) {
|
|
|
|
var stream_id = get_stream_id(node);
|
|
|
|
var sub = stream_data.get_sub_by_id(stream_id);
|
|
|
|
|
2017-05-10 20:02:21 +02:00
|
|
|
stream_data.update_calculated_fields(sub);
|
|
|
|
var html = templates.render('subscription_settings', sub);
|
|
|
|
$('.subscriptions .right .settings').html(html);
|
|
|
|
|
|
|
|
var sub_settings = 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
|
|
|
|
|
|
|
ui.update_scrollbar($("#subscription_overlay .settings"));
|
|
|
|
|
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
|
|
|
};
|
|
|
|
|
|
|
|
function stream_home_view_clicked(e) {
|
|
|
|
var sub = get_sub_for_target(e.target);
|
|
|
|
if (!sub) {
|
|
|
|
blueslip.error('stream_home_view_clicked() fails');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var sub_settings = settings_for_sub(sub);
|
|
|
|
var notification_checkboxes = sub_settings.find(".sub_notification_setting");
|
|
|
|
|
|
|
|
subs.toggle_home(sub);
|
|
|
|
|
|
|
|
if (sub.in_home_view) {
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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) {
|
|
|
|
var sub_data = {stream_id: sub.stream_id, property: property, value: value};
|
|
|
|
exports.bulk_set_stream_property([sub_data]);
|
|
|
|
};
|
|
|
|
|
2017-06-05 23:36:32 +02:00
|
|
|
exports.set_notification_setting_for_all_streams = function (notification_type, new_setting) {
|
2018-02-14 14:53:10 +01:00
|
|
|
var sub_data = [];
|
2017-04-24 04:11:25 +02:00
|
|
|
_.each(stream_data.subscribed_subs(), function (sub) {
|
|
|
|
if (sub[notification_type] !== new_setting) {
|
2018-02-14 14:53:10 +01:00
|
|
|
sub_data.push({
|
|
|
|
stream_id: sub.stream_id,
|
|
|
|
property: notification_type,
|
|
|
|
value: new_setting,
|
|
|
|
});
|
2017-04-24 04:11:25 +02:00
|
|
|
}
|
|
|
|
});
|
2018-02-14 14:53:10 +01:00
|
|
|
exports.bulk_set_stream_property(sub_data);
|
2017-04-24 04:11:25 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
function redraw_privacy_related_stuff(sub_row, sub) {
|
|
|
|
var stream_settings = settings_for_sub(sub);
|
|
|
|
var html;
|
|
|
|
|
2017-05-11 15:39:30 +02:00
|
|
|
stream_data.update_calculated_fields(sub);
|
2017-04-24 04:11:25 +02:00
|
|
|
|
|
|
|
html = templates.render('subscription_setting_icon', sub);
|
|
|
|
sub_row.find('.icon').expectOne().replaceWith($(html));
|
|
|
|
|
|
|
|
html = templates.render('subscription_type', sub);
|
|
|
|
stream_settings.find('.subscription-type-text').expectOne().html(html);
|
|
|
|
|
|
|
|
if (sub.invite_only) {
|
|
|
|
stream_settings.find(".large-icon")
|
|
|
|
.removeClass("hash").addClass("lock")
|
2018-07-03 02:16:53 +02:00
|
|
|
.html("<i class='fa fa-lock' aria-hidden='true'></i>");
|
2017-04-24 04:11:25 +02:00
|
|
|
} else {
|
|
|
|
stream_settings.find(".large-icon")
|
|
|
|
.addClass("hash").removeClass("lock")
|
|
|
|
.html("");
|
|
|
|
}
|
|
|
|
|
2017-06-14 16:33:30 +02:00
|
|
|
stream_list.redraw_stream_privacy(sub);
|
2017-04-24 04:11:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function change_stream_privacy(e) {
|
|
|
|
e.stopPropagation();
|
|
|
|
|
|
|
|
var stream_id = $(e.target).data("stream-id");
|
|
|
|
var sub = stream_data.get_sub_by_id(stream_id);
|
|
|
|
|
2018-05-03 18:52:39 +02:00
|
|
|
var privacy_setting = $('#stream_privacy_modal input[name=privacy]:checked').val();
|
2018-05-30 16:02:13 +02:00
|
|
|
var is_announcement_only = $('#stream_privacy_modal input[name=is-announcement-only]').prop('checked');
|
2018-05-03 18:52:39 +02:00
|
|
|
|
|
|
|
var invite_only;
|
|
|
|
var history_public_to_subscribers;
|
|
|
|
|
|
|
|
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();
|
2017-04-24 04:11:25 +02:00
|
|
|
var data = {
|
|
|
|
stream_name: sub.name,
|
|
|
|
// toggle the privacy setting
|
2018-05-03 18:52:39 +02:00
|
|
|
is_private: JSON.stringify(invite_only),
|
2018-05-30 16:02:13 +02:00
|
|
|
is_announcement_only: JSON.stringify(is_announcement_only),
|
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 () {
|
|
|
|
sub = stream_data.get_sub_by_id(stream_id);
|
|
|
|
var sub_row = $(".stream-row[data-stream-id='" + stream_id + "']");
|
|
|
|
|
|
|
|
// save new privacy settings.
|
2018-05-03 18:52:39 +02:00
|
|
|
sub.invite_only = invite_only;
|
2018-05-30 16:02:13 +02:00
|
|
|
sub.is_announcement_only = is_announcement_only;
|
2018-05-03 18:52:39 +02:00
|
|
|
sub.history_public_to_subscribers = history_public_to_subscribers;
|
2017-04-24 04:11:25 +02:00
|
|
|
|
|
|
|
redraw_privacy_related_stuff(sub_row, sub);
|
|
|
|
$("#stream_privacy_modal").remove();
|
2018-01-06 12:25:03 +01:00
|
|
|
|
|
|
|
// For auto update, without rendering whole template
|
|
|
|
stream_data.update_calculated_fields(sub);
|
2018-05-22 01:14:18 +02:00
|
|
|
if (!sub.can_change_stream_permissions) {
|
2018-01-06 12:25:03 +01:00
|
|
|
$(".change-stream-privacy").hide();
|
|
|
|
}
|
2017-04-24 04:11:25 +02:00
|
|
|
},
|
|
|
|
error: function () {
|
|
|
|
$("#change-stream-privacy-button").text(i18n.t("Try again"));
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function stream_desktop_notifications_clicked(e) {
|
|
|
|
var sub = get_sub_for_target(e.target);
|
2018-06-04 21:09:11 +02:00
|
|
|
sub.desktop_notifications = !sub.desktop_notifications;
|
2017-04-24 04:11:25 +02:00
|
|
|
exports.set_stream_property(sub, 'desktop_notifications', sub.desktop_notifications);
|
|
|
|
}
|
|
|
|
|
|
|
|
function stream_audible_notifications_clicked(e) {
|
|
|
|
var sub = get_sub_for_target(e.target);
|
2018-06-04 21:09:11 +02:00
|
|
|
sub.audible_notifications = !sub.audible_notifications;
|
2017-04-24 04:11:25 +02:00
|
|
|
exports.set_stream_property(sub, 'audible_notifications', sub.audible_notifications);
|
|
|
|
}
|
|
|
|
|
2017-08-17 16:55:32 +02:00
|
|
|
function stream_push_notifications_clicked(e) {
|
|
|
|
var sub = get_sub_for_target(e.target);
|
2018-06-04 21:09:11 +02:00
|
|
|
sub.push_notifications = !sub.push_notifications;
|
2017-08-17 16:55:32 +02:00
|
|
|
exports.set_stream_property(sub, 'push_notifications', sub.push_notifications);
|
|
|
|
}
|
|
|
|
|
2017-11-21 05:58:26 +01:00
|
|
|
function stream_email_notifications_clicked(e) {
|
|
|
|
var sub = get_sub_for_target(e.target);
|
|
|
|
sub.email_notifications = !sub.email_notifications;
|
|
|
|
exports.set_stream_property(sub, 'email_notifications', sub.email_notifications);
|
|
|
|
}
|
|
|
|
|
2017-04-24 04:11:25 +02:00
|
|
|
function stream_pin_clicked(e) {
|
|
|
|
var sub = get_sub_for_target(e.target);
|
|
|
|
if (!sub) {
|
|
|
|
blueslip.error('stream_pin_clicked() fails');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
subs.toggle_pin_to_top_stream(sub);
|
|
|
|
}
|
|
|
|
|
|
|
|
exports.change_stream_name = function (e) {
|
|
|
|
e.preventDefault();
|
|
|
|
var sub_settings = $(e.target).closest('.subscription_settings');
|
|
|
|
var stream_id = $(e.target).closest(".subscription_settings").attr("data-stream-id");
|
|
|
|
var new_name_box = sub_settings.find('.stream-name-editable');
|
|
|
|
var 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"));
|
|
|
|
ui.update_scrollbar($("#subscription_overlay .settings"));
|
2017-04-24 04:11:25 +02:00
|
|
|
},
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.change_stream_description = function (e) {
|
|
|
|
e.preventDefault();
|
|
|
|
|
|
|
|
var sub_settings = $(e.target).closest('.subscription_settings');
|
|
|
|
var sub = get_sub_for_target(sub_settings);
|
|
|
|
if (!sub) {
|
|
|
|
blueslip.error('change_stream_description() fails');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var stream_id = sub.stream_id;
|
|
|
|
var 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"));
|
|
|
|
ui.update_scrollbar($("#subscription_overlay .settings"));
|
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();
|
|
|
|
|
2017-04-25 15:25:31 +02:00
|
|
|
var stream_name = narrow_state.stream();
|
2017-04-24 04:11:25 +02:00
|
|
|
if (stream_name === undefined) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
var sub = stream_data.get_sub(stream_name);
|
|
|
|
subs.sub_or_unsub(sub);
|
|
|
|
});
|
|
|
|
|
|
|
|
$("#subscriptions_table").on("click", ".change-stream-privacy", function (e) {
|
|
|
|
var stream_id = get_stream_id(e.target);
|
|
|
|
var stream = stream_data.get_sub_by_id(stream_id);
|
|
|
|
var template_data = {
|
|
|
|
stream_id: stream_id,
|
2018-05-03 18:52:39 +02:00
|
|
|
stream_name: stream.name,
|
2018-05-30 16:02:13 +02:00
|
|
|
is_announcement_only: stream.is_announcement_only,
|
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 &&
|
|
|
|
stream.history_public_to_subscribers,
|
2017-04-24 04:11:25 +02:00
|
|
|
};
|
|
|
|
var change_privacy_modal = templates.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');
|
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
|
|
|
});
|
|
|
|
|
|
|
|
$("#subscriptions_table").on("click", "#sub_setting_not_in_home_view",
|
|
|
|
stream_home_view_clicked);
|
|
|
|
$("#subscriptions_table").on("click", "#sub_desktop_notifications_setting",
|
|
|
|
stream_desktop_notifications_clicked);
|
|
|
|
$("#subscriptions_table").on("click", "#sub_audible_notifications_setting",
|
|
|
|
stream_audible_notifications_clicked);
|
2017-08-17 16:55:32 +02:00
|
|
|
$("#subscriptions_table").on("click", "#sub_push_notifications_setting",
|
|
|
|
stream_push_notifications_clicked);
|
2017-11-21 05:58:26 +01:00
|
|
|
$("#subscriptions_table").on("click", "#sub_email_notifications_setting",
|
|
|
|
stream_email_notifications_clicked);
|
2017-04-24 04:11:25 +02:00
|
|
|
$("#subscriptions_table").on("click", "#sub_pin_setting",
|
|
|
|
stream_pin_clicked);
|
|
|
|
|
|
|
|
$("#subscriptions_table").on("submit", ".subscriber_list_add form", function (e) {
|
|
|
|
e.preventDefault();
|
|
|
|
var settings_row = $(e.target).closest('.subscription_settings');
|
|
|
|
var sub = get_sub_for_target(settings_row);
|
|
|
|
if (!sub) {
|
|
|
|
blueslip.error('.subscriber_list_add form submit fails');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var text_box = settings_row.find('input[name="principal"]');
|
|
|
|
var principal = $.trim(text_box.val());
|
2017-12-29 21:07:37 +01:00
|
|
|
var 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) {
|
|
|
|
var error = JSON.parse(xhr.responseText);
|
|
|
|
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();
|
|
|
|
|
|
|
|
var list_entry = $(e.target).closest("tr");
|
|
|
|
var principal = list_entry.children(".subscriber-email").text();
|
|
|
|
var settings_row = $(e.target).closest('.subscription_settings');
|
|
|
|
|
|
|
|
var sub = get_sub_for_target(settings_row);
|
|
|
|
if (!sub) {
|
|
|
|
blueslip.error('.subscriber_list_remove form submit fails');
|
|
|
|
return;
|
|
|
|
}
|
2017-12-29 21:07:37 +01:00
|
|
|
var 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');
|
2018-04-30 14:28:32 +02:00
|
|
|
ui.update_scrollbar($("#subscription_overlay .settings"));
|
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) {
|
|
|
|
var sub = get_sub_for_target(e.target);
|
|
|
|
var stream_row = $(this).parent();
|
|
|
|
subs.sub_or_unsub(sub);
|
|
|
|
var sub_settings = settings_for_sub(sub);
|
|
|
|
var regular_sub_settings = sub_settings.find(".regular_subscription_settings");
|
|
|
|
if (!sub.subscribed) {
|
|
|
|
regular_sub_settings.addClass("in");
|
|
|
|
exports.show_stream_row(stream_row, true);
|
|
|
|
} else {
|
|
|
|
regular_sub_settings.removeClass("in");
|
|
|
|
}
|
|
|
|
|
2017-10-03 00:15:42 +02:00
|
|
|
setup_subscriptions_stream_hash(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();
|
|
|
|
|
|
|
|
var stream_id = get_stream_id(e.target);
|
|
|
|
if (!stream_id) {
|
|
|
|
ui_report.message(i18n.t("Invalid stream id"), $(".stream_change_property_info"), 'alert-error');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
var stream_name = stream_data.maybe_get_stream_name(stream_id);
|
2018-02-21 05:16:45 +01:00
|
|
|
var deactivate_stream_modal = templates.render("deactivation-stream-modal", {stream_name: stream_name});
|
2018-02-13 11:47:17 +01:00
|
|
|
$(".subscription_settings").append(deactivate_stream_modal);
|
|
|
|
overlays.open_modal('deactivation_stream_modal');
|
|
|
|
});
|
|
|
|
|
|
|
|
$("#subscriptions_table").on("click", "#do_deactivate_stream_button", function (e) {
|
|
|
|
var stream_id = get_stream_id(e.target);
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
var 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) {
|
|
|
|
exports.show_stream_row(this, true);
|
|
|
|
var stream_id = $(this).attr("data-stream-id");
|
|
|
|
var sub = stream_data.get_sub_by_id(stream_id);
|
2017-10-03 00:15:42 +02:00
|
|
|
setup_subscriptions_stream_hash(sub);
|
2017-04-24 04:11:25 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
$(document).on('peer_subscribe.zulip', function (e, data) {
|
|
|
|
var 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
|
|
|
});
|
|
|
|
|
|
|
|
$(document).on('peer_unsubscribe.zulip', function (e, data) {
|
|
|
|
var 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
|
|
|
|
|
|
|
return exports;
|
|
|
|
|
|
|
|
}());
|
|
|
|
if (typeof module !== 'undefined') {
|
|
|
|
module.exports = stream_edit;
|
|
|
|
}
|
2018-05-28 08:04:36 +02:00
|
|
|
window.stream_edit = stream_edit;
|