2013-09-22 21:11:43 +02:00
|
|
|
var all_msg_list = new MessageList(
|
|
|
|
undefined, undefined,
|
|
|
|
{muting_enabled: false}
|
|
|
|
);
|
2013-07-25 22:08:16 +02:00
|
|
|
var home_msg_list = new MessageList('zhome',
|
2013-08-10 01:31:31 +02:00
|
|
|
new Filter([["in", "home"]]),
|
2013-09-22 21:11:43 +02:00
|
|
|
{
|
|
|
|
muting_enabled: true,
|
|
|
|
summarize_read: feature_flags.summarize_read_while_narrowed?'home':false
|
|
|
|
}
|
2013-07-25 22:08:16 +02:00
|
|
|
);
|
2013-02-12 20:01:24 +01:00
|
|
|
var narrowed_msg_list;
|
2013-02-22 20:48:31 +01:00
|
|
|
var current_msg_list = home_msg_list;
|
2013-09-06 00:19:15 +02:00
|
|
|
|
2014-01-04 01:17:09 +01:00
|
|
|
// The following three Dicts point to the same objects
|
2013-09-06 00:19:15 +02:00
|
|
|
// All people we've seen
|
2013-08-07 23:56:51 +02:00
|
|
|
var people_dict = new Dict();
|
2014-01-04 01:17:09 +01:00
|
|
|
var people_by_name_dict = new Dict();
|
2013-09-06 00:19:15 +02:00
|
|
|
// People in this realm
|
|
|
|
var realm_people_dict = new Dict();
|
|
|
|
|
2013-08-19 21:17:10 +02:00
|
|
|
var recent_subjects = new Dict({fold_case: true});
|
2012-09-22 01:11:54 +02:00
|
|
|
|
2013-03-04 23:44:07 +01:00
|
|
|
var queued_mark_as_read = [];
|
|
|
|
var queued_flag_timer;
|
|
|
|
|
2012-10-16 22:06:03 +02:00
|
|
|
var get_updates_params = {
|
2013-03-28 18:09:27 +01:00
|
|
|
pointer: -1
|
2012-10-17 23:45:03 +02:00
|
|
|
};
|
2012-11-27 21:06:17 +01:00
|
|
|
var get_updates_failures = 0;
|
2012-10-17 23:45:03 +02:00
|
|
|
|
2012-11-27 23:17:30 +01:00
|
|
|
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().
|
|
|
|
var have_scrolled_away_from_top = true;
|
|
|
|
|
2013-02-12 22:32:14 +01:00
|
|
|
// 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;
|
2013-03-28 19:16:48 +01:00
|
|
|
// Includes both scroll and arrow events. Negative means scroll up,
|
|
|
|
// positive means scroll down.
|
|
|
|
var last_viewport_movement_direction = 1;
|
2013-02-12 22:32:14 +01:00
|
|
|
|
2013-02-20 20:49:49 +01:00
|
|
|
var furthest_read = -1;
|
|
|
|
var server_furthest_read = -1;
|
2013-11-26 19:06:21 +01:00
|
|
|
var unread_messages_read_in_narrow = false;
|
2013-03-13 21:53:36 +01:00
|
|
|
var pointer_update_in_flight = false;
|
2013-08-19 19:02:52 +02:00
|
|
|
var suppress_unread_counts = true;
|
2013-02-20 20:49:49 +01:00
|
|
|
|
2013-06-27 22:18:28 +02:00
|
|
|
var events_stored_during_tutorial = [];
|
|
|
|
|
2013-08-02 23:56:28 +02:00
|
|
|
var waiting_on_browser_scroll = true;
|
|
|
|
|
2013-09-06 00:19:15 +02:00
|
|
|
function add_person(person, in_realm) {
|
2013-03-25 23:26:14 +01:00
|
|
|
page_params.people_list.push(person);
|
2013-08-07 23:56:51 +02:00
|
|
|
people_dict.set(person.email, person);
|
2014-01-04 01:17:09 +01:00
|
|
|
people_by_name_dict.set(person.full_name, person);
|
2013-06-18 21:09:29 +02:00
|
|
|
person.pm_recipient_count = 0;
|
2013-01-14 22:18:30 +01:00
|
|
|
}
|
|
|
|
|
2013-09-06 00:19:15 +02:00
|
|
|
function add_person_in_realm(person) {
|
|
|
|
realm_people_dict.set(person.email, person);
|
|
|
|
add_person(person);
|
|
|
|
}
|
|
|
|
|
2013-03-29 18:22:23 +01:00
|
|
|
function remove_person(person) {
|
|
|
|
var i;
|
|
|
|
for (i = 0; i < page_params.people_list.length; i++) {
|
|
|
|
if (page_params.people_list[i].email === person.email) {
|
|
|
|
page_params.people_list.splice(i, 1);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2013-08-07 23:56:51 +02:00
|
|
|
people_dict.del(person.email);
|
2014-01-04 01:17:09 +01:00
|
|
|
people_by_name_dict.del(person.full_name);
|
2013-09-06 00:19:15 +02:00
|
|
|
realm_people_dict.del(person.email);
|
2013-03-29 18:22:23 +01:00
|
|
|
}
|
|
|
|
|
2013-07-16 21:32:33 +02:00
|
|
|
function update_person(person) {
|
|
|
|
// Currently the only attribute that can change is full_name, so
|
|
|
|
// we just push out changes to that field. As we add more things
|
|
|
|
// that can change, this will need to either get complicated or be
|
|
|
|
// replaced by MVC
|
2013-08-07 23:56:51 +02:00
|
|
|
if (! people_dict.has(person.email)) {
|
2013-07-22 23:22:50 +02:00
|
|
|
blueslip.error("Got update_person event for unexpected user",
|
|
|
|
{email: person.email});
|
|
|
|
return;
|
|
|
|
}
|
2014-01-04 01:17:09 +01:00
|
|
|
var person_obj = people_dict.get(person.email);
|
2014-01-21 22:24:20 +01:00
|
|
|
|
2014-01-21 23:04:56 +01:00
|
|
|
if (_.has(person, 'full_name')) {
|
|
|
|
if (people_by_name_dict.has(person_obj.full_name)) {
|
|
|
|
people_by_name_dict.set(person.full_name, person_obj);
|
|
|
|
people_by_name_dict.del(person_obj.full_name);
|
|
|
|
}
|
2014-01-21 22:24:20 +01:00
|
|
|
|
2014-01-21 23:04:56 +01:00
|
|
|
person_obj.full_name = person.full_name;
|
2014-01-21 22:24:20 +01:00
|
|
|
|
2014-01-21 23:04:56 +01:00
|
|
|
if (person.email === page_params.email) {
|
|
|
|
page_params.fullname = person.full_name;
|
|
|
|
}
|
2014-01-22 01:47:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (_.has(person, 'is_admin')) {
|
|
|
|
person_obj.is_admin = person.is_admin;
|
|
|
|
|
|
|
|
if (person.email === page_params.email) {
|
|
|
|
page_params.is_admin = person.is_admin;
|
|
|
|
admin.show_or_hide_menu_item();
|
|
|
|
}
|
2013-07-16 21:32:33 +02:00
|
|
|
}
|
2014-01-21 22:24:20 +01:00
|
|
|
|
2013-07-16 21:32:33 +02:00
|
|
|
activity.set_user_statuses([]);
|
|
|
|
|
|
|
|
// TODO: update sender names on messages
|
|
|
|
}
|
|
|
|
|
2013-05-07 17:13:55 +02:00
|
|
|
function within_viewport(row_offset, row_height) {
|
2013-05-22 18:06:40 +02:00
|
|
|
// Returns true if a message is fully within the effectively visible
|
2013-05-20 19:01:35 +02:00
|
|
|
// part of the viewport.
|
2013-05-07 17:13:55 +02:00
|
|
|
var message_top = row_offset.top;
|
|
|
|
var message_bottom = message_top + row_height;
|
2013-06-06 16:10:12 +02:00
|
|
|
var info = viewport.message_viewport_info();
|
2013-05-20 19:01:35 +02:00
|
|
|
var viewport_top = info.visible_top;
|
|
|
|
var viewport_bottom = viewport_top + info.visible_height;
|
2013-03-23 05:09:41 +01:00
|
|
|
return (message_top > viewport_top) && (message_bottom < viewport_bottom);
|
|
|
|
}
|
|
|
|
|
2013-06-05 17:56:43 +02:00
|
|
|
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();
|
|
|
|
|
2013-08-01 17:47:48 +02:00
|
|
|
if (next_row.length === 0) {
|
2013-06-05 17:56:43 +02:00
|
|
|
return;
|
2013-08-01 17:47:48 +02:00
|
|
|
}
|
2013-06-05 17:56:43 +02:00
|
|
|
|
2013-06-06 16:10:12 +02:00
|
|
|
var info = viewport.message_viewport_info();
|
2013-06-05 17:56:43 +02:00
|
|
|
var top_threshold = info.visible_top + (1/10 * info.visible_height);
|
|
|
|
var bottom_threshold = info.visible_top + (9/10 * info.visible_height);
|
|
|
|
|
2013-08-05 21:32:57 +02:00
|
|
|
function message_is_far_enough_down() {
|
|
|
|
if (viewport.at_top()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
var message_top = next_row.offset().top;
|
|
|
|
|
2013-08-05 21:42:59 +02:00
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
|
2013-08-05 21:32:57 +02:00
|
|
|
|
2013-08-05 21:42:59 +02:00
|
|
|
// If at least part of the message is below top_threshold (10% from
|
|
|
|
// the top), then we also leave it alone.
|
2013-08-05 21:32:57 +02:00
|
|
|
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;
|
2013-06-05 17:56:43 +02:00
|
|
|
}
|
|
|
|
|
2013-08-05 21:32:57 +02:00
|
|
|
function message_is_far_enough_up() {
|
|
|
|
return viewport.at_bottom() ||
|
|
|
|
(next_row.offset().top <= bottom_threshold);
|
2013-06-05 17:56:43 +02:00
|
|
|
}
|
|
|
|
|
2013-08-05 21:32:57 +02:00
|
|
|
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)) {
|
2013-06-05 17:56:43 +02:00
|
|
|
return false; // try other side
|
2013-08-01 17:47:48 +02:00
|
|
|
}
|
2013-08-05 21:32:57 +02:00
|
|
|
while (!in_view(next_row)) {
|
|
|
|
candidate = get_next_row(next_row);
|
2013-08-01 17:47:48 +02:00
|
|
|
if (candidate.length === 0) {
|
2013-06-05 17:56:43 +02:00
|
|
|
break;
|
2013-08-01 17:47:48 +02:00
|
|
|
}
|
2013-06-05 17:56:43 +02:00
|
|
|
next_row = candidate;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-08-05 21:32:57 +02:00
|
|
|
if (!adjust(message_is_far_enough_down, rows.next_visible)) {
|
|
|
|
adjust(message_is_far_enough_up, rows.prev_visible);
|
2013-08-01 17:47:48 +02:00
|
|
|
}
|
2013-06-05 17:56:43 +02:00
|
|
|
|
|
|
|
current_msg_list.select_id(rows.id(next_row), {from_scroll: true});
|
|
|
|
}
|
|
|
|
|
2013-07-03 19:32:16 +02:00
|
|
|
function recenter_view(message, opts) {
|
2013-07-30 05:11:50 +02:00
|
|
|
opts = opts || {};
|
|
|
|
|
2013-06-03 17:04:01 +02:00
|
|
|
// Barnowl-style recentering: if the pointer is too high, move it to
|
2013-06-04 22:08:52 +02:00
|
|
|
// the 1/2 marks. If the pointer is too low, move it to the 1/7 mark.
|
2013-06-03 17:04:01 +02:00
|
|
|
// See keep_pointer_in_view() for related logic to keep the pointer onscreen.
|
2012-10-05 16:31:10 +02:00
|
|
|
|
2013-06-06 16:10:12 +02:00
|
|
|
var viewport_info = viewport.message_viewport_info();
|
2013-06-04 22:08:52 +02:00
|
|
|
var top_threshold = viewport_info.visible_top;
|
2013-05-30 20:39:28 +02:00
|
|
|
|
2013-06-04 22:08:52 +02:00
|
|
|
var bottom_threshold = viewport_info.visible_top + viewport_info.visible_height;
|
2013-03-28 19:16:48 +01:00
|
|
|
|
2013-05-30 20:39:28 +02:00
|
|
|
var message_top = message.offset().top;
|
2013-06-05 17:28:10 +02:00
|
|
|
var message_height = message.outerHeight(true);
|
|
|
|
var message_bottom = message_top + message_height;
|
|
|
|
|
2013-05-30 20:39:28 +02:00
|
|
|
var is_above = message_top < top_threshold;
|
2013-06-04 22:08:52 +02:00
|
|
|
var is_below = message_bottom > bottom_threshold;
|
2013-05-30 20:39:28 +02:00
|
|
|
|
2013-07-03 19:32:16 +02:00
|
|
|
if (opts.from_scroll) {
|
2013-03-28 19:16:48 +01:00
|
|
|
// 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).
|
2013-05-30 20:39:28 +02:00
|
|
|
if (is_above && last_viewport_movement_direction >= 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (is_below && last_viewport_movement_direction <= 0) {
|
|
|
|
return;
|
|
|
|
}
|
2013-03-28 19:16:48 +01:00
|
|
|
}
|
|
|
|
|
2013-07-03 19:32:16 +02:00
|
|
|
if (is_above || opts.force_center) {
|
2013-06-05 17:28:10 +02:00
|
|
|
viewport.set_message_position(message_top, message_height, viewport_info, 1/2);
|
2013-05-30 20:39:28 +02:00
|
|
|
} else if (is_below) {
|
2013-06-05 17:28:10 +02:00
|
|
|
viewport.set_message_position(message_top, message_height, viewport_info, 1/7);
|
2012-10-05 16:31:10 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function scroll_to_selected() {
|
2013-02-14 23:48:37 +01:00
|
|
|
var selected_row = current_msg_list.selected_row();
|
2013-08-01 17:47:48 +02:00
|
|
|
if (selected_row && (selected_row.length !== 0)) {
|
2013-02-14 23:48:37 +01:00
|
|
|
recenter_view(selected_row);
|
2013-08-01 17:47:48 +02:00
|
|
|
}
|
2012-09-07 20:44:55 +02:00
|
|
|
}
|
|
|
|
|
2013-02-12 22:32:14 +01:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-03 19:49:12 +01:00
|
|
|
function get_private_message_recipient(message, attr) {
|
2012-09-13 22:00:11 +02:00
|
|
|
var recipient, i;
|
2013-07-29 22:49:42 +02:00
|
|
|
var other_recipients = _.filter(message.display_recipient,
|
|
|
|
function (element) {
|
2013-03-25 23:26:14 +01:00
|
|
|
return element.email !== page_params.email;
|
2012-10-15 21:57:41 +02:00
|
|
|
});
|
2012-10-17 23:07:46 +02:00
|
|
|
if (other_recipients.length === 0) {
|
2012-12-03 19:49:12 +01:00
|
|
|
// private message with oneself
|
2012-10-27 02:31:46 +02:00
|
|
|
return message.display_recipient[0][attr];
|
2012-10-17 23:07:46 +02:00
|
|
|
}
|
2012-09-13 22:00:11 +02:00
|
|
|
|
2012-10-27 02:31:46 +02:00
|
|
|
recipient = other_recipients[0][attr];
|
2012-10-15 21:57:41 +02:00
|
|
|
for (i = 1; i < other_recipients.length; i++) {
|
2012-10-27 02:31:46 +02:00
|
|
|
recipient += ', ' + other_recipients[i][attr];
|
2012-09-26 21:25:49 +02:00
|
|
|
}
|
|
|
|
return recipient;
|
|
|
|
}
|
|
|
|
|
2013-05-02 02:46:36 +02:00
|
|
|
// Returns messages from the given message list in the specified range, inclusive
|
2013-02-12 20:01:24 +01:00
|
|
|
function message_range(msg_list, start, end) {
|
2013-05-02 02:46:36 +02:00
|
|
|
if (start === -1) {
|
|
|
|
blueslip.error("message_range given a start of -1");
|
2013-02-12 20:41:01 +01:00
|
|
|
}
|
|
|
|
|
2013-05-02 02:46:36 +02:00
|
|
|
var all = msg_list.all();
|
|
|
|
var compare = function (a, b) { return a.id < b; };
|
|
|
|
|
|
|
|
var start_idx = util.lower_bound(all, start, compare);
|
|
|
|
var end_idx = util.lower_bound(all, end, compare);
|
|
|
|
return all.slice(start_idx, end_idx + 1);
|
2013-02-12 20:41:01 +01:00
|
|
|
}
|
|
|
|
|
2013-08-05 21:50:29 +02:00
|
|
|
function batched_flag_updater(flag, op) {
|
|
|
|
var queue = [];
|
2013-02-12 20:41:01 +01:00
|
|
|
|
2013-05-02 21:14:17 +02:00
|
|
|
function on_success(data, status, jqXHR) {
|
2013-08-05 21:50:29 +02:00
|
|
|
queue = _.filter(queue, function (message) {
|
2013-05-02 21:14:17 +02:00
|
|
|
return data.messages.indexOf(message) === -1;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2013-08-05 21:50:29 +02:00
|
|
|
function server_request() {
|
2013-12-18 19:55:18 +01:00
|
|
|
channel.post({
|
2013-08-05 21:50:29 +02:00
|
|
|
url: '/json/update_message_flags',
|
2014-01-07 23:40:31 +01:00
|
|
|
idempotent: true,
|
2013-08-05 21:50:29 +02:00
|
|
|
data: {messages: JSON.stringify(queue),
|
|
|
|
op: op,
|
|
|
|
flag: flag},
|
|
|
|
success: on_success
|
|
|
|
});
|
|
|
|
}
|
2013-07-25 22:08:16 +02:00
|
|
|
|
2013-08-05 21:50:29 +02:00
|
|
|
var start = _.debounce(server_request, 1000);
|
|
|
|
|
|
|
|
function add(message) {
|
|
|
|
if (message.flags === undefined) {
|
|
|
|
message.flags = [];
|
|
|
|
}
|
|
|
|
if (op === 'add') {
|
|
|
|
message.flags.push(flag);
|
|
|
|
} else {
|
|
|
|
message.flags = _.without(message.flags, flag);
|
|
|
|
}
|
|
|
|
queue.push(message.id);
|
|
|
|
start();
|
2013-07-25 22:08:16 +02:00
|
|
|
}
|
2013-08-05 21:50:29 +02:00
|
|
|
|
|
|
|
return add;
|
2013-03-04 23:44:07 +01:00
|
|
|
}
|
2013-02-12 22:32:14 +01:00
|
|
|
|
2013-08-05 21:50:29 +02:00
|
|
|
var send_read = batched_flag_updater('read', 'add');
|
|
|
|
var send_summarize_in_stream = batched_flag_updater('summarize_in_stream', 'add');
|
|
|
|
var send_summarize_in_home = batched_flag_updater('summarize_in_home', 'add');
|
|
|
|
|
2013-05-14 03:58:07 +02:00
|
|
|
function update_unread_counts() {
|
2013-08-19 19:02:52 +02:00
|
|
|
if (suppress_unread_counts) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-05-14 03:58:07 +02:00
|
|
|
// Pure computation:
|
2013-05-17 21:32:26 +02:00
|
|
|
var res = unread.get_counts();
|
2013-05-14 03:58:07 +02:00
|
|
|
|
|
|
|
// Side effects from here down:
|
|
|
|
// This updates some DOM elements directly, so try to
|
|
|
|
// avoid excessive calls to this.
|
2013-05-22 18:06:40 +02:00
|
|
|
stream_list.update_dom_with_unread_counts(res);
|
2013-07-18 21:55:43 +02:00
|
|
|
notifications.update_title_count(res.home_unread_messages);
|
2013-06-19 00:00:40 +02:00
|
|
|
notifications.update_pm_count(res.private_message_count);
|
2013-07-18 21:55:43 +02:00
|
|
|
notifications_bar.update(res.home_unread_messages);
|
2013-03-04 23:44:07 +01:00
|
|
|
}
|
|
|
|
|
2013-08-19 19:02:52 +02:00
|
|
|
function enable_unread_counts() {
|
|
|
|
suppress_unread_counts = false;
|
|
|
|
update_unread_counts();
|
|
|
|
}
|
|
|
|
|
2013-03-15 20:07:38 +01:00
|
|
|
function mark_all_as_read(cont) {
|
2013-07-30 00:35:44 +02:00
|
|
|
_.each(all_msg_list.all(), function (msg) {
|
2013-03-04 23:44:07 +01:00
|
|
|
msg.flags = msg.flags || [];
|
|
|
|
msg.flags.push('read');
|
|
|
|
});
|
2013-05-17 21:32:26 +02:00
|
|
|
unread.declare_bankruptcy();
|
2013-03-04 23:44:07 +01:00
|
|
|
update_unread_counts();
|
|
|
|
|
2013-12-18 19:55:18 +01:00
|
|
|
channel.post({
|
2013-03-04 23:44:07 +01:00
|
|
|
url: '/json/update_message_flags',
|
2014-01-07 23:40:31 +01:00
|
|
|
idempotent: true,
|
2013-03-04 23:44:07 +01:00
|
|
|
data: {messages: JSON.stringify([]),
|
|
|
|
all: true,
|
|
|
|
op: 'add',
|
|
|
|
flag: 'read'},
|
2013-03-15 20:07:38 +01:00
|
|
|
success: cont});
|
2013-03-04 23:44:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
function process_loaded_for_unread(messages) {
|
2013-10-18 00:56:49 +02:00
|
|
|
activity.process_loaded_messages(messages);
|
|
|
|
activity.update_huddles();
|
2013-05-17 21:32:26 +02:00
|
|
|
unread.process_loaded_messages(messages);
|
2013-03-04 23:44:07 +01:00
|
|
|
update_unread_counts();
|
2013-11-14 22:57:55 +01:00
|
|
|
ui.resize_page_components();
|
2013-03-04 23:44:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Takes a list of messages and marks them as read
|
2013-08-05 20:15:35 +02:00
|
|
|
function mark_messages_as_read(messages, options) {
|
2013-07-26 17:19:13 +02:00
|
|
|
options = options || {};
|
2013-08-05 21:50:29 +02:00
|
|
|
var processed = false;
|
2013-03-04 23:44:07 +01:00
|
|
|
|
2013-07-25 22:08:16 +02:00
|
|
|
_.each(messages, function (message) {
|
2013-08-05 20:15:35 +02:00
|
|
|
if (!unread.message_unread(message)) {
|
|
|
|
// Don't do anything if the message is already read.
|
|
|
|
return;
|
|
|
|
}
|
2013-11-26 19:06:21 +01:00
|
|
|
if (current_msg_list === narrowed_msg_list) {
|
|
|
|
unread_messages_read_in_narrow = true;
|
|
|
|
}
|
2013-08-05 20:15:35 +02:00
|
|
|
|
2013-08-05 21:50:29 +02:00
|
|
|
send_read(message);
|
2013-08-12 23:34:41 +02:00
|
|
|
summary.maybe_mark_summarized(message);
|
2013-07-25 22:08:16 +02:00
|
|
|
|
2013-07-01 23:28:27 +02:00
|
|
|
message.unread = false;
|
2013-07-26 17:19:13 +02:00
|
|
|
unread.process_read_message(message, options);
|
|
|
|
home_msg_list.show_message_as_read(message, options);
|
|
|
|
all_msg_list.show_message_as_read(message, options);
|
|
|
|
if (narrowed_msg_list) {
|
|
|
|
narrowed_msg_list.show_message_as_read(message, options);
|
|
|
|
}
|
2013-08-29 22:58:00 +02:00
|
|
|
notifications.close_notification(message);
|
2013-08-05 21:50:29 +02:00
|
|
|
processed = true;
|
2013-03-04 23:44:07 +01:00
|
|
|
});
|
|
|
|
|
2013-08-05 21:50:29 +02:00
|
|
|
if (processed) {
|
2013-08-05 20:15:35 +02:00
|
|
|
update_unread_counts();
|
2013-03-04 23:44:07 +01:00
|
|
|
}
|
2013-08-05 20:15:35 +02:00
|
|
|
}
|
2013-03-04 23:44:07 +01:00
|
|
|
|
2013-08-05 20:15:35 +02:00
|
|
|
function mark_message_as_read(message, options) {
|
|
|
|
mark_messages_as_read([message], options);
|
2013-02-12 20:41:01 +01:00
|
|
|
}
|
|
|
|
|
2013-07-11 17:14:11 +02:00
|
|
|
// If we ever materially change the algorithm for this function, we
|
|
|
|
// may need to update notifications.received_messages as well.
|
|
|
|
function process_visible_unread_messages(update_cursor) {
|
|
|
|
if (! notifications.window_has_focus()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-07-26 19:14:33 +02:00
|
|
|
if (feature_flags.mark_read_at_bottom) {
|
|
|
|
if (viewport.bottom_message_visible()) {
|
|
|
|
mark_current_list_as_read();
|
|
|
|
}
|
|
|
|
} else {
|
2013-10-30 19:53:17 +01:00
|
|
|
mark_messages_as_read(viewport.visible_messages(true));
|
2013-07-11 17:14:11 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-26 17:19:13 +02:00
|
|
|
function mark_current_list_as_read(options) {
|
2013-08-05 20:15:35 +02:00
|
|
|
mark_messages_as_read(current_msg_list.all(), options);
|
2013-07-08 19:53:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
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});
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-06-19 20:05:44 +02:00
|
|
|
function update_pointer() {
|
|
|
|
if (!pointer_update_in_flight) {
|
2013-03-13 21:53:36 +01:00
|
|
|
pointer_update_in_flight = true;
|
2013-12-18 19:55:18 +01:00
|
|
|
return channel.post({
|
2013-03-13 21:53:36 +01:00
|
|
|
url: '/json/update_pointer',
|
2014-01-07 23:40:31 +01:00
|
|
|
idempotent: true,
|
2013-03-13 21:53:36 +01:00
|
|
|
data: {pointer: furthest_read},
|
|
|
|
success: function () {
|
|
|
|
server_furthest_read = furthest_read;
|
|
|
|
pointer_update_in_flight = false;
|
|
|
|
},
|
|
|
|
error: function () {
|
|
|
|
pointer_update_in_flight = false;
|
|
|
|
}
|
|
|
|
});
|
2013-06-19 20:05:44 +02:00
|
|
|
} 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.
|
2013-07-26 23:28:21 +02:00
|
|
|
var deferred = $.Deferred();
|
|
|
|
|
|
|
|
setTimeout(function () {
|
|
|
|
deferred.resolve(unconditionally_send_pointer_update());
|
|
|
|
}, 100);
|
|
|
|
return deferred;
|
2013-06-19 20:05:44 +02:00
|
|
|
} else {
|
|
|
|
return update_pointer();
|
2013-02-20 04:19:18 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-14 21:18:11 +02:00
|
|
|
function process_message_for_recent_subjects(message, remove_message) {
|
2013-04-10 22:24:15 +02:00
|
|
|
var current_timestamp = 0;
|
2013-05-14 21:18:11 +02:00
|
|
|
var count = 0;
|
2013-08-19 21:17:10 +02:00
|
|
|
var stream = message.stream;
|
2013-08-15 21:11:07 +02:00
|
|
|
var canon_subject = stream_data.canonicalized_name(message.subject);
|
2013-04-10 22:24:15 +02:00
|
|
|
|
2013-08-19 21:17:10 +02:00
|
|
|
if (! recent_subjects.has(stream)) {
|
|
|
|
recent_subjects.set(stream, []);
|
2013-04-10 22:24:15 +02:00
|
|
|
} else {
|
2013-08-19 21:17:10 +02:00
|
|
|
recent_subjects.set(stream,
|
|
|
|
_.filter(recent_subjects.get(stream), function (item) {
|
2013-08-07 23:56:51 +02:00
|
|
|
var is_duplicate = (item.canon_subject.toLowerCase() === canon_subject.toLowerCase());
|
|
|
|
if (is_duplicate) {
|
|
|
|
current_timestamp = item.timestamp;
|
|
|
|
count = item.count;
|
|
|
|
}
|
|
|
|
|
|
|
|
return !is_duplicate;
|
|
|
|
}));
|
2013-04-10 22:24:15 +02:00
|
|
|
}
|
|
|
|
|
2013-08-19 21:17:10 +02:00
|
|
|
var recents = recent_subjects.get(stream);
|
2013-05-14 21:18:11 +02:00
|
|
|
|
|
|
|
if (remove_message !== undefined) {
|
|
|
|
count = count - 1;
|
|
|
|
} else {
|
|
|
|
count = count + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (count !== 0) {
|
|
|
|
recents.push({subject: message.subject,
|
2013-05-22 15:45:31 +02:00
|
|
|
canon_subject: canon_subject,
|
2013-05-14 21:18:11 +02:00
|
|
|
count: count,
|
|
|
|
timestamp: Math.max(message.timestamp, current_timestamp)});
|
|
|
|
}
|
2013-04-10 22:24:15 +02:00
|
|
|
|
|
|
|
recents.sort(function (a, b) {
|
2013-04-12 18:12:43 +02:00
|
|
|
return b.timestamp - a.timestamp;
|
2013-04-10 22:24:15 +02:00
|
|
|
});
|
|
|
|
|
2013-08-19 21:17:10 +02:00
|
|
|
recent_subjects.set(stream, recents);
|
2013-04-10 22:24:15 +02:00
|
|
|
}
|
|
|
|
|
2013-11-07 00:20:14 +01:00
|
|
|
function set_topic_edit_properties(message) {
|
|
|
|
message.always_visible_topic_edit = false;
|
|
|
|
message.on_hover_topic_edit = false;
|
2013-11-30 22:03:12 +01:00
|
|
|
if (feature_flags.disable_message_editing) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-11-07 00:20:14 +01:00
|
|
|
// Messages with no topics should always have an edit icon visible
|
|
|
|
// to encourage updating them. Admins can also edit any topic.
|
|
|
|
if (message.subject === compose.empty_subject_placeholder()) {
|
|
|
|
message.always_visible_topic_edit = true;
|
2013-11-19 20:21:12 +01:00
|
|
|
} else if (page_params.is_admin) {
|
2013-11-07 00:20:14 +01:00
|
|
|
message.on_hover_topic_edit = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-16 21:09:58 +02:00
|
|
|
var msg_metadata_cache = {};
|
2013-07-29 23:35:36 +02:00
|
|
|
function add_message_metadata(message) {
|
2013-05-16 21:09:58 +02:00
|
|
|
var cached_msg = msg_metadata_cache[message.id];
|
2013-04-29 22:56:50 +02:00
|
|
|
if (cached_msg !== undefined) {
|
|
|
|
// Copy the match subject and content over if they exist on
|
|
|
|
// the new message
|
|
|
|
if (message.match_subject !== undefined) {
|
|
|
|
cached_msg.match_subject = message.match_subject;
|
|
|
|
cached_msg.match_content = message.match_content;
|
|
|
|
}
|
|
|
|
return cached_msg;
|
2012-11-20 19:50:28 +01:00
|
|
|
}
|
2012-12-14 18:12:54 +01:00
|
|
|
get_updates_params.last = Math.max(get_updates_params.last || 0, message.id);
|
2012-10-25 23:32:20 +02:00
|
|
|
|
2012-10-12 17:26:04 +02:00
|
|
|
var involved_people;
|
|
|
|
|
2013-06-14 17:46:01 +02:00
|
|
|
message.sent_by_me = (message.sender_email === page_params.email);
|
|
|
|
|
2013-03-27 18:29:44 +01:00
|
|
|
message.flags = message.flags || [];
|
2013-04-03 23:30:06 +02:00
|
|
|
message.historical = (message.flags !== undefined &&
|
|
|
|
message.flags.indexOf('historical') !== -1);
|
2013-03-27 18:29:44 +01:00
|
|
|
message.starred = message.flags.indexOf("starred") !== -1;
|
2013-05-31 16:15:27 +02:00
|
|
|
message.mentioned = message.flags.indexOf("mentioned") !== -1 ||
|
|
|
|
message.flags.indexOf("wildcard_mentioned") !== -1;
|
2013-05-08 23:17:49 +02:00
|
|
|
message.collapsed = message.flags.indexOf("collapsed") !== -1;
|
2013-08-30 21:15:01 +02:00
|
|
|
message.alerted = message.flags.indexOf("has_alert_word") !== -1;
|
2013-03-27 18:29:44 +01:00
|
|
|
|
2012-10-10 16:34:42 +02:00
|
|
|
switch (message.type) {
|
2012-10-10 22:57:21 +02:00
|
|
|
case 'stream':
|
2012-10-10 23:31:26 +02:00
|
|
|
message.is_stream = true;
|
2013-04-18 17:13:43 +02:00
|
|
|
message.stream = message.display_recipient;
|
2013-08-14 01:02:22 +02:00
|
|
|
composebox_typeahead.add_topic(message.stream, message.subject);
|
2012-10-10 16:34:42 +02:00
|
|
|
message.reply_to = message.sender_email;
|
2012-10-12 17:26:04 +02:00
|
|
|
|
2013-04-10 22:24:15 +02:00
|
|
|
process_message_for_recent_subjects(message);
|
|
|
|
|
2012-10-13 00:32:59 +02:00
|
|
|
involved_people = [{'full_name': message.sender_full_name,
|
2012-10-12 17:26:04 +02:00
|
|
|
'email': message.sender_email}];
|
2013-11-07 00:20:14 +01:00
|
|
|
set_topic_edit_properties(message);
|
2012-09-26 19:57:19 +02:00
|
|
|
break;
|
|
|
|
|
2012-12-03 19:49:12 +01:00
|
|
|
case 'private':
|
|
|
|
message.is_private = true;
|
2013-07-26 20:27:06 +02:00
|
|
|
message.reply_to = util.normalize_recipients(
|
|
|
|
get_private_message_recipient(message, 'email'));
|
2012-12-03 19:49:12 +01:00
|
|
|
message.display_reply_to = get_private_message_recipient(message, 'full_name');
|
2012-10-12 17:26:04 +02:00
|
|
|
|
|
|
|
involved_people = message.display_recipient;
|
2012-09-26 19:57:19 +02:00
|
|
|
break;
|
2012-10-12 17:26:04 +02:00
|
|
|
}
|
|
|
|
|
2012-10-13 00:17:48 +02:00
|
|
|
// Add new people involved in this message to the people list
|
2013-07-30 00:35:44 +02:00
|
|
|
_.each(involved_people, function (person) {
|
2012-10-13 00:17:48 +02:00
|
|
|
// Do the hasOwnProperty() call via the prototype to avoid problems
|
|
|
|
// with keys like "hasOwnProperty"
|
2013-08-07 23:56:51 +02:00
|
|
|
if (! people_dict.has(person.email)) {
|
2013-01-14 22:18:30 +01:00
|
|
|
add_person(person);
|
2013-06-18 21:09:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (message.type === 'private' && message.sent_by_me) {
|
|
|
|
// Track the number of PMs we've sent to this person to improve autocomplete
|
2013-08-07 23:56:51 +02:00
|
|
|
people_dict.get(person.email).pm_recipient_count += 1;
|
2012-09-04 20:31:23 +02:00
|
|
|
}
|
2012-10-13 00:17:48 +02:00
|
|
|
});
|
2012-08-30 20:24:29 +02:00
|
|
|
|
2013-08-29 21:33:26 +02:00
|
|
|
alert_words.process_message(message);
|
2013-05-16 21:09:58 +02:00
|
|
|
msg_metadata_cache[message.id] = message;
|
2012-11-20 19:50:28 +01:00
|
|
|
return message;
|
2012-08-29 17:12:21 +02:00
|
|
|
}
|
|
|
|
|
2013-07-02 18:14:13 +02:00
|
|
|
function add_messages(messages, msg_list, messages_are_new) {
|
2013-08-01 17:47:48 +02:00
|
|
|
if (!messages) {
|
2012-09-28 19:27:52 +02:00
|
|
|
return;
|
2013-08-01 17:47:48 +02:00
|
|
|
}
|
2012-09-28 19:27:52 +02:00
|
|
|
|
2013-01-16 19:50:18 +01:00
|
|
|
util.destroy_loading_indicator($('#page_loading_indicator'));
|
2013-02-12 19:28:21 +01:00
|
|
|
util.destroy_first_run_message();
|
2012-11-20 17:35:30 +01:00
|
|
|
|
2013-09-22 16:41:33 +02:00
|
|
|
msg_list.add_messages(messages, messages_are_new);
|
2013-11-06 18:57:38 +01:00
|
|
|
|
|
|
|
if (msg_list === home_msg_list && messages_are_new) {
|
|
|
|
_.each(messages, function (message) {
|
|
|
|
if (message.sent_by_me) {
|
|
|
|
compose.mark_end_to_end_receive_time(message.id);
|
2013-11-06 19:05:06 +01:00
|
|
|
setTimeout(function () {
|
|
|
|
compose.mark_end_to_end_display_time(message.id);
|
|
|
|
}, 0);
|
2013-11-06 18:57:38 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2012-09-24 21:21:23 +02:00
|
|
|
}
|
|
|
|
|
2013-07-02 18:14:13 +02:00
|
|
|
function maybe_add_narrowed_messages(messages, msg_list, messages_are_new) {
|
2013-04-25 19:38:21 +02:00
|
|
|
var ids = [];
|
2013-07-30 00:35:44 +02:00
|
|
|
_.each(messages, function (elem) {
|
2013-04-25 19:38:21 +02:00
|
|
|
ids.push(elem.id);
|
|
|
|
});
|
|
|
|
|
2013-12-18 19:55:18 +01:00
|
|
|
channel.post({
|
2013-04-25 19:38:21 +02:00
|
|
|
url: '/json/messages_in_narrow',
|
2014-01-07 23:40:31 +01:00
|
|
|
idempotent: true,
|
2013-04-25 19:38:21 +02:00
|
|
|
data: {msg_ids: JSON.stringify(ids),
|
|
|
|
narrow: JSON.stringify(narrow.public_operators())},
|
|
|
|
timeout: 5000,
|
|
|
|
success: function (data) {
|
|
|
|
if (msg_list !== current_msg_list) {
|
|
|
|
// We unnarrowed in the mean time
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-05-01 22:00:07 +02:00
|
|
|
var new_messages = [];
|
2013-10-09 22:42:15 +02:00
|
|
|
var elsewhere_messages = [];
|
2013-07-30 00:35:44 +02:00
|
|
|
_.each(messages, function (elem) {
|
2013-05-01 22:00:07 +02:00
|
|
|
if (data.messages.hasOwnProperty(elem.id)) {
|
|
|
|
elem.match_subject = data.messages[elem.id].match_subject;
|
|
|
|
elem.match_content = data.messages[elem.id].match_content;
|
|
|
|
new_messages.push(elem);
|
2013-10-09 22:42:15 +02:00
|
|
|
} else {
|
|
|
|
elsewhere_messages.push(elem);
|
2013-05-01 22:00:07 +02:00
|
|
|
}
|
2013-04-25 19:38:21 +02:00
|
|
|
});
|
|
|
|
|
2013-07-29 23:35:36 +02:00
|
|
|
new_messages = _.map(new_messages, add_message_metadata);
|
2013-07-02 18:14:13 +02:00
|
|
|
add_messages(new_messages, msg_list, messages_are_new);
|
2013-07-11 17:14:11 +02:00
|
|
|
process_visible_unread_messages();
|
2013-10-09 22:42:15 +02:00
|
|
|
notifications.possibly_notify_new_messages_outside_viewport(new_messages);
|
|
|
|
notifications.notify_messages_outside_current_search(elsewhere_messages);
|
2013-04-25 19:38:21 +02:00
|
|
|
},
|
|
|
|
error: function (xhr) {
|
|
|
|
// We might want to be more clever here
|
|
|
|
setTimeout(function () {
|
|
|
|
if (msg_list === current_msg_list) {
|
|
|
|
// Don't actually try again if we unnarrowed
|
|
|
|
// while waiting
|
2013-07-02 18:14:13 +02:00
|
|
|
maybe_add_narrowed_messages(messages, msg_list, messages_are_new);
|
2013-04-25 19:38:21 +02:00
|
|
|
}
|
|
|
|
}, 5000);
|
|
|
|
}});
|
|
|
|
}
|
|
|
|
|
2013-05-14 21:18:11 +02:00
|
|
|
function update_messages(events) {
|
2013-07-30 00:35:44 +02:00
|
|
|
_.each(events, function (event) {
|
2013-05-14 21:18:11 +02:00
|
|
|
var msg = all_msg_list.get(event.message_id);
|
2013-06-04 21:38:42 +02:00
|
|
|
if (msg === undefined) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-01-08 19:44:07 +01:00
|
|
|
msg.alerted = event.flags.indexOf("has_alert_word") !== -1;
|
2014-01-08 21:20:09 +01:00
|
|
|
msg.mentioned = event.flags.indexOf("mentioned") !== -1 ||
|
|
|
|
event.flags.indexOf("wildcard_mentioned") !== -1;
|
2014-01-08 19:44:07 +01:00
|
|
|
|
2013-12-09 17:43:20 +01:00
|
|
|
ui.un_cache_message_content_height(msg.id);
|
|
|
|
|
2013-05-14 21:18:11 +02:00
|
|
|
if (event.rendered_content !== undefined) {
|
|
|
|
msg.content = event.rendered_content;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (event.subject !== undefined) {
|
2013-09-03 22:07:59 +02:00
|
|
|
// A topic edit may affect multiple messages, listed in
|
|
|
|
// event.message_ids. event.message_id is still the first message
|
|
|
|
// where the user initiated the edit.
|
|
|
|
_.each(event.message_ids, function (id) {
|
|
|
|
var msg = all_msg_list.get(id);
|
|
|
|
if (msg === undefined) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove the recent subjects entry for the old subject;
|
|
|
|
// must be called before we update msg.subject
|
|
|
|
process_message_for_recent_subjects(msg, true);
|
|
|
|
// Update the unread counts; again, this must be called
|
|
|
|
// before we update msg.subject
|
|
|
|
unread.update_unread_subjects(msg, event);
|
|
|
|
|
|
|
|
msg.subject = event.subject;
|
|
|
|
msg.subject_links = event.subject_links;
|
2013-11-07 00:20:14 +01:00
|
|
|
set_topic_edit_properties(msg);
|
2013-09-03 22:07:59 +02:00
|
|
|
// Add the recent subjects entry for the new subject; must
|
|
|
|
// be called after we update msg.subject
|
|
|
|
process_message_for_recent_subjects(msg);
|
|
|
|
});
|
2013-05-14 21:18:11 +02:00
|
|
|
}
|
2013-05-22 18:19:24 +02:00
|
|
|
|
2013-08-14 22:00:32 +02:00
|
|
|
var row = current_msg_list.get_row(event.message_id);
|
2013-06-04 21:39:51 +02:00
|
|
|
if (row.length > 0) {
|
|
|
|
message_edit.end(row);
|
2013-05-22 18:19:24 +02:00
|
|
|
}
|
2013-05-21 17:48:46 +02:00
|
|
|
|
|
|
|
msg.last_edit_timestamp = event.edit_timestamp;
|
|
|
|
delete msg.last_edit_timestr;
|
2014-01-08 19:44:07 +01:00
|
|
|
|
2014-01-08 22:02:02 +01:00
|
|
|
notifications.received_messages([msg]);
|
2014-01-08 19:44:07 +01:00
|
|
|
alert_words.process_message(msg);
|
2013-05-14 21:18:11 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
home_msg_list.rerender();
|
|
|
|
if (current_msg_list === narrowed_msg_list) {
|
|
|
|
narrowed_msg_list.rerender();
|
|
|
|
}
|
|
|
|
update_unread_counts();
|
|
|
|
stream_list.update_streams_sidebar();
|
|
|
|
}
|
|
|
|
|
2013-12-04 21:47:51 +01:00
|
|
|
function insert_new_messages(messages) {
|
|
|
|
messages = _.map(messages, add_message_metadata);
|
|
|
|
|
|
|
|
if (feature_flags.summarize_read_while_narrowed) {
|
|
|
|
_.each(messages, function (message) {
|
|
|
|
if (message.sent_by_me) {
|
|
|
|
summary.maybe_mark_summarized(message);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// You must add add messages to home_msg_list BEFORE
|
|
|
|
// calling process_loaded_for_unread.
|
|
|
|
add_messages(messages, home_msg_list, true);
|
|
|
|
add_messages(messages, all_msg_list, true);
|
|
|
|
|
|
|
|
if (narrow.active()) {
|
|
|
|
if (narrow.filter().can_apply_locally()) {
|
|
|
|
add_messages(messages, narrowed_msg_list, true);
|
|
|
|
notifications.possibly_notify_new_messages_outside_viewport(messages);
|
|
|
|
} else {
|
|
|
|
// if we cannot apply locally, we have to wait for this callback to happen to notify
|
|
|
|
maybe_add_narrowed_messages(messages, narrowed_msg_list, true);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
notifications.possibly_notify_new_messages_outside_viewport(messages);
|
|
|
|
}
|
|
|
|
|
|
|
|
process_loaded_for_unread(messages);
|
|
|
|
|
|
|
|
if (narrow.narrowed_by_reply()) {
|
|
|
|
// If you send a message when narrowed to a recipient, move the
|
|
|
|
// pointer to it.
|
|
|
|
|
|
|
|
var i;
|
|
|
|
var selected_id = current_msg_list.selected_id();
|
|
|
|
|
|
|
|
// Iterate backwards to find the last message sent_by_me, stopping at
|
|
|
|
// the pointer position.
|
|
|
|
for (i = messages.length-1; i>=0; i--){
|
|
|
|
var id = messages[i].id;
|
|
|
|
if (id <= selected_id) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (messages[i].sent_by_me && current_msg_list.get(id) !== undefined) {
|
|
|
|
// If this is a reply we just sent, advance the pointer to it.
|
|
|
|
current_msg_list.select_id(messages[i].id, {then_scroll: true,
|
|
|
|
from_scroll: true});
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
process_visible_unread_messages();
|
|
|
|
notifications.received_messages(messages);
|
|
|
|
stream_list.update_streams_sidebar();
|
|
|
|
}
|
|
|
|
|
2013-06-27 17:26:13 +02:00
|
|
|
function get_updates_success(data) {
|
|
|
|
var messages = [];
|
|
|
|
var messages_to_update = [];
|
|
|
|
var new_pointer;
|
|
|
|
|
2013-12-12 21:04:00 +01:00
|
|
|
_.each(data.events, function (event) {
|
|
|
|
get_updates_params.last_event_id = Math.max(get_updates_params.last_event_id,
|
|
|
|
event.id);
|
|
|
|
});
|
|
|
|
|
2013-06-27 22:18:28 +02:00
|
|
|
if (tutorial.is_running()) {
|
|
|
|
events_stored_during_tutorial = events_stored_during_tutorial.concat(data.events);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (events_stored_during_tutorial.length > 0) {
|
|
|
|
data.events = events_stored_during_tutorial.concat(data.events);
|
|
|
|
events_stored_during_tutorial = [];
|
|
|
|
}
|
|
|
|
|
2013-07-30 00:35:44 +02:00
|
|
|
_.each(data.events, function (event) {
|
2013-06-27 17:26:13 +02:00
|
|
|
switch (event.type) {
|
|
|
|
case 'message':
|
|
|
|
var msg = event.message;
|
|
|
|
msg.flags = event.flags;
|
|
|
|
messages.push(msg);
|
|
|
|
break;
|
|
|
|
case 'pointer':
|
|
|
|
new_pointer = event.pointer;
|
|
|
|
break;
|
|
|
|
case 'restart':
|
|
|
|
reload.initiate({message: "The application has been updated; reloading!"});
|
|
|
|
break;
|
|
|
|
case 'update_message':
|
|
|
|
messages_to_update.push(event);
|
|
|
|
break;
|
|
|
|
case 'realm_user':
|
|
|
|
if (event.op === 'add') {
|
2013-09-06 00:19:15 +02:00
|
|
|
add_person_in_realm(event.person);
|
2013-06-27 17:26:13 +02:00
|
|
|
} else if (event.op === 'remove') {
|
|
|
|
remove_person(event.person);
|
2013-07-16 21:32:33 +02:00
|
|
|
} else if (event.op === 'update') {
|
|
|
|
update_person(event.person);
|
2013-06-27 17:26:13 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'subscriptions':
|
|
|
|
if (event.op === 'add') {
|
2013-07-30 00:35:44 +02:00
|
|
|
_.each(event.subscriptions, function (subscription) {
|
2013-07-25 22:48:55 +02:00
|
|
|
$(document).trigger($.Event('subscription_add.zulip',
|
2013-06-27 17:26:13 +02:00
|
|
|
{subscription: subscription}));
|
|
|
|
});
|
|
|
|
} else if (event.op === 'remove') {
|
2013-07-30 00:35:44 +02:00
|
|
|
_.each(event.subscriptions, function (subscription) {
|
2013-07-25 22:48:55 +02:00
|
|
|
$(document).trigger($.Event('subscription_remove.zulip',
|
2013-06-27 17:26:13 +02:00
|
|
|
{subscription: subscription}));
|
|
|
|
});
|
2013-07-16 22:21:41 +02:00
|
|
|
} else if (event.op === 'update') {
|
|
|
|
subs.update_subscription_properties(event.name, event.property, event.value);
|
2013-09-07 03:28:17 +02:00
|
|
|
} else if (event.op === 'peer_add' || event.op === 'peer_remove') {
|
|
|
|
_.each(event.subscriptions, function (sub) {
|
2013-09-16 21:13:11 +02:00
|
|
|
var js_event_type;
|
|
|
|
if (event.op === 'peer_add') {
|
|
|
|
js_event_type = 'peer_subscribe.zulip';
|
|
|
|
|
|
|
|
stream_data.add_subscriber(sub, event.user_email);
|
|
|
|
} else if (event.op === 'peer_remove') {
|
|
|
|
js_event_type = 'peer_unsubscribe.zulip';
|
|
|
|
|
|
|
|
stream_data.remove_subscriber(sub, event.user_email);
|
|
|
|
}
|
|
|
|
|
|
|
|
$(document).trigger(js_event_type, {stream_name: sub,
|
|
|
|
user_email: event.user_email});
|
2013-09-07 03:28:17 +02:00
|
|
|
});
|
2013-09-16 21:13:11 +02:00
|
|
|
|
2013-06-27 17:26:13 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'presence':
|
2013-07-11 21:51:26 +02:00
|
|
|
var users = {};
|
|
|
|
users[event.email] = event.presence;
|
|
|
|
activity.set_user_statuses(users, event.server_timestamp);
|
2013-06-27 17:26:13 +02:00
|
|
|
break;
|
2014-01-23 17:52:28 +01:00
|
|
|
case 'update_message_flags':
|
|
|
|
var new_value = event.operation === "add";
|
|
|
|
switch(event.flag) {
|
|
|
|
case 'starred':
|
|
|
|
_.each(event.messages, function (message_id) {
|
|
|
|
ui.update_starred(message_id, new_value);
|
|
|
|
});
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
2013-08-06 20:21:32 +02:00
|
|
|
case 'referral':
|
|
|
|
referral.update_state(event.referrals.granted, event.referrals.used);
|
|
|
|
break;
|
2013-08-22 19:54:35 +02:00
|
|
|
case 'realm_emoji':
|
|
|
|
emoji.update_emojis(event.realm_emoji);
|
|
|
|
break;
|
2013-09-04 21:54:24 +02:00
|
|
|
case 'alert_words':
|
|
|
|
alert_words.words = event.alert_words;
|
|
|
|
break;
|
2013-09-11 00:36:21 +02:00
|
|
|
case 'muted_topics':
|
|
|
|
muting_ui.handle_updates(event.muted_topics);
|
|
|
|
break;
|
2013-06-27 17:26:13 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
if (messages.length !== 0) {
|
2013-12-04 21:47:51 +01:00
|
|
|
insert_new_messages(messages);
|
2013-06-27 17:26:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (new_pointer !== undefined
|
|
|
|
&& new_pointer > furthest_read)
|
|
|
|
{
|
|
|
|
furthest_read = new_pointer;
|
|
|
|
server_furthest_read = new_pointer;
|
|
|
|
home_msg_list.select_id(new_pointer, {then_scroll: true, use_closest: true});
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((home_msg_list.selected_id() === -1) && !home_msg_list.empty()) {
|
|
|
|
home_msg_list.select_id(home_msg_list.first().id, {then_scroll: false});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (messages_to_update.length !== 0) {
|
|
|
|
update_messages(messages_to_update);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-10 20:21:22 +02:00
|
|
|
var get_updates_xhr;
|
|
|
|
var get_updates_timeout;
|
2012-11-20 23:15:50 +01:00
|
|
|
function get_updates(options) {
|
2013-07-30 05:11:50 +02:00
|
|
|
options = _.extend({dont_block: false}, options);
|
2012-11-20 23:15:50 +01:00
|
|
|
|
2012-10-29 22:02:10 +01:00
|
|
|
get_updates_params.pointer = furthest_read;
|
2012-11-27 21:06:17 +01:00
|
|
|
get_updates_params.dont_block = options.dont_block || get_updates_failures > 0;
|
2013-03-28 18:09:27 +01:00
|
|
|
if (get_updates_params.queue_id === undefined) {
|
|
|
|
get_updates_params.queue_id = page_params.event_queue_id;
|
|
|
|
get_updates_params.last_event_id = page_params.last_event_id;
|
|
|
|
}
|
2012-10-17 23:10:34 +02:00
|
|
|
|
2013-12-12 19:44:21 +01:00
|
|
|
if (get_updates_xhr !== undefined) {
|
|
|
|
get_updates_xhr.abort();
|
|
|
|
}
|
|
|
|
if (get_updates_timeout !== undefined) {
|
|
|
|
clearTimeout(get_updates_timeout);
|
|
|
|
}
|
|
|
|
get_updates_timeout = undefined;
|
2013-12-18 19:55:18 +01:00
|
|
|
get_updates_xhr = channel.post({
|
2013-03-26 18:07:40 +01:00
|
|
|
url: '/json/get_events',
|
2012-10-16 22:06:03 +02:00
|
|
|
data: get_updates_params,
|
2014-01-07 23:40:31 +01:00
|
|
|
idempotent: true,
|
2013-03-25 23:26:14 +01:00
|
|
|
timeout: page_params.poll_timeout,
|
2012-08-31 21:33:04 +02:00
|
|
|
success: function (data) {
|
2013-12-12 18:46:54 +01:00
|
|
|
get_updates_xhr = undefined;
|
2012-11-27 21:06:17 +01:00
|
|
|
get_updates_failures = 0;
|
2012-09-05 22:33:04 +02:00
|
|
|
$('#connection-error').hide();
|
|
|
|
|
2013-06-27 17:26:13 +02:00
|
|
|
get_updates_success(data);
|
2013-12-12 21:04:00 +01:00
|
|
|
get_updates_timeout = setTimeout(get_updates, 0);
|
2012-08-31 21:33:04 +02:00
|
|
|
},
|
2012-09-05 22:17:14 +02:00
|
|
|
error: function (xhr, error_type, exn) {
|
2013-12-12 18:46:54 +01:00
|
|
|
get_updates_xhr = undefined;
|
2013-03-23 00:11:09 +01:00
|
|
|
// If we are old enough to have messages outside of the
|
|
|
|
// Tornado cache or if we're old enough that our message
|
|
|
|
// queue has been garbage collected, immediately reload.
|
2013-01-23 17:33:07 +01:00
|
|
|
if ((xhr.status === 400) &&
|
2013-03-23 00:11:09 +01:00
|
|
|
($.parseJSON(xhr.responseText).msg.indexOf("too old") !== -1 ||
|
|
|
|
$.parseJSON(xhr.responseText).msg.indexOf("Bad event queue id") !== -1)) {
|
2013-11-22 18:06:22 +01:00
|
|
|
page_params.event_queue_expired = true;
|
2013-01-23 17:33:07 +01:00
|
|
|
reload.initiate({immediate: true});
|
|
|
|
}
|
|
|
|
|
2013-11-14 20:06:02 +01:00
|
|
|
if (error_type === 'abort') {
|
|
|
|
// Don't restart if we explicitly aborted
|
|
|
|
return;
|
|
|
|
} else if (error_type === 'timeout') {
|
2012-09-05 22:17:14 +02:00
|
|
|
// Retry indefinitely on timeout.
|
2012-11-27 21:06:17 +01:00
|
|
|
get_updates_failures = 0;
|
2012-09-05 22:33:04 +02:00
|
|
|
$('#connection-error').hide();
|
2012-09-05 22:17:14 +02:00
|
|
|
} else {
|
2012-11-27 21:06:17 +01:00
|
|
|
get_updates_failures += 1;
|
2012-09-05 22:17:14 +02:00
|
|
|
}
|
|
|
|
|
2012-11-27 21:06:17 +01:00
|
|
|
if (get_updates_failures >= 5) {
|
2012-08-31 21:33:04 +02:00
|
|
|
$('#connection-error').show();
|
|
|
|
} else {
|
2012-09-05 22:33:04 +02:00
|
|
|
$('#connection-error').hide();
|
2012-08-31 21:33:04 +02:00
|
|
|
}
|
2012-09-05 22:33:04 +02:00
|
|
|
|
2012-11-27 21:06:17 +01:00
|
|
|
var retry_sec = Math.min(90, Math.exp(get_updates_failures/2));
|
2012-10-10 20:21:22 +02:00
|
|
|
get_updates_timeout = setTimeout(get_updates, retry_sec*1000);
|
2012-08-31 21:33:04 +02:00
|
|
|
}
|
|
|
|
});
|
2012-08-29 17:12:21 +02:00
|
|
|
}
|
2012-09-04 20:31:23 +02:00
|
|
|
|
2013-06-27 22:00:49 +02:00
|
|
|
function force_get_updates() {
|
|
|
|
get_updates_timeout = setTimeout(get_updates, 0);
|
|
|
|
}
|
|
|
|
|
2013-11-20 19:42:37 +01:00
|
|
|
function cleanup_event_queue() {
|
|
|
|
// Submit a request to the server to cleanup our event queue
|
2013-11-22 18:06:22 +01:00
|
|
|
if (page_params.event_queue_expired === true) {
|
|
|
|
return;
|
|
|
|
}
|
2013-12-18 19:55:18 +01:00
|
|
|
channel.del({
|
2013-11-20 19:42:37 +01:00
|
|
|
url: '/json/events',
|
2013-12-18 19:55:18 +01:00
|
|
|
data: {queue_id: page_params.event_queue_id}
|
2013-11-20 19:42:37 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
window.addEventListener("beforeunload", function (event) {
|
|
|
|
cleanup_event_queue();
|
|
|
|
});
|
|
|
|
|
2013-08-02 23:47:44 +02:00
|
|
|
function process_result(messages, opts) {
|
|
|
|
$('#get_old_messages_error').hide();
|
|
|
|
|
|
|
|
if ((messages.length === 0) && (current_msg_list === narrowed_msg_list) &&
|
|
|
|
narrowed_msg_list.empty()) {
|
|
|
|
// Even after trying to load more messages, we have no
|
|
|
|
// messages to display in this narrow.
|
|
|
|
narrow.show_empty_narrow_message();
|
|
|
|
}
|
2012-11-09 01:06:07 +01:00
|
|
|
|
2013-08-02 23:47:44 +02:00
|
|
|
messages = _.map(messages, add_message_metadata);
|
2012-12-19 23:58:02 +01:00
|
|
|
|
2013-08-02 23:47:44 +02:00
|
|
|
// If we're loading more messages into the home view, save them to
|
|
|
|
// the all_msg_list as well, as the home_msg_list is reconstructed
|
|
|
|
// from all_msg_list.
|
|
|
|
if (opts.msg_list === home_msg_list) {
|
|
|
|
process_loaded_for_unread(messages);
|
|
|
|
add_messages(messages, all_msg_list, false);
|
2013-08-01 17:47:48 +02:00
|
|
|
}
|
2012-12-19 23:58:02 +01:00
|
|
|
|
2013-08-02 23:47:44 +02:00
|
|
|
if (messages.length !== 0 && !opts.cont_will_add_messages) {
|
|
|
|
add_messages(messages, opts.msg_list, false);
|
|
|
|
}
|
2013-01-07 23:35:00 +01:00
|
|
|
|
2013-08-02 23:47:44 +02:00
|
|
|
stream_list.update_streams_sidebar();
|
2013-02-23 19:38:25 +01:00
|
|
|
|
2013-08-02 23:47:44 +02:00
|
|
|
if (opts.cont !== undefined) {
|
|
|
|
opts.cont(messages);
|
|
|
|
}
|
|
|
|
}
|
2013-05-13 21:57:13 +02:00
|
|
|
|
2013-08-02 23:47:44 +02:00
|
|
|
function get_old_messages_success(data, opts) {
|
2013-08-02 23:56:28 +02:00
|
|
|
if (waiting_on_browser_scroll) {
|
|
|
|
setTimeout(function () {
|
|
|
|
get_old_messages_success(data, opts);
|
|
|
|
}, 25);
|
|
|
|
return;
|
2013-08-09 23:02:43 +02:00
|
|
|
}
|
|
|
|
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
|
|
|
|
util.destroy_loading_indicator($('#page_loading_indicator'));
|
|
|
|
tutorial.defer(function () { get_old_messages_success(data, opts); });
|
|
|
|
return;
|
2013-08-02 23:56:28 +02:00
|
|
|
}
|
|
|
|
|
2013-08-02 23:47:44 +02: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;
|
|
|
|
}
|
|
|
|
if (! data) {
|
|
|
|
// The server occationally returns no data during a
|
|
|
|
// restart. Ignore those responses and try again
|
|
|
|
setTimeout(function () {
|
|
|
|
load_old_messages(opts);
|
|
|
|
}, 0);
|
|
|
|
return;
|
|
|
|
}
|
2013-02-22 20:48:31 +01:00
|
|
|
|
2013-08-02 23:47:44 +02:00
|
|
|
process_result(data.messages, opts);
|
2013-12-07 00:17:39 +01:00
|
|
|
ui.resize_bottom_whitespace();
|
2013-08-02 23:47:44 +02:00
|
|
|
}
|
2013-01-07 23:35:00 +01:00
|
|
|
|
2013-08-02 23:47:44 +02:00
|
|
|
function load_old_messages(opts) {
|
|
|
|
opts = _.extend({cont_will_add_messages: false}, opts);
|
2013-05-02 02:59:15 +02:00
|
|
|
|
2013-08-02 23:47:44 +02:00
|
|
|
var data = {anchor: opts.anchor,
|
|
|
|
num_before: opts.num_before,
|
|
|
|
num_after: opts.num_after};
|
|
|
|
|
|
|
|
if (opts.msg_list.narrowed && narrow.active()) {
|
2013-12-13 20:20:28 +01:00
|
|
|
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) {
|
2014-01-17 00:05:35 +01:00
|
|
|
data.narrow = JSON.stringify(page_params.narrow);
|
2013-01-07 23:35:00 +01:00
|
|
|
}
|
|
|
|
|
2013-12-18 19:55:18 +01:00
|
|
|
channel.post({
|
2012-10-25 00:42:45 +02:00
|
|
|
url: '/json/get_old_messages',
|
2012-12-19 23:58:02 +01:00
|
|
|
data: data,
|
2014-01-07 23:40:31 +01:00
|
|
|
idempotent: true,
|
2012-10-25 00:42:45 +02:00
|
|
|
success: function (data) {
|
2013-08-02 23:47:44 +02:00
|
|
|
get_old_messages_success(data, opts);
|
2012-10-25 00:42:45 +02:00
|
|
|
},
|
|
|
|
error: function (xhr, error_type, exn) {
|
2013-04-10 17:42:14 +02:00
|
|
|
if (opts.msg_list.narrowed && opts.msg_list !== current_msg_list) {
|
2013-03-15 20:57:37 +01:00
|
|
|
// We unnarrowed before getting an error so don't
|
|
|
|
// bother trying again or doing further processing.
|
|
|
|
return;
|
|
|
|
}
|
2013-01-07 23:35:00 +01:00
|
|
|
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?
|
2013-08-02 23:47:44 +02:00
|
|
|
process_result([], opts);
|
2013-01-07 23:35:00 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-10-25 00:42:45 +02:00
|
|
|
// We might want to be more clever here
|
2013-06-18 20:03:46 +02:00
|
|
|
$('#get_old_messages_error').show();
|
2012-10-25 00:42:45 +02:00
|
|
|
setTimeout(function () {
|
2013-03-14 19:42:37 +01:00
|
|
|
load_old_messages(opts);
|
2012-10-25 00:42:45 +02:00
|
|
|
}, 5000);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2012-11-20 23:15:50 +01:00
|
|
|
function restart_get_updates(options) {
|
|
|
|
get_updates(options);
|
2012-10-19 21:51:36 +02:00
|
|
|
}
|
|
|
|
|
2012-11-27 23:17:30 +01:00
|
|
|
function reset_load_more_status() {
|
|
|
|
load_more_enabled = true;
|
|
|
|
have_scrolled_away_from_top = true;
|
2013-03-15 20:57:37 +01:00
|
|
|
ui.hide_loading_more_messages_indicator();
|
2012-11-27 23:17:30 +01:00
|
|
|
}
|
|
|
|
|
2013-02-12 20:01:24 +01:00
|
|
|
function load_more_messages(msg_list) {
|
2013-06-14 23:47:37 +02:00
|
|
|
var batch_size = 100;
|
2013-02-12 20:01:24 +01:00
|
|
|
var oldest_message_id;
|
2012-11-27 23:17:30 +01:00
|
|
|
if (!load_more_enabled) {
|
|
|
|
return;
|
|
|
|
}
|
2013-01-16 19:50:18 +01:00
|
|
|
ui.show_loading_more_messages_indicator();
|
2012-11-27 23:17:30 +01:00
|
|
|
load_more_enabled = false;
|
2013-02-12 20:01:24 +01:00
|
|
|
if (msg_list.first() === undefined) {
|
2013-03-25 23:26:14 +01:00
|
|
|
oldest_message_id = page_params.initial_pointer;
|
2013-02-12 20:01:24 +01:00
|
|
|
} else {
|
|
|
|
oldest_message_id = msg_list.first().id;
|
2012-12-04 20:04:36 +01:00
|
|
|
}
|
2013-03-14 19:42:37 +01:00
|
|
|
load_old_messages({
|
|
|
|
anchor: oldest_message_id,
|
|
|
|
num_before: batch_size,
|
|
|
|
num_after: 0,
|
|
|
|
msg_list: msg_list,
|
|
|
|
cont: function (messages) {
|
|
|
|
ui.hide_loading_more_messages_indicator();
|
2013-04-23 19:26:29 +02:00
|
|
|
if (messages.length >= batch_size) {
|
2013-03-14 19:42:37 +01:00
|
|
|
load_more_enabled = true;
|
|
|
|
}
|
2013-04-10 17:42:14 +02:00
|
|
|
}
|
2013-03-14 19:42:37 +01:00
|
|
|
});
|
2012-10-19 22:28:57 +02:00
|
|
|
}
|
|
|
|
|
2012-10-10 20:21:22 +02:00
|
|
|
var watchdog_time = $.now();
|
2012-11-20 21:37:31 +01:00
|
|
|
setInterval(function () {
|
2012-10-10 20:21:22 +02:00
|
|
|
var new_time = $.now();
|
|
|
|
if ((new_time - watchdog_time) > 20000) { // 20 seconds.
|
2013-12-19 23:19:09 +01:00
|
|
|
// Defensively reset watchdog_time here in case there's an
|
|
|
|
// exception in one of the event handlers
|
|
|
|
watchdog_time = new_time;
|
2013-11-18 22:03:41 +01:00
|
|
|
// Our app's JS wasn't running, which probably means the machine was
|
|
|
|
// asleep.
|
|
|
|
$(document).trigger($.Event('unsuspend'));
|
2012-10-10 20:21:22 +02:00
|
|
|
}
|
|
|
|
watchdog_time = new_time;
|
|
|
|
}, 5000);
|
|
|
|
|
2013-11-18 22:03:41 +01:00
|
|
|
$(function () {
|
|
|
|
$(document).on('unsuspend', function () {
|
|
|
|
// Immediately poll for new updates on unsuspend
|
2013-12-12 18:39:19 +01:00
|
|
|
blueslip.log("Restarting get_updates due to unsuspend");
|
2013-11-18 22:03:41 +01:00
|
|
|
get_updates_failures = 0;
|
|
|
|
restart_get_updates({dont_block: true});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2013-06-19 17:36:48 +02:00
|
|
|
function fast_forward_pointer() {
|
2013-12-18 19:55:18 +01:00
|
|
|
channel.post({
|
2013-02-20 04:19:18 +01:00
|
|
|
url: '/json/get_profile',
|
2014-01-07 23:40:31 +01:00
|
|
|
idempotent: true,
|
2013-03-25 23:26:14 +01:00
|
|
|
data: {email: page_params.email},
|
2013-02-20 04:19:18 +01:00
|
|
|
success: function (data) {
|
2013-03-15 20:07:38 +01:00
|
|
|
mark_all_as_read(function () {
|
|
|
|
furthest_read = data.max_message_id;
|
2013-06-19 20:05:44 +02:00
|
|
|
unconditionally_send_pointer_update().then(function () {
|
|
|
|
ui.change_tab_to('#home');
|
2013-07-22 23:27:32 +02:00
|
|
|
reload.initiate({immediate: true, save_state: false});
|
2013-06-19 20:05:44 +02:00
|
|
|
});
|
2013-03-15 20:07:38 +01:00
|
|
|
});
|
2013-02-20 04:19:18 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2013-08-02 23:38:26 +02:00
|
|
|
|
2013-08-08 20:24:25 +02:00
|
|
|
function consider_bankruptcy() {
|
2013-08-19 19:02:52 +02:00
|
|
|
// 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.
|
|
|
|
|
2013-08-08 20:24:25 +02:00
|
|
|
if (!page_params.furthest_read_time) {
|
|
|
|
// We've never read a message.
|
2013-08-19 19:02:52 +02:00
|
|
|
enable_unread_counts();
|
2013-08-08 20:24:25 +02:00
|
|
|
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');
|
2013-08-19 19:02:52 +02:00
|
|
|
} else {
|
|
|
|
enable_unread_counts();
|
2013-08-08 20:24:25 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-06 00:18:29 +02:00
|
|
|
_.each(page_params.people_list, function (person) {
|
|
|
|
people_dict.set(person.email, person);
|
2014-01-04 01:17:09 +01:00
|
|
|
people_by_name_dict.set(person.full_name, person);
|
2013-09-06 00:19:15 +02:00
|
|
|
realm_people_dict.set(person.email, person);
|
2013-09-06 00:18:29 +02:00
|
|
|
person.pm_recipient_count = 0;
|
|
|
|
});
|
2013-08-02 23:38:26 +02:00
|
|
|
|
2013-09-06 00:18:29 +02:00
|
|
|
// The special account feedback@zulip.com is used for in-app
|
|
|
|
// feedback and should always show up as an autocomplete option.
|
|
|
|
if (! people_dict.has('feedback@zulip.com')){
|
|
|
|
add_person({"email": "feedback@zulip.com",
|
|
|
|
"full_name": "Zulip Feedback Bot"});
|
|
|
|
}
|
2013-08-02 23:38:26 +02:00
|
|
|
|
2013-09-06 00:18:29 +02:00
|
|
|
function main() {
|
2013-08-02 23:38:26 +02:00
|
|
|
activity.set_user_statuses(page_params.initial_presences,
|
|
|
|
page_params.initial_servertime);
|
|
|
|
|
|
|
|
server_furthest_read = page_params.initial_pointer;
|
2013-08-13 22:52:56 +02:00
|
|
|
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;
|
2013-08-02 23:38:26 +02:00
|
|
|
|
2013-08-08 20:24:25 +02:00
|
|
|
// Before trying to load messages: is this user way behind?
|
|
|
|
consider_bankruptcy();
|
|
|
|
|
2013-08-02 23:38:26 +02:00
|
|
|
// 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) {
|
2013-12-13 20:20:28 +01:00
|
|
|
// Only advance the pointer when not narrowed
|
2013-12-18 19:06:55 +01:00
|
|
|
if (event.id === -1) {
|
|
|
|
return;
|
|
|
|
}
|
2013-12-13 20:20:28 +01:00
|
|
|
if (event.msg_list === home_msg_list && page_params.narrow_stream === undefined) {
|
2013-11-26 19:06:21 +01:00
|
|
|
if (event.id > furthest_read) {
|
|
|
|
furthest_read = event.id;
|
|
|
|
}
|
2013-08-02 23:38:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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 = message_range(event.msg_list, event.id, event.previously_selected);
|
|
|
|
} else {
|
|
|
|
messages = message_range(event.msg_list, event.previously_selected, event.id);
|
|
|
|
}
|
|
|
|
mark_messages_as_read(messages, {from: 'pointer'});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// get the initial message list
|
|
|
|
function load_more(messages) {
|
2013-11-14 22:28:57 +01:00
|
|
|
|
2013-08-02 23:38:26 +02: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
|
2013-12-18 16:28:57 +01:00
|
|
|
if (home_msg_list.selected_id() === -1 && !home_msg_list.empty()) {
|
2013-08-02 23:38:26 +02:00
|
|
|
home_msg_list.select_id(page_params.initial_pointer,
|
|
|
|
{then_scroll: true, use_closest: true});
|
|
|
|
}
|
|
|
|
|
|
|
|
// catch the user up
|
|
|
|
if (messages.length !== 0) {
|
|
|
|
var latest_id = messages[messages.length-1].id;
|
|
|
|
if (latest_id < page_params.max_message_id) {
|
|
|
|
load_old_messages({
|
|
|
|
anchor: latest_id,
|
|
|
|
num_before: 0,
|
|
|
|
num_after: 400,
|
|
|
|
msg_list: home_msg_list,
|
|
|
|
cont: load_more
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// now start subscribing to updates
|
|
|
|
get_updates();
|
|
|
|
|
|
|
|
// backfill more messages after the user is idle
|
|
|
|
var backfill_batch_size = 1000;
|
|
|
|
$(document).idle({'idle': 1000*10,
|
|
|
|
'onIdle': function () {
|
|
|
|
var first_id = all_msg_list.first().id;
|
|
|
|
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) {
|
|
|
|
load_old_messages({
|
|
|
|
anchor: page_params.initial_pointer,
|
|
|
|
num_before: 200,
|
|
|
|
num_after: 200,
|
|
|
|
msg_list: home_msg_list,
|
|
|
|
cont: load_more
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
get_updates();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-02 23:56:28 +02:00
|
|
|
function install_main_scroll_handler() {
|
|
|
|
waiting_on_browser_scroll = false;
|
|
|
|
// Unregister this special un-throttled scroll handler.
|
|
|
|
$(window).unbind("scroll");
|
|
|
|
// Register the normal throttled scroll handler.
|
|
|
|
ui.register_scroll_handler();
|
|
|
|
}
|
|
|
|
|
2013-08-02 23:38:26 +02:00
|
|
|
$(function () {
|
2013-08-02 23:56:28 +02:00
|
|
|
// On a page load or reload, the browser will, at an idle time, scroll to
|
|
|
|
// the top of the page. We can't intercept this browser-induced scroll, so
|
|
|
|
// to make sure it doesn't in interfere with our scrolling to the correct
|
|
|
|
// place in your message feed, we let the browser scroll happen first.
|
|
|
|
|
|
|
|
// After some time, if we haven't seen the browser scroll, give up
|
|
|
|
// and call main.
|
|
|
|
var browser_scroll_timer = setTimeout(function () {
|
|
|
|
install_main_scroll_handler();
|
|
|
|
}, 500);
|
|
|
|
|
2013-08-07 20:15:34 +02:00
|
|
|
$(window).scroll(function () {
|
2013-08-02 23:56:28 +02:00
|
|
|
if (viewport.scrollTop() < viewport.height()) {
|
|
|
|
// This is the browser-induced scroll to the top of the
|
|
|
|
// page that we were waiting for. It only happens once,
|
|
|
|
// so stop waiting and call main.
|
|
|
|
install_main_scroll_handler();
|
|
|
|
clearTimeout(browser_scroll_timer);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2013-08-02 23:38:26 +02:00
|
|
|
main();
|
|
|
|
});
|