HttpController.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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) {
  26. next = function (error) {}
  27. }
  28. const { doc_id } = req.params
  29. const { project_id } = req.params
  30. logger.log({ project_id, doc_id }, 'compressing doc history')
  31. return UpdatesManager.processUncompressedUpdatesWithLock(
  32. project_id,
  33. doc_id,
  34. function (error) {
  35. if (error != null) {
  36. return next(error)
  37. }
  38. return res.sendStatus(204)
  39. }
  40. )
  41. },
  42. flushProject(req, res, next) {
  43. if (next == null) {
  44. next = function (error) {}
  45. }
  46. const { project_id } = req.params
  47. logger.log({ project_id }, 'compressing project history')
  48. return UpdatesManager.processUncompressedUpdatesForProject(
  49. project_id,
  50. function (error) {
  51. if (error != null) {
  52. return next(error)
  53. }
  54. return res.sendStatus(204)
  55. }
  56. )
  57. },
  58. flushAll(req, res, next) {
  59. // limit on projects to flush or -1 for all (default)
  60. if (next == null) {
  61. next = function (error) {}
  62. }
  63. const limit = req.query.limit != null ? parseInt(req.query.limit, 10) : -1
  64. logger.log({ limit }, 'flushing all projects')
  65. return UpdatesManager.flushAll(limit, function (error, result) {
  66. if (error != null) {
  67. return next(error)
  68. }
  69. const { failed, succeeded, all } = result
  70. const status = `${succeeded.length} succeeded, ${failed.length} failed`
  71. if (limit === 0) {
  72. return res
  73. .status(200)
  74. .send(`${status}\nwould flush:\n${all.join('\n')}\n`)
  75. } else if (failed.length > 0) {
  76. logger.log({ failed, succeeded }, 'error flushing projects')
  77. return res
  78. .status(500)
  79. .send(`${status}\nfailed to flush:\n${failed.join('\n')}\n`)
  80. } else {
  81. return res
  82. .status(200)
  83. .send(
  84. `${status}\nflushed ${succeeded.length} projects of ${all.length}\n`
  85. )
  86. }
  87. })
  88. },
  89. checkDanglingUpdates(req, res, next) {
  90. if (next == null) {
  91. next = function (error) {}
  92. }
  93. logger.log('checking dangling updates')
  94. return UpdatesManager.getDanglingUpdates(function (error, result) {
  95. if (error != null) {
  96. return next(error)
  97. }
  98. if (result.length > 0) {
  99. logger.log({ dangling: result }, 'found dangling updates')
  100. return res.status(500).send(`dangling updates:\n${result.join('\n')}\n`)
  101. } else {
  102. return res.status(200).send('no dangling updates found\n')
  103. }
  104. })
  105. },
  106. checkDoc(req, res, next) {
  107. if (next == null) {
  108. next = function (error) {}
  109. }
  110. const { doc_id } = req.params
  111. const { project_id } = req.params
  112. logger.log({ project_id, doc_id }, 'checking doc history')
  113. return DiffManager.getDocumentBeforeVersion(
  114. project_id,
  115. doc_id,
  116. 1,
  117. function (error, document, rewoundUpdates) {
  118. if (error != null) {
  119. return next(error)
  120. }
  121. const broken = []
  122. for (const update of Array.from(rewoundUpdates)) {
  123. for (const op of Array.from(update.op)) {
  124. if (op.broken === true) {
  125. broken.push(op)
  126. }
  127. }
  128. }
  129. if (broken.length > 0) {
  130. return res.send(broken)
  131. } else {
  132. return res.sendStatus(204)
  133. }
  134. }
  135. )
  136. },
  137. getDiff(req, res, next) {
  138. let from, to
  139. if (next == null) {
  140. next = function (error) {}
  141. }
  142. const { doc_id } = req.params
  143. const { project_id } = req.params
  144. if (req.query.from != null) {
  145. from = parseInt(req.query.from, 10)
  146. } else {
  147. from = null
  148. }
  149. if (req.query.to != null) {
  150. to = parseInt(req.query.to, 10)
  151. } else {
  152. to = null
  153. }
  154. logger.log({ project_id, doc_id, from, to }, 'getting diff')
  155. return DiffManager.getDiff(project_id, doc_id, from, to, function (
  156. error,
  157. diff
  158. ) {
  159. if (error != null) {
  160. return next(error)
  161. }
  162. return res.json({ diff })
  163. })
  164. },
  165. getUpdates(req, res, next) {
  166. let before, min_count
  167. if (next == null) {
  168. next = function (error) {}
  169. }
  170. const { project_id } = req.params
  171. if (req.query.before != null) {
  172. before = parseInt(req.query.before, 10)
  173. }
  174. if (req.query.min_count != null) {
  175. min_count = parseInt(req.query.min_count, 10)
  176. }
  177. return UpdatesManager.getSummarizedProjectUpdates(
  178. project_id,
  179. { before, min_count },
  180. function (error, updates, nextBeforeTimestamp) {
  181. if (error != null) {
  182. return next(error)
  183. }
  184. return res.json({
  185. updates,
  186. nextBeforeTimestamp
  187. })
  188. }
  189. )
  190. },
  191. restore(req, res, next) {
  192. if (next == null) {
  193. next = function (error) {}
  194. }
  195. let { doc_id, project_id, version } = req.params
  196. const user_id = req.headers['x-user-id']
  197. version = parseInt(version, 10)
  198. return RestoreManager.restoreToBeforeVersion(
  199. project_id,
  200. doc_id,
  201. version,
  202. user_id,
  203. function (error) {
  204. if (error != null) {
  205. return next(error)
  206. }
  207. return res.sendStatus(204)
  208. }
  209. )
  210. },
  211. pushDocHistory(req, res, next) {
  212. if (next == null) {
  213. next = function (error) {}
  214. }
  215. const { project_id } = req.params
  216. const { doc_id } = req.params
  217. logger.log({ project_id, doc_id }, 'pushing all finalised changes to s3')
  218. return PackManager.pushOldPacks(project_id, doc_id, function (error) {
  219. if (error != null) {
  220. return next(error)
  221. }
  222. return res.sendStatus(204)
  223. })
  224. },
  225. pullDocHistory(req, res, next) {
  226. if (next == null) {
  227. next = function (error) {}
  228. }
  229. const { project_id } = req.params
  230. const { doc_id } = req.params
  231. logger.log({ project_id, doc_id }, 'pulling all packs from s3')
  232. return PackManager.pullOldPacks(project_id, doc_id, function (error) {
  233. if (error != null) {
  234. return next(error)
  235. }
  236. return res.sendStatus(204)
  237. })
  238. },
  239. healthCheck(req, res) {
  240. return HealthChecker.check(function (err) {
  241. if (err != null) {
  242. logger.err({ err }, 'error performing health check')
  243. return res.sendStatus(500)
  244. } else {
  245. return res.sendStatus(200)
  246. }
  247. })
  248. },
  249. checkLock(req, res) {
  250. return HealthChecker.checkLock(function (err) {
  251. if (err != null) {
  252. logger.err({ err }, 'error performing lock check')
  253. return res.sendStatus(500)
  254. } else {
  255. return res.sendStatus(200)
  256. }
  257. })
  258. }
  259. }