DocumentUpdaterManager.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. * DS102: Remove unnecessary code created because of implicit returns
  11. * DS207: Consider shorter variations of null checks
  12. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  13. */
  14. let DocumentUpdaterManager
  15. const request = require('request')
  16. const logger = require('logger-sharelatex')
  17. const Settings = require('@overleaf/settings')
  18. module.exports = DocumentUpdaterManager = {
  19. _requestDocument(project_id, doc_id, url, callback) {
  20. if (callback == null) {
  21. callback = function (error, content, version) {}
  22. }
  23. logger.log({ project_id, doc_id }, 'getting doc from document updater')
  24. return request.get(url, function (error, res, body) {
  25. if (error != null) {
  26. return callback(error)
  27. }
  28. if (res.statusCode >= 200 && res.statusCode < 300) {
  29. try {
  30. body = JSON.parse(body)
  31. } catch (error1) {
  32. error = error1
  33. return callback(error)
  34. }
  35. logger.log(
  36. { project_id, doc_id, version: body.version },
  37. 'got doc from document updater'
  38. )
  39. return callback(null, body.lines.join('\n'), body.version)
  40. } else {
  41. error = new Error(
  42. `doc updater returned a non-success status code: ${res.statusCode}`
  43. )
  44. logger.error(
  45. { err: error, project_id, doc_id, url },
  46. 'error accessing doc updater'
  47. )
  48. return callback(error)
  49. }
  50. })
  51. },
  52. getDocument(project_id, doc_id, callback) {
  53. const url = `${Settings.apis.documentupdater.url}/project/${project_id}/doc/${doc_id}`
  54. DocumentUpdaterManager._requestDocument(project_id, doc_id, url, callback)
  55. },
  56. peekDocument(project_id, doc_id, callback) {
  57. const url = `${Settings.apis.documentupdater.url}/project/${project_id}/doc/${doc_id}/peek`
  58. DocumentUpdaterManager._requestDocument(project_id, doc_id, url, callback)
  59. },
  60. setDocument(project_id, doc_id, content, user_id, callback) {
  61. if (callback == null) {
  62. callback = function (error) {}
  63. }
  64. const url = `${Settings.apis.documentupdater.url}/project/${project_id}/doc/${doc_id}`
  65. logger.log({ project_id, doc_id }, 'setting doc in document updater')
  66. return request.post(
  67. {
  68. url,
  69. json: {
  70. lines: content.split('\n'),
  71. source: 'restore',
  72. user_id,
  73. undoing: true,
  74. },
  75. },
  76. function (error, res, body) {
  77. if (error != null) {
  78. return callback(error)
  79. }
  80. if (res.statusCode >= 200 && res.statusCode < 300) {
  81. return callback(null)
  82. } else {
  83. error = new Error(
  84. `doc updater returned a non-success status code: ${res.statusCode}`
  85. )
  86. logger.error(
  87. { err: error, project_id, doc_id, url },
  88. 'error accessing doc updater'
  89. )
  90. return callback(error)
  91. }
  92. }
  93. )
  94. },
  95. }
  96. module.exports.promises = {
  97. // peekDocument returns two arguments so we can't use util.promisify, which only handles a single argument, we need
  98. // to treat this it as a special case.
  99. peekDocument: (project_id, doc_id) => {
  100. return new Promise((resolve, reject) => {
  101. DocumentUpdaterManager.peekDocument(
  102. project_id,
  103. doc_id,
  104. (err, content, version) => {
  105. if (err) {
  106. reject(err)
  107. } else {
  108. resolve([content, version])
  109. }
  110. }
  111. )
  112. })
  113. },
  114. }