mirror of
				https://github.com/zulip/zulip.git
				synced 2025-11-04 05:53:43 +00:00 
			
		
		
		
	
		
			
				
	
	
		
			370 lines
		
	
	
		
			12 KiB
		
	
	
	
		
			JavaScript
		
	
	
	
	
	
			
		
		
	
	
			370 lines
		
	
	
		
			12 KiB
		
	
	
	
		
			JavaScript
		
	
	
	
	
	
var all_msg_list = new MessageList(
 | 
						|
    undefined, undefined,
 | 
						|
    {muting_enabled: false}
 | 
						|
);
 | 
						|
var home_msg_list = new MessageList('zhome',
 | 
						|
    new Filter([{operator: "in", operand: "home"}]), {muting_enabled: true}
 | 
						|
);
 | 
						|
var narrowed_msg_list;
 | 
						|
var current_msg_list = home_msg_list;
 | 
						|
 | 
						|
var recent_subjects = new Dict({fold_case: true});
 | 
						|
 | 
						|
var queued_mark_as_read = [];
 | 
						|
var queued_flag_timer;
 | 
						|
 | 
						|
var have_scrolled_away_from_top = true;
 | 
						|
 | 
						|
// Toggles re-centering the pointer in the window
 | 
						|
// when Home is next clicked by the user
 | 
						|
var recenter_pointer_on_display = false;
 | 
						|
var suppress_scroll_pointer_update = false;
 | 
						|
// Includes both scroll and arrow events. Negative means scroll up,
 | 
						|
// positive means scroll down.
 | 
						|
var last_viewport_movement_direction = 1;
 | 
						|
 | 
						|
var furthest_read = -1;
 | 
						|
var server_furthest_read = -1;
 | 
						|
var unread_messages_read_in_narrow = false;
 | 
						|
var pointer_update_in_flight = false;
 | 
						|
 | 
						|
function keep_pointer_in_view() {
 | 
						|
    // See recenter_view() for related logic to keep the pointer onscreen.
 | 
						|
    // This function mostly comes into place for mouse scrollers, and it
 | 
						|
    // keeps the pointer in view.  For people who purely scroll with the
 | 
						|
    // mouse, the pointer is kind of meaningless to them, but keyboard
 | 
						|
    // users will occasionally do big mouse scrolls, so this gives them
 | 
						|
    // a pointer reasonably close to the middle of the screen.
 | 
						|
    var candidate;
 | 
						|
    var next_row = current_msg_list.selected_row();
 | 
						|
 | 
						|
    if (next_row.length === 0) {
 | 
						|
        return;
 | 
						|
    }
 | 
						|
 | 
						|
    var info = viewport.message_viewport_info();
 | 
						|
    var top_threshold = info.visible_top + (1/10 * info.visible_height);
 | 
						|
    var bottom_threshold = info.visible_top + (9/10 * info.visible_height);
 | 
						|
 | 
						|
    function message_is_far_enough_down() {
 | 
						|
        if (viewport.at_top()) {
 | 
						|
            return true;
 | 
						|
        }
 | 
						|
 | 
						|
        var message_top = next_row.offset().top;
 | 
						|
 | 
						|
        // If the message starts after the very top of the screen, we just
 | 
						|
        // leave it alone.  This avoids bugs like #1608, where overzealousness
 | 
						|
        // about repositioning the pointer can cause users to miss messages.
 | 
						|
        if (message_top >= info.visible_top) {
 | 
						|
            return true;
 | 
						|
        }
 | 
						|
 | 
						|
 | 
						|
        // If at least part of the message is below top_threshold (10% from
 | 
						|
        // the top), then we also leave it alone.
 | 
						|
        var bottom_offset = message_top + next_row.outerHeight(true);
 | 
						|
        if (bottom_offset >= top_threshold) {
 | 
						|
            return true;
 | 
						|
        }
 | 
						|
 | 
						|
        // If we got this far, the message is not "in view."
 | 
						|
        return false;
 | 
						|
    }
 | 
						|
 | 
						|
    function message_is_far_enough_up() {
 | 
						|
        return viewport.at_bottom() ||
 | 
						|
            (next_row.offset().top <= bottom_threshold);
 | 
						|
    }
 | 
						|
 | 
						|
    function adjust(in_view, get_next_row) {
 | 
						|
        // return true only if we make an actual adjustment, so
 | 
						|
        // that we know to short circuit the other direction
 | 
						|
        if (in_view(next_row)) {
 | 
						|
            return false;  // try other side
 | 
						|
        }
 | 
						|
        while (!in_view(next_row)) {
 | 
						|
            candidate = get_next_row(next_row);
 | 
						|
            if (candidate.length === 0) {
 | 
						|
                break;
 | 
						|
            }
 | 
						|
            next_row = candidate;
 | 
						|
        }
 | 
						|
        return true;
 | 
						|
    }
 | 
						|
 | 
						|
    if (!adjust(message_is_far_enough_down, rows.next_visible)) {
 | 
						|
        adjust(message_is_far_enough_up, rows.prev_visible);
 | 
						|
    }
 | 
						|
 | 
						|
    current_msg_list.select_id(rows.id(next_row), {from_scroll: true});
 | 
						|
}
 | 
						|
 | 
						|
