DocumentUpdaterManager.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. let DocumentUpdaterManager
  14. const request = require('request')
  15. const logger = require('logger-sharelatex')
  16. const Settings = require('@overleaf/settings')
  17. module.exports = DocumentUpdaterManager = {
  18. _requestDocument(project_id, doc_id, url, callback) {
  19. if (callback == null) {
  20. callback = function () {}
  21. }
  22. logger.log({ 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(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.log(
  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 Error(
  41. `doc updater returned a non-success status code: ${res.statusCode}`
  42. )
  43. logger.error(
  44. { err: error, project_id, doc_id, url },
  45. 'error accessing doc updater'
  46. )
  47. return callback(error)
  48. }
  49. })
  50. },
  51. getDocument(project_id, doc_id, callback) {
  52. const url = `${Settings.apis.documentupdater.url}/project/${project_id}/doc/${doc_id}`
  53. DocumentUpdaterManager._requestDocument(project_id, doc_id, url, callback)
  54. },
  55. peekDocument(project_id, doc_id, callback) {
  56. const url = `${Settings.apis.documentupdater.url}/project/${project_id}/doc/${doc_id}/peek`
  57. DocumentUpdaterManager._requestDocument(project_id, doc_id, url, callback)
  58. },
  59. setDocument(project_id, doc_id, content, user_id, callback) {
  60. if (callback == null) {
  61. callback = function () {}
  62. }
  63. const url = `${Settings.apis.documentupdater.url}/project/${project_id}/doc/${doc_id}`
  64. logger.log({ project_id, doc_id }, 'setting doc in document updater')
  65. return request.post(
  66. {
  67. url,
  68. json: {
  69. lines: content.split('\n'),
  70. source: 'restore',
  71. user_id,
  72. undoing: true,
  73. },
  74. },
  75. function (error, res, body) {
  76. if (error != null) {
  77. return callback(error)
  78. }
  79. if (res.statusCode >= 200 && res.statusCode < 300) {
  80. return callback(null)
  81. } else {
  82. error = new Error(
  83. `doc updater returned a non-success status code: ${res.statusCode}`
  84. )
  85. logger.error(
  86. { err: error, project_id, doc_id, url },
  87. 'error accessing doc updater'
  88. )
  89. return callback(error)
  90. }
  91. }
  92. )
  93. },
  94. }
  95. module.exports.promises = {
  96. // peekDocument returns two arguments so we can't use util.promisify, which only handles a single argument, we need
  97. // to treat this it as a special case.
  98. peekDocument: (project_id, doc_id) => {
  99. return new Promise((resolve, reject) => {
  100. DocumentUpdaterManager.peekDocument(
  101. project_id,
  102. doc_id,
  103. (err, content, version) => {
  104. if (err) {
  105. reject(err)
  106. } else {
  107. resolve([content, version])
  108. }
  109. }
  110. )
  111. })
  112. },
  113. }