mirror of https://github.com/zulip/zulip.git
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:
parent
89ff62dafa
commit
93b83b28a7
|
@ -18,12 +18,12 @@ run_test('insert_recent_private_message', () => {
|
|||
pmc.recent.insert('2', 2001);
|
||||
pmc.recent.insert('1', 3001);
|
||||
|
||||
// try to backdate user1's timestamp
|
||||
// try to backdate user1's latest message
|
||||
pmc.recent.insert('1', 555);
|
||||
|
||||
assert.deepEqual(pmc.recent.get(), [
|
||||
{user_ids_string: '1', timestamp: 3001},
|
||||
{user_ids_string: '2', timestamp: 2001},
|
||||
{user_ids_string: '1', max_message_id: 3001},
|
||||
{user_ids_string: '2', max_message_id: 2001},
|
||||
]);
|
||||
|
||||
assert.deepEqual(pmc.recent.get_strings(), ['1', '2']);
|
||||
|
|
|
@ -56,7 +56,7 @@ exports.process_message_for_recent_private_messages = function (message) {
|
|||
|
||||
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) {
|
||||
|
|
|
@ -11,47 +11,50 @@ exports.is_partner = function (user_id) {
|
|||
};
|
||||
|
||||
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 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 = [];
|
||||
|
||||
self.insert = function (user_ids_string, timestamp) {
|
||||
let conversation = recent_timestamps.get(user_ids_string);
|
||||
self.insert = function (user_ids_string, message_id) {
|
||||
let conversation = recent_message_ids.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,
|
||||
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
|
||||
// is usually where it belongs, but we'll re-sort.
|
||||
recent_private_messages.unshift(conversation);
|
||||
} else {
|
||||
if (conversation.timestamp >= timestamp) {
|
||||
if (conversation.max_message_id >= message_id) {
|
||||
return; // don't backdate our conversation
|
||||
}
|
||||
|
||||
// update our timestamp
|
||||
conversation.timestamp = timestamp;
|
||||
// update our latest message_id
|
||||
conversation.max_message_id = message_id;
|
||||
}
|
||||
|
||||
recent_private_messages.sort(function (a, b) {
|
||||
return b.timestamp - a.timestamp;
|
||||
return b.max_message_id - a.max_message_id;
|
||||
});
|
||||
};
|
||||
|
||||
self.get = function () {
|
||||
// returns array of structs with user_ids_string and
|
||||
// timestamp
|
||||
// message_id
|
||||
return recent_private_messages;
|
||||
};
|
||||
|
||||
self.get_strings = function () {
|
||||
// returns array of structs with user_ids_string and
|
||||
// timestamp
|
||||
// message_id
|
||||
return _.pluck(recent_private_messages, 'user_ids_string');
|
||||
};
|
||||
|
||||
|
|
Loading…
Reference in New Issue