2014-01-30 19:25:25 +01:00
|
|
|
var server_events = (function () {
|
|
|
|
|
|
|
|
var exports = {};
|
|
|
|
|
|
|
|
var waiting_on_homeview_load = true;
|
|
|
|
|
|
|
|
var events_stored_during_tutorial = [];
|
2014-01-30 20:50:18 +01:00
|
|
|
var events_stored_while_loading = [];
|
2014-01-30 19:25:25 +01:00
|
|
|
|
2014-01-30 20:29:00 +01:00
|
|
|
var get_events_xhr;
|
|
|
|
var get_events_timeout;
|
|
|
|
var get_events_failures = 0;
|
2014-01-30 21:01:52 +01:00
|
|
|
var get_events_params = {};
|
2014-01-30 19:25:25 +01:00
|
|
|
|
2016-08-23 03:53:05 +02:00
|
|
|
function dispatch_normal_event(event) {
|
|
|
|
switch (event.type) {
|
|
|
|
case 'alert_words':
|
|
|
|
alert_words.words = event.alert_words;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'default_streams':
|
|
|
|
page_params.realm_default_streams = event.default_streams;
|
2017-04-08 21:02:08 +02:00
|
|
|
settings_streams.update_default_streams_table();
|
2016-08-23 03:53:05 +02:00
|
|
|
break;
|
|
|
|
|
2017-01-24 01:48:35 +01:00
|
|
|
case 'hotspots':
|
|
|
|
hotspots.show(event.hotspots);
|
|
|
|
page_params.hotspots = event.hotspots;
|
|
|
|
break;
|
|
|
|
|
2016-08-23 03:53:05 +02:00
|
|
|
case 'muted_topics':
|
|
|
|
muting_ui.handle_updates(event.muted_topics);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'presence':
|
2017-02-26 17:10:36 +01:00
|
|
|
activity.set_user_status(event.email, event.presence, event.server_timestamp);
|
2016-08-23 03:53:05 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'restart':
|
|
|
|
var reload_options = {save_pointer: true,
|
|
|
|
save_narrow: true,
|
|
|
|
save_compose: true,
|
2017-01-12 00:17:43 +01:00
|
|
|
message: "The application has been updated; reloading!",
|
2016-08-23 03:53:05 +02:00
|
|
|
};
|
|
|
|
if (event.immediate) {
|
|
|
|
reload_options.immediate = true;
|
|
|
|
}
|
|
|
|
reload.initiate(reload_options);
|
|
|
|
break;
|
|
|
|
|
2016-12-02 13:23:23 +01:00
|
|
|
case 'reaction':
|
|
|
|
if (event.op === 'add') {
|
|
|
|
reactions.add_reaction(event);
|
|
|
|
} else if (event.op === 'remove') {
|
|
|
|
reactions.remove_reaction(event);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2016-08-23 03:53:05 +02:00
|
|
|
case 'realm':
|
|
|
|
if (event.op === 'update' && event.property === 'name') {
|
|
|
|
page_params.realm_name = event.value;
|
|
|
|
notifications.redraw_title();
|
2017-03-18 20:19:44 +01:00
|
|
|
} else if (event.op === 'update' && event.property === 'description') {
|
|
|
|
page_params.realm_description = event.value;
|
2017-04-08 18:13:39 +02:00
|
|
|
settings_org.update_realm_description(event.value);
|
2016-08-23 03:53:05 +02:00
|
|
|
} else if (event.op === 'update' && event.property === 'invite_required') {
|
|
|
|
page_params.realm_invite_required = event.value;
|
|
|
|
} else if (event.op === 'update' && event.property === 'invite_by_admins_only') {
|
|
|
|
page_params.realm_invite_by_admins_only = event.value;
|
2017-03-13 14:42:03 +01:00
|
|
|
} else if (event.op === 'update' && event.property === 'inline_image_preview') {
|
|
|
|
page_params.realm_inline_image_preview = event.value;
|
|
|
|
} else if (event.op === 'update' && event.property === 'inline_url_embed_preview') {
|
|
|
|
page_params.realm_inline_url_embed_preview = event.value;
|
2016-08-23 03:53:05 +02:00
|
|
|
} else if (event.op === 'update' && event.property === 'create_stream_by_admins_only') {
|
|
|
|
page_params.realm_create_stream_by_admins_only = event.value;
|
|
|
|
if (!page_params.is_admin) {
|
|
|
|
page_params.can_create_streams = !page_params.realm_create_stream_by_admins_only;
|
|
|
|
}
|
2017-03-13 18:33:49 +01:00
|
|
|
} else if (event.op === 'update' && event.property === 'name_changes_disabled') {
|
|
|
|
page_params.realm_name_changes_disabled = event.value;
|
2017-04-08 18:13:39 +02:00
|
|
|
settings_org.toggle_name_change_display();
|
2017-03-04 06:39:45 +01:00
|
|
|
} else if (event.op === 'update' && event.property === 'email_changes_disabled') {
|
|
|
|
page_params.realm_email_changes_disabled = event.value;
|
2017-04-08 18:13:39 +02:00
|
|
|
settings_org.toggle_email_change_display();
|
2016-12-20 15:41:30 +01:00
|
|
|
} else if (event.op === 'update' && event.property === 'add_emoji_by_admins_only') {
|
|
|
|
page_params.add_emoji_by_admins_only = event.value;
|
2016-08-23 03:53:05 +02:00
|
|
|
} else if (event.op === 'update' && event.property === 'restricted_to_domain') {
|
|
|
|
page_params.realm_restricted_to_domain = event.value;
|
2016-11-30 10:42:58 +01:00
|
|
|
} else if (event.op === 'update' && event.property === 'message_retention_days') {
|
|
|
|
page_params.message_retention_days = event.value;
|
2017-04-08 18:13:39 +02:00
|
|
|
settings_org.update_message_retention_days();
|
2016-08-23 03:53:05 +02:00
|
|
|
} else if (event.op === 'update_dict' && event.property === 'default') {
|
|
|
|
$.each(event.data, function (key, value) {
|
|
|
|
page_params['realm_' + key] = value;
|
|
|
|
});
|
2016-11-02 21:51:56 +01:00
|
|
|
if (event.data.authentication_methods !== undefined) {
|
2017-04-08 18:13:39 +02:00
|
|
|
settings_org.populate_auth_methods(event.data.authentication_methods);
|
2016-11-02 21:51:56 +01:00
|
|
|
}
|
2016-08-23 03:53:05 +02:00
|
|
|
} else if (event.op === 'update' && event.property === 'default_language') {
|
|
|
|
page_params.realm_default_language = event.value;
|
2017-04-08 18:13:39 +02:00
|
|
|
settings_org.reset_realm_default_language();
|
2016-11-29 08:57:35 +01:00
|
|
|
} else if (event.op === 'update' && event.property === 'waiting_period_threshold') {
|
|
|
|
page_params.realm_waiting_period_threshold = event.value;
|
2017-02-26 20:35:23 +01:00
|
|
|
} else if (event.op === 'update_dict' && event.property === 'icon') {
|
|
|
|
page_params.realm_icon_url = event.data.icon_url;
|
2017-03-06 06:06:23 +01:00
|
|
|
page_params.realm_icon_source = event.data.icon_source;
|
2017-02-26 20:35:23 +01:00
|
|
|
realm_icon.rerender();
|
2016-08-23 03:53:05 +02:00
|
|
|
}
|
2017-02-26 20:35:23 +01:00
|
|
|
|
2016-08-23 03:53:05 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'realm_bot':
|
|
|
|
if (event.op === 'add') {
|
|
|
|
bot_data.add(event.bot);
|
2017-04-08 20:08:35 +02:00
|
|
|
settings_users.update_user_data(event.bot.user_id, event.bot);
|
2016-08-23 03:53:05 +02:00
|
|
|
} else if (event.op === 'remove') {
|
2017-02-07 19:48:17 +01:00
|
|
|
bot_data.deactivate(event.bot.email);
|
2017-03-10 09:23:59 +01:00
|
|
|
event.bot.is_active = false;
|
2017-04-08 20:08:35 +02:00
|
|
|
settings_users.update_user_data(event.bot.user_id, event.bot);
|
2016-08-23 03:53:05 +02:00
|
|
|
} else if (event.op === 'update') {
|
2017-02-25 08:49:12 +01:00
|
|
|
if (_.has(event.bot, 'owner_id')) {
|
|
|
|
event.bot.owner = people.get_person_from_user_id(event.bot.owner_id).email;
|
|
|
|
}
|
2016-08-23 03:53:05 +02:00
|
|
|
bot_data.update(event.bot.email, event.bot);
|
2017-04-08 20:08:35 +02:00
|
|
|
settings_users.update_user_data(event.bot.user_id, event.bot);
|
2016-08-23 03:53:05 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'realm_emoji':
|
|
|
|
emoji.update_emojis(event.realm_emoji);
|
2017-04-08 17:24:07 +02:00
|
|
|
settings_emoji.populate_emoji(event.realm_emoji);
|
2016-08-23 03:53:05 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'realm_filters':
|
|
|
|
page_params.realm_filters = event.realm_filters;
|
|
|
|
echo.set_realm_filters(page_params.realm_filters);
|
2017-04-08 23:24:03 +02:00
|
|
|
settings_filters.populate_filters(page_params.realm_filters);
|
2016-08-23 03:53:05 +02:00
|
|
|
break;
|
|
|
|
|
2016-12-26 19:19:02 +01:00
|
|
|
case 'realm_domains':
|
2017-01-21 08:19:03 +01:00
|
|
|
var i;
|
2016-12-26 19:19:02 +01:00
|
|
|
if (event.op === 'add') {
|
2017-03-31 20:10:29 +02:00
|
|
|
page_params.domains.push(event.realm_domain);
|
2017-01-21 08:19:03 +01:00
|
|
|
} else if (event.op === 'change') {
|
|
|
|
for (i = 0; i < page_params.domains.length; i += 1) {
|
2017-03-31 20:10:29 +02:00
|
|
|
if (page_params.domains[i].domain === event.realm_domain.domain) {
|
|
|
|
page_params.domains[i].allow_subdomains = event.realm_domain.allow_subdomains;
|
2017-01-21 08:19:03 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2016-12-26 19:19:02 +01:00
|
|
|
} else if (event.op === 'remove') {
|
2017-01-21 09:09:27 +01:00
|
|
|
for (i = 0; i < page_params.domains.length; i += 1) {
|
|
|
|
if (page_params.domains[i].domain === event.domain) {
|
2016-12-26 19:19:02 +01:00
|
|
|
page_params.domains.splice(i, 1);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-04-08 18:13:39 +02:00
|
|
|
settings_org.populate_realm_domains(page_params.domains);
|
2016-12-26 19:19:02 +01:00
|
|
|
break;
|
2017-01-21 08:19:03 +01:00
|
|
|
|
2016-08-23 03:53:05 +02:00
|
|
|
case 'realm_user':
|
|
|
|
if (event.op === 'add') {
|
|
|
|
people.add_in_realm(event.person);
|
|
|
|
} else if (event.op === 'remove') {
|
2016-12-15 22:44:42 +01:00
|
|
|
people.deactivate(event.person);
|
2016-08-23 03:53:05 +02:00
|
|
|
} else if (event.op === 'update') {
|
2017-01-21 16:49:27 +01:00
|
|
|
user_events.update_person(event.person);
|
2016-08-23 03:53:05 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'referral':
|
|
|
|
referral.update_state(event.referrals.granted, event.referrals.used);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'stream':
|
|
|
|
if (event.op === 'update') {
|
|
|
|
// Legacy: Stream properties are still managed by subs.js on the client side.
|
2017-03-19 16:03:07 +01:00
|
|
|
stream_events.update_property(
|
2017-03-05 02:17:29 +01:00
|
|
|
event.stream_id,
|
|
|
|
event.property,
|
|
|
|
event.value
|
|
|
|
);
|
2017-04-08 21:02:08 +02:00
|
|
|
settings_streams.update_default_streams_table();
|
2016-10-15 21:10:10 +02:00
|
|
|
} else if (event.op === 'create') {
|
|
|
|
stream_data.create_streams(event.streams);
|
2017-02-15 17:38:44 +01:00
|
|
|
} else if (event.op === 'delete') {
|
|
|
|
_.each(event.streams, function (stream) {
|
|
|
|
if (stream_data.is_subscribed(stream.name)) {
|
|
|
|
stream_list.remove_sidebar_row(stream.stream_id);
|
|
|
|
}
|
|
|
|
subs.remove_stream(stream.stream_id);
|
|
|
|
stream_data.delete_sub(stream.stream_id);
|
2017-04-08 21:02:08 +02:00
|
|
|
settings_streams.remove_default_stream(stream.stream_id);
|
2017-02-18 20:38:19 +01:00
|
|
|
stream_data.remove_default_stream(stream.stream_id);
|
2017-02-15 17:38:44 +01:00
|
|
|
});
|
2016-08-23 03:53:05 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'subscription':
|
2016-11-08 15:04:18 +01:00
|
|
|
var person;
|
|
|
|
var email;
|
|
|
|
|
2016-08-23 03:53:05 +02:00
|
|
|
if (event.op === 'add') {
|
2017-03-05 00:09:48 +01:00
|
|
|
_.each(event.subscriptions, function (rec) {
|
|
|
|
var sub = stream_data.get_sub_by_id(rec.stream_id);
|
|
|
|
if (sub) {
|
2017-03-19 16:47:30 +01:00
|
|
|
stream_events.mark_subscribed(sub, rec.subscribers);
|
2017-03-05 00:09:48 +01:00
|
|
|
} else {
|
|
|
|
blueslip.error('Subscribing to unknown stream' + rec.stream_id);
|
|
|
|
}
|
2016-08-23 03:53:05 +02:00
|
|
|
});
|
2016-10-30 14:49:43 +01:00
|
|
|
} else if (event.op === 'peer_add') {
|
2016-10-31 20:18:32 +01:00
|
|
|
// TODO: remove email shim here and fix called functions
|
|
|
|
// to use user_ids
|
2016-11-08 15:04:18 +01:00
|
|
|
person = people.get_person_from_user_id(event.user_id);
|
|
|
|
email = person.email;
|
2016-10-30 14:49:43 +01:00
|
|
|
_.each(event.subscriptions, function (sub) {
|
2017-03-03 23:14:06 +01:00
|
|
|
if (stream_data.add_subscriber(sub, event.user_id)) {
|
|
|
|
$(document).trigger(
|
|
|
|
'peer_subscribe.zulip',
|
|
|
|
{stream_name: sub, user_email: email});
|
|
|
|
} else {
|
|
|
|
blueslip.warn('Cannot process peer_add event');
|
|
|
|
}
|
2016-10-30 14:49:43 +01:00
|
|
|
});
|
|
|
|
} else if (event.op === 'peer_remove') {
|
2016-11-08 15:04:18 +01:00
|
|
|
// TODO: remove email shim here and fix called functions
|
|
|
|
// to use user_ids
|
|
|
|
person = people.get_person_from_user_id(event.user_id);
|
|
|
|
email = person.email;
|
2016-10-30 14:49:43 +01:00
|
|
|
_.each(event.subscriptions, function (sub) {
|
2017-03-03 23:14:06 +01:00
|
|
|
if (stream_data.remove_subscriber(sub, event.user_id)) {
|
|
|
|
$(document).trigger(
|
|
|
|
'peer_unsubscribe.zulip',
|
|
|
|
{stream_name: sub, user_email: email});
|
|
|
|
} else {
|
|
|
|
blueslip.warn('Cannot process peer_remove event.');
|
|
|
|
}
|
2016-10-30 14:49:43 +01:00
|
|
|
});
|
2016-08-23 03:53:05 +02:00
|
|
|
} else if (event.op === 'remove') {
|
|
|
|
_.each(event.subscriptions, function (rec) {
|
|
|
|
var sub = stream_data.get_sub_by_id(rec.stream_id);
|
2017-03-19 16:47:30 +01:00
|
|
|
stream_events.mark_unsubscribed(sub);
|
2016-08-23 03:53:05 +02:00
|
|
|
});
|
|
|
|
} else if (event.op === 'update') {
|
2017-03-19 16:03:07 +01:00
|
|
|
stream_events.update_property(
|
2017-03-05 02:17:29 +01:00
|
|
|
event.stream_id,
|
|
|
|
event.property,
|
|
|
|
event.value
|
|
|
|
);
|
2016-08-23 03:53:05 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2016-10-12 20:57:59 +02:00
|
|
|
case 'typing':
|
|
|
|
if (event.sender.user_id === page_params.user_id) {
|
|
|
|
// typing notifications are sent to the user who is typing
|
|
|
|
// as well as recipients; we ignore such self-generated events.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (event.op === 'start') {
|
2017-03-22 15:11:41 +01:00
|
|
|
typing_events.display_notification(event);
|
2016-10-12 20:57:59 +02:00
|
|
|
} else if (event.op === 'stop') {
|
2017-03-22 15:11:41 +01:00
|
|
|
typing_events.hide_notification(event);
|
2016-10-12 20:57:59 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2016-08-23 03:53:05 +02:00
|
|
|
case 'update_display_settings':
|
|
|
|
if (event.setting_name === 'twenty_four_hour_time') {
|
2016-12-08 21:09:41 +01:00
|
|
|
page_params.twenty_four_hour_time = event.setting;
|
2017-03-02 07:29:01 +01:00
|
|
|
// Rerender the whole message list UI
|
|
|
|
home_msg_list.rerender();
|
|
|
|
if (current_msg_list === message_list.narrowed) {
|
|
|
|
message_list.narrowed.rerender();
|
|
|
|
}
|
2016-08-23 03:53:05 +02:00
|
|
|
}
|
2017-03-02 08:30:53 +01:00
|
|
|
if (event.setting_name === 'emoji_alt_code') {
|
|
|
|
page_params.emoji_alt_code = event.setting;
|
|
|
|
// Rerender the whole message list UI
|
|
|
|
home_msg_list.rerender();
|
|
|
|
if (current_msg_list === message_list.narrowed) {
|
|
|
|
message_list.narrowed.rerender();
|
|
|
|
}
|
|
|
|
}
|
2016-08-23 03:53:05 +02:00
|
|
|
if (event.setting_name === 'left_side_userlist') {
|
|
|
|
// TODO: Make this change the view immediately rather
|
|
|
|
// than requiring a reload or page resize.
|
2016-12-08 21:09:41 +01:00
|
|
|
page_params.left_side_userlist = event.setting;
|
2016-08-23 03:53:05 +02:00
|
|
|
}
|
|
|
|
if (event.setting_name === 'default_language') {
|
|
|
|
// TODO: Make this change the view immediately rather
|
|
|
|
// than requiring a reload or page resize.
|
2016-12-08 21:09:41 +01:00
|
|
|
page_params.default_language = event.setting;
|
|
|
|
}
|
|
|
|
if ($("#settings.tab-pane.active").length) {
|
2017-04-06 15:34:42 +02:00
|
|
|
settings_display.update_page();
|
2016-08-23 03:53:05 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'update_global_notifications':
|
|
|
|
notifications.handle_global_notification_updates(event.notification_name,
|
|
|
|
event.setting);
|
2016-12-08 21:09:41 +01:00
|
|
|
if ($("#settings.tab-pane.active").length) {
|
2017-04-06 16:01:07 +02:00
|
|
|
settings_notifications.update_page();
|
2016-12-08 21:09:41 +01:00
|
|
|
}
|
2016-08-23 03:53:05 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'update_message_flags':
|
|
|
|
var new_value = event.operation === "add";
|
2016-12-01 20:02:56 +01:00
|
|
|
switch (event.flag) {
|
2016-08-23 03:53:05 +02:00
|
|
|
case 'starred':
|
|
|
|
_.each(event.messages, function (message_id) {
|
|
|
|
ui.update_starred(message_id, new_value);
|
|
|
|
});
|
|
|
|
break;
|
|
|
|
case 'read':
|
|
|
|
var msgs_to_update = _.map(event.messages, function (message_id) {
|
|
|
|
return message_store.get(message_id);
|
|
|
|
});
|
2017-03-18 01:41:56 +01:00
|
|
|
unread_ops.mark_messages_as_read(msgs_to_update, {from: "server"});
|
2016-08-23 03:53:05 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-30 20:29:00 +01:00
|
|
|
function get_events_success(events) {
|
2014-01-30 19:25:25 +01:00
|
|
|
var messages = [];
|
|
|
|
var messages_to_update = [];
|
|
|
|
var new_pointer;
|
|
|
|
|
2016-12-05 07:02:18 +01:00
|
|
|
var clean_event = function clean_event(event) {
|
2014-01-31 06:24:15 +01:00
|
|
|
// Only log a whitelist of the event to remove private data
|
|
|
|
return _.pick(event, 'id', 'type', 'op');
|
|
|
|
};
|
|
|
|
|
2014-01-30 19:25:25 +01:00
|
|
|
_.each(events, function (event) {
|
2014-01-31 06:24:15 +01:00
|
|
|
try {
|
|
|
|
get_events_params.last_event_id = Math.max(get_events_params.last_event_id,
|
|
|
|
event.id);
|
|
|
|
} catch (ex) {
|
|
|
|
blueslip.error('Failed to update last_event_id',
|
|
|
|
{event: clean_event(event)},
|
|
|
|
ex.stack);
|
|
|
|
}
|
2014-01-30 19:25:25 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
if (tutorial.is_running()) {
|
|
|
|
events_stored_during_tutorial = events_stored_during_tutorial.concat(events);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (events_stored_during_tutorial.length > 0) {
|
|
|
|
events = events_stored_during_tutorial.concat(events);
|
|
|
|
events_stored_during_tutorial = [];
|
|
|
|
}
|
|
|
|
|
2014-01-30 20:50:18 +01:00
|
|
|
if (waiting_on_homeview_load) {
|
|
|
|
events_stored_while_loading = events_stored_while_loading.concat(events);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (events_stored_while_loading.length > 0) {
|
|
|
|
events = events_stored_while_loading.concat(events);
|
|
|
|
events_stored_while_loading = [];
|
|
|
|
}
|
|
|
|
|
2014-01-31 06:24:15 +01:00
|
|
|
var dispatch_event = function dispatch_event(event) {
|
2014-01-30 19:25:25 +01:00
|
|
|
switch (event.type) {
|
|
|
|
case 'message':
|
|
|
|
var msg = event.message;
|
|
|
|
msg.flags = event.flags;
|
|
|
|
if (event.local_message_id !== undefined) {
|
|
|
|
msg.local_id = event.local_message_id;
|
|
|
|
}
|
|
|
|
messages.push(msg);
|
|
|
|
break;
|
2016-08-23 03:53:05 +02:00
|
|
|
|
2014-01-30 19:25:25 +01:00
|
|
|
case 'pointer':
|
|
|
|
new_pointer = event.pointer;
|
|
|
|
break;
|
2016-08-23 03:53:05 +02:00
|
|
|
|
2014-01-30 19:25:25 +01:00
|
|
|
case 'update_message':
|
|
|
|
messages_to_update.push(event);
|
|
|
|
break;
|
|
|
|
|
2016-08-23 03:53:05 +02:00
|
|
|
default:
|
|
|
|
return dispatch_normal_event(event);
|
2014-01-30 19:25:25 +01:00
|
|
|
}
|
2014-01-31 06:24:15 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
_.each(events, function (event) {
|
|
|
|
try {
|
|
|
|
dispatch_event(event);
|
|
|
|
} catch (ex1) {
|
2014-02-10 22:48:49 +01:00
|
|
|
blueslip.error('Failed to process an event\n' +
|
|
|
|
blueslip.exception_msg(ex1),
|
2014-01-31 06:24:15 +01:00
|
|
|
{event: clean_event(event)},
|
|
|
|
ex1.stack);
|
|
|
|
}
|
2014-01-30 19:25:25 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
if (messages.length !== 0) {
|
2014-01-31 06:24:15 +01:00
|
|
|
try {
|
|
|
|
messages = echo.process_from_server(messages);
|
2017-03-19 20:23:48 +01:00
|
|
|
message_events.insert_new_messages(messages);
|
2014-01-31 06:24:15 +01:00
|
|
|
} catch (ex2) {
|
2014-02-10 22:48:49 +01:00
|
|
|
blueslip.error('Failed to insert new messages\n' +
|
|
|
|
blueslip.exception_msg(ex2),
|
2014-01-31 06:24:15 +01:00
|
|
|
undefined,
|
|
|
|
ex2.stack);
|
|
|
|
}
|
2014-01-30 19:25:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (new_pointer !== undefined
|
2016-12-01 13:30:30 +01:00
|
|
|
&& new_pointer > pointer.furthest_read) {
|
2016-04-12 17:38:47 +02:00
|
|
|
pointer.furthest_read = new_pointer;
|
|
|
|
pointer.server_furthest_read = new_pointer;
|
2014-01-30 19:25:25 +01:00
|
|
|
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) {
|
2014-01-31 06:24:15 +01:00
|
|
|
try {
|
2017-03-19 20:23:48 +01:00
|
|
|
message_events.update_messages(messages_to_update);
|
2014-01-31 06:24:15 +01:00
|
|
|
} catch (ex3) {
|
2014-02-10 22:48:49 +01:00
|
|
|
blueslip.error('Failed to update messages\n' +
|
|
|
|
blueslip.exception_msg(ex3),
|
2014-01-31 06:24:15 +01:00
|
|
|
undefined,
|
|
|
|
ex3.stack);
|
|
|
|
}
|
2014-01-30 19:25:25 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-30 20:50:18 +01:00
|
|
|
function get_events(options) {
|
2014-01-30 19:25:25 +01:00
|
|
|
options = _.extend({dont_block: false}, options);
|
|
|
|
|
2016-03-31 07:26:14 +02:00
|
|
|
if (reload.is_in_progress()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-01-30 21:01:52 +01:00
|
|
|
get_events_params.dont_block = options.dont_block || get_events_failures > 0;
|
|
|
|
if (get_events_params.queue_id === undefined) {
|
|
|
|
get_events_params.queue_id = page_params.event_queue_id;
|
|
|
|
get_events_params.last_event_id = page_params.last_event_id;
|
2014-01-30 19:25:25 +01:00
|
|
|
}
|
|
|
|
|
2014-01-30 20:29:00 +01:00
|
|
|
if (get_events_xhr !== undefined) {
|
|
|
|
get_events_xhr.abort();
|
2014-01-30 19:25:25 +01:00
|
|
|
}
|
2014-01-30 20:29:00 +01:00
|
|
|
if (get_events_timeout !== undefined) {
|
|
|
|
clearTimeout(get_events_timeout);
|
2014-01-30 19:25:25 +01:00
|
|
|
}
|
2014-01-30 20:29:00 +01:00
|
|
|
get_events_timeout = undefined;
|
2016-10-19 02:46:21 +02:00
|
|
|
get_events_xhr = channel.get({
|
|
|
|
url: '/json/events',
|
2014-01-30 21:01:52 +01:00
|
|
|
data: get_events_params,
|
2014-01-30 19:25:25 +01:00
|
|
|
idempotent: true,
|
|
|
|
timeout: page_params.poll_timeout,
|
|
|
|
success: function (data) {
|
2014-01-31 06:24:15 +01:00
|
|
|
try {
|
|
|
|
get_events_xhr = undefined;
|
|
|
|
get_events_failures = 0;
|
2017-03-23 20:37:08 +01:00
|
|
|
ui_report.hide_error($("#connection-error"));
|
2014-01-30 19:25:25 +01:00
|
|
|
|
2014-01-31 06:24:15 +01:00
|
|
|
get_events_success(data.events);
|
|
|
|
} catch (ex) {
|
2014-02-10 22:48:49 +01:00
|
|
|
blueslip.error('Failed to handle get_events success\n' +
|
|
|
|
blueslip.exception_msg(ex),
|
2014-01-31 06:24:15 +01:00
|
|
|
undefined,
|
|
|
|
ex.stack);
|
|
|
|
}
|
2014-01-30 20:29:00 +01:00
|
|
|
get_events_timeout = setTimeout(get_events, 0);
|
2014-01-30 19:25:25 +01:00
|
|
|
},
|
2016-12-02 14:06:06 +01:00
|
|
|
error: function (xhr, error_type) {
|
2014-01-31 06:24:15 +01:00
|
|
|
try {
|
|
|
|
get_events_xhr = undefined;
|
2017-03-23 05:13:23 +01:00
|
|
|
// If we're old enough that our message queue has been
|
|
|
|
// garbage collected, immediately reload.
|
2014-01-31 06:24:15 +01:00
|
|
|
if ((xhr.status === 400) &&
|
2017-03-23 05:13:23 +01:00
|
|
|
(JSON.parse(xhr.responseText).msg.indexOf("Bad event queue id") !== -1)) {
|
2014-01-31 06:24:15 +01:00
|
|
|
page_params.event_queue_expired = true;
|
2015-11-29 03:00:00 +01:00
|
|
|
reload.initiate({immediate: true,
|
|
|
|
save_pointer: false,
|
2017-03-23 06:27:29 +01:00
|
|
|
save_narrow: true,
|
2015-11-29 03:47:36 +01:00
|
|
|
save_compose: true});
|
2014-01-31 06:24:15 +01:00
|
|
|
}
|
2014-01-30 19:25:25 +01:00
|
|
|
|
2014-01-31 06:24:15 +01:00
|
|
|
if (error_type === 'abort') {
|
|
|
|
// Don't restart if we explicitly aborted
|
|
|
|
return;
|
|
|
|
} else if (error_type === 'timeout') {
|
|
|
|
// Retry indefinitely on timeout.
|
|
|
|
get_events_failures = 0;
|
2017-03-23 20:37:08 +01:00
|
|
|
ui_report.hide_error($("#connection-error"));
|
2014-01-31 06:24:15 +01:00
|
|
|
} else {
|
|
|
|
get_events_failures += 1;
|
|
|
|
}
|
2014-01-30 19:25:25 +01:00
|
|
|
|
2014-01-31 06:24:15 +01:00
|
|
|
if (get_events_failures >= 5) {
|
2017-03-23 20:37:08 +01:00
|
|
|
ui_report.show_error($("#connection-error"));
|
2014-01-31 06:24:15 +01:00
|
|
|
} else {
|
2017-03-23 20:37:08 +01:00
|
|
|
ui_report.hide_error($("#connection-error"));
|
2014-01-31 06:24:15 +01:00
|
|
|
}
|
|
|
|
} catch (ex) {
|
2014-02-10 22:48:49 +01:00
|
|
|
blueslip.error('Failed to handle get_events error\n' +
|
|
|
|
blueslip.exception_msg(ex),
|
2014-01-31 06:24:15 +01:00
|
|
|
undefined,
|
|
|
|
ex.stack);
|
2014-01-30 19:25:25 +01:00
|
|
|
}
|
2014-01-30 20:29:00 +01:00
|
|
|
var retry_sec = Math.min(90, Math.exp(get_events_failures/2));
|
|
|
|
get_events_timeout = setTimeout(get_events, retry_sec*1000);
|
2017-01-12 00:17:43 +01:00
|
|
|
},
|
2014-01-30 19:25:25 +01:00
|
|
|
});
|
2014-01-30 20:50:18 +01:00
|
|
|
}
|
2014-01-30 19:25:25 +01:00
|
|
|
|
2014-01-30 20:29:00 +01:00
|
|
|
exports.assert_get_events_running = function assert_get_events_running(error_message) {
|
|
|
|
if (get_events_xhr === undefined && get_events_timeout === undefined) {
|
|
|
|
exports.restart_get_events({dont_block: true});
|
2014-01-30 19:25:25 +01:00
|
|
|
blueslip.error(error_message);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-01-30 20:29:00 +01:00
|
|
|
exports.restart_get_events = function restart_get_events(options) {
|
2014-01-30 20:50:18 +01:00
|
|
|
get_events(options);
|
2014-01-30 19:25:25 +01:00
|
|
|
};
|
|
|
|
|
2014-01-30 20:29:00 +01:00
|
|
|
exports.force_get_events = function force_get_events() {
|
2014-01-30 20:50:18 +01:00
|
|
|
get_events_timeout = setTimeout(get_events, 0);
|
2014-01-30 19:25:25 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
exports.home_view_loaded = function home_view_loaded() {
|
|
|
|
waiting_on_homeview_load = false;
|
2014-01-30 20:50:18 +01:00
|
|
|
get_events_success([]);
|
2014-01-30 19:25:25 +01:00
|
|
|
$(document).trigger("home_view_loaded.zulip");
|
|
|
|
};
|
|
|
|
|
|
|
|
var watchdog_time = $.now();
|
|
|
|
setInterval(function () {
|
|
|
|
var new_time = $.now();
|
|
|
|
if ((new_time - watchdog_time) > 20000) { // 20 seconds.
|
|
|
|
// Defensively reset watchdog_time here in case there's an
|
|
|
|
// exception in one of the event handlers
|
|
|
|
watchdog_time = new_time;
|
|
|
|
// Our app's JS wasn't running, which probably means the machine was
|
|
|
|
// asleep.
|
|
|
|
$(document).trigger($.Event('unsuspend'));
|
|
|
|
}
|
|
|
|
watchdog_time = new_time;
|
|
|
|
}, 5000);
|
|
|
|
|
2014-03-07 00:44:15 +01:00
|
|
|
util.execute_early(function () {
|
2014-01-30 19:25:25 +01:00
|
|
|
$(document).on('unsuspend', function () {
|
2014-01-30 20:29:00 +01:00
|
|
|
// Immediately poll for new events on unsuspend
|
|
|
|
blueslip.log("Restarting get_events due to unsuspend");
|
|
|
|
get_events_failures = 0;
|
|
|
|
exports.restart_get_events({dont_block: true});
|
2014-01-30 19:25:25 +01:00
|
|
|
});
|
2014-01-30 20:50:18 +01:00
|
|
|
get_events();
|
2014-01-30 19:25:25 +01:00
|
|
|
});
|
|
|
|
|
2014-02-05 19:49:18 +01:00
|
|
|
exports.cleanup_event_queue = function cleanup_event_queue() {
|
2014-01-30 19:25:25 +01:00
|
|
|
// Submit a request to the server to cleanup our event queue
|
|
|
|
if (page_params.event_queue_expired === true) {
|
|
|
|
return;
|
|
|
|
}
|
2016-03-31 08:01:52 +02:00
|
|
|
// Set expired because in a reload we may be called twice.
|
|
|
|
page_params.event_queue_expired = true;
|
2014-01-30 19:25:25 +01:00
|
|
|
channel.del({
|
|
|
|
url: '/json/events',
|
2017-01-12 00:17:43 +01:00
|
|
|
data: {queue_id: page_params.event_queue_id},
|
2014-01-30 19:25:25 +01:00
|
|
|
});
|
2014-02-05 19:49:18 +01:00
|
|
|
};
|
2014-01-30 19:25:25 +01:00
|
|
|
|
2016-12-02 14:06:06 +01:00
|
|
|
window.addEventListener("beforeunload", function () {
|
2014-02-05 19:49:18 +01:00
|
|
|
exports.cleanup_event_queue();
|
2014-01-30 19:25:25 +01:00
|
|
|
});
|
|
|
|
|
2016-08-04 17:44:35 +02:00
|
|
|
// For unit testing
|
|
|
|
exports._get_events_success = get_events_success;
|
2014-01-30 19:25:25 +01:00
|
|
|
|
|
|
|
return exports;
|
|
|
|
|
|
|
|
}());
|
|
|
|
if (typeof module !== 'undefined') {
|
|
|
|
module.exports = server_events;
|
|
|
|
}
|