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();
|
2013-09-27 16:26:04 +02:00
|
|
|
current_msg_list.rerender_after_muting_changes();
|
|
|
|
if (current_msg_list !== home_msg_list) {
|
|
|
|
home_msg_list.rerender_after_muting_changes();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-08-27 01:47:30 +02:00
|
|
|
exports.notify_with_undo_option = (function () {
|
|
|
|
var meta = {
|
|
|
|
stream: null,
|
|
|
|
topic: null,
|
|
|
|
hide_me_time: null,
|
|
|
|
alert_hover_state: false,
|
2017-01-12 00:17:43 +01:00
|
|
|
$mute: null,
|
2016-08-27 01:47:30 +02:00
|
|
|
};
|
|
|
|
var animate = {
|
2016-12-02 14:06:06 +01:00
|
|
|
fadeOut: function () {
|
2016-08-27 01:47:30 +02:00
|
|
|
if (meta.$mute) {
|
|
|
|
meta.$mute.fadeOut(500).removeClass("show");
|
|
|
|
}
|
|
|
|
},
|
2016-12-02 14:06:06 +01:00
|
|
|
fadeIn: function () {
|
2016-08-27 01:47:30 +02:00
|
|
|
if (meta.$mute) {
|
|
|
|
meta.$mute.fadeIn(500).addClass("show");
|
|
|
|
}
|
2017-01-12 00:17:43 +01:00
|
|
|
},
|
2016-08-27 01:47:30 +02:00
|
|
|
};
|
2016-12-15 07:26:09 +01:00
|
|
|
setInterval(function () {
|
2016-08-27 01:47:30 +02:00
|
|
|
if (meta.hide_me_time < new Date().getTime() && !meta.alert_hover_state) {
|
|
|
|
animate.fadeOut();
|
|
|
|
}
|
|
|
|
}, 100);
|
|
|
|
|
|
|
|
return function (stream, topic) {
|
|
|
|
var $exit = $("#unmute_muted_topic_notification .exit-me");
|
|
|
|
|
|
|
|
if (!meta.$mute) {
|
|
|
|
meta.$mute = $("#unmute_muted_topic_notification");
|
|
|
|
|
|
|
|
$exit.click(function () {
|
|
|
|
animate.fadeOut();
|
|
|
|
});
|
|
|
|
|
|
|
|
meta.$mute.find("#unmute").click(function () {
|
|
|
|
// it should reference the meta variable and not get stuck with
|
|
|
|
// a pass-by-value of stream, topic.
|
2017-03-23 06:57:36 +01:00
|
|
|
exports.unmute(meta.stream, meta.topic);
|
2016-08-27 01:47:30 +02:00
|
|
|
animate.fadeOut();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
meta.stream = stream;
|
|
|
|
meta.topic = topic;
|
|
|
|
// add a four second delay before closing up.
|
|
|
|
meta.hide_me_time = new Date().getTime() + 4000;
|
|
|
|
|
|
|
|
meta.$mute.find(".topic").html(topic);
|
|
|
|
meta.$mute.find(".stream").html(stream);
|
|
|
|
|
|
|
|
animate.fadeIn();
|
|
|
|
|
|
|
|
// if the user mouses over the notification, don't hide it.
|
|
|
|
meta.$mute.mouseenter(function () {
|
|
|
|
meta.alert_hover_state = true;
|
|
|
|
});
|
|
|
|
|
|
|
|
// once the user's mouse leaves the notification, restart the countdown.
|
|
|
|
meta.$mute.mouseleave(function () {
|
|
|
|
meta.alert_hover_state = false;
|
|
|
|
// add at least 2000ms but if more than that exists just keep the
|
|
|
|
// current amount.
|
|
|
|
meta.hide_me_time = Math.max(meta.hide_me_time, new Date().getTime() + 2000);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
}());
|
|
|
|
|
2017-03-25 20:23:43 +01:00
|
|
|
exports.dismiss_mute_confirmation = function () {
|
|
|
|
var $mute = $("#unmute_muted_topic_notification");
|
|
|
|
if ($mute) {
|
|
|
|
$mute.fadeOut(500).removeClass("show");
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-08-29 21:40:38 +02:00
|
|
|
exports.persist_mute = function (stream_name, topic_name) {
|
2013-09-10 20:07:24 +02:00
|
|
|
var data = {
|
2017-08-29 21:40:38 +02:00
|
|
|
stream: stream_name,
|
|
|
|
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,
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.persist_unmute = function (stream_name, topic_name) {
|
|
|
|
var data = {
|
|
|
|
stream: stream_name,
|
|
|
|
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.mute_topic = function (stream, topic) {
|
|
|
|
muting.add_muted_topic(stream, topic);
|
2017-02-11 09:55:18 +01:00
|
|
|
unread_ui.update_unread_counts();
|
2017-02-11 09:17:19 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
exports.unmute_topic = function (stream, topic) {
|
|
|
|
muting.remove_muted_topic(stream, topic);
|
2017-02-11 09:55:18 +01:00
|
|
|
unread_ui.update_unread_counts();
|
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();
|
|
|
|
_.each(muted_topics, function (list) {
|
|
|
|
var row = templates.render('muted_topic_ui_row', {stream: list[0], topic: list[1]});
|
|
|
|
muted_topics_table.append(row);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2017-03-23 06:57:36 +01:00
|
|
|
exports.mute = function (stream, topic) {
|
|
|
|
stream_popover.hide_topic_popover();
|
|
|
|
exports.mute_topic(stream, topic);
|
2017-08-29 21:40:38 +02:00
|
|
|
exports.rerender();
|
|
|
|
exports.persist_mute(stream, topic);
|
2017-03-23 06:57:36 +01:00
|
|
|
exports.notify_with_undo_option(stream, topic);
|
|
|
|
exports.set_up_muted_topics_ui(muting.get_muted_topics());
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.unmute = function (stream, topic) {
|
|
|
|
// 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();
|
|
|
|
exports.unmute_topic(stream, topic);
|
2017-08-29 21:40:38 +02:00
|
|
|
exports.rerender();
|
|
|
|
exports.persist_unmute(stream, topic);
|
2017-03-23 06:57:36 +01:00
|
|
|
exports.set_up_muted_topics_ui(muting.get_muted_topics());
|
2017-03-25 20:23:43 +01:00
|
|
|
exports.dismiss_mute_confirmation();
|
2017-03-23 06:57:36 +01:00
|
|
|
};
|
|
|
|
|
2017-03-24 23:25:04 +01:00
|
|
|
exports.toggle_mute = function (msg) {
|
|
|
|
if (muting.is_topic_muted(msg.stream, msg.subject)) {
|
|
|
|
exports.unmute(msg.stream, msg.subject);
|
|
|
|
} else if (msg.type === 'stream') {
|
|
|
|
exports.mute(msg.stream, msg.subject);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-09-27 19:38:56 +02:00
|
|
|
$(function () {
|
2017-02-11 09:17:19 +01:00
|
|
|
exports.update_muted_topics(page_params.muted_topics);
|
2013-09-27 19:38:56 +02: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;
|
|
|
|
}
|