function recenter_view(message, opts) {
 | 
						|
    opts = opts || {};
 | 
						|
 | 
						|
    // Barnowl-style recentering: if the pointer is too high, move it to
 | 
						|
    // the 1/2 marks. If the pointer is too low, move it to the 1/7 mark.
 | 
						|
    // See keep_pointer_in_view() for related logic to keep the pointer onscreen.
 | 
						|
 | 
						|
    var viewport_info = viewport.message_viewport_info();
 | 
						|
    var top_threshold = viewport_info.visible_top;
 | 
						|
 | 
						|
    var bottom_threshold = viewport_info.visible_top + viewport_info.visible_height;
 | 
						|
 | 
						|
    var message_top = message.offset().top;
 | 
						|
    var message_height = message.outerHeight(true);
 | 
						|
    var message_bottom = message_top + message_height;
 | 
						|
 | 
						|
    var is_above = message_top < top_threshold;
 | 
						|
    var is_below = message_bottom > bottom_threshold;
 | 
						|
 | 
						|
    if (opts.from_scroll) {
 | 
						|
        // If the message you're trying to center on is already in view AND
 | 
						|
        // you're already trying to move in the direction of that message,
 | 
						|
        // don't try to recenter. This avoids disorienting jumps when the
 | 
						|
        // pointer has gotten itself outside the threshold (e.g. by
 | 
						|
        // autoscrolling).
 | 
						|
        if (is_above && last_viewport_movement_direction >= 0) {
 | 
						|
            return;
 | 
						|
        }
 | 
						|
        if (is_below && last_viewport_movement_direction <= 0) {
 | 
						|
            return;
 | 
						|
        }
 | 
						|
    }
 | 
						|
 | 
						|
    if (is_above || opts.force_center) {
 | 
						|
        viewport.set_message_position(message_top, message_height, viewport_info, 1/2);
 | 
						|
    } else if (is_below) {
 | 
						|
        viewport.set_message_position(message_top, message_height, viewport_info, 1/7);
 | 
						|
    }
 | 
						|
}
 | 
						|
 | 
						|
function scroll_to_selected() {
 | 
						|
    var selected_row = current_msg_list.selected_row();
 | 
						|
    if (selected_row && (selected_row.length !== 0)) {
 | 
						|
        recenter_view(selected_row);
 | 
						|
    }
 | 
						|
}
 | 
						|
 | 
						|
function maybe_scroll_to_selected() {
 | 
						|
    // If we have been previously instructed to re-center to the
 | 
						|
    // selected message, then do so
 | 
						|
    if (recenter_pointer_on_display) {
 | 
						|
        scroll_to_selected();
 | 
						|
        recenter_pointer_on_display = false;
 | 
						|
    }
 | 
						|
}
 | 
						|
 | 
						|
function get_private_message_recipient(message, attr, fallback_attr) {
 | 
						|
    var recipient, i;
 | 
						|
    var other_recipients = _.filter(message.display_recipient,
 | 
						|
                                  function (element) {
 | 
						|
                                      return element.email !== page_params.email;
 | 
						|
                                  });
 | 
						|
    if (other_recipients.length === 0) {
 | 
						|
        // private message with oneself
 | 
						|
        return message.display_recipient[0][attr];
 | 
						|
    }
 | 
						|
 | 
						|
    recipient = other_recipients[0][attr];
 | 
						|
    if (recipient === undefined && fallback_attr !== undefined) {
 | 
						|
        recipient = other_recipients[0][fallback_attr];
 | 
						|
    }
 | 
						|
    for (i = 1; i < other_recipients.length; i++) {
 | 
						|
        var attr_value = other_recipients[i][attr];
 | 
						|
        if (attr_value === undefined && fallback_attr !== undefined) {
 | 
						|
            attr_value = other_recipients[i][fallback_attr];
 | 
						|
        }
 | 
						|
        recipient += ', ' + attr_value;
 | 
						|
    }
 | 
						|
    return recipient;
 | 
						|
}
 | 
						|
 | 
						|
