2019-03-24 08:37:53 +01:00
|
|
|
var Dict = require('./dict').Dict;
|
2019-02-08 11:56:33 +01:00
|
|
|
|
2013-08-20 22:05:56 +02:00
|
|
|
var muting = (function () {
|
|
|
|
|
|
|
|
var exports = {};
|
|
|
|
|
2018-12-13 22:26:10 +01:00
|
|
|
var muted_topics = new Dict();
|
2013-08-20 22:05:56 +02:00
|
|
|
|
2018-12-13 22:26:10 +01:00
|
|
|
exports.add_muted_topic = function (stream_id, topic) {
|
|
|
|
var sub_dict = muted_topics.get(stream_id);
|
2013-08-20 22:05:56 +02:00
|
|
|
if (!sub_dict) {
|
2013-09-10 21:33:59 +02:00
|
|
|
sub_dict = new Dict({fold_case: true});
|
2018-12-13 22:26:10 +01:00
|
|
|
muted_topics.set(stream_id, sub_dict);
|
2013-08-20 22:05:56 +02:00
|
|
|
}
|
|
|
|
sub_dict.set(topic, true);
|
|
|
|
};
|
|
|
|
|
2018-12-13 22:26:10 +01:00
|
|
|
exports.remove_muted_topic = function (stream_id, topic) {
|
|
|
|
var sub_dict = muted_topics.get(stream_id);
|
2013-09-09 20:33:25 +02:00
|
|
|
if (sub_dict) {
|
|
|
|
sub_dict.del(topic);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-12-13 22:26:10 +01:00
|
|
|
exports.is_topic_muted = function (stream_id, topic) {
|
|
|
|
if (stream_id === undefined) {
|
2013-09-10 22:42:48 +02:00
|
|
|
return false;
|
|
|
|
}
|
2018-12-13 22:26:10 +01:00
|
|
|
var sub_dict = muted_topics.get(stream_id);
|
2013-08-20 22:05:56 +02:00
|
|
|
return sub_dict && sub_dict.get(topic);
|
|
|
|
};
|
|
|
|
|
2013-09-10 17:01:38 +02:00
|
|
|
exports.get_muted_topics = function () {
|
|
|
|
var topics = [];
|
2018-12-13 22:26:10 +01:00
|
|
|
muted_topics.each(function (sub_dict, stream_id) {
|
2013-09-10 17:01:38 +02:00
|
|
|
_.each(sub_dict.keys(), function (topic) {
|
2018-12-14 18:08:08 +01:00
|
|
|
topics.push([stream_id, topic]);
|
2013-09-10 17:01:38 +02:00
|
|
|
});
|
|
|
|
});
|
|
|
|
return topics;
|
|
|
|
};
|
|
|
|
|
2013-09-10 20:34:41 +02:00
|
|
|
exports.set_muted_topics = function (tuples) {
|
2018-12-13 22:26:10 +01:00
|
|
|
muted_topics = new Dict();
|
2013-09-10 20:34:41 +02:00
|
|
|
|
|
|
|
_.each(tuples, function (tuple) {
|
2018-12-13 22:26:10 +01:00
|
|
|
var stream_name = tuple[0];
|
2013-09-10 20:34:41 +02:00
|
|
|
var topic = tuple[1];
|
|
|
|
|
2018-12-13 22:26:10 +01:00
|
|
|
var stream_id = stream_data.get_stream_id(stream_name);
|
|
|
|
|
|
|
|
if (!stream_id) {
|
|
|
|
blueslip.warn('Unknown stream in set_muted_topics: ' + stream_name);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
exports.add_muted_topic(stream_id, topic);
|
2013-09-10 20:34:41 +02:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2018-12-15 19:53:20 +01:00
|
|
|
exports.initialize = function () {
|
|
|
|
exports.set_muted_topics(page_params.muted_topics);
|
|
|
|
};
|
|
|
|
|
2013-08-20 22:05:56 +02:00
|
|
|
return exports;
|
|
|
|
}());
|
|
|
|
if (typeof module !== 'undefined') {
|
|
|
|
module.exports = muting;
|
|
|
|
}
|
2018-05-28 08:04:36 +02:00
|
|
|
window.muting = muting;
|