DocumentUpdaterManager.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. /* eslint-disable
  2. camelcase,
  3. no-unused-vars,
  4. */
  5. // TODO: This file was created by bulk-decaffeinate.
  6. // Fix any style issues and re-enable lint.
  7. /*
  8. * decaffeinate suggestions:
  9. * DS102: Remove unnecessary code created because of implicit returns
  10. * DS207: Consider shorter variations of null checks
  11. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  12. */
  13. import request from 'request'
  14. import logger from '@overleaf/logger'
  15. import Settings from '@overleaf/settings'
  16. import OError from '@overleaf/o-error'
  17. export function getDocument(project_id, doc_id, callback) {
  18. if (callback == null) {
  19. callback = function () {}
  20. }
  21. const url = `${Settings.apis.documentupdater.url}/project/${project_id}/doc/${doc_id}`
  22. logger.debug({ project_id, doc_id }, 'getting doc from document updater')
  23. return request.get(url, function (error, res, body) {
  24. if (error != null) {
  25. return callback(OError.tag(error))
  26. }
  27. if (res.statusCode >= 200 && res.statusCode < 300) {
  28. try {
  29. body = JSON.parse(body)
  30. } catch (error1) {
  31. error = error1
  32. return callback(error)
  33. }
  34. logger.debug(
  35. { project_id, doc_id, version: body.version },
  36. 'got doc from document updater'
  37. )
  38. return callback(null, body.lines.join('\n'), body.version)
  39. } else {
  40. error = new OError(
  41. `doc updater returned a non-success status code: ${res.statusCode}`,
  42. { project_id, doc_id, url }
  43. )
  44. return callback(error)
  45. }
  46. })
  47. }
  48. export function setDocument(project_id, doc_id, content, user_id, callback) {
  49. if (callback == null) {
  50. callback = function () {}
  51. }
  52. const url = `${Settings.apis.documentupdater.url}/project/${project_id}/doc/${doc_id}`
  53. logger.debug({ project_id, doc_id }, 'setting doc in document updater')
  54. return request.post(
  55. {
  56. url,
  57. json: {
  58. lines: content.split('\n'),
  59. source: 'restore',
  60. user_id,
  61. undoing: true,
  62. },
  63. },
  64. function (error, res, body) {
  65. if (error != null) {
  66. return callback(OError.tag(error))
  67. }
  68. if (res.statusCode >= 200 && res.statusCode < 300) {
  69. return callback(null)
  70. } else {
  71. error = new OError(
  72. `doc updater returned a non-success status code: ${res.statusCode}`,
  73. { project_id, doc_id, url }
  74. )
  75. return callback(error)
  76. }
  77. }
  78. )
  79. }