Move process_message_for_recent_topics() to stream_data.js.

This commit is contained in:
Steve Howell 2016-10-28 08:31:18 -07:00 committed by Tim Abbott
parent c799ac3126
commit 8acdf718bc
4 changed files with 114 additions and 46 deletions

View File

@ -149,6 +149,74 @@ var stream_data = require('js/stream_data.js');
}());
(function test_process_message_for_recent_topics() {
var message = {
stream: 'Rome',
timestamp: 101,
subject: 'toPic1'
};
stream_data.process_message_for_recent_topics(message);
var history = stream_data.recent_subjects.get('Rome');
assert.deepEqual(history, [
{
subject: 'toPic1',
canon_subject: 'topic1',
count: 1,
timestamp: 101
}
]);
message = {
stream: 'Rome',
timestamp: 102,
subject: 'Topic1'
};
stream_data.process_message_for_recent_topics(message);
history = stream_data.recent_subjects.get('Rome');
assert.deepEqual(history, [
{
subject: 'Topic1',
canon_subject: 'topic1',
count: 2,
timestamp: 102
}
]);
message = {
stream: 'Rome',
timestamp: 103,
subject: 'topic2'
};
stream_data.process_message_for_recent_topics(message);
history = stream_data.recent_subjects.get('Rome');
assert.deepEqual(history, [
{
subject: 'topic2',
canon_subject: 'topic2',
count: 1,
timestamp: 103
},
{
subject: 'Topic1',
canon_subject: 'topic1',
count: 2,
timestamp: 102
}
]);
stream_data.process_message_for_recent_topics(message, true);
history = stream_data.recent_subjects.get('Rome');
assert.deepEqual(history, [
{
subject: 'Topic1',
canon_subject: 'topic1',
count: 2,
timestamp: 102
}
]);
}());
(function test_admin_options() {
function make_sub() {
return {

View File

@ -183,9 +183,9 @@ exports.try_deliver_locally = function try_deliver_locally(message_request) {
exports.edit_locally = function edit_locally(message, raw_content, new_topic) {
message.raw_content = raw_content;
if (new_topic !== undefined) {
message_store.process_message_for_recent_topics(message, true);
stream_data.process_message_for_recent_topics(message, true);
message.subject = new_topic;
message_store.process_message_for_recent_topics(message);
stream_data.process_message_for_recent_topics(message);
}
message.content = exports.apply_markdown(raw_content);

View File

@ -39,47 +39,6 @@ exports.get_private_message_recipient = function (message, attr, fallback_attr)
return recipient;
};
exports.process_message_for_recent_topics = function process_message_for_recent_topics(message, remove_message) {
var current_timestamp = 0;
var count = 0;
var stream = message.stream;
var canon_subject = stream_data.canonicalized_name(message.subject);
if (! stream_data.recent_subjects.has(stream)) {
stream_data.recent_subjects.set(stream, []);
} else {
stream_data.recent_subjects.set(stream, _.filter(stream_data.recent_subjects.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 = stream_data.recent_subjects.get(stream);
if (remove_message !== undefined) {
count = count - 1;
} else {
count = count + 1;
}
if (count !== 0) {
recents.push({subject: message.subject,
canon_subject: canon_subject,
count: count,
timestamp: Math.max(message.timestamp, current_timestamp)});
}
recents.sort(function (a, b) {
return b.timestamp - a.timestamp;
});
stream_data.recent_subjects.set(stream, recents);
};
exports.process_message_for_recent_private_messages = function process_message_for_recent_private_messages(message, remove_message) {
var current_timestamp = 0;
@ -149,7 +108,7 @@ function add_message_metadata(message) {
composebox_typeahead.add_topic(message.stream, message.subject);
message.reply_to = message.sender_email;
exports.process_message_for_recent_topics(message);
stream_data.process_message_for_recent_topics(message);
involved_people = [{'full_name': message.sender_full_name,
'email': message.sender_email}];
@ -334,7 +293,7 @@ exports.update_messages = function update_messages(events) {
// Remove the recent topics entry for the old topics;
// must be called before we update msg.subject
exports.process_message_for_recent_topics(msg, true);
stream_data.process_message_for_recent_topics(msg, true);
// Update the unread counts; again, this must be called
// before we update msg.subject
unread.update_unread_topics(msg, event);
@ -344,7 +303,7 @@ exports.update_messages = function update_messages(events) {
set_topic_edit_properties(msg);
// Add the recent topics entry for the new topics; must
// be called after we update msg.subject
exports.process_message_for_recent_topics(msg);
stream_data.process_message_for_recent_topics(msg);
});
}

View File

@ -230,6 +230,47 @@ exports.add_admin_options = function (sub) {
});
};
exports.process_message_for_recent_topics = function process_message_for_recent_topics(message, remove_message) {
var current_timestamp = 0;
var count = 0;
var stream = message.stream;
var canon_subject = exports.canonicalized_name(message.subject);
if (! exports.recent_subjects.has(stream)) {
exports.recent_subjects.set(stream, []);
} else {
exports.recent_subjects.set(stream, _.filter(exports.recent_subjects.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 = exports.recent_subjects.get(stream);
if (remove_message !== undefined) {
count = count - 1;
} else {
count = count + 1;
}
if (count !== 0) {
recents.push({subject: message.subject,
canon_subject: canon_subject,
count: count,
timestamp: Math.max(message.timestamp, current_timestamp)});
}
recents.sort(function (a, b) {
return b.timestamp - a.timestamp;
});
exports.recent_subjects.set(stream, recents);
};
exports.get_streams_for_settings_page = function () {
// Build up our list of subscribed streams from the data we already have.
var subscribed_rows = exports.subscribed_subs();