pm_conversations: Refactor to sort by message ID.

message_id, rather than timestamps, is our standard way to sort by
time.  And this refactor is important because we're about to start
using data from the server to populate this data structure.
This commit is contained in:
Tim Abbott 2019-11-21 11:47:14 -08:00
parent 89ff62dafa
commit 93b83b28a7
3 changed files with 18 additions and 15 deletions

View File

@ -18,12 +18,12 @@ run_test('insert_recent_private_message', () => {
pmc.recent.insert('2', 2001); pmc.recent.insert('2', 2001);
pmc.recent.insert('1', 3001); pmc.recent.insert('1', 3001);
// try to backdate user1's timestamp // try to backdate user1's latest message
pmc.recent.insert('1', 555); pmc.recent.insert('1', 555);
assert.deepEqual(pmc.recent.get(), [ assert.deepEqual(pmc.recent.get(), [
{user_ids_string: '1', timestamp: 3001}, {user_ids_string: '1', max_message_id: 3001},
{user_ids_string: '2', timestamp: 2001}, {user_ids_string: '2', max_message_id: 2001},
]); ]);
assert.deepEqual(pmc.recent.get_strings(), ['1', '2']); assert.deepEqual(pmc.recent.get_strings(), ['1', '2']);

View File

@ -56,7 +56,7 @@ exports.process_message_for_recent_private_messages = function (message) {
const user_ids_string = user_ids.join(','); const user_ids_string = user_ids.join(',');
pm_conversations.recent.insert(user_ids_string, message.timestamp); pm_conversations.recent.insert(user_ids_string, message.id);
}; };
exports.set_message_booleans = function (message) { exports.set_message_booleans = function (message) {

View File

@ -11,47 +11,50 @@ exports.is_partner = function (user_id) {
}; };
exports.recent = (function () { exports.recent = (function () {
// This data structure keeps track of the sets of users you've had
// recent conversations with, sorted by time (implemented via
// `message_id` sorting, since that's how we time-sort messages).
const self = {}; const self = {};
const recent_timestamps = new Dict({fold_case: true}); // key is user_ids_string const recent_message_ids = new Dict({fold_case: true}); // key is user_ids_string
const recent_private_messages = []; const recent_private_messages = [];
self.insert = function (user_ids_string, timestamp) { self.insert = function (user_ids_string, message_id) {
let conversation = recent_timestamps.get(user_ids_string); let conversation = recent_message_ids.get(user_ids_string);
if (conversation === undefined) { if (conversation === undefined) {
// This is a new user, so create a new object. // This is a new user, so create a new object.
conversation = { conversation = {
user_ids_string: user_ids_string, user_ids_string: user_ids_string,
timestamp: timestamp, max_message_id: message_id,
}; };
recent_timestamps.set(user_ids_string, conversation); recent_message_ids.set(user_ids_string, conversation);
// Optimistically insert the new message at the front, since that // Optimistically insert the new message at the front, since that
// is usually where it belongs, but we'll re-sort. // is usually where it belongs, but we'll re-sort.
recent_private_messages.unshift(conversation); recent_private_messages.unshift(conversation);
} else { } else {
if (conversation.timestamp >= timestamp) { if (conversation.max_message_id >= message_id) {
return; // don't backdate our conversation return; // don't backdate our conversation
} }
// update our timestamp // update our latest message_id
conversation.timestamp = timestamp; conversation.max_message_id = message_id;
} }
recent_private_messages.sort(function (a, b) { recent_private_messages.sort(function (a, b) {
return b.timestamp - a.timestamp; return b.max_message_id - a.max_message_id;
}); });
}; };
self.get = function () { self.get = function () {
// returns array of structs with user_ids_string and // returns array of structs with user_ids_string and
// timestamp // message_id
return recent_private_messages; return recent_private_messages;
}; };
self.get_strings = function () { self.get_strings = function () {
// returns array of structs with user_ids_string and // returns array of structs with user_ids_string and
// timestamp // message_id
return _.pluck(recent_private_messages, 'user_ids_string'); return _.pluck(recent_private_messages, 'user_ids_string');
}; };