diff --git a/static/js/stream_list.js b/static/js/stream_list.js index 0ebe37279d..a7a1930489 100644 --- a/static/js/stream_list.js +++ b/static/js/stream_list.js @@ -223,6 +223,14 @@ function animate_mention_changes(new_mention_count) { last_mention_count = new_mention_count; } + +exports.set_presence_list_counts = function (count_dict) { + // count_dict maps people (emails, actually) to counts + count_dict.each(function (count, person) { + exports.set_count("private", person, count); + }); +}; + exports.update_dom_with_unread_counts = function (counts) { // counts is just a data object that gets calculated elsewhere // Our job is to update some DOM elements. @@ -239,10 +247,7 @@ exports.update_dom_with_unread_counts = function (counts) { }); }); - // counts.pm_count maps people to counts - counts.pm_count.each(function (count, person) { - exports.set_count("private", person, count); - }); + exports.set_presence_list_counts(counts.pm_count); // integer counts exports.set_count("global", "private", counts.private_message_count); diff --git a/static/js/ui.js b/static/js/ui.js index 7506687db6..fe5a3d2843 100644 --- a/static/js/ui.js +++ b/static/js/ui.js @@ -1483,10 +1483,18 @@ exports.set_presence_list = function (users, presence_info) { var user_info = [my_info].concat(_.map(user_emails, info_for)); $('#user_presences').html(templates.render('user_presence_rows', {users: user_info})); - // FIXME: This should probably be rendered in the template directly, but - // currently we have to update unread counts after rendering the template - // or they get blown away. - update_unread_counts(); + + // Update the counts in the presence list. + if (!suppress_unread_counts) { + // We do this after rendering the template, to avoid dealing with + // the suppress_unread_counts conditional in the template. + var count_dict = new Dict(); + _.each(users, function (email) { + count_dict.set(email, unread.num_unread_for_person(email)); + }); + count_dict.set(page_params.email, unread.num_unread_for_person(page_params.email)); + stream_list.set_presence_list_counts(count_dict); + } }; // Save the compose content cursor position and restore when we diff --git a/static/js/unread.js b/static/js/unread.js index 5af002664f..84c62628b3 100644 --- a/static/js/unread.js +++ b/static/js/unread.js @@ -173,6 +173,13 @@ exports.num_unread_for_subject = function (stream, subject) { return num_unread; }; +exports.num_unread_for_person = function (email) { + if (!unread_counts['private'].has(email)) { + return 0; + } + return _.keys(unread_counts['private'].get(email)).length; +}; + return exports; }()); if (typeof module !== 'undefined') { diff --git a/tools/jslint/check-all.js b/tools/jslint/check-all.js index 722090f55a..ae2f5cbffc 100644 --- a/tools/jslint/check-all.js +++ b/tools/jslint/check-all.js @@ -56,7 +56,7 @@ var globals = + ' mark_message_as_read' + ' send_summarize_in_home' + ' send_summarize_in_stream' - + ' update_unread_counts' + + ' suppress_unread_counts' ; diff --git a/zerver/tests/frontend/node/unread.js b/zerver/tests/frontend/node/unread.js index 5213184ee2..5c669d8eb3 100644 --- a/zerver/tests/frontend/node/unread.js +++ b/zerver/tests/frontend/node/unread.js @@ -196,6 +196,19 @@ var zero_counts = { assert.equal(counts.private_message_count, 0); }()); +(function test_num_unread_for_person() { + var email = 'alice@zulip.com'; + assert.equal(unread.num_unread_for_person(email), 0); + + var message = { + id: 15, + reply_to: email, + type: 'private' + }; + unread.process_loaded_messages([message]); + assert.equal(unread.num_unread_for_person(email), 1); +}()); + (function test_mentions() { narrow.active = function () {