Avoid side effects in ui.set_presence_list().

Have ui.set_presence_list() only touch the presence list.

Before this change, it was calling update_unread_counts(), which
has a bunch of side effects unrelated to the presence list.

(imported from commit 690f754d78874a03fa36f8ff8765d5a63e431d28)
This commit is contained in:
Steve Howell 2013-08-22 13:06:04 -04:00
parent 6de6ced290
commit 58c141ad79
5 changed files with 42 additions and 9 deletions

View File

@ -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);

View File

@ -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

View File

@ -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') {

View File

@ -56,7 +56,7 @@ var globals =
+ ' mark_message_as_read'
+ ' send_summarize_in_home'
+ ' send_summarize_in_stream'
+ ' update_unread_counts'
+ ' suppress_unread_counts'
;

View File

@ -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 () {