DocManager.js 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. /* eslint-disable
  2. camelcase,
  3. handle-callback-err,
  4. no-dupe-keys,
  5. no-undef,
  6. standard/no-callback-literal,
  7. */
  8. // TODO: This file was created by bulk-decaffeinate.
  9. // Fix any style issues and re-enable lint.
  10. /*
  11. * decaffeinate suggestions:
  12. * DS102: Remove unnecessary code created because of implicit returns
  13. * DS207: Consider shorter variations of null checks
  14. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  15. */
  16. let DocManager
  17. const MongoManager = require('./MongoManager')
  18. const Errors = require('./Errors')
  19. const logger = require('logger-sharelatex')
  20. const _ = require('underscore')
  21. const DocArchive = require('./DocArchiveManager')
  22. const RangeManager = require('./RangeManager')
  23. const Settings = require('@overleaf/settings')
  24. module.exports = DocManager = {
  25. // TODO: For historical reasons, the doc version is currently stored in the docOps
  26. // collection (which is all that this collection contains). In future, we should
  27. // migrate this version property to be part of the docs collection, to guarantee
  28. // consitency between lines and version when writing/reading, and for a simpler schema.
  29. _getDoc(project_id, doc_id, filter, callback) {
  30. if (filter == null) {
  31. filter = {}
  32. }
  33. if (callback == null) {
  34. callback = function (error, doc) {}
  35. }
  36. if (filter.inS3 !== true) {
  37. return callback('must include inS3 when getting doc')
  38. }
  39. return MongoManager.findDoc(project_id, doc_id, filter, function (
  40. err,
  41. doc
  42. ) {
  43. if (err != null) {
  44. return callback(err)
  45. } else if (doc == null) {
  46. return callback(
  47. new Errors.NotFoundError(
  48. `No such doc: ${doc_id} in project ${project_id}`
  49. )
  50. )
  51. } else if (doc != null ? doc.inS3 : undefined) {
  52. return DocArchive.unarchiveDoc(project_id, doc_id, function (err) {
  53. if (err != null) {
  54. logger.err({ err, project_id, doc_id }, 'error unarchiving doc')
  55. return callback(err)
  56. }
  57. return DocManager._getDoc(project_id, doc_id, filter, callback)
  58. })
  59. } else {
  60. if (filter.version) {
  61. return MongoManager.getDocVersion(doc_id, function (error, version) {
  62. if (error != null) {
  63. return callback(error)
  64. }
  65. doc.version = version
  66. return callback(err, doc)
  67. })
  68. } else {
  69. return callback(err, doc)
  70. }
  71. }
  72. })
  73. },
  74. isDocDeleted(projectId, docId, callback) {
  75. MongoManager.findDoc(projectId, docId, { deleted: true }, function (
  76. err,
  77. doc
  78. ) {
  79. if (err) {
  80. return callback(err)
  81. }
  82. if (!doc) {
  83. return callback(
  84. new Errors.NotFoundError(`No such project/doc: ${projectId}/${docId}`)
  85. )
  86. }
  87. // `doc.deleted` is `undefined` for non deleted docs
  88. callback(null, Boolean(doc.deleted))
  89. })
  90. },
  91. getFullDoc(project_id, doc_id, callback) {
  92. if (callback == null) {
  93. callback = function (err, doc) {}
  94. }
  95. return DocManager._getDoc(
  96. project_id,
  97. doc_id,
  98. {
  99. lines: true,
  100. rev: true,
  101. deleted: true,
  102. version: true,
  103. ranges: true,
  104. inS3: true
  105. },
  106. function (err, doc) {
  107. if (err != null) {
  108. return callback(err)
  109. }
  110. return callback(err, doc)
  111. }
  112. )
  113. },
  114. getDocLines(project_id, doc_id, callback) {
  115. if (callback == null) {
  116. callback = function (err, doc) {}
  117. }
  118. return DocManager._getDoc(
  119. project_id,
  120. doc_id,
  121. { lines: true, inS3: true },
  122. function (err, doc) {
  123. if (err != null) {
  124. return callback(err)
  125. }
  126. return callback(err, doc)
  127. }
  128. )
  129. },
  130. getAllDeletedDocs(project_id, filter, callback) {
  131. MongoManager.getProjectsDeletedDocs(project_id, filter, callback)
  132. },
  133. getAllNonDeletedDocs(project_id, filter, callback) {
  134. if (callback == null) {
  135. callback = function (error, docs) {}
  136. }
  137. return DocArchive.unArchiveAllDocs(project_id, function (error) {
  138. if (error != null) {
  139. return callback(error)
  140. }
  141. return MongoManager.getProjectsDocs(
  142. project_id,
  143. { include_deleted: false },
  144. filter,
  145. function (error, docs) {
  146. if (typeof err !== 'undefined' && err !== null) {
  147. return callback(error)
  148. } else if (docs == null) {
  149. return callback(
  150. new Errors.NotFoundError(`No docs for project ${project_id}`)
  151. )
  152. } else {
  153. return callback(null, docs)
  154. }
  155. }
  156. )
  157. })
  158. },
  159. updateDoc(project_id, doc_id, lines, version, ranges, callback) {
  160. if (callback == null) {
  161. callback = function (error, modified, rev) {}
  162. }
  163. if (lines == null || version == null || ranges == null) {
  164. return callback(new Error('no lines, version or ranges provided'))
  165. }
  166. return DocManager._getDoc(
  167. project_id,
  168. doc_id,
  169. {
  170. version: true,
  171. rev: true,
  172. lines: true,
  173. version: true,
  174. ranges: true,
  175. inS3: true
  176. },
  177. function (err, doc) {
  178. let updateLines, updateRanges, updateVersion
  179. if (err != null && !(err instanceof Errors.NotFoundError)) {
  180. logger.err(
  181. { project_id, doc_id, err },
  182. 'error getting document for update'
  183. )
  184. return callback(err)
  185. }
  186. ranges = RangeManager.jsonRangesToMongo(ranges)
  187. if (doc == null) {
  188. // If the document doesn't exist, we'll make sure to create/update all parts of it.
  189. updateLines = true
  190. updateVersion = true
  191. updateRanges = true
  192. } else {
  193. updateLines = !_.isEqual(doc.lines, lines)
  194. updateVersion = doc.version !== version
  195. updateRanges = RangeManager.shouldUpdateRanges(doc.ranges, ranges)
  196. }
  197. let modified = false
  198. let rev = (doc != null ? doc.rev : undefined) || 0
  199. const updateLinesAndRangesIfNeeded = function (cb) {
  200. if (updateLines || updateRanges) {
  201. const update = {}
  202. if (updateLines) {
  203. update.lines = lines
  204. }
  205. if (updateRanges) {
  206. update.ranges = ranges
  207. }
  208. logger.log({ project_id, doc_id }, 'updating doc lines and ranges')
  209. modified = true
  210. rev += 1 // rev will be incremented in mongo by MongoManager.upsertIntoDocCollection
  211. return MongoManager.upsertIntoDocCollection(
  212. project_id,
  213. doc_id,
  214. update,
  215. cb
  216. )
  217. } else {
  218. logger.log(
  219. { project_id, doc_id },
  220. 'doc lines have not changed - not updating'
  221. )
  222. return cb()
  223. }
  224. }
  225. const updateVersionIfNeeded = function (cb) {
  226. if (updateVersion) {
  227. logger.log(
  228. {
  229. project_id,
  230. doc_id,
  231. oldVersion: doc != null ? doc.version : undefined,
  232. newVersion: version
  233. },
  234. 'updating doc version'
  235. )
  236. modified = true
  237. return MongoManager.setDocVersion(doc_id, version, cb)
  238. } else {
  239. logger.log(
  240. { project_id, doc_id, version },
  241. 'doc version has not changed - not updating'
  242. )
  243. return cb()
  244. }
  245. }
  246. return updateLinesAndRangesIfNeeded(function (error) {
  247. if (error != null) {
  248. return callback(error)
  249. }
  250. return updateVersionIfNeeded(function (error) {
  251. if (error != null) {
  252. return callback(error)
  253. }
  254. return callback(null, modified, rev)
  255. })
  256. })
  257. }
  258. )
  259. },
  260. patchDoc(project_id, doc_id, meta, callback) {
  261. const projection = { _id: 1, deleted: true }
  262. MongoManager.findDoc(project_id, doc_id, projection, (error, doc) => {
  263. if (error != null) {
  264. return callback(error)
  265. }
  266. if (!doc) {
  267. return callback(
  268. new Errors.NotFoundError(
  269. `No such project/doc to delete: ${project_id}/${doc_id}`
  270. )
  271. )
  272. }
  273. if (meta.deleted && Settings.docstore.archiveOnSoftDelete) {
  274. // The user will not read this doc anytime soon. Flush it out of mongo.
  275. DocArchive.archiveDocById(project_id, doc_id, (err) => {
  276. if (err) {
  277. logger.warn(
  278. { project_id, doc_id, err },
  279. 'archiving a single doc in the background failed'
  280. )
  281. }
  282. })
  283. }
  284. MongoManager.patchDoc(project_id, doc_id, meta, callback)
  285. })
  286. }
  287. }