2017-03-19 18:19:48 +01:00
|
|
|
var message_fetch = (function () {
|
|
|
|
|
|
|
|
var exports = {};
|
|
|
|
|
2018-03-08 21:25:14 +01:00
|
|
|
var consts = {
|
2018-06-04 21:13:07 +02:00
|
|
|
backfill_idle_time: 10 * 1000,
|
2018-03-08 21:25:14 +01:00
|
|
|
error_retry_time: 5000,
|
|
|
|
backfill_batch_size: 1000,
|
2018-03-09 15:32:28 +01:00
|
|
|
narrow_before: 50,
|
|
|
|
narrow_after: 50,
|
2018-03-08 21:25:14 +01:00
|
|
|
num_before_pointer: 200,
|
|
|
|
num_after_pointer: 200,
|
|
|
|
backward_batch_size: 100,
|
2018-03-11 20:19:30 +01:00
|
|
|
forward_batch_size: 100,
|
2018-03-08 21:25:14 +01:00
|
|
|
catch_up_batch_size: 1000,
|
|
|
|
};
|
|
|
|
|
2018-03-16 13:05:54 +01:00
|
|
|
function process_result(data, opts) {
|
|
|
|
var messages = data.messages;
|
|
|
|
|
2018-11-29 22:28:47 +01:00
|
|
|
if (!$('#connection-error').hasClass('get-events-error')) {
|
2018-02-25 13:01:38 +01:00
|
|
|
ui_report.hide_error($("#connection-error"));
|
|
|
|
}
|
2017-03-19 18:19:48 +01:00
|
|
|
|
2018-06-06 18:50:09 +02:00
|
|
|
if (messages.length === 0 && current_msg_list === message_list.narrowed &&
|
2017-03-19 18:19:48 +01:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2017-12-16 23:05:32 +01:00
|
|
|
_.each(messages, message_store.set_message_booleans);
|
2017-03-19 18:19:48 +01:00
|
|
|
messages = _.map(messages, message_store.add_message_metadata);
|
|
|
|
|
2018-04-23 20:24:55 +02:00
|
|
|
// In case any of the newly fetched messages are new, add them to
|
|
|
|
// our unread data structures. It's important that this run even
|
|
|
|
// when fetching in a narrow, since we might return unread
|
|
|
|
// messages that aren't in the home view data set (e.g. on a muted
|
|
|
|
// stream).
|
|
|
|
message_util.do_unread_count_updates(messages);
|
|
|
|
|
2017-03-19 18:19:48 +01:00
|
|
|
// 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) {
|
2017-03-19 22:43:38 +01:00
|
|
|
message_util.add_messages(messages, message_list.all, {messages_are_new: false});
|
2017-03-19 18:19:48 +01:00
|
|
|
}
|
|
|
|
|
2018-03-09 15:35:23 +01:00
|
|
|
if (messages.length !== 0) {
|
2017-03-19 22:43:38 +01:00
|
|
|
message_util.add_messages(messages, opts.msg_list, {messages_are_new: false});
|
2017-03-19 18:19:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
activity.process_loaded_messages(messages);
|
|
|
|
stream_list.update_streams_sidebar();
|
|
|
|
pm_list.update_private_messages();
|
2018-07-06 20:23:20 +02:00
|
|
|
stream_list.maybe_scroll_narrow_into_view();
|
2017-03-19 18:19:48 +01:00
|
|
|
|
|
|
|
if (opts.cont !== undefined) {
|
2018-12-13 00:41:57 +01:00
|
|
|
opts.cont(data, opts);
|
2017-03-19 18:19:48 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-09 02:17:54 +01:00
|
|
|
function get_messages_success(data, opts) {
|
2017-03-19 18:19:48 +01:00
|
|
|
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;
|
|
|
|
}
|
2018-06-04 21:09:11 +02:00
|
|
|
if (!data) {
|
2017-11-09 16:26:38 +01:00
|
|
|
// The server occasionally returns no data during a
|
2017-03-19 18:19:48 +01:00
|
|
|
// restart. Ignore those responses and try again
|
|
|
|
setTimeout(function () {
|
2018-03-09 02:17:54 +01:00
|
|
|
exports.load_messages(opts);
|
2017-03-19 18:19:48 +01:00
|
|
|
}, 0);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-03-16 13:05:54 +01:00
|
|
|
process_result(data, opts);
|
2017-03-19 18:19:48 +01:00
|
|
|
resize.resize_bottom_whitespace();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-03-09 02:17:54 +01:00
|
|
|
exports.load_messages = function (opts) {
|
2017-03-19 18:19:48 +01:00
|
|
|
var data = {anchor: opts.anchor,
|
|
|
|
num_before: opts.num_before,
|
|
|
|
num_after: opts.num_after};
|
|
|
|
|
2017-04-25 15:25:31 +02:00
|
|
|
if (opts.msg_list.narrowed && narrow_state.active()) {
|
|
|
|
var operators = narrow_state.public_operators();
|
2017-03-19 18:19:48 +01:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2017-11-03 16:53:21 +01:00
|
|
|
data.client_gravatar = true;
|
|
|
|
|
2017-03-19 18:19:48 +01:00
|
|
|
channel.get({
|
|
|
|
url: '/json/messages',
|
|
|
|
data: data,
|
|
|
|
idempotent: true,
|
|
|
|
success: function (data) {
|
2018-03-09 02:17:54 +01:00
|
|
|
get_messages_success(data, opts);
|
2017-03-19 18:19:48 +01:00
|
|
|
},
|
|
|
|
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?
|
2018-03-16 13:05:54 +01:00
|
|
|
var data = {
|
|
|
|
messages: [],
|
|
|
|
};
|
|
|
|
process_result(data, opts);
|
2017-03-19 18:19:48 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We might want to be more clever here
|
2017-05-08 15:18:43 +02:00
|
|
|
$('#connection-error').addClass("show");
|
2017-03-19 18:19:48 +01:00
|
|
|
setTimeout(function () {
|
2018-03-09 02:17:54 +01:00
|
|
|
exports.load_messages(opts);
|
2018-03-08 21:25:14 +01:00
|
|
|
}, consts.error_retry_time);
|
2017-03-19 18:19:48 +01:00
|
|
|
},
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2018-03-09 15:32:28 +01:00
|
|
|
exports.load_messages_for_narrow = function (opts) {
|
2018-03-16 14:15:30 +01:00
|
|
|
var msg_list = message_list.narrowed;
|
|
|
|
|
|
|
|
msg_list.fetch_status.start_initial_narrow();
|
|
|
|
|
2018-03-09 15:32:28 +01:00
|
|
|
message_fetch.load_messages({
|
|
|
|
anchor: opts.then_select_id.toFixed(),
|
|
|
|
num_before: consts.narrow_before,
|
|
|
|
num_after: consts.narrow_after,
|
2018-03-16 14:15:30 +01:00
|
|
|
msg_list: msg_list,
|
2018-04-23 05:32:24 +02:00
|
|
|
use_first_unread_anchor: opts.use_first_unread_anchor,
|
2018-03-16 14:15:30 +01:00
|
|
|
cont: function (data) {
|
|
|
|
msg_list.fetch_status.finish_initial_narrow({
|
|
|
|
found_oldest: data.found_oldest,
|
|
|
|
found_newest: data.found_newest,
|
|
|
|
});
|
2018-03-09 22:12:38 +01:00
|
|
|
message_scroll.hide_indicators();
|
2018-03-09 15:32:28 +01:00
|
|
|
opts.cont();
|
|
|
|
},
|
|
|
|
});
|
|
|
|
};
|
2017-03-19 18:19:48 +01:00
|
|
|
|
2018-03-21 16:03:03 +01:00
|
|
|
exports.get_backfill_anchor = function (msg_list) {
|
|
|
|
var oldest_message_id;
|
|
|
|
|
2018-03-21 16:19:28 +01:00
|
|
|
if (msg_list === home_msg_list) {
|
|
|
|
msg_list = message_list.all;
|
|
|
|
}
|
|
|
|
|
2018-03-21 16:03:03 +01:00
|
|
|
if (msg_list.first() === undefined) {
|
|
|
|
oldest_message_id = page_params.pointer;
|
|
|
|
} else {
|
|
|
|
oldest_message_id = msg_list.first().id;
|
|
|
|
}
|
|
|
|
return oldest_message_id;
|
|
|
|
};
|
|
|
|
|
2018-03-11 20:19:30 +01:00
|
|
|
exports.get_frontfill_anchor = function (msg_list) {
|
|
|
|
if (msg_list === home_msg_list) {
|
|
|
|
msg_list = message_list.all;
|
|
|
|
}
|
|
|
|
|
|
|
|
var last_msg = msg_list.last();
|
|
|
|
|
|
|
|
if (last_msg) {
|
|
|
|
return last_msg.id;
|
|
|
|
}
|
|
|
|
|
|
|
|
return page_params.pointer;
|
|
|
|
};
|
|
|
|
|
2018-03-09 02:23:49 +01:00
|
|
|
exports.maybe_load_older_messages = function (opts) {
|
|
|
|
// This function gets called when you scroll to the top
|
|
|
|
// of your window, and you want to get messages older
|
|
|
|
// than what the browers originally fetched.
|
2018-03-08 20:18:36 +01:00
|
|
|
var msg_list = opts.msg_list;
|
2018-03-09 22:23:23 +01:00
|
|
|
if (!msg_list.fetch_status.can_load_older_messages()) {
|
|
|
|
// We may already be loading old messages or already
|
|
|
|
// got the oldest one.
|
2017-03-19 18:19:48 +01:00
|
|
|
return;
|
|
|
|
}
|
2018-03-21 16:39:03 +01:00
|
|
|
|
2018-03-08 20:18:36 +01:00
|
|
|
opts.show_loading();
|
2018-03-21 16:39:03 +01:00
|
|
|
exports.do_backfill({
|
|
|
|
msg_list: msg_list,
|
|
|
|
num_before: consts.backward_batch_size,
|
|
|
|
cont: function () {
|
|
|
|
opts.hide_loading();
|
|
|
|
},
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.do_backfill = function (opts) {
|
|
|
|
var msg_list = opts.msg_list;
|
|
|
|
|
2018-03-09 22:23:23 +01:00
|
|
|
msg_list.fetch_status.start_older_batch();
|
2018-03-21 18:09:45 +01:00
|
|
|
if (msg_list === home_msg_list) {
|
|
|
|
message_list.all.fetch_status.start_older_batch();
|
|
|
|
}
|
2018-03-08 21:25:14 +01:00
|
|
|
|
2018-03-21 16:03:03 +01:00
|
|
|
var anchor = exports.get_backfill_anchor(msg_list).toFixed();
|
2018-03-08 21:25:14 +01:00
|
|
|
|
2018-03-09 02:17:54 +01:00
|
|
|
exports.load_messages({
|
2018-03-21 16:03:03 +01:00
|
|
|
anchor: anchor,
|
2018-03-21 16:39:03 +01:00
|
|
|
num_before: opts.num_before,
|
2017-03-19 18:19:48 +01:00
|
|
|
num_after: 0,
|
|
|
|
msg_list: msg_list,
|
2018-03-16 13:05:54 +01:00
|
|
|
cont: function (data) {
|
2018-03-09 22:23:23 +01:00
|
|
|
msg_list.fetch_status.finish_older_batch({
|
2018-03-16 13:05:54 +01:00
|
|
|
found_oldest: data.found_oldest,
|
2018-12-13 00:53:35 +01:00
|
|
|
history_limited: data.history_limited,
|
2018-03-09 22:23:23 +01:00
|
|
|
});
|
2018-03-21 18:09:45 +01:00
|
|
|
if (msg_list === home_msg_list) {
|
|
|
|
message_list.all.fetch_status.finish_older_batch({
|
|
|
|
found_oldest: data.found_oldest,
|
2018-12-13 00:53:35 +01:00
|
|
|
history_limited: data.history_limited,
|
2018-03-21 18:09:45 +01:00
|
|
|
});
|
|
|
|
}
|
2018-03-21 16:39:03 +01:00
|
|
|
if (opts.cont) {
|
|
|
|
opts.cont();
|
|
|
|
}
|
2017-03-19 18:19:48 +01:00
|
|
|
},
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2018-03-11 20:19:30 +01:00
|
|
|
exports.maybe_load_newer_messages = function (opts) {
|
|
|
|
// This function gets called when you scroll to the top
|
|
|
|
// of your window, and you want to get messages newer
|
|
|
|
// than what the browers originally fetched.
|
|
|
|
var msg_list = opts.msg_list;
|
|
|
|
|
|
|
|
if (!msg_list.fetch_status.can_load_newer_messages()) {
|
|
|
|
// We may already be loading new messages or already
|
|
|
|
// got the newest one.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
msg_list.fetch_status.start_newer_batch();
|
|
|
|
if (msg_list === home_msg_list) {
|
|
|
|
message_list.all.fetch_status.start_newer_batch();
|
|
|
|
}
|
|
|
|
|
|
|
|
var anchor = exports.get_frontfill_anchor(msg_list).toFixed();
|
|
|
|
|
|
|
|
exports.load_messages({
|
|
|
|
anchor: anchor,
|
|
|
|
num_before: 0,
|
|
|
|
num_after: consts.forward_batch_size,
|
|
|
|
msg_list: msg_list,
|
|
|
|
cont: function (data) {
|
|
|
|
msg_list.fetch_status.finish_newer_batch({
|
|
|
|
found_newest: data.found_newest,
|
|
|
|
});
|
|
|
|
if (msg_list === home_msg_list) {
|
|
|
|
message_list.all.fetch_status.finish_newer_batch({
|
|
|
|
found_newest: data.found_newest,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2018-03-20 15:32:43 +01:00
|
|
|
exports.start_backfilling_messages = function () {
|
|
|
|
// backfill more messages after the user is idle
|
|
|
|
$(document).idle({idle: consts.backfill_idle_time,
|
|
|
|
onIdle: function () {
|
2018-03-21 17:47:40 +01:00
|
|
|
exports.do_backfill({
|
2018-03-20 15:32:43 +01:00
|
|
|
num_before: consts.backfill_batch_size,
|
|
|
|
msg_list: home_msg_list,
|
|
|
|
});
|
|
|
|
}});
|
|
|
|
};
|
|
|
|
|
2017-07-04 01:30:47 +02:00
|
|
|
exports.initialize = function () {
|
2017-03-19 18:19:48 +01:00
|
|
|
// get the initial message list
|
2018-12-13 00:41:57 +01:00
|
|
|
function load_more(data, opts) {
|
2017-03-19 18:19:48 +01:00
|
|
|
// 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()) {
|
2017-04-24 21:33:48 +02:00
|
|
|
home_msg_list.select_id(page_params.pointer,
|
2017-03-19 18:19:48 +01:00
|
|
|
{then_scroll: true, use_closest: true,
|
|
|
|
target_scroll_offset: page_params.initial_offset});
|
|
|
|
}
|
|
|
|
|
2018-12-13 00:41:57 +01:00
|
|
|
if (opts.num_before > 0) {
|
|
|
|
// Only the initial call with num_before=consts.num_before_pointer
|
|
|
|
// has an older batch to finish.
|
|
|
|
home_msg_list.fetch_status.finish_older_batch({
|
|
|
|
found_oldest: data.found_oldest,
|
2018-12-13 00:53:35 +01:00
|
|
|
history_limited: data.history_limited,
|
2018-12-13 00:41:57 +01:00
|
|
|
});
|
|
|
|
message_list.all.fetch_status.finish_older_batch({
|
|
|
|
found_oldest: data.found_oldest,
|
2018-12-13 00:53:35 +01:00
|
|
|
history_limited: data.history_limited,
|
2018-12-13 00:41:57 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-03-20 15:28:24 +01:00
|
|
|
home_msg_list.fetch_status.finish_newer_batch({
|
|
|
|
found_newest: data.found_newest,
|
|
|
|
});
|
|
|
|
|
2018-03-26 20:54:49 +02:00
|
|
|
message_list.all.fetch_status.finish_newer_batch({
|
|
|
|
found_newest: data.found_newest,
|
|
|
|
});
|
|
|
|
|
2018-03-20 15:28:24 +01:00
|
|
|
if (data.found_newest) {
|
|
|
|
server_events.home_view_loaded();
|
|
|
|
exports.start_backfilling_messages();
|
|
|
|
return;
|
2017-03-19 18:19:48 +01:00
|
|
|
}
|
|
|
|
|
2018-03-20 15:28:24 +01:00
|
|
|
// If we fall through here, we need to keep fetching more data, and
|
|
|
|
// we'll call back to the function we're in.
|
|
|
|
var messages = data.messages;
|
2018-06-04 21:13:07 +02:00
|
|
|
var latest_id = messages[messages.length - 1].id;
|
2018-03-20 15:28:24 +01:00
|
|
|
|
2018-12-13 00:49:25 +01:00
|
|
|
home_msg_list.fetch_status.start_newer_batch();
|
|
|
|
message_list.all.fetch_status.start_newer_batch();
|
2018-03-20 15:28:24 +01:00
|
|
|
exports.load_messages({
|
|
|
|
anchor: latest_id.toFixed(),
|
|
|
|
num_before: 0,
|
|
|
|
num_after: consts.catch_up_batch_size,
|
|
|
|
msg_list: home_msg_list,
|
|
|
|
cont: load_more,
|
|
|
|
});
|
2017-03-19 18:19:48 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if (page_params.have_initial_messages) {
|
2018-03-20 15:28:24 +01:00
|
|
|
home_msg_list.fetch_status.start_newer_batch();
|
2018-03-26 20:54:49 +02:00
|
|
|
message_list.all.fetch_status.start_newer_batch();
|
2018-12-13 00:41:57 +01:00
|
|
|
home_msg_list.fetch_status.start_older_batch();
|
|
|
|
message_list.all.fetch_status.start_older_batch();
|
2018-03-09 02:17:54 +01:00
|
|
|
exports.load_messages({
|
2017-04-24 21:33:48 +02:00
|
|
|
anchor: page_params.pointer,
|
2018-03-08 21:25:14 +01:00
|
|
|
num_before: consts.num_before_pointer,
|
|
|
|
num_after: consts.num_after_pointer,
|
2017-03-19 18:19:48 +01:00
|
|
|
msg_list: home_msg_list,
|
|
|
|
cont: load_more,
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
server_events.home_view_loaded();
|
|
|
|
}
|
2017-07-04 01:30:47 +02:00
|
|
|
};
|
2017-03-19 18:19:48 +01:00
|
|
|
|
|
|
|
return exports;
|
|
|
|
|
|
|
|
}());
|
|
|
|
if (typeof module !== 'undefined') {
|
|
|
|
module.exports = message_fetch;
|
|
|
|
}
|
2018-05-28 08:04:36 +02:00
|
|
|
window.message_fetch = message_fetch;
|