HttpController.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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.send(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.send(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(project_id, doc_id, 1, function(
  114. error,
  115. document,
  116. rewoundUpdates
  117. ) {
  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.send(204)
  133. }
  134. })
  135. },
  136. getDiff(req, res, next) {
  137. let from, to
  138. if (next == null) {
  139. next = function(error) {}
  140. }
  141. const { doc_id } = req.params
  142. const { project_id } = req.params
  143. if (req.query.from != null) {
  144. from = parseInt(req.query.from, 10)
  145. } else {
  146. from = null
  147. }
  148. if (req.query.to != null) {
  149. to = parseInt(req.query.to, 10)
  150. } else {
  151. to = null
  152. }
  153. logger.log({ project_id, doc_id, from, to }, 'getting diff')
  154. return DiffManager.getDiff(project_id, doc_id, from, to, function(
  155. error,
  156. diff
  157. ) {
  158. if (error != null) {
  159. return next(error)
  160. }
  161. return res.json({ diff })
  162. })
  163. },
  164. getUpdates(req, res, next) {
  165. let before, min_count
  166. if (next == null) {
  167. next = function(error) {}
  168. }
  169. const { project_id } = req.params
  170. if (req.query.before != null) {
  171. before = parseInt(req.query.before, 10)
  172. }
  173. if (req.query.min_count != null) {
  174. min_count = parseInt(req.query.min_count, 10)
  175. }
  176. return UpdatesManager.getSummarizedProjectUpdates(
  177. project_id,
  178. { before, min_count },
  179. function(error, updates, nextBeforeTimestamp) {
  180. if (error != null) {
  181. return next(error)
  182. }
  183. return res.json({
  184. updates,
  185. nextBeforeTimestamp
  186. })
  187. }
  188. )
  189. },
  190. restore(req, res, next) {
  191. if (next == null) {
  192. next = function(error) {}
  193. }
  194. let { doc_id, project_id, version } = req.params
  195. const user_id = req.headers['x-user-id']
  196. version = parseInt(version, 10)
  197. return RestoreManager.restoreToBeforeVersion(
  198. project_id,
  199. doc_id,
  200. version,
  201. user_id,
  202. function(error) {
  203. if (error != null) {
  204. return next(error)
  205. }
  206. return res.send(204)
  207. }
  208. )
  209. },
  210. pushDocHistory(req, res, next) {
  211. if (next == null) {
  212. next = function(error) {}
  213. }
  214. const { project_id } = req.params
  215. const { doc_id } = req.params
  216. logger.log({ project_id, doc_id }, 'pushing all finalised changes to s3')
  217. return PackManager.pushOldPacks(project_id, doc_id, function(error) {
  218. if (error != null) {
  219. return next(error)
  220. }
  221. return res.send(204)
  222. })
  223. },
  224. pullDocHistory(req, res, next) {
  225. if (next == null) {
  226. next = function(error) {}
  227. }
  228. const { project_id } = req.params
  229. const { doc_id } = req.params
  230. logger.log({ project_id, doc_id }, 'pulling all packs from s3')
  231. return PackManager.pullOldPacks(project_id, doc_id, function(error) {
  232. if (error != null) {
  233. return next(error)
  234. }
  235. return res.send(204)
  236. })
  237. },
  238. healthCheck(req, res) {
  239. return HealthChecker.check(function(err) {
  240. if (err != null) {
  241. logger.err({ err }, 'error performing health check')
  242. return res.send(500)
  243. } else {
  244. return res.send(200)
  245. }
  246. })
  247. },
  248. checkLock(req, res) {
  249. return HealthChecker.checkLock(function(err) {
  250. if (err != null) {
  251. logger.err({ err }, 'error performing lock check')
  252. return res.send(500)
  253. } else {
  254. return res.send(200)
  255. }
  256. })
  257. }
  258. }