Track unread counts using stream_id.

This commit is contained in:
Steve Howell 2017-05-13 10:26:54 -07:00 committed by Tim Abbott
parent c125ba1d08
commit 5d33d02235
7 changed files with 135 additions and 94 deletions

View File

@ -169,8 +169,18 @@ function is_odd(i) { return i % 2 === 1; }
} }
}; };
global.unread.topic_has_any_unread = function (stream, topic) { var devel_stream_id = 555;
return (stream === 'devel' && topic === 'python');
global.stream_data.get_stream_id = function (stream_name) {
if (stream_name === 'devel') {
return devel_stream_id;
}
return 999;
};
global.unread.topic_has_any_unread = function (stream_id, topic) {
return (stream_id === devel_stream_id && topic === 'python');
}; };
var next_item = tg.get_next_topic(curr_stream, curr_topic); var next_item = tg.get_next_topic(curr_stream, curr_topic);

View File

@ -31,6 +31,8 @@ global.compile_template('topic_list_item');
return 1; return 1;
}; };
global.stream_data.get_stream_id = function () { return 99; };
var parent_elem = $('<div>'); var parent_elem = $('<div>');
var widget = topic_list.build_widget(parent_elem, stream, active_topic, max_topics); var widget = topic_list.build_widget(parent_elem, stream, active_topic, max_topics);
var topic_html = widget.get_dom(); var topic_html = widget.get_dom();

View File

