Fix messages being added to the home view out of order.

So here's the reproduction recipe:
(1) Find a narrow that doesn't have any messages since 4 days ago

(2) Directly visit that narrow in your browser (or wait for someone to
do a deploy and thus auto-reload)

(3) Wait until load_old_messages has been called at least once

(4) Un-narrow

(5) Scroll up, and notice that the 400 most recent messages are above
sets of older messages.

The cause is that the code in add_messages assumed that
selected_message_id was within the range of message already in the
home view.  This is true in most cases where add_message is being
called, but it is not true in the case that the user was in a narrowed
view containing only very old messages (And thus selected_message_id
would be older than everything in the home view).

We can fix this by tracking persistent_message_id separately and using
that for the relevant test in add_messages.

(imported from commit f0da2561ba68f729343b260adc398029fae6acf7)
This commit is contained in:
Tim Abbott 2013-02-08 13:24:39 -05:00
parent 9edb616feb
commit 2d9a21654b
3 changed files with 19 additions and 17 deletions

View File

@ -69,7 +69,7 @@ var globals =
+ ' select_message select_message_by_id'
+ ' scroll_to_selected disable_pointer_movement get_private_message_recipient'
+ ' load_old_messages'
+ ' selected_message selected_message_id'
+ ' selected_message selected_message_id persistent_message_id'
+ ' at_top_of_viewport at_bottom_of_viewport'
+ ' viewport'
+ ' load_more_messages reset_load_more_status have_scrolled_away_from_top'

View File

@ -2,9 +2,6 @@ var narrow = (function () {
var exports = {};
// For tracking where you were before you narrowed.
var persistent_message_id = 0;
// For narrowing based on a particular message
var target_id = 0;
@ -214,11 +211,6 @@ exports.activate = function (operators, opts) {
allow_collapse = opts.allow_collapse;
// Your pointer isn't changed when narrowed.
if (! was_narrowed) {
persistent_message_id = selected_message_id;
}
// Before we clear the table, check if anything was highlighted.
var highlighted = search.something_is_highlighted();

View File

@ -6,6 +6,9 @@ var people_dict = {};
var viewport = $(window);
// For tracking where you are in the home view
var persistent_message_id = -1;
var selected_message_id = -1; /* to be filled in on document.ready */
var selected_message = $(); /* = rows.get(selected_message_id) */
var get_updates_params = {
@ -155,6 +158,7 @@ function send_pointer_update() {
}
$(function () {
persistent_message_id = initial_pointer;
furthest_read = initial_pointer;
server_furthest_read = initial_pointer;
$(document).idle({idle: 1000,
@ -168,13 +172,17 @@ function update_selected_message(message, opts) {
message.addClass(cls);
var new_selected_id = rows.id(message);
if (! narrow.active() && lurk_stream === undefined && new_selected_id > furthest_read)
{
// Narrowing is a temporary view on top of the home view and
// doesn't permanently affect where you are.
// Similarly, lurk mode does not affect your pointer.
furthest_read = new_selected_id;
// Narrowing is a temporary view on top of the home view and
// doesn't affect your pointer in the home view.
// Similarly, lurk mode does not affect your pointer.
if (! narrow.active() && lurk_stream === undefined) {
persistent_message_id = new_selected_id;
if (new_selected_id > furthest_read)
{
furthest_read = new_selected_id;
}
}
selected_message_id = new_selected_id;
selected_message = message;
}
@ -484,11 +492,12 @@ function add_messages(messages, add_to_home) {
messages = $.map(messages, add_message_metadata);
if (add_to_home) {
// persistent_message_id is guaranteed to be between the top and bottom
var top_messages_home = $.grep(messages, function (elem, idx) {
return (elem.id < selected_message_id && ! message_in_table.zhome[elem.id]);
return (elem.id < persistent_message_id && ! message_in_table.zhome[elem.id]);
});
var bottom_messages_home = $.grep(messages, function (elem, idx) {
return (elem.id >= selected_message_id && ! message_in_table.zhome[elem.id]);
return (elem.id >= persistent_message_id && ! message_in_table.zhome[elem.id]);
});
message_array = top_messages_home.concat(message_array).concat(bottom_messages_home);
add_to_table(top_messages_home, 'zhome', narrow.in_home, "top", true);
@ -499,6 +508,7 @@ function add_messages(messages, add_to_home) {
}
if (narrow.active()) {
// selected_message_id is guaranteed to be between the top and bottom
var top_messages_narrow = $.grep(messages, function (elem, idx) {
return (elem.id < selected_message_id && ! message_in_table.zfilt[elem.id]);
});