recent topics: Extract get_recent_topic_names().

We now have all of our callers into recent_topics code just
receive a list of topic names from get_recent_topic_names().

This is more encapsulated than handing off tiny little
structures to the three callers, two of whom immediately
mapped the objects to names, and one of whom needlessly
used the now defunct name canon_subject field.

The consolidation here removes some "subject" references, and
now all lookup are by stream id, not stream name.

The diff here is a bit daunting, but it's mostly simplification
of tests and calling code.  Two of the callers now need to look
up stream ids, but they are otherwise streamlined.

The main change here is to stream_data.js, and we replace the
`canon_subject` and `subject` fields with `name`.
This commit is contained in:
Steve Howell 2017-07-24 09:15:28 -04:00 committed by Tim Abbott
parent 611f44b339
commit b95e785335
8 changed files with 61 additions and 94 deletions

View File

@ -575,15 +575,21 @@ init();
};
var recent_data = {};
global.stream_data.populate_stream_topics_for_tests(recent_data);
suggestions = search.get_suggestions('te');
expected = [
"te",
];
assert.deepEqual(suggestions.strings, expected);
recent_data[devel_id] = [
{subject: 'REXX'},
{name: 'REXX'},
];
recent_data[office_id] = [
{subject: 'team'},
{subject: 'ignore'},
{subject: 'test'},
{name: 'team'},
{name: 'ignore'},
{name: 'test'},
];
global.stream_data.populate_stream_topics_for_tests(recent_data);
@ -745,9 +751,9 @@ init();
global.stream_data.populate_stream_topics_for_tests({
office: [
{subject: 'team'},
{subject: 'ignore'},
{subject: 'test'},
{name: 'team'},
{name: 'ignore'},
{name: 'test'},
],
});

View File