@ -50,8 +50,8 @@ var zero_counts = {
private_message_count: 0, private_message_count: 0,
home_unread_messages: 0, home_unread_messages: 0,
mentioned_message_count: 0, mentioned_message_count: 0,
stream_count: new Dict({fold_case: true}), stream_count: new Dict(),
subject_count: new Dict({fold_case: true}), subject_count: new Dict(),
pm_count: new Dict(), pm_count: new Dict(),
unread_in_current_view: 0, unread_in_current_view: 0,
}; };
@ -86,25 +86,27 @@ var zero_counts = {
var count = unread.num_unread_for_subject('social', 'lunch'); var count = unread.num_unread_for_subject('social', 'lunch');
assert.equal(count, 0); assert.equal(count, 0);
var stream_id = 100;
var message = { var message = {
id: 15, id: 15,
type: 'stream', type: 'stream',
stream: 'social', stream_id: stream_id,
subject: 'lunch', subject: 'lunch',
}; };
var other_message = { var other_message = {
id: 16, id: 16,
type: 'stream', type: 'stream',
stream: 'social', stream_id: stream_id,
subject: 'lunch', subject: 'lunch',
}; };
unread.process_loaded_messages([message, other_message]); unread.process_loaded_messages([message, other_message]);
count = unread.num_unread_for_subject('Social', 'lunch'); count = unread.num_unread_for_subject(stream_id, 'lunch');
assert.equal(count, 2); assert.equal(count, 2);
assert(unread.topic_has_any_unread('Social', 'lunch')); assert(unread.topic_has_any_unread(stream_id, 'lunch'));
var event = { var event = {
subject: 'dinner', subject: 'dinner',
@ -112,10 +114,10 @@ var zero_counts = {
unread.update_unread_topics(message, event); unread.update_unread_topics(message, event);
count = unread.num_unread_for_subject('social', 'lunch'); count = unread.num_unread_for_subject(stream_id, 'lunch');
assert.equal(count, 1); assert.equal(count, 1);
count = unread.num_unread_for_subject('social', 'dinner'); count = unread.num_unread_for_subject(stream_id, 'dinner');
assert.equal(count, 1); assert.equal(count, 1);
event = { event = {
@ -124,13 +126,13 @@ var zero_counts = {
unread.update_unread_topics(other_message, event); unread.update_unread_topics(other_message, event);
count = unread.num_unread_for_subject('social', 'lunch'); count = unread.num_unread_for_subject(stream_id, 'lunch');
assert.equal(count, 0); assert.equal(count, 0);
assert(!unread.topic_has_any_unread('social', 'lunch')); assert(!unread.topic_has_any_unread(stream_id, 'lunch'));
count = unread.num_unread_for_subject('social', 'snack'); count = unread.num_unread_for_subject(stream_id, 'snack');
assert.equal(count, 1); assert.equal(count, 1);
assert(unread.topic_has_any_unread('social', 'snack')); assert(unread.topic_has_any_unread(stream_id, 'snack'));
// Test defensive code. Trying to update a message we don't know // Test defensive code. Trying to update a message we don't know
// about should be a no-op. // about should be a no-op.
@ -142,12 +144,12 @@ var zero_counts = {
// cleanup // cleanup
message.subject = 'dinner'; message.subject = 'dinner';
unread.process_read_message(message); unread.process_read_message(message);
count = unread.num_unread_for_subject('social', 'dinner'); count = unread.num_unread_for_subject(stream_id, 'dinner');
assert.equal(count, 0); assert.equal(count, 0);
other_message.subject = 'snack'; other_message.subject = 'snack';
unread.process_read_message(other_message); unread.process_read_message(other_message);
count = unread.num_unread_for_subject('social', 'snack'); count = unread.num_unread_for_subject(stream_id, 'snack');
assert.equal(count, 0); assert.equal(count, 0);
}()); }());
@ -166,26 +168,35 @@ stream_data.get_stream_id = function () {
unread.declare_bankruptcy(); unread.declare_bankruptcy();
var stream_id = 101;
var unknown_stream_id = 555;
stream_data.get_sub_by_id = function (stream_id) {
if (stream_id === 101) {
return {name: 'social'};
}
};
var message = { var message = {
id: 15, id: 15,
type: 'stream', type: 'stream',
stream: 'social', stream_id: stream_id,
subject: 'test_muting', subject: 'test_muting',
}; };
unread.process_loaded_messages([message]); unread.process_loaded_messages([message]);
var counts = unread.get_counts(); var counts = unread.get_counts();
assert.equal(counts.stream_count.get('social'), 1); assert.equal(counts.stream_count.get(stream_id), 1);
assert.equal(counts.home_unread_messages, 1); assert.equal(counts.home_unread_messages, 1);
assert.equal(unread.num_unread_for_stream('social'), 1); assert.equal(unread.num_unread_for_stream(stream_id), 1);
muting.add_muted_topic('social', 'test_muting'); muting.add_muted_topic('social', 'test_muting');
counts = unread.get_counts(); counts = unread.get_counts();
assert.equal(counts.stream_count.get('social'), 0); assert.equal(counts.stream_count.get(stream_id), 0);
assert.equal(counts.home_unread_messages, 0); assert.equal(counts.home_unread_messages, 0);
assert.equal(unread.num_unread_for_stream('social'), 0); assert.equal(unread.num_unread_for_stream(stream_id), 0);
assert.equal(unread.num_unread_for_stream('unknown'), 0); assert.equal(unread.num_unread_for_stream(unknown_stream_id), 0);
}()); }());
(function test_num_unread_for_subject() { (function test_num_unread_for_subject() {
@ -193,12 +204,13 @@ stream_data.get_stream_id = function () {
// messages. // messages.
unread.declare_bankruptcy(); unread.declare_bankruptcy();
var count = unread.num_unread_for_subject('social', 'lunch'); var stream_id = 301;
var count = unread.num_unread_for_subject(stream_id, 'lunch');
assert.equal(count, 0); assert.equal(count, 0);
var message = { var message = {
type: 'stream', type: 'stream',
stream: 'social', stream_id: stream_id,
subject: 'lunch', subject: 'lunch',
}; };
@ -209,7 +221,7 @@ stream_data.get_stream_id = function () {
unread.process_loaded_messages([message]); unread.process_loaded_messages([message]);
} }
count = unread.num_unread_for_subject('social', 'lunch'); count = unread.num_unread_for_subject(stream_id, 'lunch');
assert.equal(count, num_msgs); assert.equal(count, num_msgs);
for (i = 0; i < num_msgs; i += 1) { for (i = 0; i < num_msgs; i += 1) {
@ -217,7 +229,7 @@ stream_data.get_stream_id = function () {
unread.process_read_message(message); unread.process_read_message(message);
} }
count = unread.num_unread_for_subject('social', 'lunch'); count = unread.num_unread_for_subject(stream_id, 'lunch');
assert.equal(count, 0); assert.equal(count, 0);
}()); }());
@ -233,10 +245,18 @@ stream_data.get_stream_id = function () {
return true; return true;
}; };
var stream_id = 401;
stream_data.get_sub_by_id = function () {
return {
name: 'whatever',
};
};
var message = { var message = {
id: 15, id: 15,
type: 'stream', type: 'stream',
stream: 'social', stream_id: stream_id,
subject: 'lunch', subject: 'lunch',
}; };
@ -251,7 +271,7 @@ stream_data.get_stream_id = function () {
counts = unread.get_counts(); counts = unread.get_counts();
assert.equal(counts.home_unread_messages, 1); assert.equal(counts.home_unread_messages, 1);
assert.equal(counts.stream_count.get('social'), 1); assert.equal(counts.stream_count.get(stream_id), 1);
unread.process_read_message(message); unread.process_read_message(message);
counts = unread.get_counts(); counts = unread.get_counts();
assert.equal(counts.home_unread_messages, 0); assert.equal(counts.home_unread_messages, 0);
@ -273,10 +293,12 @@ stream_data.get_stream_id = function () {
var message = { var message = {
id: 999, id: 999,
type: 'stream', type: 'stream',
stream: 'foo', stream_id: 555,
subject: 'phantom', subject: 'phantom',
}; };
stream_data.get_sub_by_id = function () { return; };
unread.process_read_message(message); unread.process_read_message(message);
var counts = unread.get_counts(); var counts = unread.get_counts();
assert.equal(counts.home_unread_messages, 0); assert.equal(counts.home_unread_messages, 0);
@ -368,7 +390,7 @@ stream_data.get_stream_id = function () {
var message = { var message = {
id: 15, id: 15,
type: 'stream', type: 'stream',
stream: 'social', stream_id: 999,
subject: 'lunch', subject: 'lunch',
mentioned: true, mentioned: true,
}; };

View File

@ -216,7 +216,6 @@ function build_stream_sidebar_li(sub) {
function build_stream_sidebar_row(sub) { function build_stream_sidebar_row(sub) {
var self = {}; var self = {};
var list_item = build_stream_sidebar_li(sub); var list_item = build_stream_sidebar_li(sub);
var stream_name = sub.name;
self.update_whether_active = function () { self.update_whether_active = function () {
if (stream_data.is_active(sub)) { if (stream_data.is_active(sub)) {
@ -236,7 +235,7 @@ function build_stream_sidebar_row(sub) {
self.update_unread_count = function () { self.update_unread_count = function () {
var count = unread.num_unread_for_stream(stream_name); var count = unread.num_unread_for_stream(sub.stream_id);
update_count_in_dom(list_item, count); update_count_in_dom(list_item, count);
}; };
@ -275,8 +274,7 @@ function set_count(type, name, count) {
update_count_in_dom(unread_count_elem, count); update_count_in_dom(unread_count_elem, count);
} }
function set_stream_unread_count(stream_name, count) { function set_stream_unread_count(stream_id, count) {
var stream_id = stream_data.get_stream_id(stream_name);
var unread_count_elem = exports.get_stream_li(stream_id); var unread_count_elem = exports.get_stream_li(stream_id);
update_count_in_dom(unread_count_elem, count); update_count_in_dom(unread_count_elem, count);
} }
@ -313,14 +311,14 @@ exports.update_dom_with_unread_counts = function (counts) {
// and the buddy lists in the right sidebar. // and the buddy lists in the right sidebar.
// counts.stream_count maps streams to counts // counts.stream_count maps streams to counts
counts.stream_count.each(function (count, stream) { counts.stream_count.each(function (count, stream_id) {
set_stream_unread_count(stream, count); set_stream_unread_count(stream_id, count);
}); });
// counts.subject_count maps streams to hashes of topics to counts // counts.subject_count maps streams to hashes of topics to counts
counts.subject_count.each(function (subject_hash, stream) { counts.subject_count.each(function (subject_hash, stream_id) {
subject_hash.each(function (count, subject) { subject_hash.each(function (count, subject) {
topic_list.set_count(stream, subject, count); topic_list.set_count(stream_id, subject, count);
}); });
}); });

View File

@ -163,8 +163,9 @@ exports.get_next_topic = function (curr_stream, curr_topic) {
return _.map(topics, function (obj) { return obj.subject; }); return _.map(topics, function (obj) { return obj.subject; });
} }
function has_unread_messages(stream, topic) { function has_unread_messages(stream_name, topic) {
return unread.topic_has_any_unread(stream, topic); var stream_id = stream_data.get_stream_id(stream_name);
return unread.topic_has_any_unread(stream_id, topic);
} }
return exports.next_topic( return exports.next_topic(

View File

@ -39,8 +39,8 @@ function update_unread_count(unread_count_elem, count) {
} }
} }
exports.set_count = function (stream_name, topic, count) { exports.set_count = function (stream_id, topic, count) {
if (active_widget && active_widget.is_for_stream(stream_name)) { if (active_widget && active_widget.is_for_stream(stream_id)) {
active_widget.set_count(topic, count); active_widget.set_count(topic, count);
} }
}; };
@ -49,6 +49,14 @@ exports.build_widget = function (parent_elem, stream, active_topic, max_topics)
var self = {}; var self = {};
self.topic_items = new Dict({fold_case: true}); self.topic_items = new Dict({fold_case: true});
var my_stream_id = stream_data.get_stream_id(stream);
if (!my_stream_id) {
blueslip.error('Cannot find stream for ' + stream);
return;
}
function build_list(stream, active_topic, max_topics) { function build_list(stream, active_topic, max_topics) {
var topics = stream_data.get_recent_topics(stream) || []; var topics = stream_data.get_recent_topics(stream) || [];
@ -64,7 +72,7 @@ exports.build_widget = function (parent_elem, stream, active_topic, max_topics)
_.each(topics, function (subject_obj, idx) { _.each(topics, function (subject_obj, idx) {
var show_topic; var show_topic;
var topic_name = subject_obj.subject; var topic_name = subject_obj.subject;
var num_unread = unread.num_unread_for_subject(stream, subject_obj.canon_subject); var num_unread = unread.num_unread_for_subject(my_stream_id, subject_obj.canon_subject);
if (zoomed) { if (zoomed) {
show_topic = true; show_topic = true;
@ -107,8 +115,8 @@ exports.build_widget = function (parent_elem, stream, active_topic, max_topics)
return parent_elem; return parent_elem;
}; };
self.is_for_stream = function (stream_name) { self.is_for_stream = function (stream_id) {
return stream.toLowerCase() === stream_name.toLowerCase(); return stream_id === my_stream_id;
}; };
self.get_stream_name = function () { self.get_stream_name = function () {

View File

@ -14,7 +14,7 @@ exports.unread_topic_counter = (function () {
var self = {}; var self = {};
function str_dict() { function str_dict() {
// Use this when keys are streams and topics. // Use this when keys are topics
return new Dict({fold_case: true}); return new Dict({fold_case: true});
} }
@ -23,35 +23,35 @@ exports.unread_topic_counter = (function () {
return new Dict(); return new Dict();
} }
var unread_topics = str_dict(); // dict of stream -> topic -> msg id var unread_topics = num_dict(); // dict of stream -> topic -> msg id
self.clear = function () { self.clear = function () {
unread_topics = str_dict(); unread_topics = num_dict();
}; };
self.update = function (stream, subject, new_subject, msg_id) { self.update = function (stream_id, subject, new_subject, msg_id) {
if (unread_topics.has(stream) && if (unread_topics.has(stream_id) &&
unread_topics.get(stream).has(subject) && unread_topics.get(stream_id).has(subject) &&
unread_topics.get(stream).get(subject).get(msg_id)) { unread_topics.get(stream_id).get(subject).get(msg_id)) {
// Move the unread subject count to the new subject // Move the unread subject count to the new subject
unread_topics.get(stream).get(subject).del(msg_id); unread_topics.get(stream_id).get(subject).del(msg_id);
if (unread_topics.get(stream).get(subject).num_items() === 0) { if (unread_topics.get(stream_id).get(subject).num_items() === 0) {
unread_topics.get(stream).del(subject); unread_topics.get(stream_id).del(subject);
} }
unread_topics.get(stream).setdefault(new_subject, num_dict()); unread_topics.get(stream_id).setdefault(new_subject, num_dict());
unread_topics.get(stream).get(new_subject).set(msg_id, true); unread_topics.get(stream_id).get(new_subject).set(msg_id, true);
} }
}; };
self.add = function (stream, subject, msg_id) { self.add = function (stream_id, subject, msg_id) {
unread_topics.setdefault(stream, str_dict()); unread_topics.setdefault(stream_id, str_dict());
unread_topics.get(stream).setdefault(subject, num_dict()); unread_topics.get(stream_id).setdefault(subject, num_dict());
unread_topics.get(stream).get(subject).set(msg_id, true); unread_topics.get(stream_id).get(subject).set(msg_id, true);
}; };
self.del = function (stream, subject, msg_id) { self.del = function (stream_id, subject, msg_id) {
var stream_dict = unread_topics.get(stream); var stream_dict = unread_topics.get(stream_id);
if (stream_dict) { if (stream_dict) {
var subject_dict = stream_dict.get(subject); var subject_dict = stream_dict.get(subject);
if (subject_dict) { if (subject_dict) {
@ -63,31 +63,30 @@ exports.unread_topic_counter = (function () {
self.get_counts = function () { self.get_counts = function () {
var res = {}; var res = {};
res.stream_unread_messages = 0; res.stream_unread_messages = 0;
res.stream_count = str_dict(); // hash by stream -> count res.stream_count = num_dict(); // hash by stream_id -> count
res.subject_count = str_dict(); // hash of hashes (stream, then subject -> count) res.subject_count = num_dict(); // hash of hashes (stream_id, then subject -> count)
unread_topics.each(function (_, stream) { unread_topics.each(function (_, stream_id) {
// We track unread counts for streams that may be currently // We track unread counts for streams that may be currently
// unsubscribed. Since users may re-subscribe, we don't // unsubscribed. Since users may re-subscribe, we don't
// completely throw away the data. But we do ignore it here, // completely throw away the data. But we do ignore it here,
// so that callers have a view of the **current** world. // so that callers have a view of the **current** world.
if (! stream_data.is_subscribed(stream)) { var sub = stream_data.get_sub_by_id(stream_id);
if (!sub || !stream_data.is_subscribed(sub.name)) {
return true; return true;
} }
if (unread_topics.has(stream)) { if (unread_topics.has(stream_id)) {
res.subject_count.set(stream, str_dict()); res.subject_count.set(stream_id, str_dict());
var stream_count = 0; var stream_count = 0;
unread_topics.get(stream).each(function (msgs, subject) { unread_topics.get(stream_id).each(function (msgs, subject) {
var subject_count = msgs.num_items(); var subject_count = msgs.num_items();
res.subject_count.get(stream).set(subject, subject_count); res.subject_count.get(stream_id).set(subject, subject_count);
if (!muting.is_topic_muted(stream, subject)) { if (!muting.is_topic_muted(sub.name, subject)) {
stream_count += subject_count; stream_count += subject_count;
} }
}); });
res.stream_count.set(stream, stream_count); res.stream_count.set(stream_id, stream_count);
var stream_id = stream_data.get_stream_id(stream);
if (stream_data.in_home_view(stream_id)) { if (stream_data.in_home_view(stream_id)) {
res.stream_unread_messages += stream_count; res.stream_unread_messages += stream_count;
} }
@ -98,15 +97,16 @@ exports.unread_topic_counter = (function () {
return res; return res;
}; };
self.get_stream_count = function (stream) { self.get_stream_count = function (stream_id) {
var stream_count = 0; var stream_count = 0;
if (!unread_topics.has(stream)) { if (!unread_topics.has(stream_id)) {
return 0; return 0;
} }
unread_topics.get(stream).each(function (msgs, subject) { unread_topics.get(stream_id).each(function (msgs, subject) {
if (!muting.is_topic_muted(stream, subject)) { var sub = stream_data.get_sub_by_id(stream_id);
if (sub && !muting.is_topic_muted(sub.name, subject)) {
stream_count += msgs.num_items(); stream_count += msgs.num_items();
} }
}); });
@ -114,17 +114,17 @@ exports.unread_topic_counter = (function () {
return stream_count; return stream_count;
}; };
self.get = function (stream, subject) { self.get = function (stream_id, subject) {
var num_unread = 0; var num_unread = 0;
if (unread_topics.has(stream) && if (unread_topics.has(stream_id) &&
unread_topics.get(stream).has(subject)) { unread_topics.get(stream_id).has(subject)) {
num_unread = unread_topics.get(stream).get(subject).num_items(); num_unread = unread_topics.get(stream_id).get(subject).num_items();
} }
return num_unread; return num_unread;
}; };
self.topic_has_any_unread = function (stream, topic) { self.topic_has_any_unread = function (stream_id, topic) {
var stream_dct = unread_topics.get(stream); var stream_dct = unread_topics.get(stream_id);
if (!stream_dct) { if (!stream_dct) {
return false; return false;
@ -152,7 +152,7 @@ exports.message_unread = function (message) {
exports.update_unread_topics = function (msg, event) { exports.update_unread_topics = function (msg, event) {
if (event.subject !== undefined) { if (event.subject !== undefined) {
exports.unread_topic_counter.update( exports.unread_topic_counter.update(
msg.stream, msg.stream_id,
msg.subject, msg.subject,
event.subject, event.subject,
msg.id msg.id
@ -177,7 +177,7 @@ exports.process_loaded_messages = function (messages) {
if (message.type === 'stream') { if (message.type === 'stream') {
exports.unread_topic_counter.add( exports.unread_topic_counter.add(
message.stream, message.stream_id,
message.subject, message.subject,
message.id message.id
); );
@ -203,7 +203,7 @@ exports.process_read_message = function (message) {
if (message.type === 'stream') { if (message.type === 'stream') {
exports.unread_topic_counter.del( exports.unread_topic_counter.del(
message.stream, message.stream_id,
message.subject, message.subject,
message.id message.id
); );
@ -262,16 +262,16 @@ exports.get_counts = function () {
return res; return res;
}; };
exports.num_unread_for_stream = function (stream) { exports.num_unread_for_stream = function (stream_id) {
return exports.unread_topic_counter.get_stream_count(stream); return exports.unread_topic_counter.get_stream_count(stream_id);
}; };
exports.num_unread_for_subject = function (stream, subject) { exports.num_unread_for_subject = function (stream_id, subject) {
return exports.unread_topic_counter.get(stream, subject); return exports.unread_topic_counter.get(stream_id, subject);
}; };
exports.topic_has_any_unread = function (stream, topic) { exports.topic_has_any_unread = function (stream_id, topic) {
return exports.unread_topic_counter.topic_has_any_unread(stream, topic); return exports.unread_topic_counter.topic_has_any_unread(stream_id, topic);
}; };
exports.num_unread_for_person = function (user_ids_string) { exports.num_unread_for_person = function (user_ids_string) {