HttpController.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. /* eslint-disable
  2. camelcase,
  3. handle-callback-err,
  4. no-unused-vars,
  5. */
  6. // TODO: This file was created by bulk-decaffeinate.
  7. // Fix any style issues and re-enable lint.
  8. /*
  9. * decaffeinate suggestions:
  10. * DS101: Remove unnecessary use of Array.from
  11. * DS102: Remove unnecessary code created because of implicit returns
  12. * DS207: Consider shorter variations of null checks
  13. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  14. */
  15. let HttpController;
  16. const UpdatesManager = require("./UpdatesManager");
  17. const DiffManager = require("./DiffManager");
  18. const PackManager = require("./PackManager");
  19. const RestoreManager = require("./RestoreManager");
  20. const logger = require("logger-sharelatex");
  21. const HealthChecker = require("./HealthChecker");
  22. const _ = require("underscore");
  23. module.exports = (HttpController = {
  24. flushDoc(req, res, next) {
  25. if (next == null) { next = function(error) {}; }
  26. const { doc_id } = req.params;
  27. const { project_id } = req.params;
  28. logger.log({project_id, doc_id}, "compressing doc history");
  29. return UpdatesManager.processUncompressedUpdatesWithLock(project_id, doc_id, function(error) {
  30. if (error != null) { return next(error); }
  31. return res.send(204);
  32. });
  33. },
  34. flushProject(req, res, next) {
  35. if (next == null) { next = function(error) {}; }
  36. const { project_id } = req.params;
  37. logger.log({project_id}, "compressing project history");
  38. return UpdatesManager.processUncompressedUpdatesForProject(project_id, function(error) {
  39. if (error != null) { return next(error); }
  40. return res.send(204);
  41. });
  42. },
  43. flushAll(req, res, next) {
  44. // limit on projects to flush or -1 for all (default)
  45. if (next == null) { next = function(error) {}; }
  46. const limit = (req.query.limit != null) ? parseInt(req.query.limit, 10) : -1;
  47. logger.log({limit}, "flushing all projects");
  48. return UpdatesManager.flushAll(limit, function(error, result) {
  49. if (error != null) { return next(error); }
  50. const {failed, succeeded, all} = result;
  51. const status = `${succeeded.length} succeeded, ${failed.length} failed`;
  52. if (limit === 0) {
  53. return res.status(200).send(`${status}\nwould flush:\n${all.join('\n')}\n`);
  54. } else if (failed.length > 0) {
  55. logger.log({failed, succeeded}, "error flushing projects");
  56. return res.status(500).send(`${status}\nfailed to flush:\n${failed.join('\n')}\n`);
  57. } else {
  58. return res.status(200).send(`${status}\nflushed ${succeeded.length} projects of ${all.length}\n`);
  59. }
  60. });
  61. },
  62. checkDanglingUpdates(req, res, next) {
  63. if (next == null) { next = function(error) {}; }
  64. logger.log("checking dangling updates");
  65. return UpdatesManager.getDanglingUpdates(function(error, result) {
  66. if (error != null) { return next(error); }
  67. if (result.length > 0) {
  68. logger.log({dangling: result}, "found dangling updates");
  69. return res.status(500).send(`dangling updates:\n${result.join('\n')}\n`);
  70. } else {
  71. return res.status(200).send("no dangling updates found\n");
  72. }
  73. });
  74. },
  75. checkDoc(req, res, next) {
  76. if (next == null) { next = function(error) {}; }
  77. const { doc_id } = req.params;
  78. const { project_id } = req.params;
  79. logger.log({project_id, doc_id}, "checking doc history");
  80. return DiffManager.getDocumentBeforeVersion(project_id, doc_id, 1, function(error, document, rewoundUpdates) {
  81. if (error != null) { return next(error); }
  82. const broken = [];
  83. for (const update of Array.from(rewoundUpdates)) {
  84. for (const op of Array.from(update.op)) {
  85. if (op.broken === true) {
  86. broken.push(op);
  87. }
  88. }
  89. }
  90. if (broken.length > 0) {
  91. return res.send(broken);
  92. } else {
  93. return res.send(204);
  94. }
  95. });
  96. },
  97. getDiff(req, res, next) {
  98. let from, to;
  99. if (next == null) { next = function(error) {}; }
  100. const { doc_id } = req.params;
  101. const { project_id } = req.params;
  102. if (req.query.from != null) {
  103. from = parseInt(req.query.from, 10);
  104. } else {
  105. from = null;
  106. }
  107. if (req.query.to != null) {
  108. to = parseInt(req.query.to, 10);
  109. } else {
  110. to = null;
  111. }
  112. logger.log({project_id, doc_id, from, to}, "getting diff");
  113. return DiffManager.getDiff(project_id, doc_id, from, to, function(error, diff) {
  114. if (error != null) { return next(error); }
  115. return res.json({diff});
  116. });
  117. },
  118. getUpdates(req, res, next) {
  119. let before, min_count;
  120. if (next == null) { next = function(error) {}; }
  121. const { project_id } = req.params;
  122. if (req.query.before != null) {
  123. before = parseInt(req.query.before, 10);
  124. }
  125. if (req.query.min_count != null) {
  126. min_count = parseInt(req.query.min_count, 10);
  127. }
  128. return UpdatesManager.getSummarizedProjectUpdates(project_id, {before, min_count}, function(error, updates, nextBeforeTimestamp) {
  129. if (error != null) { return next(error); }
  130. return res.json({
  131. updates,
  132. nextBeforeTimestamp
  133. });
  134. });
  135. },
  136. restore(req, res, next) {
  137. if (next == null) { next = function(error) {}; }
  138. let {doc_id, project_id, version} = req.params;
  139. const user_id = req.headers["x-user-id"];
  140. version = parseInt(version, 10);
  141. return RestoreManager.restoreToBeforeVersion(project_id, doc_id, version, user_id, function(error) {
  142. if (error != null) { return next(error); }
  143. return res.send(204);
  144. });
  145. },
  146. pushDocHistory(req, res, next) {
  147. if (next == null) { next = function(error) {}; }
  148. const { project_id } = req.params;
  149. const { doc_id } = req.params;
  150. logger.log({project_id, doc_id}, "pushing all finalised changes to s3");
  151. return PackManager.pushOldPacks(project_id, doc_id, function(error) {
  152. if (error != null) { return next(error); }
  153. return res.send(204);
  154. });
  155. },
  156. pullDocHistory(req, res, next) {
  157. if (next == null) { next = function(error) {}; }
  158. const { project_id } = req.params;
  159. const { doc_id } = req.params;
  160. logger.log({project_id, doc_id}, "pulling all packs from s3");
  161. return PackManager.pullOldPacks(project_id, doc_id, function(error) {
  162. if (error != null) { return next(error); }
  163. return res.send(204);
  164. });
  165. },
  166. healthCheck(req, res){
  167. return HealthChecker.check(function(err){
  168. if (err != null) {
  169. logger.err({err}, "error performing health check");
  170. return res.send(500);
  171. } else {
  172. return res.send(200);
  173. }
  174. });
  175. },
  176. checkLock(req, res){
  177. return HealthChecker.checkLock(function(err) {
  178. if (err != null) {
  179. logger.err({err}, "error performing lock check");
  180. return res.send(500);
  181. } else {
  182. return res.send(200);
  183. }
  184. });
  185. }
  186. });