Track recent topics (and active streams) using stream id.

This commit changes the key for recent_topics to be a
stream id.  For streams that have been renamed, we will now
get accurate data on recent topics and active streams as
long as stream_data.get_stream_id(stream_name) returns a
valid value.
This commit is contained in:
Steve Howell 2017-05-14 06:39:02 -07:00 committed by Tim Abbott
parent 5d33d02235
commit efb35afeb7
4 changed files with 62 additions and 37 deletions

View File

@ -432,16 +432,29 @@ init();
return 'office'; return 'office';
}; };
global.stream_data.populate_stream_topics_for_tests({ var devel_id = 44;
devel: [ var office_id = 77;
{subject: 'REXX'},
], global.stream_data.get_stream_id = function (stream_name) {
office: [ switch (stream_name) {
{subject: 'team'}, case 'office': return office_id;
{subject: 'ignore'}, case 'devel': return devel_id;
{subject: 'test'}, }
], };
});
var recent_data = {};
recent_data[devel_id] = [
{subject: 'REXX'},
];
recent_data[office_id] = [
{subject: 'team'},
{subject: 'ignore'},
{subject: 'test'},
];
global.stream_data.populate_stream_topics_for_tests(recent_data);
suggestions = search.get_suggestions('te'); suggestions = search.get_suggestions('te');
expected = [ expected = [

View File

@ -240,8 +240,17 @@ var people = global.people;
}()); }());
(function test_process_message_for_recent_topics() { (function test_process_message_for_recent_topics() {
var stream_id = 55;
var rome = {
name: 'Rome',
stream_id: stream_id,
};
stream_data.add_sub('Rome', rome);
var message = { var message = {
stream: 'Rome', stream_id: stream_id,
timestamp: 101, timestamp: 101,
subject: 'toPic1', subject: 'toPic1',
}; };
@ -258,7 +267,7 @@ var people = global.people;
]); ]);
message = { message = {
stream: 'Rome', stream_id: stream_id,
timestamp: 102, timestamp: 102,
subject: 'Topic1', subject: 'Topic1',
}; };
@ -274,7 +283,7 @@ var people = global.people;
]); ]);
message = { message = {
stream: 'Rome', stream_id: stream_id,
timestamp: 103, timestamp: 103,
subject: 'topic2', subject: 'topic2',
}; };
@ -321,13 +330,13 @@ var people = global.people;
stream_data.unsubscribe_myself(sub); stream_data.unsubscribe_myself(sub);
assert(!stream_data.is_active(sub)); assert(!stream_data.is_active(sub));
sub = {name: 'lunch', subscribed: false, stream_id: 1}; sub = {name: 'lunch', subscribed: false, stream_id: 222};
stream_data.add_sub('lunch', sub); stream_data.add_sub('lunch', sub);
assert(!stream_data.is_active(sub)); assert(!stream_data.is_active(sub));
var message = { var message = {
stream: 'lunch', stream_id: 222,
timestamp: 108, timestamp: 108,
subject: 'topic2', subject: 'topic2',
}; };

View File

@ -19,19 +19,21 @@ var topic_list = require('js/topic_list.js');
global.compile_template('topic_list_item'); global.compile_template('topic_list_item');
(function test_topic_list_build_widget() { (function test_topic_list_build_widget() {
var stream_id = 555;
var stream = "devel"; var stream = "devel";
var active_topic = "testing"; var active_topic = "testing";
var max_topics = 5; var max_topics = 5;
var topics = [ var recent_topics = {};
recent_topics[stream_id] = [
{subject: "coding"}, {subject: "coding"},
]; ];
global.stream_data.populate_stream_topics_for_tests({devel: topics}); global.stream_data.populate_stream_topics_for_tests(recent_topics);
global.unread.num_unread_for_subject = function () { global.unread.num_unread_for_subject = function () {
return 1; return 1;
}; };
global.stream_data.get_stream_id = function () { return 99; }; global.stream_data.get_stream_id = function () { return stream_id; };
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);

View File

@ -7,7 +7,7 @@ var exports = {};
// Call clear_subscriptions() to initialize it. // Call clear_subscriptions() to initialize it.
var stream_info; var stream_info;
var subs_by_stream_id; var subs_by_stream_id;
var recent_topics = new Dict({fold_case: true}); var recent_topics = new Dict(); // stream_id -> array of objects
var stream_ids_by_name = new Dict({fold_case: true}); var stream_ids_by_name = new Dict({fold_case: true});
@ -21,7 +21,7 @@ exports.clear_subscriptions = function () {
exports.clear_subscriptions(); exports.clear_subscriptions();
exports.is_active = function (sub) { exports.is_active = function (sub) {
return recent_topics.has(sub.name) || sub.newly_subscribed; return recent_topics.has(sub.stream_id) || sub.newly_subscribed;
}; };
exports.rename_sub = function (sub, new_name) { exports.rename_sub = function (sub, new_name) {
@ -367,23 +367,19 @@ exports.process_message_for_recent_topics = function process_message_for_recent_
message, remove_message) { message, remove_message) {
var current_timestamp = 0; var current_timestamp = 0;
var count = 0; var count = 0;
var stream = message.stream; var stream_id = message.stream_id;
var canon_subject = exports.canonicalized_name(message.subject); var canon_subject = exports.canonicalized_name(message.subject);
if (! recent_topics.has(stream)) { var recents = recent_topics.get(stream_id) || [];
recent_topics.set(stream, []);
} else {
recent_topics.set(stream, _.filter(recent_topics.get(stream), function (item) {
var is_duplicate = (item.canon_subject.toLowerCase() === canon_subject.toLowerCase());
if (is_duplicate) {
current_timestamp = item.timestamp;
count = item.count;
}
return !is_duplicate;
}));
}
var recents = recent_topics.get(stream); recents = _.filter(recents, function (item) {
var is_duplicate = (item.canon_subject.toLowerCase() === canon_subject.toLowerCase());
if (is_duplicate) {
current_timestamp = item.timestamp;
count = item.count;
}
return !is_duplicate;
});
if (remove_message !== undefined) { if (remove_message !== undefined) {
count = count - 1; count = count - 1;
@ -402,7 +398,7 @@ exports.process_message_for_recent_topics = function process_message_for_recent_
return b.timestamp - a.timestamp; return b.timestamp - a.timestamp;
}); });
recent_topics.set(stream, recents); recent_topics.set(stream_id, recents);
}; };
exports.get_streams_for_settings_page = function () { exports.get_streams_for_settings_page = function () {
@ -451,12 +447,17 @@ exports.initialize_from_page_params = function () {
}; };
exports.get_recent_topics = function (stream_name) { exports.get_recent_topics = function (stream_name) {
return recent_topics.get(stream_name); var stream_id = exports.get_stream_id(stream_name);
if (!stream_id) {
return [];
}
return recent_topics.get(stream_id);
}; };
exports.populate_stream_topics_for_tests = function (stream_map) { exports.populate_stream_topics_for_tests = function (stream_map) {
// This is only used by tests. // This is only used by tests.
recent_topics = Dict.from(stream_map, {fold_case: true}); recent_topics = Dict.from(stream_map);
}; };
exports.get_newbie_stream = function () { exports.get_newbie_stream = function () {