function respond_to_message(opts) {
 | 
						|
    var message, msg_type;
 | 
						|
    // Before initiating a reply to a message, if there's an
 | 
						|
    // in-progress composition, snapshot it.
 | 
						|
    compose.snapshot_message();
 | 
						|
 | 
						|
    message = current_msg_list.selected_message();
 | 
						|
 | 
						|
    if (message === undefined) {
 | 
						|
        return;
 | 
						|
    }
 | 
						|
 | 
						|
    unread.mark_message_as_read(message);
 | 
						|
 | 
						|
    var stream = '';
 | 
						|
    var subject = '';
 | 
						|
    if (message.type === "stream") {
 | 
						|
        stream = message.stream;
 | 
						|
        subject = message.subject;
 | 
						|
    }
 | 
						|
 | 
						|
    var pm_recipient = message.reply_to;
 | 
						|
    if (opts.reply_type === "personal" && message.type === "private") {
 | 
						|
        // reply_to for private messages is everyone involved, so for
 | 
						|
        // personals replies we need to set the the private message
 | 
						|
        // recipient to just the sender
 | 
						|
        pm_recipient = message.sender_email;
 | 
						|
    }
 | 
						|
    if (opts.reply_type === 'personal' || message.type === 'private') {
 | 
						|
        msg_type = 'private';
 | 
						|
    } else {
 | 
						|
        msg_type = message.type;
 | 
						|
    }
 | 
						|
    compose.start(msg_type, {'stream': stream, 'subject': subject,
 | 
						|
                             'private_message_recipient': pm_recipient,
 | 
						|
                             'replying_to_message': message,
 | 
						|
                             'trigger': opts.trigger});
 | 
						|
 | 
						|
}
 | 
						|
 | 
						|
function update_pointer() {
 | 
						|
    if (!pointer_update_in_flight) {
 | 
						|
        pointer_update_in_flight = true;
 | 
						|
        return channel.post({
 | 
						|
            url:      '/json/update_pointer',
 | 
						|
            idempotent: true,
 | 
						|
            data:     {pointer: furthest_read},
 | 
						|
            success: function () {
 | 
						|
                server_furthest_read = furthest_read;
 | 
						|
                pointer_update_in_flight = false;
 | 
						|
            },
 | 
						|
            error: function () {
 | 
						|
                pointer_update_in_flight = false;
 | 
						|
            }
 | 
						|
        });
 | 
						|
    } else {
 | 
						|
        // Return an empty, resolved Deferred.
 | 
						|
        return $.when();
 | 
						|
    }
 | 
						|
}
 | 
						|
 | 
						|
function send_pointer_update() {
 | 
						|
    // Only bother if you've read new messages.
 | 
						|
    if (furthest_read > server_furthest_read) {
 | 
						|
        update_pointer();
 | 
						|
    }
 | 
						|
}
 | 
						|
 | 
						|
function unconditionally_send_pointer_update() {
 | 
						|
    if (pointer_update_in_flight) {
 | 
						|
        // Keep trying.
 | 
						|
        var deferred = $.Deferred();
 | 
						|
 | 
						|
        setTimeout(function () {
 | 
						|
            deferred.resolve(unconditionally_send_pointer_update());
 | 
						|
        }, 100);
 | 
						|
        return deferred;
 | 
						|
    } else {
 | 
						|
        return update_pointer();
 | 
						|
    }
 | 
						|
}
 | 
						|
 | 
						|
function fast_forward_pointer() {
 | 
						|
    channel.post({
 | 
						|
        url: '/json/get_profile',
 | 
						|
        idempotent: true,
 | 
						|
        data: {email: page_params.email},
 | 
						|
        success: function (data) {
 | 
						|
            unread.mark_all_as_read(function () {
 | 
						|
                furthest_read = data.max_message_id;
 | 
						|
                unconditionally_send_pointer_update().then(function () {
 | 
						|
                    ui.change_tab_to('#home');
 | 
						|
                    reload.initiate({immediate: true,
 | 
						|
                                     save_pointer: false,
 | 
						|
                                     save_narrow: false,
 | 
						|
                                     save_compose: false});
 | 
						|
                });
 | 
						|
            });
 | 
						|
        }
 | 
						|
    });
 | 
						|
}
 | 
						|
 | 
						|
