HttpController.js 6.1 KB

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