message_fetch: Raise fatal error in an impossible case.

When fetching older/new messages, we used to resort to the pointer
to act as anchor when message list was empty.

This appears to be an impossible case, as
`fetch_status.can_load_newer_messages`
should be false in this case and user cannot be scrolling an
empty message_list in the first case.

Hence, we raise a fatal error to inform user of the same.
This commit is contained in:
Aman Agrawal
2020-06-06 20:05:50 +05:30
committed by Tim Abbott
parent 872d43713a
commit 77bbbf7ae0
2 changed files with 68 additions and 22 deletions

View File

@@ -16,9 +16,8 @@ zrequire('people');
set_global('recent_topics', { set_global('recent_topics', {
process_messages: noop, process_messages: noop,
}); });
set_global('page_params', { // Still required for page_params.initial_pointer
pointer: 444, set_global('page_params', {});
});
set_global('ui_report', { set_global('ui_report', {
hide_error: noop, hide_error: noop,
}); });
@@ -311,6 +310,23 @@ run_test('loading_newer', () => {
can_call_again: true, can_call_again: true,
}); });
// The msg_list is empty and we are calling frontfill, which should
// raise fatal error.
if (opts.empty_msg_list) {
assert.throws(
() => {
message_fetch.maybe_load_newer_messages({
msg_list: msg_list,
show_loading: noop,
hide_loading: noop,
});
},
{
name: "Error",
message: "There are no message available to frontfill.",
}
);
} else {
message_fetch.maybe_load_newer_messages({ message_fetch.maybe_load_newer_messages({
msg_list: msg_list, msg_list: msg_list,
show_loading: noop, show_loading: noop,
@@ -324,6 +340,7 @@ run_test('loading_newer', () => {
response: data.resp, response: data.resp,
}); });
} }
}
(function test_narrow() { (function test_narrow() {
const msg_list = simulate_narrow(); const msg_list = simulate_narrow();
@@ -345,6 +362,18 @@ run_test('loading_newer', () => {
test_happy_path({ test_happy_path({
msg_list: msg_list, msg_list: msg_list,
data: data, data: data,
empty_msg_list: true,
});
msg_list.append_to_view = () => {};
// Instead of using 444 as page_param.pointer, we
// should have a message with that id in the message_list.
msg_list.append(message_range(444, 445), false);
test_happy_path({
msg_list: msg_list,
data: data,
empty_msg_list: false,
}); });
assert.equal(msg_list.data.fetch_status.can_load_newer_messages(), true); assert.equal(msg_list.data.fetch_status.can_load_newer_messages(), true);
@@ -402,6 +431,16 @@ run_test('loading_newer', () => {
test_happy_path({ test_happy_path({
msg_list: msg_list, msg_list: msg_list,
data: data[0], data: data[0],
empty_msg_list: true,
});
message_list.all.append_to_view = () => {};
message_list.all.append(message_range(444, 445), false);
test_happy_path({
msg_list: msg_list,
data: data[0],
empty_msg_list: false,
}); });
assert.equal(msg_list.data.fetch_status.can_load_newer_messages(), true); assert.equal(msg_list.data.fetch_status.can_load_newer_messages(), true);

View File

@@ -252,18 +252,19 @@ exports.load_messages_for_narrow = function (opts) {
}; };
exports.get_backfill_anchor = function (msg_list) { exports.get_backfill_anchor = function (msg_list) {
let oldest_message_id;
if (msg_list === home_msg_list) { if (msg_list === home_msg_list) {
msg_list = message_list.all; msg_list = message_list.all;
} }
if (msg_list.first() === undefined) { const oldest_msg = msg_list.first();
oldest_message_id = page_params.pointer; if (oldest_msg) {
} else { return oldest_msg.id;
oldest_message_id = msg_list.first().id;
} }
return oldest_message_id;
// msg_list is empty, which is an impossible
// case, raise a fatal error.
blueslip.fatal("There are no message available to backfill.");
return;
}; };
exports.get_frontfill_anchor = function (msg_list) { exports.get_frontfill_anchor = function (msg_list) {
@@ -277,7 +278,13 @@ exports.get_frontfill_anchor = function (msg_list) {
return last_msg.id; return last_msg.id;
} }
return page_params.pointer; // Although it is impossible that we reach here since we
// are already checking `msg_list.fetch_status.can_load_newer_messages`
// and user cannot be scrolling down on an empty message_list to
// fetch more data, and if user is, then the available data is wrong
// and we raise a fatal error.
blueslip.fatal("There are no message available to frontfill.");
return;
}; };
exports.maybe_load_older_messages = function (opts) { exports.maybe_load_older_messages = function (opts) {
@@ -315,7 +322,7 @@ exports.do_backfill = function (opts) {
}; };
exports.maybe_load_newer_messages = function (opts) { exports.maybe_load_newer_messages = function (opts) {
// This function gets called when you scroll to the top // This function gets called when you scroll to the bottom
// of your window, and you want to get messages newer // of your window, and you want to get messages newer
// than what the browsers originally fetched. // than what the browsers originally fetched.
const msg_list = opts.msg_list; const msg_list = opts.msg_list;