Skip to content

Commit

Permalink
muting.js: Track muted streams using stream id.
Browse files Browse the repository at this point in the history
This should prevent some glitches with stream rename events.
  • Loading branch information
showell authored and timabbott committed May 15, 2017
1 parent 8eb9723 commit c7f710b
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 9 deletions.
16 changes: 15 additions & 1 deletion frontend_tests/node_tests/muting.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,24 @@
set_global('page_params', {});
add_dependencies({
unread: 'js/unread.js',
stream_data: 'js/stream_data',
unread: 'js/unread',
});

set_global('blueslip', {
warn: function () {},
});

var muting = require('js/muting.js');

function make_sub(name, stream_id) {
global.stream_data.add_sub(name, {stream_id: stream_id, name: name});
}

make_sub('devel', 1);
make_sub('office', 2);
make_sub('social', 3);
make_sub('design', 4);

(function test_edge_cases() {
// private messages
assert(!muting.is_topic_muted(undefined, undefined));
Expand Down
43 changes: 35 additions & 8 deletions static/js/muting.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,32 @@ var muting = (function () {

var exports = {};

var muted_topics = new Dict({fold_case: true});
var muted_topics = new Dict();

exports.add_muted_topic = function (stream, topic) {
var sub_dict = muted_topics.get(stream);
var stream_id = stream_data.get_stream_id(stream);

if (!stream_id) {
return;
}

var sub_dict = muted_topics.get(stream_id);
if (!sub_dict) {
sub_dict = new Dict({fold_case: true});
muted_topics.set(stream, sub_dict);
muted_topics.set(stream_id, sub_dict);
}
sub_dict.set(topic, true);
};

exports.remove_muted_topic = function (stream, topic) {
var sub_dict = muted_topics.get(stream);
var stream_id = stream_data.get_stream_id(stream);

if (!stream_id) {
blueslip.warn('cannot unmute stream ' + stream);
return;
}

var sub_dict = muted_topics.get(stream_id);
if (sub_dict) {
sub_dict.del(topic);
}
Expand All @@ -24,22 +37,36 @@ exports.is_topic_muted = function (stream, topic) {
if (stream === undefined) {
return false;
}
var sub_dict = muted_topics.get(stream);

var stream_id = stream_data.get_stream_id(stream);

if (!stream_id) {
return false;
}

var sub_dict = muted_topics.get(stream_id);
return sub_dict && sub_dict.get(topic);
};

exports.get_muted_topics = function () {
var topics = [];
muted_topics.each(function (sub_dict, stream) {
muted_topics.each(function (sub_dict, stream_id) {
var sub = stream_data.get_sub_by_id(stream_id);

if (!sub) {
blueslip.error('cannot find stream ' + stream_id);
return;
}

_.each(sub_dict.keys(), function (topic) {
topics.push([stream, topic]);
topics.push([sub.name, topic]);
});
});
return topics;
};

exports.set_muted_topics = function (tuples) {
muted_topics = new Dict({fold_case: true});
muted_topics = new Dict();

_.each(tuples, function (tuple) {
var stream = tuple[0];
Expand Down

0 comments on commit c7f710b

Please sign in to comment.