@ -257,15 +257,8 @@ var people = global.people;
};
stream_data.process_message_for_recent_topics(message);
var history = stream_data.get_recent_topics('Rome');
assert.deepEqual(history, [
{
subject: 'toPic1',
canon_subject: 'topic1',
count: 1,
timestamp: 101,
},
]);
var history = stream_data.get_recent_topic_names(rome.stream_id);
assert.deepEqual(history, ['toPic1']);
message = {
stream_id: stream_id,
@ -273,15 +266,8 @@ var people = global.people;
subject: 'Topic1',
};
stream_data.process_message_for_recent_topics(message);
history = stream_data.get_recent_topics('Rome');
assert.deepEqual(history, [
{
subject: 'Topic1',
canon_subject: 'topic1',
count: 2,
timestamp: 102,
},
]);
history = stream_data.get_recent_topic_names(rome.stream_id);
assert.deepEqual(history, ['Topic1']);
message = {
stream_id: stream_id,
@ -289,32 +275,12 @@ var people = global.people;
subject: 'topic2',
};
stream_data.process_message_for_recent_topics(message);
history = stream_data.get_recent_topics('Rome');
assert.deepEqual(history, [
{
subject: 'topic2',
canon_subject: 'topic2',
count: 1,
timestamp: 103,
},
{
subject: 'Topic1',
canon_subject: 'topic1',
count: 2,
timestamp: 102,
},
]);
history = stream_data.get_recent_topic_names(rome.stream_id);
assert.deepEqual(history, ['topic2', 'Topic1']);
stream_data.process_message_for_recent_topics(message, true);
history = stream_data.get_recent_topics('Rome');
assert.deepEqual(history, [
{
subject: 'Topic1',
canon_subject: 'topic1',
count: 2,
timestamp: 102,
},
]);
history = stream_data.get_recent_topic_names(rome.stream_id);
assert.deepEqual(history, ['Topic1']);
}());
(function test_is_active() {

View File

@ -174,28 +174,27 @@ function is_odd(i) { return i % 2 === 1; }
return ['announce', 'muted', 'devel', 'test here'];
};
global.stream_data.get_recent_topics = function (stream) {
if (stream === 'muted') {
return [
{subject: 'red herring'},
];
}
if (stream === 'devel') {
return [
{subject: 'muted'},
{subject: 'python'},
];
}
var muted_stream_id = 400;
var devel_stream_id = 401;
var stream_id_dct = {
muted: muted_stream_id,
devel: devel_stream_id,
};
var devel_stream_id = 555;
global.stream_data.get_stream_id = function (stream_name) {
if (stream_name === 'devel') {
return devel_stream_id;
global.stream_data.get_recent_topic_names = function (stream_id) {
switch (stream_id) {
case muted_stream_id:
return ['red herring'];
case devel_stream_id:
return ['muted', 'python'];
}
return 999;
return [];
};
global.stream_data.get_stream_id = function (stream_name) {
return stream_id_dct[stream_name];
};
global.stream_data.name_in_home_view = function (stream_name) {

View File

@ -25,7 +25,7 @@ global.compile_template('topic_list_item');
var recent_topics = {};
recent_topics[stream_id] = [
{subject: "coding"},
{name: "coding"},
];
global.stream_data.populate_stream_topics_for_tests(recent_topics);
global.unread.num_unread_for_subject = function () {

View File

@ -311,11 +311,15 @@ function get_topic_suggestions(last, operators) {
return [];
}
var topics = stream_data.get_recent_topics(stream);
stream = stream_data.get_name(stream);
var stream_id = stream_data.get_stream_id(stream);
if (!stream_id) {
return [];
}
if (!topics) {
var topics = stream_data.get_recent_topic_names(stream_id);
if (!topics || !topics.length) {
return [];
}
@ -323,10 +327,6 @@ function get_topic_suggestions(last, operators) {
// super huge, but still slice off enough topics to find matches.
topics = topics.slice(0, 300);
topics = _.map(topics, function (topic) {
return topic.subject; // "subject" is just the name of the topic
});
if (guess !== '') {
topics = _.filter(topics, function (topic) {
return phrase_match(topic, guess);

View File

@ -385,12 +385,13 @@ exports.process_message_for_recent_topics = function process_message_for_recent_
var current_timestamp = 0;
var count = 0;
var stream_id = message.stream_id;
var canon_subject = exports.canonicalized_name(message.subject);
var canon_topic = message.subject.toLowerCase();
var recents = recent_topics.get(stream_id) || [];
recents = _.filter(recents, function (item) {
var is_duplicate = (item.canon_subject.toLowerCase() === canon_subject.toLowerCase());
var is_duplicate = (
item.name.toLowerCase() === canon_topic);
if (is_duplicate) {
current_timestamp = item.timestamp;
count = item.count;
@ -405,8 +406,7 @@ exports.process_message_for_recent_topics = function process_message_for_recent_
}
if (count !== 0) {
recents.push({subject: message.subject,
canon_subject: canon_subject,
recents.push({name: message.subject,
count: count,
timestamp: Math.max(message.timestamp, current_timestamp)});
}
@ -472,19 +472,16 @@ exports.initialize_from_page_params = function () {
delete page_params.never_subscribed;
};
exports.get_recent_topics_for_id = function (stream_id) {
return recent_topics.get(stream_id);
};
exports.get_recent_topic_names = function (stream_id) {
var topic_objs = recent_topics.get(stream_id);
exports.get_recent_topics = function (stream_name) {
// TODO: deprecate this and have callers use
// get_recent_topics_for_id
var stream_id = exports.get_stream_id(stream_name);
if (!stream_id) {
if (!topic_objs) {
return [];
}
return recent_topics.get(stream_id);
return _.map(topic_objs, function (obj) {
return obj.name;
});
};
exports.populate_stream_topics_for_tests = function (stream_map) {

View File

@ -163,8 +163,8 @@ exports.get_next_topic = function (curr_stream, curr_topic) {
});
function get_unmuted_topics(stream_name) {
var topic_objs = stream_data.get_recent_topics(stream_name) || [];
var topics = _.map(topic_objs, function (obj) { return obj.subject; });
var stream_id = stream_data.get_stream_id(stream_name);
var topics = stream_data.get_recent_topic_names(stream_id);
topics = _.reject(topics, function (topic) {
return muting.is_topic_muted(stream_name, topic);
});

View File

@ -52,7 +52,7 @@ exports.build_widget = function (parent_elem, my_stream_id, active_topic, max_to
var my_stream_name = stream_data.get_sub_by_id(my_stream_id).name;
function build_list(active_topic, max_topics) {
var topics = stream_data.get_recent_topics_for_id(my_stream_id) || [];
var topic_names = stream_data.get_recent_topic_names(my_stream_id);
if (active_topic) {
active_topic = active_topic.toLowerCase();
@ -63,10 +63,9 @@ exports.build_widget = function (parent_elem, my_stream_id, active_topic, max_to
var ul = $('<ul class="topic-list">');
ul.attr('data-stream', my_stream_name);
_.each(topics, function (subject_obj, idx) {
_.each(topic_names, function (topic_name, idx) {
var show_topic;
var topic_name = subject_obj.subject;
var num_unread = unread.num_unread_for_subject(my_stream_id, subject_obj.canon_subject);
var num_unread = unread.num_unread_for_subject(my_stream_id, topic_name);
if (zoomed) {
show_topic = true;