DocumentUpdaterManager.js 2.4 KB

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