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) {
return (stream === 'devel' && topic === 'python');
var devel_stream_id = 555;
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);

View File

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

View File

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

View File

@ -216,7 +216,6 @@ function build_stream_sidebar_li(sub) {
function build_stream_sidebar_row(sub) {
var self = {};
var list_item = build_stream_sidebar_li(sub);
var stream_name = sub.name;
self.update_whether_active = function () {
if (stream_data.is_active(sub)) {
@ -236,7 +235,7 @@ function build_stream_sidebar_row(sub) {
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);
};
@ -275,8 +274,7 @@ function set_count(type, name, count) {
update_count_in_dom(unread_count_elem, count);
}
function set_stream_unread_count(stream_name, count) {
var stream_id = stream_data.get_stream_id(stream_name);
function set_stream_unread_count(stream_id, count) {
var unread_count_elem = exports.get_stream_li(stream_id);
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.
// counts.stream_count maps streams to counts
counts.stream_count.each(function (count, stream) {
set_stream_unread_count(stream, count);
counts.stream_count.each(function (count, stream_id) {
set_stream_unread_count(stream_id, count);
});
// 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) {
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; });
}
function has_unread_messages(stream, topic) {
return unread.topic_has_any_unread(stream, topic);
function has_unread_messages(stream_name, topic) {
var stream_id = stream_data.get_stream_id(stream_name);
return unread.topic_has_any_unread(stream_id, 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) {
if (active_widget && active_widget.is_for_stream(stream_name)) {
exports.set_count = function (stream_id, topic, count) {
if (active_widget && active_widget.is_for_stream(stream_id)) {
active_widget.set_count(topic, count);
}
};
@ -49,6 +49,14 @@ exports.build_widget = function (parent_elem, stream, active_topic, max_topics)
var self = {};
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) {
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) {
var show_topic;
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) {
show_topic = true;
@ -107,8 +115,8 @@ exports.build_widget = function (parent_elem, stream, active_topic, max_topics)
return parent_elem;
};
self.is_for_stream = function (stream_name) {
return stream.toLowerCase() === stream_name.toLowerCase();
self.is_for_stream = function (stream_id) {
return stream_id === my_stream_id;
};
self.get_stream_name = function () {

View File

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