From eac155d3b82c50b99f91b3de63618cad1fae0adb Mon Sep 17 00:00:00 2001 From: Steve Howell Date: Wed, 2 Aug 2017 15:23:05 -0400 Subject: [PATCH] unread: Use reverse_lookup in unread_topic_counter. --- frontend_tests/node_tests/unread.js | 9 ------- static/js/unread.js | 39 +++++++++++------------------ 2 files changed, 15 insertions(+), 33 deletions(-) diff --git a/frontend_tests/node_tests/unread.js b/frontend_tests/node_tests/unread.js index a28a0caf13..97c8c3d9f9 100644 --- a/frontend_tests/node_tests/unread.js +++ b/frontend_tests/node_tests/unread.js @@ -466,14 +466,5 @@ stream_data.get_stream_id = function () { unread.process_read_message(message); var counts = unread.get_counts(); assert.equal(counts.private_message_count, 0); - - // Test undefined stream_id - message = { type: 'stream' }; - - global.blueslip.error = function (msg) { - assert.equal(msg, 'No stream_id found for message undefined'); - }; - - unread.process_read_message(message); }()); diff --git a/static/js/unread.js b/static/js/unread.js index 358f2ba89d..214b1aa124 100644 --- a/static/js/unread.js +++ b/static/js/unread.js @@ -77,30 +77,30 @@ exports.unread_topic_counter = (function () { } var unread_topics = num_dict(); // dict of stream -> topic -> msg id + var reverse_lookup = num_dict(); self.clear = function () { unread_topics = num_dict(); + reverse_lookup = num_dict(); }; - self.update = function (stream_id, topic, new_topic, msg_id) { - self.del(stream_id, topic, msg_id); + self.update = function (msg_id, stream_id, new_topic) { + self.del(msg_id); self.add(stream_id, new_topic, msg_id); }; self.add = function (stream_id, topic, msg_id) { unread_topics.setdefault(stream_id, str_dict()); - unread_topics.get(stream_id).setdefault(topic, num_dict()); - unread_topics.get(stream_id).get(topic).set(msg_id, true); + var dict = unread_topics.get(stream_id).setdefault(topic, num_dict()); + dict.set(msg_id, true); + reverse_lookup.set(msg_id, dict); }; - - self.del = function (stream_id, topic, msg_id) { - var stream_dict = unread_topics.get(stream_id); - if (stream_dict) { - var topic_dict = stream_dict.get(topic); - if (topic_dict) { - topic_dict.del(msg_id); - } + self.del = function (msg_id) { + var dict = reverse_lookup.get(msg_id); + if (dict) { + dict.del(msg_id); + reverse_lookup.del(msg_id); } }; @@ -219,10 +219,9 @@ exports.message_unread = function (message) { exports.update_unread_topics = function (msg, event) { if (event.subject !== undefined) { exports.unread_topic_counter.update( + msg.id, msg.stream_id, - msg.subject, - event.subject, - msg.id + event.subject ); } }; @@ -259,15 +258,7 @@ exports.process_read_message = function (message) { } if (message.type === 'stream') { - if (message.stream_id === undefined) { - blueslip.error('No stream_id found for message ' + message.id); - return; - } - exports.unread_topic_counter.del( - message.stream_id, - message.subject, - message.id - ); + exports.unread_topic_counter.del(message.id); } exports.unread_mentions_counter.del(message.id);