function consider_bankruptcy() {
 | 
						|
    // Until we've handled possibly declaring bankruptcy, don't show
 | 
						|
    // unread counts since they only consider messages that are loaded
 | 
						|
    // client side and may be different from the numbers reported by
 | 
						|
    // the server.
 | 
						|
 | 
						|
    if (!page_params.furthest_read_time) {
 | 
						|
        // We've never read a message.
 | 
						|
        unread.enable();
 | 
						|
        return;
 | 
						|
    }
 | 
						|
 | 
						|
    var now = new XDate(true).getTime() / 1000;
 | 
						|
    if ((page_params.unread_count > 500) &&
 | 
						|
        (now - page_params.furthest_read_time > 60 * 60 * 24 * 2)) { // 2 days.
 | 
						|
        var unread_info = templates.render('bankruptcy_modal',
 | 
						|
                                           {"unread_count": page_params.unread_count});
 | 
						|
        $('#bankruptcy-unread-count').html(unread_info);
 | 
						|
        $('#bankruptcy').modal('show');
 | 
						|
    } else {
 | 
						|
        unread.enable();
 | 
						|
    }
 | 
						|
}
 | 
						|
 | 
						|
// This is annoying to move to unread.js because the natural name
 | 
						|
// would be unread.process_loaded_messages, which this calls
 | 
						|
function process_loaded_for_unread(messages) {
 | 
						|
    activity.process_loaded_messages(messages);
 | 
						|
    activity.update_huddles();
 | 
						|
    unread.process_loaded_messages(messages);
 | 
						|
    unread.update_unread_counts();
 | 
						|
    resize.resize_page_components();
 | 
						|
}
 | 
						|
 | 
						|
function main() {
 | 
						|
    activity.set_user_statuses(page_params.initial_presences,
 | 
						|
                               page_params.initial_servertime);
 | 
						|
 | 
						|
    server_furthest_read = page_params.initial_pointer;
 | 
						|
    if (page_params.orig_initial_pointer !== undefined &&
 | 
						|
        page_params.orig_initial_pointer > server_furthest_read) {
 | 
						|
        server_furthest_read = page_params.orig_initial_pointer;
 | 
						|
    }
 | 
						|
    furthest_read = server_furthest_read;
 | 
						|
 | 
						|
    // Before trying to load messages: is this user way behind?
 | 
						|
    consider_bankruptcy();
 | 
						|
 | 
						|
    // We only send pointer updates when the user has been idle for a
 | 
						|
    // short while to avoid hammering the server
 | 
						|
    $(document).idle({idle: 1000,
 | 
						|
                      onIdle: send_pointer_update,
 | 
						|
                      keepTracking: true});
 | 
						|
 | 
						|
    $(document).on('message_selected.zulip', function (event) {
 | 
						|
        // Only advance the pointer when not narrowed
 | 
						|
        if (event.id === -1) {
 | 
						|
            return;
 | 
						|
        }
 | 
						|
        // Additionally, don't advance the pointer server-side
 | 
						|
        // if the selected message is local-only
 | 
						|
        if (event.msg_list === home_msg_list && page_params.narrow_stream === undefined) {
 | 
						|
            if (event.id > furthest_read &&
 | 
						|
                home_msg_list.get(event.id).local_id === undefined) {
 | 
						|
                furthest_read = event.id;
 | 
						|
            }
 | 
						|
        }
 | 
						|
 | 
						|
        if (event.mark_read && event.previously_selected !== -1) {
 | 
						|
            // Mark messages between old pointer and new pointer as read
 | 
						|
            var messages;
 | 
						|
            if (event.id < event.previously_selected) {
 | 
						|
                messages = event.msg_list.message_range(event.id, event.previously_selected);
 | 
						|
            } else {
 | 
						|
                messages = event.msg_list.message_range(event.previously_selected, event.id);
 | 
						|
            }
 | 
						|
            unread.mark_messages_as_read(messages, {from: 'pointer'});
 | 
						|
        }
 | 
						|
    });
 | 
						|
}
 | 
						|
 | 
						|
$(function () {
 | 
						|
    main();
 | 
						|
});
 |