DocManager.js 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  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('settings-sharelatex')
  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. checkDocExists(project_id, doc_id, callback) {
  75. if (callback == null) {
  76. callback = function (err, exists) {}
  77. }
  78. return DocManager._getDoc(
  79. project_id,
  80. doc_id,
  81. { _id: 1, inS3: true },
  82. function (err, doc) {
  83. if (err != null) {
  84. return callback(err)
  85. }
  86. return callback(err, doc != null)
  87. }
  88. )
  89. },
  90. isDocDeleted(projectId, docId, callback) {
  91. MongoManager.findDoc(projectId, docId, { deleted: true }, function (
  92. err,
  93. doc
  94. ) {
  95. if (err) {
  96. return callback(err)
  97. }
  98. if (!doc) {
  99. return callback(
  100. new Errors.NotFoundError(`No such project/doc: ${projectId}/${docId}`)
  101. )
  102. }
  103. // `doc.deleted` is `undefined` for non deleted docs
  104. callback(null, Boolean(doc.deleted))
  105. })
  106. },
  107. getFullDoc(project_id, doc_id, callback) {
  108. if (callback == null) {
  109. callback = function (err, doc) {}
  110. }
  111. return DocManager._getDoc(
  112. project_id,
  113. doc_id,
  114. {
  115. lines: true,
  116. rev: true,
  117. deleted: true,
  118. version: true,
  119. ranges: true,
  120. inS3: true
  121. },
  122. function (err, doc) {
  123. if (err != null) {
  124. return callback(err)
  125. }
  126. return callback(err, doc)
  127. }
  128. )
  129. },
  130. getDocLines(project_id, doc_id, callback) {
  131. if (callback == null) {
  132. callback = function (err, doc) {}
  133. }
  134. return DocManager._getDoc(
  135. project_id,
  136. doc_id,
  137. { lines: true, inS3: true },
  138. function (err, doc) {
  139. if (err != null) {
  140. return callback(err)
  141. }
  142. return callback(err, doc)
  143. }
  144. )
  145. },
  146. getAllNonDeletedDocs(project_id, filter, callback) {
  147. if (callback == null) {
  148. callback = function (error, docs) {}
  149. }
  150. return DocArchive.unArchiveAllDocs(project_id, function (error) {
  151. if (error != null) {
  152. return callback(error)
  153. }
  154. return MongoManager.getProjectsDocs(
  155. project_id,
  156. { include_deleted: false },
  157. filter,
  158. function (error, docs) {
  159. if (typeof err !== 'undefined' && err !== null) {
  160. return callback(error)
  161. } else if (docs == null) {
  162. return callback(
  163. new Errors.NotFoundError(`No docs for project ${project_id}`)
  164. )
  165. } else {
  166. return callback(null, docs)
  167. }
  168. }
  169. )
  170. })
  171. },
  172. updateDoc(project_id, doc_id, lines, version, ranges, callback) {
  173. if (callback == null) {
  174. callback = function (error, modified, rev) {}
  175. }
  176. if (lines == null || version == null || ranges == null) {
  177. return callback(new Error('no lines, version or ranges provided'))
  178. }
  179. return DocManager._getDoc(
  180. project_id,
  181. doc_id,
  182. {
  183. version: true,
  184. rev: true,
  185. lines: true,
  186. version: true,
  187. ranges: true,
  188. inS3: true
  189. },
  190. function (err, doc) {
  191. let updateLines, updateRanges, updateVersion
  192. if (err != null && !(err instanceof Errors.NotFoundError)) {
  193. logger.err(
  194. { project_id, doc_id, err },
  195. 'error getting document for update'
  196. )
  197. return callback(err)
  198. }
  199. ranges = RangeManager.jsonRangesToMongo(ranges)
  200. if (doc == null) {
  201. // If the document doesn't exist, we'll make sure to create/update all parts of it.
  202. updateLines = true
  203. updateVersion = true
  204. updateRanges = true
  205. } else {
  206. updateLines = !_.isEqual(doc.lines, lines)
  207. updateVersion = doc.version !== version
  208. updateRanges = RangeManager.shouldUpdateRanges(doc.ranges, ranges)
  209. }
  210. let modified = false
  211. let rev = (doc != null ? doc.rev : undefined) || 0
  212. const updateLinesAndRangesIfNeeded = function (cb) {
  213. if (updateLines || updateRanges) {
  214. const update = {}
  215. if (updateLines) {
  216. update.lines = lines
  217. }
  218. if (updateRanges) {
  219. update.ranges = ranges
  220. }
  221. logger.log({ project_id, doc_id }, 'updating doc lines and ranges')
  222. modified = true
  223. rev += 1 // rev will be incremented in mongo by MongoManager.upsertIntoDocCollection
  224. return MongoManager.upsertIntoDocCollection(
  225. project_id,
  226. doc_id,
  227. update,
  228. cb
  229. )
  230. } else {
  231. logger.log(
  232. { project_id, doc_id },
  233. 'doc lines have not changed - not updating'
  234. )
  235. return cb()
  236. }
  237. }
  238. const updateVersionIfNeeded = function (cb) {
  239. if (updateVersion) {
  240. logger.log(
  241. {
  242. project_id,
  243. doc_id,
  244. oldVersion: doc != null ? doc.version : undefined,
  245. newVersion: version
  246. },
  247. 'updating doc version'
  248. )
  249. modified = true
  250. return MongoManager.setDocVersion(doc_id, version, cb)
  251. } else {
  252. logger.log(
  253. { project_id, doc_id, version },
  254. 'doc version has not changed - not updating'
  255. )
  256. return cb()
  257. }
  258. }
  259. return updateLinesAndRangesIfNeeded(function (error) {
  260. if (error != null) {
  261. return callback(error)
  262. }
  263. return updateVersionIfNeeded(function (error) {
  264. if (error != null) {
  265. return callback(error)
  266. }
  267. return callback(null, modified, rev)
  268. })
  269. })
  270. }
  271. )
  272. },
  273. deleteDoc(project_id, doc_id, callback) {
  274. if (callback == null) {
  275. callback = function (error) {}
  276. }
  277. return DocManager.checkDocExists(project_id, doc_id, function (
  278. error,
  279. exists
  280. ) {
  281. if (error != null) {
  282. return callback(error)
  283. }
  284. if (!exists) {
  285. return callback(
  286. new Errors.NotFoundError(
  287. `No such project/doc to delete: ${project_id}/${doc_id}`
  288. )
  289. )
  290. }
  291. if (Settings.docstore.archiveOnSoftDelete) {
  292. // The user will not read this doc anytime soon. Flush it out of mongo.
  293. DocArchive.archiveDocById(project_id, doc_id, (err) => {
  294. if (err) {
  295. logger.warn(
  296. { project_id, doc_id, err },
  297. 'archiving a single doc in the background failed'
  298. )
  299. }
  300. })
  301. }
  302. return MongoManager.markDocAsDeleted(project_id, doc_id, callback)
  303. })
  304. }
  305. }