Extract message_fetch.js.

This commit is contained in:
Steve Howell 2017-03-19 10:19:48 -07:00 committed by Tim Abbott
parent 9866124b78
commit 1114c8bf62
7 changed files with 235 additions and 221 deletions

View File

@ -87,6 +87,7 @@
"unread": false, "unread": false,
"alert_words_ui": false, "alert_words_ui": false,
"message_store": false, "message_store": false,
"message_fetch": false,
"favicon": false, "favicon": false,
"condense": false, "condense": false,
"floating_recipient_bar": false, "floating_recipient_bar": false,

View File

@ -55,7 +55,7 @@ var message_store = require('js/message_store.js');
display_recipient: [me, bob, cindy], display_recipient: [me, bob, cindy],
flags: ['has_alert_word'], flags: ['has_alert_word'],
}; };
message_store._add_message_metadata(message); message_store.add_message_metadata(message);
assert.equal(message.is_private, true); assert.equal(message.is_private, true);
assert.equal(message.reply_to, 'bob@example.com,cindy@example.com'); assert.equal(message.reply_to, 'bob@example.com,cindy@example.com');

224
static/js/message_fetch.js Normal file
View File

@ -0,0 +1,224 @@
var message_fetch = (function () {
var exports = {};
// If the browser hasn't scrolled away from the top of the page
// since the last time that we ran load_more_messages(), we do
// not load_more_messages().
var load_more_enabled = true;
exports.reset_load_more_status = function reset_load_more_status() {
load_more_enabled = true;
ui.have_scrolled_away_from_top = true;
ui.hide_loading_more_messages_indicator();
};
function process_result(messages, opts) {
$('#get_old_messages_error').hide();
if ((messages.length === 0) && (current_msg_list === message_list.narrowed) &&
message_list.narrowed.empty()) {
// Even after trying to load more messages, we have no
// messages to display in this narrow.
narrow.show_empty_narrow_message();
}
messages = _.map(messages, message_store.add_message_metadata);
// If we're loading more messages into the home view, save them to
// the message_list.all as well, as the home_msg_list is reconstructed
// from message_list.all.
if (opts.msg_list === home_msg_list) {
message_store.do_unread_count_updates(messages);
message_store.add_messages(messages, message_list.all, {messages_are_new: false});
}
if (messages.length !== 0 && !opts.cont_will_add_messages) {
message_store.add_messages(messages, opts.msg_list, {messages_are_new: false});
}
activity.process_loaded_messages(messages);
stream_list.update_streams_sidebar();
pm_list.update_private_messages();
if (opts.cont !== undefined) {
opts.cont(messages);
}
}
function get_old_messages_success(data, opts) {
if (tutorial.is_running()) {
// Don't actually process the messages until the tutorial is
// finished, but do disable the loading indicator so it isn't
// distracting in the background
loading.destroy_indicator($('#page_loading_indicator'));
tutorial.defer(function () { get_old_messages_success(data, opts); });
return;
}
if (opts.msg_list.narrowed && opts.msg_list !== current_msg_list) {
// We unnarrowed before receiving new messages so
// don't bother processing the newly arrived messages.
return;
}
if (! data) {
// The server occationally returns no data during a
// restart. Ignore those responses and try again
setTimeout(function () {
exports.load_old_messages(opts);
}, 0);
return;
}
process_result(data.messages, opts);
resize.resize_bottom_whitespace();
}
exports.load_old_messages = function load_old_messages(opts) {
opts = _.extend({cont_will_add_messages: false}, opts);
var data = {anchor: opts.anchor,
num_before: opts.num_before,
num_after: opts.num_after};
if (opts.msg_list.narrowed && narrow.active()) {
var operators = narrow.public_operators();
if (page_params.narrow !== undefined) {
operators = operators.concat(page_params.narrow);
}
data.narrow = JSON.stringify(operators);
}
if (opts.msg_list === home_msg_list && page_params.narrow_stream !== undefined) {
data.narrow = JSON.stringify(page_params.narrow);
}
if (opts.use_first_unread_anchor) {
data.use_first_unread_anchor = true;
}
channel.get({
url: '/json/messages',
data: data,
idempotent: true,
success: function (data) {
get_old_messages_success(data, opts);
},
error: function (xhr) {
if (opts.msg_list.narrowed && opts.msg_list !== current_msg_list) {
// We unnarrowed before getting an error so don't
// bother trying again or doing further processing.
return;
}
if (xhr.status === 400) {
// Bad request: We probably specified a narrow operator
// for a nonexistent stream or something. We shouldn't
// retry or display a connection error.
//
// FIXME: Warn the user when this has happened?
process_result([], opts);
return;
}
// We might want to be more clever here
$('#get_old_messages_error').show();
setTimeout(function () {
exports.load_old_messages(opts);
}, 5000);
},
});
};
exports.load_more_messages = function load_more_messages(msg_list) {
var batch_size = 100;
var oldest_message_id;
if (!load_more_enabled) {
return;
}
ui.show_loading_more_messages_indicator();
load_more_enabled = false;
if (msg_list.first() === undefined) {
oldest_message_id = page_params.initial_pointer;
} else {
oldest_message_id = msg_list.first().id;
}
exports.load_old_messages({
anchor: oldest_message_id.toFixed(),
num_before: batch_size,
num_after: 0,
msg_list: msg_list,
cont: function (messages) {
ui.hide_loading_more_messages_indicator();
if (messages.length >= batch_size) {
load_more_enabled = true;
}
},
});
};
util.execute_early(function () {
// get the initial message list
function load_more(messages) {
// If we received the initially selected message, select it on the client side,
// but not if the user has already selected another one during load.
//
// We fall back to the closest selected id, as the user may have removed
// a stream from the home before already
if (home_msg_list.selected_id() === -1 && !home_msg_list.empty()) {
home_msg_list.select_id(page_params.initial_pointer,
{then_scroll: true, use_closest: true,
target_scroll_offset: page_params.initial_offset});
}
// catch the user up
if (messages.length !== 0) {
var latest_id = messages[messages.length-1].id;
if (latest_id < page_params.max_message_id) {
exports.load_old_messages({
anchor: latest_id.toFixed(),
num_before: 0,
num_after: 400,
msg_list: home_msg_list,
cont: load_more,
});
return;
}
}
server_events.home_view_loaded();
// backfill more messages after the user is idle
var backfill_batch_size = 1000;
$(document).idle({idle: 1000*10,
onIdle: function () {
var first_id = message_list.all.first().id;
exports.load_old_messages({
anchor: first_id,
num_before: backfill_batch_size,
num_after: 0,
msg_list: home_msg_list,
});
}});
}
if (page_params.have_initial_messages) {
exports.load_old_messages({
anchor: page_params.initial_pointer,
num_before: 200,
num_after: 200,
msg_list: home_msg_list,
cont: load_more,
});
} else {
server_events.home_view_loaded();
}
});
return exports;
}());
if (typeof module !== 'undefined') {
module.exports = message_fetch;
}

