2019-03-24 08:37:53 +01:00
|
|
|
var Dict = require('./dict').Dict;
|
2019-02-08 11:56:33 +01:00
|
|
|
|
2017-06-01 07:42:57 +02:00
|
|
|
var recent_senders = (function () {
|
|
|
|
|
|
|
|
var exports = {};
|
|
|
|
|
2017-08-29 16:57:47 +02:00
|
|
|
var topic_senders = new Dict(); // key is stream-id, value is Dict
|
|
|
|
var stream_senders = new Dict(); // key is stream-id, value is Dict
|
2017-06-01 07:42:57 +02:00
|
|
|
|
|
|
|
exports.process_message_for_senders = function (message) {
|
|
|
|
var stream_id = message.stream_id.toString();
|
2018-12-23 16:01:30 +01:00
|
|
|
var topic = util.get_message_topic(message);
|
2017-08-29 16:57:47 +02:00
|
|
|
|
|
|
|
// Process most recent sender to topic
|
|
|
|
var topic_dict = topic_senders.get(stream_id) || new Dict({fold_case: true});
|
2018-12-23 16:01:30 +01:00
|
|
|
var sender_message_ids = topic_dict.get(topic) || new Dict();
|
2018-02-08 21:08:24 +01:00
|
|
|
var old_message_id = sender_message_ids.get(message.sender_id);
|
2017-06-01 07:42:57 +02:00
|
|
|
|
2018-02-08 21:08:24 +01:00
|
|
|
if (old_message_id === undefined || old_message_id < message.id) {
|
|
|
|
sender_message_ids.set(message.sender_id, message.id);
|
2017-06-01 07:42:57 +02:00
|
|
|
}
|
|
|
|
|
2018-12-23 16:01:30 +01:00
|
|
|
topic_dict.set(topic, sender_message_ids);
|
2017-08-29 16:57:47 +02:00
|
|
|
topic_senders.set(stream_id, topic_dict);
|
|
|
|
|
|
|
|
// Process most recent sender to whole stream
|
2018-02-08 21:08:24 +01:00
|
|
|
sender_message_ids = stream_senders.get(stream_id) || new Dict();
|
|
|
|
old_message_id = sender_message_ids.get(message.sender_id);
|
2017-08-29 16:57:47 +02:00
|
|
|
|
2018-02-08 21:08:24 +01:00
|
|
|
if (old_message_id === undefined || old_message_id < message.id) {
|
|
|
|
sender_message_ids.set(message.sender_id, message.id);
|
2017-08-29 16:57:47 +02:00
|
|
|
}
|
|
|
|
|
2018-02-08 21:08:24 +01:00
|
|
|
stream_senders.set(stream_id, sender_message_ids);
|
2017-06-01 07:42:57 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
exports.compare_by_recency = function (user_a, user_b, stream_id, topic) {
|
|
|
|
stream_id = stream_id.toString();
|
|
|
|
|
2018-02-08 21:08:24 +01:00
|
|
|
var a_message_id;
|
|
|
|
var b_message_id;
|
2017-08-29 16:57:47 +02:00
|
|
|
|
|
|
|
var topic_dict = topic_senders.get(stream_id);
|
2017-07-28 19:39:44 +02:00
|
|
|
if (topic !== undefined && topic_dict !== undefined) {
|
2018-02-08 21:08:24 +01:00
|
|
|
var sender_message_ids = topic_dict.get(topic);
|
|
|
|
if (sender_message_ids !== undefined) {
|
|
|
|
b_message_id = sender_message_ids.get(user_b.user_id) || Number.NEGATIVE_INFINITY;
|
|
|
|
a_message_id = sender_message_ids.get(user_a.user_id) || Number.NEGATIVE_INFINITY;
|
2017-08-29 16:57:47 +02:00
|
|
|
|
2018-02-08 21:08:24 +01:00
|
|
|
if (a_message_id !== b_message_id) {
|
|
|
|
return b_message_id - a_message_id;
|
2017-08-29 16:57:47 +02:00
|
|
|
}
|
2017-06-01 07:42:57 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-29 16:57:47 +02:00
|
|
|
// Check recency for whole stream as tiebreaker
|
|
|
|
var stream_dict = stream_senders.get(stream_id);
|
|
|
|
if (stream_dict !== undefined) {
|
2018-02-08 21:08:24 +01:00
|
|
|
b_message_id = stream_dict.get(user_b.user_id) || Number.NEGATIVE_INFINITY;
|
|
|
|
a_message_id = stream_dict.get(user_a.user_id) || Number.NEGATIVE_INFINITY;
|
2017-08-29 16:57:47 +02:00
|
|
|
|
2018-02-08 21:08:24 +01:00
|
|
|
if (a_message_id !== b_message_id) {
|
|
|
|
return b_message_id - a_message_id;
|
2017-08-29 16:57:47 +02:00
|
|
|
}
|
|
|
|
}
|
2017-07-28 19:39:44 +02:00
|
|
|
|
2017-06-01 07:42:57 +02:00
|
|
|
return 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
return exports;
|
|
|
|
}());
|
|
|
|
|
|
|
|
if (typeof module !== 'undefined') {
|
|
|
|
module.exports = recent_senders;
|
|
|
|
}
|
2018-05-28 08:04:36 +02:00
|
|
|
window.recent_senders = recent_senders;
|