ProjectHistoryHandler.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /* eslint-disable
  2. camelcase,
  3. node/handle-callback-err,
  4. max-len,
  5. no-unused-vars,
  6. */
  7. // TODO: This file was created by bulk-decaffeinate.
  8. // Fix any style issues and re-enable lint.
  9. /*
  10. * decaffeinate suggestions:
  11. * DS102: Remove unnecessary code created because of implicit returns
  12. * DS103: Rewrite code to no longer use __guard__
  13. * DS207: Consider shorter variations of null checks
  14. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  15. */
  16. const { Project } = require('../../models/Project')
  17. const ProjectDetailsHandler = require('./ProjectDetailsHandler')
  18. const logger = require('logger-sharelatex')
  19. const settings = require('@overleaf/settings')
  20. const HistoryManager = require('../History/HistoryManager')
  21. const ProjectEntityUpdateHandler = require('./ProjectEntityUpdateHandler')
  22. const { promisifyAll } = require('../../util/promises')
  23. const ProjectHistoryHandler = {
  24. setHistoryId(project_id, history_id, callback) {
  25. // reject invalid history ids
  26. if (callback == null) {
  27. callback = function (err) {}
  28. }
  29. if (!history_id || typeof history_id !== 'number') {
  30. return callback(new Error('invalid history id'))
  31. }
  32. // use $exists:false to prevent overwriting any existing history id, atomically
  33. return Project.updateOne(
  34. { _id: project_id, 'overleaf.history.id': { $exists: false } },
  35. { 'overleaf.history.id': history_id },
  36. function (err, result) {
  37. if (err != null) {
  38. return callback(err)
  39. }
  40. if ((result != null ? result.n : undefined) === 0) {
  41. return callback(new Error('history exists'))
  42. }
  43. return callback()
  44. }
  45. )
  46. },
  47. getHistoryId(project_id, callback) {
  48. if (callback == null) {
  49. callback = function (err, result) {}
  50. }
  51. return ProjectDetailsHandler.getDetails(
  52. project_id,
  53. function (err, project) {
  54. if (err != null) {
  55. return callback(err)
  56. } // n.b. getDetails returns an error if the project doesn't exist
  57. return callback(
  58. null,
  59. __guard__(
  60. __guard__(
  61. project != null ? project.overleaf : undefined,
  62. x1 => x1.history
  63. ),
  64. x => x.id
  65. )
  66. )
  67. }
  68. )
  69. },
  70. upgradeHistory(project_id, callback) {
  71. // project must have an overleaf.history.id before allowing display of new history
  72. if (callback == null) {
  73. callback = function (err, result) {}
  74. }
  75. return Project.updateOne(
  76. { _id: project_id, 'overleaf.history.id': { $exists: true } },
  77. {
  78. 'overleaf.history.display': true,
  79. 'overleaf.history.upgradedAt': new Date(),
  80. },
  81. function (err, result) {
  82. if (err != null) {
  83. return callback(err)
  84. }
  85. // return an error if overleaf.history.id wasn't present
  86. if ((result != null ? result.n : undefined) === 0) {
  87. return callback(new Error('history not upgraded'))
  88. }
  89. return callback()
  90. }
  91. )
  92. },
  93. downgradeHistory(project_id, callback) {
  94. if (callback == null) {
  95. callback = function (err, result) {}
  96. }
  97. return Project.updateOne(
  98. { _id: project_id, 'overleaf.history.upgradedAt': { $exists: true } },
  99. {
  100. 'overleaf.history.display': false,
  101. $unset: { 'overleaf.history.upgradedAt': 1 },
  102. },
  103. function (err, result) {
  104. if (err != null) {
  105. return callback(err)
  106. }
  107. if ((result != null ? result.n : undefined) === 0) {
  108. return callback(new Error('history not downgraded'))
  109. }
  110. return callback()
  111. }
  112. )
  113. },
  114. ensureHistoryExistsForProject(project_id, callback) {
  115. // We can only set a history id for a project that doesn't have one. The
  116. // history id is cached in the project history service, and changing an
  117. // existing value corrupts the history, leaving it in an irrecoverable
  118. // state. Setting a history id when one wasn't present before is ok,
  119. // because undefined history ids aren't cached.
  120. if (callback == null) {
  121. callback = function (err) {}
  122. }
  123. return ProjectHistoryHandler.getHistoryId(
  124. project_id,
  125. function (err, history_id) {
  126. if (err != null) {
  127. return callback(err)
  128. }
  129. if (history_id != null) {
  130. return callback()
  131. } // history already exists, success
  132. return HistoryManager.initializeProject(function (err, history) {
  133. if (err != null) {
  134. return callback(err)
  135. }
  136. if (!(history != null ? history.overleaf_id : undefined)) {
  137. return callback(new Error('failed to initialize history id'))
  138. }
  139. return ProjectHistoryHandler.setHistoryId(
  140. project_id,
  141. history.overleaf_id,
  142. function (err) {
  143. if (err != null) {
  144. return callback(err)
  145. }
  146. return ProjectEntityUpdateHandler.resyncProjectHistory(
  147. project_id,
  148. function (err) {
  149. if (err != null) {
  150. return callback(err)
  151. }
  152. return HistoryManager.flushProject(project_id, callback)
  153. }
  154. )
  155. }
  156. )
  157. })
  158. }
  159. )
  160. },
  161. }
  162. function __guard__(value, transform) {
  163. return typeof value !== 'undefined' && value !== null
  164. ? transform(value)
  165. : undefined
  166. }
  167. ProjectHistoryHandler.promises = promisifyAll(ProjectHistoryHandler)
  168. module.exports = ProjectHistoryHandler