2013-09-10 20:07:24 +02:00
|
|
|
var muting_ui = (function () {
|
|
|
|
|
|
|
|
var exports = {};
|
|
|
|
|
2013-09-11 17:27:43 +02:00
|
|
|
function timestamp_ms() {
|
|
|
|
return (new Date()).getTime();
|
|
|
|
}
|
|
|
|
|
|
|
|
var last_topic_update = 0;
|
|
|
|
|
2013-09-27 16:26:04 +02:00
|
|
|
exports.rerender = function () {
|
2017-08-29 21:40:38 +02:00
|
|
|
// Note: We tend to optimistically rerender muting preferences before
|
|
|
|
// the back end actually acknowledges the mute. This gives a more
|
|
|
|
// immediate feel to the user, and if the back end fails temporarily,
|
|
|
|
// re-doing a mute or unmute is a pretty recoverable thing.
|
|
|
|
|
2013-09-30 22:03:10 +02:00
|
|
|
stream_list.update_streams_sidebar();
|
2018-04-27 17:35:02 +02:00
|
|
|
if (current_msg_list.muting_enabled) {
|
|
|
|
current_msg_list.update_muting_and_rerender();
|
|
|
|
}
|
2013-09-27 16:26:04 +02:00
|
|
|
if (current_msg_list !== home_msg_list) {
|
2018-04-27 17:35:02 +02:00
|
|
|
home_msg_list.update_muting_and_rerender();
|
2013-09-27 16:26:04 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-12-13 23:05:26 +01:00
|
|
|
exports.persist_mute = function (stream_id, topic_name) {
|
2013-09-10 20:07:24 +02:00
|
|
|
var data = {
|
2018-12-24 18:30:36 +01:00
|
|
|
stream_id: stream_id,
|
2017-08-29 21:40:38 +02:00
|
|
|
topic: topic_name,
|
|
|
|
op: 'add',
|
2013-09-10 20:07:24 +02:00
|
|
|
};
|
2013-09-11 17:27:43 +02:00
|
|
|
last_topic_update = timestamp_ms();
|
2017-08-29 21:40:38 +02:00
|
|
|
channel.patch({
|
|
|
|
url: '/json/users/me/subscriptions/muted_topics',
|
|
|
|
idempotent: true,
|
|
|
|
data: data,
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2018-12-13 23:05:26 +01:00
|
|
|
exports.persist_unmute = function (stream_id, topic_name) {
|
2017-08-29 21:40:38 +02:00
|
|
|
var data = {
|
2018-12-24 18:30:36 +01:00
|
|
|
stream_id: stream_id,
|
2017-08-29 21:40:38 +02:00
|
|
|
topic: topic_name,
|
|
|
|
op: 'remove',
|
|
|
|
};
|
|
|
|
last_topic_update = timestamp_ms();
|
|
|
|
channel.patch({
|
2017-01-10 01:23:53 +01:00
|
|
|
url: '/json/users/me/subscriptions/muted_topics',
|
2014-01-07 23:40:31 +01:00
|
|
|
idempotent: true,
|
2017-01-12 00:17:43 +01:00
|
|
|
data: data,
|
2013-09-10 20:07:24 +02:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2013-09-11 00:36:21 +02:00
|
|
|
exports.handle_updates = function (muted_topics) {
|
2013-09-11 17:27:43 +02:00
|
|
|
if (timestamp_ms() < last_topic_update + 1000) {
|
|
|
|
// This topic update is either the one that we just rendered, or,
|
|
|
|
// much less likely, it's coming from another device and would probably
|
|
|
|
// be overwriting this device's preferences with stale data.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-02-11 09:17:19 +01:00
|
|
|
exports.update_muted_topics(muted_topics);
|
2013-09-27 16:26:04 +02:00
|
|
|
exports.rerender();
|
2013-09-11 00:36:21 +02:00
|
|
|
};
|
|
|
|
|
2017-02-11 09:17:19 +01:00
|
|
|
exports.update_muted_topics = function (muted_topics) {
|
|
|
|
muting.set_muted_topics(muted_topics);
|
2017-02-11 09:55:18 +01:00
|
|
|
unread_ui.update_unread_counts();
|
2017-02-11 09:17:19 +01:00
|
|
|
};
|
|
|
|
|
2016-11-26 04:39:53 +01:00
|
|
|
exports.set_up_muted_topics_ui = function (muted_topics) {
|
|
|
|
var muted_topics_table = $("#muted_topics_table tbody");
|
|
|
|
muted_topics_table.empty();
|
2018-12-14 17:52:01 +01:00
|
|
|
_.each(muted_topics, function (tup) {
|
2018-12-14 18:08:08 +01:00
|
|
|
var stream_id = tup[0];
|
2018-12-14 17:52:01 +01:00
|
|
|
var topic = tup[1];
|
|
|
|
|
2018-12-14 18:08:08 +01:00
|
|
|
var stream = stream_data.maybe_get_stream_name(stream_id);
|
2018-12-14 17:52:01 +01:00
|
|
|
|
2018-12-14 18:08:08 +01:00
|
|
|
if (!stream) {
|
|
|
|
blueslip.warn('Unknown stream_id in set_up_muted_topics_ui: ' + stream_id);
|
2018-12-14 17:52:01 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var template_data = {
|
|
|
|
stream: stream,
|
|
|
|
stream_id: stream_id,
|
|
|
|
topic: topic,
|
|
|
|
};
|
|
|
|
|
|
|
|
var row = templates.render('muted_topic_ui_row', template_data);
|
2016-11-26 04:39:53 +01:00
|
|
|
muted_topics_table.append(row);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2018-12-14 17:20:35 +01:00
|
|
|
exports.mute = function (stream_id, topic) {
|
2018-12-20 18:04:46 +01:00
|
|
|
var stream_name = stream_data.maybe_get_stream_name(stream_id);
|
|
|
|
|
2017-03-23 06:57:36 +01:00
|
|
|
stream_popover.hide_topic_popover();
|
2018-12-13 22:26:10 +01:00
|
|
|
muting.add_muted_topic(stream_id, topic);
|
2017-10-18 01:40:19 +02:00
|
|
|
unread_ui.update_unread_counts();
|
2017-08-29 21:40:38 +02:00
|
|
|
exports.rerender();
|
2018-12-13 23:05:26 +01:00
|
|
|
exports.persist_mute(stream_id, topic);
|
2018-12-21 15:58:28 +01:00
|
|
|
feedback_widget.show({
|
2018-12-20 18:04:46 +01:00
|
|
|
populate: function (container) {
|
2018-12-21 00:36:13 +01:00
|
|
|
var rendered_html = templates.render('topic_muted');
|
|
|
|
container.html(rendered_html);
|
2018-12-20 18:04:46 +01:00
|
|
|
container.find(".stream").text(stream_name);
|
|
|
|
container.find(".topic").text(topic);
|
|
|
|
},
|
|
|
|
on_undo: function () {
|
|
|
|
exports.unmute(stream_id, topic);
|
|
|
|
},
|
2018-12-20 19:48:37 +01:00
|
|
|
title_text: i18n.t("Topic muted"),
|
|
|
|
undo_button_text: i18n.t("Unmute"),
|
2018-12-20 18:04:46 +01:00
|
|
|
});
|
2017-03-23 06:57:36 +01:00
|
|
|
exports.set_up_muted_topics_ui(muting.get_muted_topics());
|
|
|
|
};
|
|
|
|
|
2018-12-14 17:20:35 +01:00
|
|
|
exports.unmute = function (stream_id, topic) {
|
2017-03-23 06:57:36 +01:00
|
|
|
// we don't run a unmute_notify function because it isn't an issue as much
|
|
|
|
// if someone accidentally unmutes a stream rather than if they mute it
|
|
|
|
// and miss out on info.
|
|
|
|
stream_popover.hide_topic_popover();
|
2018-12-13 22:26:10 +01:00
|
|
|
muting.remove_muted_topic(stream_id, topic);
|
2017-10-18 01:40:19 +02:00
|
|
|
unread_ui.update_unread_counts();
|
2017-08-29 21:40:38 +02:00
|
|
|
exports.rerender();
|
2018-12-13 23:05:26 +01:00
|
|
|
exports.persist_unmute(stream_id, topic);
|
2017-03-23 06:57:36 +01:00
|
|
|
exports.set_up_muted_topics_ui(muting.get_muted_topics());
|
2018-12-21 15:58:28 +01:00
|
|
|
feedback_widget.dismiss();
|
2017-03-23 06:57:36 +01:00
|
|
|
};
|
|
|
|
|
2018-12-22 19:18:59 +01:00
|
|
|
exports.toggle_mute = function (message) {
|
|
|
|
var stream_id = message.stream_id;
|
|
|
|
var topic = util.get_message_topic(message);
|
|
|
|
|
|
|
|
if (muting.is_topic_muted(stream_id, topic)) {
|
|
|
|
exports.unmute(stream_id, topic);
|
|
|
|
} else if (message.type === 'stream') {
|
|
|
|
exports.mute(stream_id, topic);
|
2017-03-24 23:25:04 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-09-10 20:07:24 +02:00
|
|
|
return exports;
|
|
|
|
}());
|
2016-12-04 08:59:56 +01:00
|
|
|
|
|
|
|
if (typeof module !== 'undefined') {
|
|
|
|
module.exports = muting_ui;
|
|
|
|
}
|
2018-05-28 08:04:36 +02:00
|
|
|
window.muting_ui = muting_ui;
|