2020-08-01 03:43:15 +02:00
|
|
|
"use strict";
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const pending_requests = [];
|
2014-02-07 04:27:16 +01:00
|
|
|
|
2016-12-05 07:02:18 +01:00
|
|
|
function add_pending_request(jqXHR) {
|
2014-02-07 04:27:16 +01:00
|
|
|
pending_requests.push(jqXHR);
|
|
|
|
if (pending_requests.length > 50) {
|
2020-07-15 00:34:28 +02:00
|
|
|
blueslip.warn(
|
|
|
|
"The length of pending_requests is over 50. Most likely " +
|
|
|
|
"they are not being correctly removed.",
|
|
|
|
);
|
2014-02-07 04:27:16 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-05 07:02:18 +01:00
|
|
|
function remove_pending_request(jqXHR) {
|
2020-02-08 05:48:46 +01:00
|
|
|
const pending_request_index = pending_requests.indexOf(jqXHR);
|
2015-11-10 19:01:34 +01:00
|
|
|
if (pending_request_index !== -1) {
|
2014-02-07 04:27:16 +01:00
|
|
|
pending_requests.splice(pending_request_index, 1);
|
|
|
|
}
|
|
|
|
}
|
2013-12-18 19:55:18 +01:00
|
|
|
|
2014-01-07 23:40:31 +01:00
|
|
|
function call(args, idempotent) {
|
2020-02-14 00:26:34 +01:00
|
|
|
if (reload_state.is_in_progress() && !args.ignore_reload) {
|
|
|
|
// If we're in the process of reloading, most HTTP requests
|
|
|
|
// are useless, with exceptions like cleaning up our event
|
|
|
|
// queue and blueslip (Which doesn't use channel.js).
|
2020-09-24 07:50:36 +02:00
|
|
|
return undefined;
|
2020-02-14 00:26:34 +01:00
|
|
|
}
|
|
|
|
|
2013-12-17 22:18:13 +01:00
|
|
|
// Wrap the error handlers to reload the page if we get a CSRF error
|
|
|
|
// (What probably happened is that the user logged out in another tab).
|
2019-11-02 00:06:25 +01:00
|
|
|
let orig_error = args.error;
|
2013-12-17 22:18:13 +01:00
|
|
|
if (orig_error === undefined) {
|
|
|
|
orig_error = function () {};
|
|
|
|
}
|
|
|
|
args.error = function wrapped_error(xhr, error_type, xhn) {
|
2014-02-07 04:27:16 +01:00
|
|
|
remove_pending_request(xhr);
|
|
|
|
|
2020-02-26 00:19:57 +01:00
|
|
|
if (reload_state.is_in_progress()) {
|
|
|
|
// If we're in the process of reloading the browser,
|
|
|
|
// there's no point in running the success handler,
|
|
|
|
// because all of our state is about to be discarded
|
|
|
|
// anyway.
|
|
|
|
blueslip.log(`Ignoring ${args.type} ${args.url} error response while reloading`);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-02-10 22:54:55 +01:00
|
|
|
if (xhr.status === 403) {
|
|
|
|
try {
|
2020-07-15 01:29:15 +02:00
|
|
|
if (JSON.parse(xhr.responseText).code === "CSRF_FAILED") {
|
2020-07-15 00:34:28 +02:00
|
|
|
reload.initiate({
|
|
|
|
immediate: true,
|
|
|
|
save_pointer: true,
|
|
|
|
save_narrow: true,
|
|
|
|
save_compose: true,
|
|
|
|
});
|
2014-02-10 22:54:55 +01:00
|
|
|
}
|
|
|
|
} catch (ex) {
|
2020-07-15 00:34:28 +02:00
|
|
|
blueslip.error(
|
|
|
|
"Unexpected 403 response from server",
|
2020-07-20 22:18:43 +02:00
|
|
|
{xhr: xhr.responseText, args},
|
2020-07-15 00:34:28 +02:00
|
|
|
ex.stack,
|
|
|
|
);
|
2014-02-10 22:54:55 +01:00
|
|
|
}
|
2013-12-17 22:18:13 +01:00
|
|
|
}
|
2020-09-24 07:50:36 +02:00
|
|
|
orig_error(xhr, error_type, xhn);
|
2013-12-17 22:18:13 +01:00
|
|
|
};
|
2019-11-02 00:06:25 +01:00
|
|
|
let orig_success = args.success;
|
2014-01-07 23:40:31 +01:00
|
|
|
if (orig_success === undefined) {
|
|
|
|
orig_success = function () {};
|
|
|
|
}
|
|
|
|
args.success = function wrapped_success(data, textStatus, jqXHR) {
|
2014-02-07 04:27:16 +01:00
|
|
|
remove_pending_request(jqXHR);
|
|
|
|
|
2020-02-13 21:34:54 +01:00
|
|
|
if (reload_state.is_in_progress()) {
|
|
|
|
// If we're in the process of reloading the browser,
|
|
|
|
// there's no point in running the success handler,
|
|
|
|
// because all of our state is about to be discarded
|
|
|
|
// anyway.
|
|
|
|
blueslip.log(`Ignoring ${args.type} ${args.url} response while reloading`);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-01-07 23:40:31 +01:00
|
|
|
if (!data && idempotent) {
|
|
|
|
// If idempotent, retry
|
|
|
|
blueslip.log("Retrying idempotent" + args);
|
2020-07-02 01:45:54 +02:00
|
|
|
setTimeout(() => {
|
2019-11-02 00:06:25 +01:00
|
|
|
const jqXHR = $.ajax(args);
|
2014-02-07 04:27:16 +01:00
|
|
|
add_pending_request(jqXHR);
|
2014-01-07 23:40:31 +01:00
|
|
|
}, 0);
|
|
|
|
return;
|
|
|
|
}
|
2020-09-24 07:50:36 +02:00
|
|
|
orig_success(data, textStatus, jqXHR);
|
2014-01-07 23:40:31 +01:00
|
|
|
};
|
2013-12-17 22:18:13 +01:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const jqXHR = $.ajax(args);
|
2014-02-07 04:27:16 +01:00
|
|
|
add_pending_request(jqXHR);
|
|
|
|
return jqXHR;
|
2013-12-18 19:55:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
exports.get = function (options) {
|
2020-07-16 22:40:18 +02:00
|
|
|
const args = {type: "GET", dataType: "json", ...options};
|
2014-01-07 23:40:31 +01:00
|
|
|
return call(args, options.idempotent);
|
2013-12-18 19:55:18 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
exports.post = function (options) {
|
2020-07-16 22:40:18 +02:00
|
|
|
const args = {type: "POST", dataType: "json", ...options};
|
2014-01-07 23:40:31 +01:00
|
|
|
return call(args, options.idempotent);
|
2013-12-18 19:55:18 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
exports.put = function (options) {
|
2020-07-16 22:40:18 +02:00
|
|
|
const args = {type: "PUT", dataType: "json", ...options};
|
2014-01-07 23:40:31 +01:00
|
|
|
return call(args, options.idempotent);
|
2013-12-18 19:55:18 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
// Not called exports.delete because delete is a reserved word in JS
|
|
|
|
exports.del = function (options) {
|
2020-07-16 22:40:18 +02:00
|
|
|
const args = {type: "DELETE", dataType: "json", ...options};
|
2014-01-07 23:40:31 +01:00
|
|
|
return call(args, options.idempotent);
|
2013-12-18 19:55:18 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
exports.patch = function (options) {
|
|
|
|
// Send a PATCH as a POST in order to work around QtWebkit
|
|
|
|
// (Linux/Windows desktop app) not supporting PATCH body.
|
|
|
|
if (options.processData === false) {
|
|
|
|
// If we're submitting a FormData object, we need to add the
|
|
|
|
// method this way
|
|
|
|
options.data.append("method", "PATCH");
|
2014-01-14 23:24:39 +01:00
|
|
|
} else {
|
2020-07-16 22:40:18 +02:00
|
|
|
options.data = {...options.data, method: "PATCH"};
|
2013-12-18 19:55:18 +01:00
|
|
|
}
|
2014-01-07 23:40:31 +01:00
|
|
|
return exports.post(options, options.idempotent);
|
2013-12-18 19:55:18 +01:00
|
|
|
};
|
|
|
|
|
2014-03-13 15:32:44 +01:00
|
|
|
exports.xhr_error_message = function (message, xhr) {
|
|
|
|
if (xhr.status.toString().charAt(0) === "4") {
|
|
|
|
// Only display the error response for 4XX, where we've crafted
|
|
|
|
// a nice response.
|
2016-08-24 23:56:23 +02:00
|
|
|
message += ": " + JSON.parse(xhr.responseText).msg;
|
2014-03-13 15:32:44 +01:00
|
|
|
}
|
|
|
|
return message;
|
|
|
|
};
|
|
|
|
|
2019-10-25 09:45:13 +02:00
|
|
|
window.channel = exports;
|