2019-02-08 11:56:33 +01:00
|
|
|
var Dict = require('../ts/dict').Dict;
|
|
|
|
|
2017-06-01 07:46:23 +02:00
|
|
|
var pm_conversations = (function () {
|
|
|
|
|
|
|
|
var exports = {};
|
|
|
|
|
|
|
|
var partners = new Dict();
|
|
|
|
|
|
|
|
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 () {
|
|
|
|
var self = {};
|
|
|
|
var recent_timestamps = new Dict({fold_case: true}); // key is user_ids_string
|
|
|
|
var recent_private_messages = [];
|
|
|
|
|
|
|
|
self.insert = 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.
|
|
|
|
recent_private_messages.unshift(conversation);
|
|
|
|
} else {
|
|
|
|
if (conversation.timestamp >= timestamp) {
|
|
|
|
return; // don't backdate our conversation
|
|
|
|
}
|
|
|
|
|
|
|
|
// update our timestamp
|
|
|
|
conversation.timestamp = timestamp;
|
|
|
|
}
|
|
|
|
|
|
|
|
recent_private_messages.sort(function (a, b) {
|
|
|
|
return b.timestamp - a.timestamp;
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
self.get = function () {
|
|
|
|
// returns array of structs with user_ids_string and
|
|
|
|
// timestamp
|
|
|
|
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
|
|
|
|
// timestamp
|
|
|
|
return _.pluck(recent_private_messages, 'user_ids_string');
|
|
|
|
};
|
|
|
|
|
2018-02-09 18:10:39 +01:00
|
|
|
return self;
|
|
|
|
}());
|
|
|
|
|
2017-06-01 07:46:23 +02:00
|
|
|
return exports;
|
|
|
|
}());
|
|
|
|
|
|
|
|
if (typeof module !== 'undefined') {
|
|
|
|
module.exports = pm_conversations;
|
|
|
|
}
|
2018-05-28 08:04:36 +02:00
|
|
|
window.pm_conversations = pm_conversations;
|