From 6d511486cb661d622d411f4fc04b702bdd044b71 Mon Sep 17 00:00:00 2001 From: Steve Howell Date: Wed, 23 Oct 2013 14:44:31 -0400 Subject: [PATCH] Add activity.process_loaded_messages() and get_huddles(). Activity.js now has the capability to track huddles that come through in loaded messages and return them in reverse chronological order by their most recent message. Right now this only connected to a unit test, not any production code. (imported from commit 59957086fa2e454e5711472df091f178217aed2b) --- static/js/activity.js | 24 +++++++++++++++ zerver/tests/frontend/node/activity.js | 42 ++++++++++++++++++++++++++ 2 files changed, 66 insertions(+) diff --git a/static/js/activity.js b/static/js/activity.js index b6eab251cb..23676fcbc3 100644 --- a/static/js/activity.js +++ b/static/js/activity.js @@ -39,6 +39,30 @@ $("html").on("mousemove", function () { var presence_info = {}; +var huddle_timestamps = new Dict(); + +exports.process_loaded_messages = function (messages) { + _.each(messages, function (message) { + if (message.type === 'private') { + if (message.reply_to.indexOf(',') > 0) { + var old_timestamp = huddle_timestamps.get(message.reply_to); + + if (!old_timestamp || (old_timestamp < message.timestamp)) { + huddle_timestamps.set(message.reply_to, message.timestamp); + } + } + } + }); +}; + +exports.get_huddles = function () { + var huddles = huddle_timestamps.keys(); + huddles = _.sortBy(huddles, function (huddle) { + return huddle_timestamps.get(huddle); + }); + return huddles.reverse(); +}; + function sort_users(users, presence_info) { // TODO sort by unread count first, once we support that users.sort(function (a, b) { diff --git a/zerver/tests/frontend/node/activity.js b/zerver/tests/frontend/node/activity.js index 3ccf6fd09a..d138072904 100644 --- a/zerver/tests/frontend/node/activity.js +++ b/zerver/tests/frontend/node/activity.js @@ -46,3 +46,45 @@ var activity = require('js/activity.js'); 'alice@zulip.com' ]); }()); + +(function test_process_loaded_messages() { + + var huddle1 = 'bar@zulip.com,foo@zulip.com'; + var timestamp1 = 1382479029; // older + + var huddle2 = 'alice@zulip.com,bob@zulip.com'; + var timestamp2 = 1382479033; // newer + + var old_timestamp = 1382479000; + + var messages = [ + { + type: 'private', + reply_to: huddle1, + timestamp: timestamp1 + }, + { + type: 'stream' + }, + { + type: 'private', + reply_to: 'ignore@zulip.com' + }, + { + type: 'private', + reply_to: huddle2, + timestamp: timestamp2 + }, + { + type: 'private', + reply_to: huddle2, + timestamp: old_timestamp + } + ]; + + activity.process_loaded_messages(messages); + + assert.deepEqual(activity.get_huddles(), [huddle2, huddle1]); +}()); + +