Fix PM list sort ordering during scrollback situations.

Before this fix, if you scrolled back in your PM history for a
person that you've had recent conversations with, then we would
backdate the record of their most recent conversation, and this
would make the sort ordering under the "Private messages"
section incorrect.

This commit fixes this error by re-writing the function
message_store.insert_recent_private_message() to check any
prior timestamps for that user.  It also optimizes the function
a bit to short-circuit in O(1) time for cases where a recipient
already has a more recent timestamp, by having a Dict keyed
on user_ids_string.
This commit is contained in:
Steve Howell 2017-04-13 11:57:06 -07:00 committed by Tim Abbott
parent 24461762da
commit f06bd41586
2 changed files with 32 additions and 14 deletions

View File

@ -52,6 +52,9 @@ var message_store = require('js/message_store.js');
message_store.insert_recent_private_message('2', 2001);
message_store.insert_recent_private_message('1', 3001);
// try to backdate user1's timestamp
message_store.insert_recent_private_message('1', 555);
assert.deepEqual(message_store.recent_private_messages, [
{user_ids_string: '1', timestamp: 3001},
{user_ids_string: '2', timestamp: 2001},

View File

@ -71,22 +71,37 @@ exports.process_message_for_recent_private_messages = function (message) {
exports.insert_recent_private_message(user_ids_string, message.timestamp);
};
exports.insert_recent_private_message = function (user_ids_string, timestamp) {
// If this conversation is already tracked, we'll replace with new timestamp,
// so remove it from the current list.
exports.recent_private_messages = _.filter(exports.recent_private_messages,
function (recent_pm) {
return recent_pm.user_ids_string !== user_ids_string;
});
exports.insert_recent_private_message = (function () {
var recent_timestamps = new Dict({fold_case: true}); // key is user_ids_string
var new_conversation = {user_ids_string: user_ids_string,
timestamp: timestamp};
return function (user_ids_string, timestamp) {
var conversation = recent_timestamps.get(user_ids_string);
if (conversation === undefined) {
// This is a new user, so create a new object.
conversation = {
user_ids_string: user_ids_string,
timestamp: timestamp,
};
recent_timestamps.set(user_ids_string, conversation);
// Optimistically insert the new message at the front, since that
// is usually where it belongs, but we'll re-sort.
exports.recent_private_messages.unshift(conversation);
} else {
if (conversation.timestamp >= timestamp) {
return; // don't backdate our conversation
}
// update our timestamp
conversation.timestamp = timestamp;
}
exports.recent_private_messages.push(new_conversation);
exports.recent_private_messages.sort(function (a, b) {
return b.timestamp - a.timestamp;
});
};
};
}());
exports.set_topic_edit_properties = function (message) {
message.always_visible_topic_edit = false;