View File

@ -3,11 +3,6 @@ var message_store = (function () {
var exports = {}; var exports = {};
var stored_messages = {}; var stored_messages = {};
var load_more_enabled = true;
// If the browser hasn't scrolled away from the top of the page
// since the last time that we ran load_more_messages(), we do
// not load_more_messages().
exports.recent_private_messages = []; exports.recent_private_messages = [];
exports.get = function get(message_id) { exports.get = function get(message_id) {
@ -108,7 +103,7 @@ function set_topic_edit_properties(message) {
} }
} }
function add_message_metadata(message) { exports.add_message_metadata = function (message) {
var cached_msg = stored_messages[message.id]; var cached_msg = stored_messages[message.id];
if (cached_msg !== undefined) { if (cached_msg !== undefined) {
// Copy the match subject and content over if they exist on // Copy the match subject and content over if they exist on
@ -171,7 +166,7 @@ function add_message_metadata(message) {
} }
stored_messages[message.id] = message; stored_messages[message.id] = message;
return message; return message;
} };
exports.add_messages = function add_messages(messages, msg_list, opts) { exports.add_messages = function add_messages(messages, msg_list, opts) {
if (!messages) { if (!messages) {
@ -224,7 +219,7 @@ function maybe_add_narrowed_messages(messages, msg_list, messages_are_new, local
} }
}); });
new_messages = _.map(new_messages, add_message_metadata); new_messages = _.map(new_messages, exports.add_message_metadata);
exports.add_messages(new_messages, msg_list, {messages_are_new: messages_are_new}); exports.add_messages(new_messages, msg_list, {messages_are_new: messages_are_new});
unread_ops.process_visible(); unread_ops.process_visible();
notifications.possibly_notify_new_messages_outside_viewport(new_messages, local_id); notifications.possibly_notify_new_messages_outside_viewport(new_messages, local_id);
@ -387,7 +382,7 @@ exports.do_unread_count_updates = function do_unread_count_updates(messages) {
}; };
exports.insert_new_messages = function insert_new_messages(messages, local_id) { exports.insert_new_messages = function insert_new_messages(messages, local_id) {
messages = _.map(messages, add_message_metadata); messages = _.map(messages, exports.add_message_metadata);
// You must add add messages to home_msg_list BEFORE // You must add add messages to home_msg_list BEFORE
// calling unread.process_loaded_messages. // calling unread.process_loaded_messages.
@ -438,215 +433,11 @@ exports.insert_new_messages = function insert_new_messages(messages, local_id) {
pm_list.update_private_messages(); pm_list.update_private_messages();
}; };
function process_result(messages, opts) {
$('#get_old_messages_error').hide();
if ((messages.length === 0) && (current_msg_list === message_list.narrowed) &&
message_list.narrowed.empty()) {
// Even after trying to load more messages, we have no
// messages to display in this narrow.
narrow.show_empty_narrow_message();
}
messages = _.map(messages, add_message_metadata);
// If we're loading more messages into the home view, save them to
// the message_list.all as well, as the home_msg_list is reconstructed
// from message_list.all.
if (opts.msg_list === home_msg_list) {
exports.do_unread_count_updates(messages);
exports.add_messages(messages, message_list.all, {messages_are_new: false});
}
if (messages.length !== 0 && !opts.cont_will_add_messages) {
exports.add_messages(messages, opts.msg_list, {messages_are_new: false});
}
activity.process_loaded_messages(messages);
stream_list.update_streams_sidebar();
pm_list.update_private_messages();
if (opts.cont !== undefined) {
opts.cont(messages);
}
}
function get_old_messages_success(data, opts) {
if (tutorial.is_running()) {
// Don't actually process the messages until the tutorial is
// finished, but do disable the loading indicator so it isn't
// distracting in the background
loading.destroy_indicator($('#page_loading_indicator'));
tutorial.defer(function () { get_old_messages_success(data, opts); });
return;
}
if (opts.msg_list.narrowed && opts.msg_list !== current_msg_list) {
// We unnarrowed before receiving new messages so
// don't bother processing the newly arrived messages.
return;
}
if (! data) {
// The server occationally returns no data during a
// restart. Ignore those responses and try again
setTimeout(function () {
exports.load_old_messages(opts);
}, 0);
return;
}
process_result(data.messages, opts);
resize.resize_bottom_whitespace();
}
exports.load_old_messages = function load_old_messages(opts) {
opts = _.extend({cont_will_add_messages: false}, opts);
var data = {anchor: opts.anchor,
num_before: opts.num_before,
num_after: opts.num_after};
if (opts.msg_list.narrowed && narrow.active()) {
var operators = narrow.public_operators();
if (page_params.narrow !== undefined) {
operators = operators.concat(page_params.narrow);
}
data.narrow = JSON.stringify(operators);
}
if (opts.msg_list === home_msg_list && page_params.narrow_stream !== undefined) {
data.narrow = JSON.stringify(page_params.narrow);
}
if (opts.use_first_unread_anchor) {
data.use_first_unread_anchor = true;
}
channel.get({
url: '/json/messages',
data: data,
idempotent: true,
success: function (data) {
get_old_messages_success(data, opts);
},
error: function (xhr) {
if (opts.msg_list.narrowed && opts.msg_list !== current_msg_list) {
// We unnarrowed before getting an error so don't
// bother trying again or doing further processing.
return;
}
if (xhr.status === 400) {
// Bad request: We probably specified a narrow operator
// for a nonexistent stream or something. We shouldn't
// retry or display a connection error.
//
// FIXME: Warn the user when this has happened?
process_result([], opts);
return;
}
// We might want to be more clever here
$('#get_old_messages_error').show();
setTimeout(function () {
exports.load_old_messages(opts);
}, 5000);
},
});
};
exports.reset_load_more_status = function reset_load_more_status() {
load_more_enabled = true;
ui.have_scrolled_away_from_top = true;
ui.hide_loading_more_messages_indicator();
};
exports.load_more_messages = function load_more_messages(msg_list) {
var batch_size = 100;
var oldest_message_id;
if (!load_more_enabled) {
return;
}
ui.show_loading_more_messages_indicator();
load_more_enabled = false;
if (msg_list.first() === undefined) {
oldest_message_id = page_params.initial_pointer;
} else {
oldest_message_id = msg_list.first().id;
}
exports.load_old_messages({
anchor: oldest_message_id.toFixed(),
num_before: batch_size,
num_after: 0,
msg_list: msg_list,
cont: function (messages) {
ui.hide_loading_more_messages_indicator();
if (messages.length >= batch_size) {
load_more_enabled = true;
}
},
});
};
exports.clear = function clear() { exports.clear = function clear() {
this.stored_messages = {}; this.stored_messages = {};
}; };
util.execute_early(function () { util.execute_early(function () {
// get the initial message list
function load_more(messages) {
// If we received the initially selected message, select it on the client side,
// but not if the user has already selected another one during load.
//
// We fall back to the closest selected id, as the user may have removed
// a stream from the home before already
if (home_msg_list.selected_id() === -1 && !home_msg_list.empty()) {
home_msg_list.select_id(page_params.initial_pointer,
{then_scroll: true, use_closest: true,
target_scroll_offset: page_params.initial_offset});
}
// catch the user up
if (messages.length !== 0) {
var latest_id = messages[messages.length-1].id;
if (latest_id < page_params.max_message_id) {
exports.load_old_messages({
anchor: latest_id.toFixed(),
num_before: 0,
num_after: 400,
msg_list: home_msg_list,
cont: load_more,
});
return;
}
}
server_events.home_view_loaded();
// backfill more messages after the user is idle
var backfill_batch_size = 1000;
$(document).idle({idle: 1000*10,
onIdle: function () {
var first_id = message_list.all.first().id;
exports.load_old_messages({
anchor: first_id,
num_before: backfill_batch_size,
num_after: 0,
msg_list: home_msg_list,
});
}});
}
if (page_params.have_initial_messages) {
exports.load_old_messages({
anchor: page_params.initial_pointer,
num_before: 200,
num_after: 200,
msg_list: home_msg_list,
cont: load_more,
});
} else {
server_events.home_view_loaded();
}
$(document).on('message_id_changed', function (event) { $(document).on('message_id_changed', function (event) {
var old_id = event.old_id; var old_id = event.old_id;
var new_id = event.new_id; var new_id = event.new_id;
@ -676,9 +467,6 @@ util.execute_early(function () {
}); });
}); });
// This is for testing.
exports._add_message_metadata = add_message_metadata;
return exports; return exports;
}()); }());

