2019-11-02 00:06:25 +01:00
|
|
|
const Dict = require('./dict').Dict;
|
2019-02-08 11:56:33 +01:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const partners = new Dict();
|
2017-06-01 07:46:23 +02:00
|
|
|
|
|
|
|
exports.set_partner = function (user_id) {
|
|
|
|
partners.set(user_id, true);
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.is_partner = function (user_id) {
|
|
|
|
return partners.get(user_id) || false;
|
|
|
|
};
|
|
|
|
|
2018-02-09 18:10:39 +01:00
|
|
|
exports.recent = (function () {
|
2019-11-21 20:47:14 +01:00
|
|
|
// 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).
|
2019-11-02 00:06:25 +01:00
|
|
|
const self = {};
|
2019-11-21 20:47:14 +01:00
|
|
|
const recent_message_ids = new Dict({fold_case: true}); // key is user_ids_string
|
2019-11-02 00:06:25 +01:00
|
|
|
const recent_private_messages = [];
|
2018-02-09 18:10:39 +01:00
|
|
|
|
2020-01-01 15:42:46 +01:00
|
|
|
self.insert = function (user_ids, message_id) {
|
|
|
|
if (user_ids.length === 0) {
|
|
|
|
// The server sends [] for self-PMs.
|
|
|
|
user_ids = [people.my_current_user_id()];
|
2019-11-21 21:01:15 +01:00
|
|
|
}
|
2020-01-01 15:55:36 +01:00
|
|
|
user_ids.sort((a, b) => a - b);
|
|
|
|
|
2020-01-01 15:42:46 +01:00
|
|
|
const user_ids_string = user_ids.join(',');
|
2019-11-21 20:47:14 +01:00
|
|
|
let conversation = recent_message_ids.get(user_ids_string);
|
2018-02-09 18:10:39 +01:00
|
|
|
|
|
|
|
if (conversation === undefined) {
|
|
|
|
// This is a new user, so create a new object.
|
|
|
|
conversation = {
|
|
|
|
user_ids_string: user_ids_string,
|
2019-11-21 20:47:14 +01:00
|
|
|
max_message_id: message_id,
|
2018-02-09 18:10:39 +01:00
|
|
|
};
|
2019-11-21 20:47:14 +01:00
|
|
|
recent_message_ids.set(user_ids_string, conversation);
|
2018-02-09 18:10:39 +01:00
|
|
|
|
|
|
|
// 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 {
|
2019-11-21 20:47:14 +01:00
|
|
|
if (conversation.max_message_id >= message_id) {
|
2019-11-21 21:01:15 +01:00
|
|
|
// don't backdate our conversation. This is the
|
|
|
|
// common code path after initialization when
|
|
|
|
// processing old messages, since we'll already have
|
|
|
|
// the latest message_id for the conversation from
|
|
|
|
// initialization.
|
|
|
|
return;
|
2018-02-09 18:10:39 +01:00
|
|
|
}
|
|
|
|
|
2019-11-21 20:47:14 +01:00
|
|
|
// update our latest message_id
|
|
|
|
conversation.max_message_id = message_id;
|
2018-02-09 18:10:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
recent_private_messages.sort(function (a, b) {
|
2019-11-21 20:47:14 +01:00
|
|
|
return b.max_message_id - a.max_message_id;
|
2018-02-09 18:10:39 +01:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
self.get = function () {
|
|
|
|
// returns array of structs with user_ids_string and
|
2019-11-21 20:47:14 +01:00
|
|
|
// message_id
|
2018-02-09 18:10:39 +01:00
|
|
|
return recent_private_messages;
|
|
|
|
};
|
|
|
|
|
2018-02-09 21:13:38 +01:00
|
|
|
self.get_strings = function () {
|
|
|
|
// returns array of structs with user_ids_string and
|
2019-11-21 20:47:14 +01:00
|
|
|
// message_id
|
2018-02-09 21:13:38 +01:00
|
|
|
return _.pluck(recent_private_messages, 'user_ids_string');
|
|
|
|
};
|
|
|
|
|
2019-11-21 21:01:15 +01:00
|
|
|
self.initialize = function () {
|
|
|
|
_.each(page_params.recent_private_conversations, function (conversation) {
|
2020-01-01 15:42:46 +01:00
|
|
|
self.insert(conversation.user_ids, conversation.max_message_id);
|
2019-11-21 21:01:15 +01:00
|
|
|
});
|
|
|
|
delete page_params.recent_private_messages;
|
|
|
|
};
|
|
|
|
|
2018-02-09 18:10:39 +01:00
|
|
|
return self;
|
|
|
|
}());
|
|
|
|
|
2019-10-25 09:45:13 +02:00
|
|
|
window.pm_conversations = exports;
|