1. 1 : /**
  2. 2 : * @file This file provides functions for sending AJAX requests to the Ingress API.
  3. 3 : * @module send_request
  4. 4 : */
  5. 5 :
  6. 6 : /**
  7. 7 : * Sends an AJAX POST request to the Ingress API.
  8. 8 : *
  9. 9 : * @function postAjax
  10. 10 : * @param {string} action - The last part of the URL, automatically appended to the Ingress API endpoint.
  11. 11 : * @param {Object} data - JSON data to post. The method is derived automatically from action but may be overridden.
  12. 12 : * Expects to be given a Hash. Strings are not supported.
  13. 13 : * @param {Function} successCallback - Function to call on success. See jQuery API docs for available arguments.
  14. 14 : * @param {Function} errorCallback - Function to call on error. Additionally, it is logged if the request failed.
  15. 15 : * @returns {jqXHR} The jQuery wrapped XMLHttpRequest object.
  16. 16 : */
  17. 17 : window.postAjax = function (action, data, successCallback, errorCallback) {
  18. 18 : // state management functions... perhaps should be outside of this func?
  19. 19 :
  20. 20 : // var remove = function(data, textStatus, jqXHR) { window.requests.remove(jqXHR); };
  21. 21 : // var errCnt = function(jqXHR) { window.failedRequestCount++; window.requests.remove(jqXHR); };
  22. 22 :
  23. 23 : if (window.latestFailedRequestTime && window.latestFailedRequestTime < Date.now() - 120 * 1000) {
  24. 24 : // no errors in the last two minutes - clear the error count
  25. 25 : window.failedRequestCount = 0;
  26. 26 : window.latestFailedRequestTime = undefined;
  27. 27 : }
  28. 28 :
  29. 29 : var onError = function (jqXHR, textStatus, errorThrown) {
  30. 30 : window.requests.remove(jqXHR);
  31. 31 : window.failedRequestCount++;
  32. 32 :
  33. 33 : window.latestFailedRequestTime = Date.now();
  34. 34 :
  35. 35 : // pass through to the user error func, if one exists
  36. 36 : if (errorCallback) {
  37. 37 : errorCallback(jqXHR, textStatus, errorThrown);
  38. 38 : }
  39. 39 : };
  40. 40 :
  41. 41 : var onSuccess = function (data, textStatus, jqXHR) {
  42. 42 : window.requests.remove(jqXHR);
  43. 43 :
  44. 44 : // the Niantic server can return a HTTP success, but the JSON response contains an error. handle that sensibly
  45. 45 : if (data && data.error && data.error === 'out of date') {
  46. 46 : window.failedRequestCount++;
  47. 47 : // let's call the error callback in thos case...
  48. 48 : if (errorCallback) {
  49. 49 : errorCallback(jqXHR, textStatus, "data.error == 'out of date'");
  50. 50 : }
  51. 51 :
  52. 52 : window.outOfDateUserPrompt();
  53. 53 : } else {
  54. 54 : successCallback(data, textStatus, jqXHR);
  55. 55 : }
  56. 56 : };
  57. 57 :
  58. 58 : // we set this flag when we want to block all requests due to having an out of date CURRENT_VERSION
  59. 59 : if (window.blockOutOfDateRequests) {
  60. 60 : window.failedRequestCount++;
  61. 61 : window.latestFailedRequestTime = Date.now();
  62. 62 :
  63. 63 : // call the error callback, if one exists
  64. 64 : if (errorCallback) {
  65. 65 : // NOTE: error called on a setTimeout - as it won't be expected to be synchronous
  66. 66 : // ensures no recursion issues if the error handler immediately resends the request
  67. 67 : setTimeout(function () {
  68. 68 : errorCallback(null, undefined, 'window.blockOutOfDateRequests is set');
  69. 69 : }, 10);
  70. 70 : }
  71. 71 : return;
  72. 72 : }
  73. 73 :
  74. 74 : var versionStr = window.niantic_params.CURRENT_VERSION;
  75. 75 : var post_data = JSON.stringify($.extend({}, data, { v: versionStr }));
  76. 76 :
  77. 77 : var result = $.ajax({
  78. 78 : url: '/r/' + action,
  79. 79 : type: 'POST',
  80. 80 : data: post_data,
  81. 81 : context: data,
  82. 82 : dataType: 'json',
  83. 83 : success: [onSuccess],
  84. 84 : error: [onError],
  85. 85 : contentType: 'application/json; charset=utf-8',
  86. 86 : beforeSend: function (req) {
  87. 87 : req.setRequestHeader('X-CSRFToken', window.readCookie('csrftoken'));
  88. 88 : },
  89. 89 : });
  90. 90 :
  91. 91 : window.requests.add(result);
  92. 92 :
  93. 93 : return result;
  94. 94 : };
  95. 95 :
  96. 96 : /**
  97. 97 : * Displays a dialog prompt to the user when the IITC version is out of date.
  98. 98 : * Blocks all requests while the dialog is open.
  99. 99 : *
  100. 100 : * @function outOfDateUserPrompt
  101. 101 : */
  102. 102 : window.outOfDateUserPrompt = function () {
  103. 103 : // we block all requests while the dialog is open.
  104. 104 : if (!window.blockOutOfDateRequests) {
  105. 105 : window.blockOutOfDateRequests = true;
  106. 106 :
  107. 107 : window.dialog({
  108. 108 : title: 'Reload IITC',
  109. 109 : html:
  110. 110 : '<p>IITC is using an outdated version code. This will happen when Niantic updates the standard intel site.</p>' +
  111. 111 : '<p>You need to reload the page to get the updated changes.</p>' +
  112. 112 : '<p>If you have just reloaded the page, then an old version of the standard site script is cached somewhere.' +
  113. 113 : 'In this case, try clearing your cache, or waiting 15-30 minutes for the stale data to expire.</p>',
  114. 114 : buttons: {
  115. 115 : RELOAD: function () {
  116. 116 : if (window.isApp && window.app.reloadIITC) {
  117. 117 : window.app.reloadIITC();
  118. 118 : } else {
  119. 119 : window.location.reload();
  120. 120 : }
  121. 121 : },
  122. 122 : },
  123. 123 : close: function () {
  124. 124 : delete window.blockOutOfDateRequests;
  125. 125 : },
  126. 126 : });
  127. 127 : }
  128. 128 : };