View File

@ -320,7 +320,7 @@ exports.activate = function (raw_operators, opts) {
} }
var defer_selecting_closest = message_list.narrowed.empty(); var defer_selecting_closest = message_list.narrowed.empty();
message_store.load_old_messages({ message_fetch.load_old_messages({
anchor: then_select_id.toFixed(), anchor: then_select_id.toFixed(),
num_before: 50, num_before: 50,
num_after: 50, num_after: 50,
@ -338,7 +338,7 @@ exports.activate = function (raw_operators, opts) {
}); });
if (! defer_selecting_closest) { if (! defer_selecting_closest) {
message_store.reset_load_more_status(); message_fetch.reset_load_more_status();
maybe_select_closest(); maybe_select_closest();
} else { } else {
ui.show_loading_more_messages_indicator(); ui.show_loading_more_messages_indicator();
@ -468,7 +468,7 @@ exports.deactivate = function () {
condense.condense_and_collapse($("#zhome tr.message_row")); condense.condense_and_collapse($("#zhome tr.message_row"));
$('#search_query').val(''); $('#search_query').val('');
message_store.reset_load_more_status(); message_fetch.reset_load_more_status();
hashchange.save_narrow(); hashchange.save_narrow();
if (current_msg_list.selected_id() !== -1) { if (current_msg_list.selected_id() !== -1) {

View File

@ -224,7 +224,7 @@ function scroll_finished() {
if (message_viewport.scrollTop() === 0 && if (message_viewport.scrollTop() === 0 &&
ui.have_scrolled_away_from_top) { ui.have_scrolled_away_from_top) {
ui.have_scrolled_away_from_top = false; ui.have_scrolled_away_from_top = false;
message_store.load_more_messages(current_msg_list); message_fetch.load_more_messages(current_msg_list);
} else if (!ui.have_scrolled_away_from_top) { } else if (!ui.have_scrolled_away_from_top) {
ui.have_scrolled_away_from_top = true; ui.have_scrolled_away_from_top = true;
} }

View File

@ -878,6 +878,7 @@ JS_SPECS = {
'js/alert_words_ui.js', 'js/alert_words_ui.js',
'js/attachments_ui.js', 'js/attachments_ui.js',
'js/message_store.js', 'js/message_store.js',
'js/message_fetch.js',
'js/server_events.js', 'js/server_events.js',
'js/zulip.js', 'js/zulip.js',
'js/activity.js', 'js/activity.js',