recent_senders: Add sort by stream message recency.

If both users haven't posted in the current topic, then
as a second order sort, check which user has posted first
in the stream as a whole.

Fixes part of #5956; we still need to sort by sending in the
organization.
This commit is contained in:
Cory Lynch 2017-08-29 10:57:47 -04:00 committed by Tim Abbott
parent e8f835d852
commit f4a09b7758
2 changed files with 74 additions and 11 deletions

View File

@ -5,9 +5,11 @@ var _ = require('node_modules/underscore/underscore.js');
(function test_process_message_for_senders() {
var stream1 = 1;
var stream2 = 2;
var stream3 = 3;
var topic1 = "topic-1";
var topic2 = "topic-2";
var topic3 = "topic-3";
var sender1 = 1;
var sender2 = 2;
@ -29,14 +31,17 @@ var _ = require('node_modules/underscore/underscore.js');
rs.process_message_for_senders(message1);
rs.process_message_for_senders(message2);
// Users have posted in only one of the streams
assert.equal(
rs.compare_by_recency({user_id: sender1}, {user_id: sender2}, stream1, topic1) < 0,
true);
assert.equal(
rs.compare_by_recency({user_id: sender1}, {user_id: sender2}, stream2, topic1) > 0,
true);
// Users haven't posted in this stream, return zero
assert.equal(
rs.compare_by_recency({user_id: sender1}, {user_id: sender2}, stream2, undefined) === 0,
rs.compare_by_recency({user_id: sender1}, {user_id: sender2}, stream3, undefined) === 0,
true);
// New topic
@ -75,5 +80,37 @@ var _ = require('node_modules/underscore/underscore.js');
rs.compare_by_recency({user_id: sender1}, {user_id: sender2}, stream1, topic1) < 0,
true);
// Same stream, but different topics
var message6 = {
stream_id: stream3,
timestamp: _.uniqueId(),
subject: topic1,
sender_id: sender1,
};
var message7 = {
stream_id: stream3,
timestamp: _.uniqueId(),
subject: topic2,
sender_id: sender2,
};
var message8 = {
stream_id: stream3,
timestamp: _.uniqueId(),
subject: topic3,
sender_id: sender3,
};
rs.process_message_for_senders(message6);
rs.process_message_for_senders(message7);
rs.process_message_for_senders(message8);
// topic3 has a message in it, but sender1 nor sender2 have participated, so sort by stream
assert.equal(
rs.compare_by_recency({user_id: sender1}, {user_id: sender2}, stream3, topic3) > 0,
true);
assert.equal(
rs.compare_by_recency({user_id: sender2}, {user_id: sender1}, stream3, topic3) < 0,
true);
assert.equal(rs.compare_by_recency({}, {}, _.uniqueId(), ''), 0);
}());

View File

@ -2,11 +2,14 @@ var recent_senders = (function () {
var exports = {};
var senders = new Dict(); // key is stream-id, value is Dict
var topic_senders = new Dict(); // key is stream-id, value is Dict
var stream_senders = new Dict(); // key is stream-id, value is Dict
exports.process_message_for_senders = function (message) {
var stream_id = message.stream_id.toString();
var topic_dict = senders.get(stream_id) || new Dict({fold_case: true});
// Process most recent sender to topic
var topic_dict = topic_senders.get(stream_id) || new Dict({fold_case: true});
var sender_timestamps = topic_dict.get(message.subject) || new Dict();
var old_timestamp = sender_timestamps.get(message.sender_id);
@ -15,25 +18,48 @@ exports.process_message_for_senders = function (message) {
}
topic_dict.set(message.subject, sender_timestamps);
senders.set(stream_id, topic_dict);
topic_senders.set(stream_id, topic_dict);
// Process most recent sender to whole stream
sender_timestamps = stream_senders.get(stream_id) || new Dict();
old_timestamp = sender_timestamps.get(message.sender_id);
if (old_timestamp === undefined || old_timestamp < message.timestamp) {
sender_timestamps.set(message.sender_id, message.timestamp);
}
stream_senders.set(stream_id, sender_timestamps);
};
exports.compare_by_recency = function (user_a, user_b, stream_id, topic) {
stream_id = stream_id.toString();
var topic_dict = senders.get(stream_id);
var a_timestamp;
var b_timestamp;
var topic_dict = topic_senders.get(stream_id);
if (topic !== undefined && topic_dict !== undefined) {
var sender_timestamps = topic_dict.get(topic);
if (sender_timestamps !== undefined) {
var b_timestamp = sender_timestamps.get(user_b.user_id) || Number.NEGATIVE_INFINITY;
var a_timestamp = sender_timestamps.get(user_a.user_id) || Number.NEGATIVE_INFINITY;
b_timestamp = sender_timestamps.get(user_b.user_id) || Number.NEGATIVE_INFINITY;
a_timestamp = sender_timestamps.get(user_a.user_id) || Number.NEGATIVE_INFINITY;
if (a_timestamp !== b_timestamp) {
return b_timestamp - a_timestamp;
}
}
}
// TODO: We should also compare by stream (and maybe also by
// overall message recency in the realm) in the common case where
// there's no traffic on the current topic from this user.
// Check recency for whole stream as tiebreaker
var stream_dict = stream_senders.get(stream_id);
if (stream_dict !== undefined) {
b_timestamp = stream_dict.get(user_b.user_id) || Number.NEGATIVE_INFINITY;
a_timestamp = stream_dict.get(user_a.user_id) || Number.NEGATIVE_INFINITY;
if (a_timestamp !== b_timestamp) {
return b_timestamp - a_timestamp;
}
}
return 0;
};