refactor: Extract pm_conversations.recent.

This is a pretty pure code move, where we moved stuff from
message_store to pm_conversations:

    insert_recent_private_message() -> recent.insert()
    recent_private_messages -> recent.get()

The object message_store.recent_private_messages was not
encapsulated in a function before this change.  Now it is
hidden in the scope of pm_conversations.recent.

Both of the modules touched here maintain 100% line coverage.
This commit is contained in:
Steve Howell
2018-02-09 12:10:39 -05:00
committed by Tim Abbott
parent a0b58b1560
commit 4f52e095e8
6 changed files with 63 additions and 57 deletions

View File

@@ -60,20 +60,6 @@ people.add_in_realm(cindy);
global.people.initialize_current_user(me.user_id);
(function test_insert_recent_private_message() {
message_store.insert_recent_private_message('1', 1001);
message_store.insert_recent_private_message('2', 2001);
message_store.insert_recent_private_message('1', 3001);
// try to backdate user1's timestamp
message_store.insert_recent_private_message('1', 555);
assert.deepEqual(message_store.recent_private_messages, [
{user_ids_string: '1', timestamp: 3001},
{user_ids_string: '2', timestamp: 2001},
]);
}());
(function test_add_message_metadata() {
var message = {
sender_email: 'me@example.com',

View File

@@ -12,3 +12,18 @@ var pmc = zrequire('pm_conversations');
assert.equal(pmc.is_partner(user2_id), false);
assert.equal(pmc.is_partner(user3_id), true);
}());
(function test_insert_recent_private_message() {
pmc.recent.insert('1', 1001);
pmc.recent.insert('2', 2001);
pmc.recent.insert('1', 3001);
// try to backdate user1's timestamp
pmc.recent.insert('1', 555);
assert.deepEqual(pmc.recent.get(), [
{user_ids_string: '1', timestamp: 3001},
{user_ids_string: '2', timestamp: 2001},
]);
}());

View File

@@ -1,9 +1,5 @@
set_global('$', global.make_zjquery());
set_global('message_store', {
recent_private_messages: new global.Array(),
});
set_global('narrow_state', {});
set_global('resize', {
resize_stream_filters_container: function () {},
@@ -24,6 +20,7 @@ zrequire('narrow');
zrequire('Handlebars', 'handlebars');
zrequire('templates');
zrequire('people');
zrequire('pm_conversations');
zrequire('pm_list');
var alice = {
@@ -70,9 +67,9 @@ global.people.initialize_current_user(me.user_id);
var active_conversation_2 = 'me@zulip.com,alice@zulip.com';
var max_conversations = 5;
var conversations = {user_ids_string: '101,102',
timestamp: 0 };
global.message_store.recent_private_messages.push(conversations);
var user_ids_string = '101,102';
var timestamp = 0;
pm_conversations.recent.insert(user_ids_string, timestamp);
global.unread.num_unread_for_person = function () {
return 1;

View File

@@ -3,8 +3,6 @@ var message_store = (function () {
var exports = {};
var stored_messages = {};
exports.recent_private_messages = [];
exports.get = function get(message_id) {
return stored_messages[message_id];
};
@@ -55,41 +53,9 @@ exports.process_message_for_recent_private_messages = function (message) {
var user_ids_string = user_ids.join(',');
exports.insert_recent_private_message(user_ids_string, message.timestamp);
pm_conversations.recent.insert(user_ids_string, message.timestamp);
};
exports.insert_recent_private_message = (function () {
var recent_timestamps = new Dict({fold_case: true}); // key is user_ids_string
return 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.
exports.recent_private_messages.unshift(conversation);
} else {
if (conversation.timestamp >= timestamp) {
return; // don't backdate our conversation
}
// update our timestamp
conversation.timestamp = timestamp;
}
exports.recent_private_messages.sort(function (a, b) {
return b.timestamp - a.timestamp;
});
};
}());
exports.set_message_booleans = function (message) {
var flags = message.flags || [];

View File

@@ -12,6 +12,48 @@ exports.is_partner = function (user_id) {
return partners.get(user_id) || false;
};
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;
};
return self;
}());
return exports;
}());

View File

@@ -81,7 +81,7 @@ exports.close = function () {
exports._build_private_messages_list = function (active_conversation, max_private_messages) {
var private_messages = message_store.recent_private_messages || [];
var private_messages = pm_conversations.recent.get();
var display_messages = [];
var hiding_messages = false;