2019-12-26 15:34:17 +01:00
|
|
|
const FoldDict = require('./fold_dict').FoldDict;
|
2020-02-01 04:10:14 +01:00
|
|
|
const IntDict = require('./int_dict').IntDict;
|
2019-02-08 11:56:33 +01:00
|
|
|
|
2020-02-01 04:10:14 +01:00
|
|
|
const muted_topics = new IntDict();
|
2013-08-20 22:05:56 +02:00
|
|
|
|
2018-12-13 22:26:10 +01:00
|
|
|
exports.add_muted_topic = function (stream_id, topic) {
|
2019-11-02 00:06:25 +01:00
|
|
|
let sub_dict = muted_topics.get(stream_id);
|
2013-08-20 22:05:56 +02:00
|
|
|
if (!sub_dict) {
|
2019-12-26 15:34:17 +01:00
|
|
|
sub_dict = new FoldDict();
|
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) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const sub_dict = muted_topics.get(stream_id);
|
2013-09-09 20:33:25 +02:00
|
|
|
if (sub_dict) {
|
2020-02-03 07:41:38 +01:00
|
|
|
sub_dict.delete(topic);
|
2013-09-09 20:33:25 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
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;
|
|
|
|
}
|
2019-11-02 00:06:25 +01:00
|
|
|
const 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 () {
|
2019-11-02 00:06:25 +01:00
|
|
|
const topics = [];
|
2018-12-13 22:26:10 +01:00
|
|
|
muted_topics.each(function (sub_dict, stream_id) {
|
2020-02-03 08:42:48 +01:00
|
|
|
for (const topic of sub_dict.keys()) {
|
2018-12-14 18:08:08 +01:00
|
|
|
topics.push([stream_id, topic]);
|
2020-02-03 08:42:48 +01:00
|
|
|
}
|
2013-09-10 17:01:38 +02:00
|
|
|
});
|
|
|
|
return topics;
|
|
|
|
};
|
|
|
|
|
2013-09-10 20:34:41 +02:00
|
|
|
exports.set_muted_topics = function (tuples) {
|
2020-02-01 04:10:14 +01:00
|
|
|
muted_topics.clear();
|
2013-09-10 20:34:41 +02:00
|
|
|
|
|
|
|
_.each(tuples, function (tuple) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const stream_name = tuple[0];
|
|
|
|
const topic = tuple[1];
|
2013-09-10 20:34:41 +02:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const stream_id = stream_data.get_stream_id(stream_name);
|
2018-12-13 22:26:10 +01:00
|
|
|
|
|
|
|
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);
|
|
|
|
};
|
|
|
|
|
2019-10-25 09:45:13 +02:00
|
|
|
window.muting = exports;
|