typeahead: Add recent_senders module.

In recent_senders module, added a data structure to hold timestamps of
users' latest message in a topic. Also added a function to compare 2
users based on above timestamp. Added a function to process messages for
the data structure and a call in add_message_metadata. Also added node
tests for insertion of data into recent_senders.senders.
This commit is contained in:
Akhil 2017-06-01 05:42:57 +00:00
parent 73f8653da6
commit f04da3d52e
5 changed files with 120 additions and 0 deletions

View File

@ -136,6 +136,7 @@
"current_msg_list": true,
"home_msg_list": false,
"pm_list": false,
"recent_senders": false,
"unread_ui": false,
"unread_ops": false,
"user_events": false,

View File

@ -0,0 +1,74 @@
var assert = require('assert');
var rs = require('js/recent_senders.js');
var _ = require('node_modules/underscore/underscore.js');
(function test_process_message_for_senders() {
var stream1 = 1;
var stream2 = 2;
var topic1 = "topic-1";
var topic2 = "topic-2";
var sender1 = 1;
var sender2 = 2;
var sender3 = 3;
// New stream
var message1 = {
stream_id: stream1,
timestamp: _.uniqueId(),
subject: topic1,
sender_id: sender1,
};
var message2 = {
stream_id: stream2,
timestamp: _.uniqueId(),
subject: topic1,
sender_id: sender2,
};
rs.process_message_for_senders(message1);
rs.process_message_for_senders(message2);
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);
// New topic
var message3 = {
stream_id: stream1,
timestamp: _.uniqueId(),
subject: topic2,
sender_id: sender3,
};
rs.process_message_for_senders(message3);
assert.equal(
rs.compare_by_recency({user_id: sender3}, {user_id: sender2}, stream1, topic2) < 0,
true);
// New sender
var message4 = {
stream_id: stream1,
timestamp: _.uniqueId(),
subject: topic1,
sender_id: sender2,
};
rs.process_message_for_senders(message4);
assert.equal(
rs.compare_by_recency({user_id: sender1}, {user_id: sender2}, stream1, topic1) > 0,
true);
// More recent message
var message5 = {
stream_id: stream1,
timestamp: _.uniqueId(),
subject: topic1,
sender_id: sender1,
};
rs.process_message_for_senders(message5);
assert.equal(
rs.compare_by_recency({user_id: sender1}, {user_id: sender2}, stream1, topic1) < 0,
true);
}());

View File

@ -148,6 +148,8 @@ exports.add_message_metadata = function (message) {
stream_data.process_message_for_recent_topics(message);
exports.set_topic_edit_properties(message);
recent_senders.process_message_for_senders(message);
break;
case 'private':

View File

@ -0,0 +1,42 @@
var recent_senders = (function () {
var exports = {};
var 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});
var sender_timestamps = topic_dict.get(message.subject) || new Dict();
var old_timestamp = sender_timestamps.get(message.sender_id);
if (old_timestamp === undefined || old_timestamp < message.timestamp) {
sender_timestamps.set(message.sender_id, message.timestamp);
}
topic_dict.set(message.subject, sender_timestamps);
senders.set(stream_id, topic_dict);
};
exports.compare_by_recency = function (user_a, user_b, stream_id, topic) {
stream_id = stream_id.toString();
var topic_dict = senders.get(stream_id);
if (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;
return b_timestamp - a_timestamp;
}
}
return 0;
};
return exports;
}());
if (typeof module !== 'undefined') {
module.exports = recent_senders;
}

View File

@ -875,6 +875,7 @@ JS_SPECS = {
'js/unread.js',
'js/topic_list.js',
'js/pm_list.js',
'js/recent_senders.js',
'js/stream_sort.js',
'js/topic_generator.js',
'js/stream_list.js',