DocumentManager.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783
  1. /* eslint-disable
  2. camelcase,
  3. no-unused-vars,
  4. */
  5. // TODO: This file was created by bulk-decaffeinate.
  6. // Fix any style issues and re-enable lint.
  7. /*
  8. * decaffeinate suggestions:
  9. * DS101: Remove unnecessary use of Array.from
  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 DocumentManager
  15. const RedisManager = require('./RedisManager')
  16. const ProjectHistoryRedisManager = require('./ProjectHistoryRedisManager')
  17. const PersistenceManager = require('./PersistenceManager')
  18. const DiffCodec = require('./DiffCodec')
  19. const logger = require('@overleaf/logger')
  20. const Metrics = require('./Metrics')
  21. const HistoryManager = require('./HistoryManager')
  22. const RealTimeRedisManager = require('./RealTimeRedisManager')
  23. const Errors = require('./Errors')
  24. const RangesManager = require('./RangesManager')
  25. const async = require('async')
  26. const MAX_UNFLUSHED_AGE = 300 * 1000 // 5 mins, document should be flushed to mongo this time after a change
  27. module.exports = DocumentManager = {
  28. getDoc(project_id, doc_id, _callback) {
  29. if (_callback == null) {
  30. _callback = function () {}
  31. }
  32. const timer = new Metrics.Timer('docManager.getDoc')
  33. const callback = function (...args) {
  34. timer.done()
  35. return _callback(...Array.from(args || []))
  36. }
  37. return RedisManager.getDoc(
  38. project_id,
  39. doc_id,
  40. function (
  41. error,
  42. lines,
  43. version,
  44. ranges,
  45. pathname,
  46. projectHistoryId,
  47. unflushedTime
  48. ) {
  49. if (error != null) {
  50. return callback(error)
  51. }
  52. if (lines == null || version == null) {
  53. logger.debug(
  54. { project_id, doc_id },
  55. 'doc not in redis so getting from persistence API'
  56. )
  57. return PersistenceManager.getDoc(
  58. project_id,
  59. doc_id,
  60. function (
  61. error,
  62. lines,
  63. version,
  64. ranges,
  65. pathname,
  66. projectHistoryId,
  67. projectHistoryType
  68. ) {
  69. if (error != null) {
  70. return callback(error)
  71. }
  72. logger.debug(
  73. {
  74. project_id,
  75. doc_id,
  76. lines,
  77. version,
  78. pathname,
  79. projectHistoryId,
  80. projectHistoryType,
  81. },
  82. 'got doc from persistence API'
  83. )
  84. return RedisManager.putDocInMemory(
  85. project_id,
  86. doc_id,
  87. lines,
  88. version,
  89. ranges,
  90. pathname,
  91. projectHistoryId,
  92. function (error) {
  93. if (error != null) {
  94. return callback(error)
  95. }
  96. return RedisManager.setHistoryType(
  97. doc_id,
  98. projectHistoryType,
  99. function (error) {
  100. if (error != null) {
  101. return callback(error)
  102. }
  103. return callback(
  104. null,
  105. lines,
  106. version,
  107. ranges || {},
  108. pathname,
  109. projectHistoryId,
  110. null,
  111. false
  112. )
  113. }
  114. )
  115. }
  116. )
  117. }
  118. )
  119. } else {
  120. return callback(
  121. null,
  122. lines,
  123. version,
  124. ranges,
  125. pathname,
  126. projectHistoryId,
  127. unflushedTime,
  128. true
  129. )
  130. }
  131. }
  132. )
  133. },
  134. getDocAndRecentOps(project_id, doc_id, fromVersion, _callback) {
  135. if (_callback == null) {
  136. _callback = function () {}
  137. }
  138. const timer = new Metrics.Timer('docManager.getDocAndRecentOps')
  139. const callback = function (...args) {
  140. timer.done()
  141. return _callback(...Array.from(args || []))
  142. }
  143. return DocumentManager.getDoc(
  144. project_id,
  145. doc_id,
  146. function (error, lines, version, ranges, pathname, projectHistoryId) {
  147. if (error != null) {
  148. return callback(error)
  149. }
  150. if (fromVersion === -1) {
  151. return callback(
  152. null,
  153. lines,
  154. version,
  155. [],
  156. ranges,
  157. pathname,
  158. projectHistoryId
  159. )
  160. } else {
  161. return RedisManager.getPreviousDocOps(
  162. doc_id,
  163. fromVersion,
  164. version,
  165. function (error, ops) {
  166. if (error != null) {
  167. return callback(error)
  168. }
  169. return callback(
  170. null,
  171. lines,
  172. version,
  173. ops,
  174. ranges,
  175. pathname,
  176. projectHistoryId
  177. )
  178. }
  179. )
  180. }
  181. }
  182. )
  183. },
  184. setDoc(project_id, doc_id, newLines, source, user_id, undoing, _callback) {
  185. if (_callback == null) {
  186. _callback = function () {}
  187. }
  188. const timer = new Metrics.Timer('docManager.setDoc')
  189. const callback = function (...args) {
  190. timer.done()
  191. return _callback(...Array.from(args || []))
  192. }
  193. if (newLines == null) {
  194. return callback(new Error('No lines were provided to setDoc'))
  195. }
  196. const UpdateManager = require('./UpdateManager')
  197. return DocumentManager.getDoc(
  198. project_id,
  199. doc_id,
  200. function (
  201. error,
  202. oldLines,
  203. version,
  204. ranges,
  205. pathname,
  206. projectHistoryId,
  207. unflushedTime,
  208. alreadyLoaded
  209. ) {
  210. if (error != null) {
  211. return callback(error)
  212. }
  213. if (
  214. oldLines != null &&
  215. oldLines.length > 0 &&
  216. oldLines[0].text != null
  217. ) {
  218. logger.debug(
  219. { doc_id, project_id, oldLines, newLines },
  220. 'document is JSON so not updating'
  221. )
  222. return callback(null)
  223. }
  224. logger.debug(
  225. { doc_id, project_id, oldLines, newLines },
  226. 'setting a document via http'
  227. )
  228. return DiffCodec.diffAsShareJsOp(
  229. oldLines,
  230. newLines,
  231. function (error, op) {
  232. if (error != null) {
  233. return callback(error)
  234. }
  235. if (undoing) {
  236. for (const o of Array.from(op || [])) {
  237. o.u = true
  238. } // Turn on undo flag for each op for track changes
  239. }
  240. const update = {
  241. doc: doc_id,
  242. op,
  243. v: version,
  244. meta: {
  245. type: 'external',
  246. source,
  247. user_id,
  248. },
  249. }
  250. return UpdateManager.applyUpdate(
  251. project_id,
  252. doc_id,
  253. update,
  254. function (error) {
  255. if (error != null) {
  256. return callback(error)
  257. }
  258. // If the document was loaded already, then someone has it open
  259. // in a project, and the usual flushing mechanism will happen.
  260. // Otherwise we should remove it immediately since nothing else
  261. // is using it.
  262. if (alreadyLoaded) {
  263. return DocumentManager.flushDocIfLoaded(
  264. project_id,
  265. doc_id,
  266. function (error) {
  267. if (error != null) {
  268. return callback(error)
  269. }
  270. return callback(null)
  271. }
  272. )
  273. } else {
  274. return DocumentManager.flushAndDeleteDoc(
  275. project_id,
  276. doc_id,
  277. {},
  278. function (error) {
  279. // There is no harm in flushing project history if the previous
  280. // call failed and sometimes it is required
  281. HistoryManager.flushProjectChangesAsync(project_id)
  282. if (error != null) {
  283. return callback(error)
  284. }
  285. return callback(null)
  286. }
  287. )
  288. }
  289. }
  290. )
  291. }
  292. )
  293. }
  294. )
  295. },
  296. flushDocIfLoaded(project_id, doc_id, _callback) {
  297. if (_callback == null) {
  298. _callback = function () {}
  299. }
  300. const timer = new Metrics.Timer('docManager.flushDocIfLoaded')
  301. const callback = function (...args) {
  302. timer.done()
  303. return _callback(...Array.from(args || []))
  304. }
  305. return RedisManager.getDoc(
  306. project_id,
  307. doc_id,
  308. function (
  309. error,
  310. lines,
  311. version,
  312. ranges,
  313. pathname,
  314. projectHistoryId,
  315. unflushedTime,
  316. lastUpdatedAt,
  317. lastUpdatedBy
  318. ) {
  319. if (error != null) {
  320. return callback(error)
  321. }
  322. if (lines == null || version == null) {
  323. logger.debug(
  324. { project_id, doc_id },
  325. 'doc is not loaded so not flushing'
  326. )
  327. return callback(null) // TODO: return a flag to bail out, as we go on to remove doc from memory?
  328. } else {
  329. logger.debug({ project_id, doc_id, version }, 'flushing doc')
  330. return PersistenceManager.setDoc(
  331. project_id,
  332. doc_id,
  333. lines,
  334. version,
  335. ranges,
  336. lastUpdatedAt,
  337. lastUpdatedBy,
  338. function (error) {
  339. if (error != null) {
  340. return callback(error)
  341. }
  342. return RedisManager.clearUnflushedTime(doc_id, callback)
  343. }
  344. )
  345. }
  346. }
  347. )
  348. },
  349. flushAndDeleteDoc(project_id, doc_id, options, _callback) {
  350. const timer = new Metrics.Timer('docManager.flushAndDeleteDoc')
  351. const callback = function (...args) {
  352. timer.done()
  353. return _callback(...Array.from(args || []))
  354. }
  355. return DocumentManager.flushDocIfLoaded(
  356. project_id,
  357. doc_id,
  358. function (error) {
  359. if (error != null) {
  360. if (options.ignoreFlushErrors) {
  361. logger.warn(
  362. { project_id, doc_id, err: error },
  363. 'ignoring flush error while deleting document'
  364. )
  365. } else {
  366. return callback(error)
  367. }
  368. }
  369. // Flush in the background since it requires a http request
  370. HistoryManager.flushDocChangesAsync(project_id, doc_id)
  371. return RedisManager.removeDocFromMemory(
  372. project_id,
  373. doc_id,
  374. function (error) {
  375. if (error != null) {
  376. return callback(error)
  377. }
  378. return callback(null)
  379. }
  380. )
  381. }
  382. )
  383. },
  384. acceptChanges(project_id, doc_id, change_ids, _callback) {
  385. if (change_ids == null) {
  386. change_ids = []
  387. }
  388. if (_callback == null) {
  389. _callback = function () {}
  390. }
  391. const timer = new Metrics.Timer('docManager.acceptChanges')
  392. const callback = function (...args) {
  393. timer.done()
  394. return _callback(...Array.from(args || []))
  395. }
  396. return DocumentManager.getDoc(
  397. project_id,
  398. doc_id,
  399. function (error, lines, version, ranges) {
  400. if (error != null) {
  401. return callback(error)
  402. }
  403. if (lines == null || version == null) {
  404. return callback(
  405. new Errors.NotFoundError(`document not found: ${doc_id}`)
  406. )
  407. }
  408. return RangesManager.acceptChanges(
  409. change_ids,
  410. ranges,
  411. function (error, new_ranges) {
  412. if (error != null) {
  413. return callback(error)
  414. }
  415. return RedisManager.updateDocument(
  416. project_id,
  417. doc_id,
  418. lines,
  419. version,
  420. [],
  421. new_ranges,
  422. {},
  423. function (error) {
  424. if (error != null) {
  425. return callback(error)
  426. }
  427. return callback()
  428. }
  429. )
  430. }
  431. )
  432. }
  433. )
  434. },
  435. deleteComment(project_id, doc_id, comment_id, _callback) {
  436. if (_callback == null) {
  437. _callback = function () {}
  438. }
  439. const timer = new Metrics.Timer('docManager.deleteComment')
  440. const callback = function (...args) {
  441. timer.done()
  442. return _callback(...Array.from(args || []))
  443. }
  444. return DocumentManager.getDoc(
  445. project_id,
  446. doc_id,
  447. function (error, lines, version, ranges) {
  448. if (error != null) {
  449. return callback(error)
  450. }
  451. if (lines == null || version == null) {
  452. return callback(
  453. new Errors.NotFoundError(`document not found: ${doc_id}`)
  454. )
  455. }
  456. return RangesManager.deleteComment(
  457. comment_id,
  458. ranges,
  459. function (error, new_ranges) {
  460. if (error != null) {
  461. return callback(error)
  462. }
  463. return RedisManager.updateDocument(
  464. project_id,
  465. doc_id,
  466. lines,
  467. version,
  468. [],
  469. new_ranges,
  470. {},
  471. function (error) {
  472. if (error != null) {
  473. return callback(error)
  474. }
  475. return callback()
  476. }
  477. )
  478. }
  479. )
  480. }
  481. )
  482. },
  483. renameDoc(project_id, doc_id, user_id, update, projectHistoryId, _callback) {
  484. if (_callback == null) {
  485. _callback = function () {}
  486. }
  487. const timer = new Metrics.Timer('docManager.updateProject')
  488. const callback = function (...args) {
  489. timer.done()
  490. return _callback(...Array.from(args || []))
  491. }
  492. return RedisManager.renameDoc(
  493. project_id,
  494. doc_id,
  495. user_id,
  496. update,
  497. projectHistoryId,
  498. callback
  499. )
  500. },
  501. getDocAndFlushIfOld(project_id, doc_id, callback) {
  502. if (callback == null) {
  503. callback = function () {}
  504. }
  505. return DocumentManager.getDoc(
  506. project_id,
  507. doc_id,
  508. function (
  509. error,
  510. lines,
  511. version,
  512. ranges,
  513. pathname,
  514. projectHistoryId,
  515. unflushedTime,
  516. alreadyLoaded
  517. ) {
  518. if (error != null) {
  519. return callback(error)
  520. }
  521. // if doc was already loaded see if it needs to be flushed
  522. if (
  523. alreadyLoaded &&
  524. unflushedTime != null &&
  525. Date.now() - unflushedTime > MAX_UNFLUSHED_AGE
  526. ) {
  527. return DocumentManager.flushDocIfLoaded(
  528. project_id,
  529. doc_id,
  530. function (error) {
  531. if (error != null) {
  532. return callback(error)
  533. }
  534. return callback(null, lines, version)
  535. }
  536. )
  537. } else {
  538. return callback(null, lines, version)
  539. }
  540. }
  541. )
  542. },
  543. resyncDocContents(project_id, doc_id, callback) {
  544. logger.debug({ project_id, doc_id }, 'start resyncing doc contents')
  545. return RedisManager.getDoc(
  546. project_id,
  547. doc_id,
  548. function (error, lines, version, ranges, pathname, projectHistoryId) {
  549. if (error != null) {
  550. return callback(error)
  551. }
  552. if (lines == null || version == null) {
  553. logger.debug(
  554. { project_id, doc_id },
  555. 'resyncing doc contents - not found in redis - retrieving from web'
  556. )
  557. return PersistenceManager.getDoc(
  558. project_id,
  559. doc_id,
  560. { peek: true },
  561. function (
  562. error,
  563. lines,
  564. version,
  565. ranges,
  566. pathname,
  567. projectHistoryId
  568. ) {
  569. if (error != null) {
  570. logger.error(
  571. { project_id, doc_id, getDocError: error },
  572. 'resyncing doc contents - error retrieving from web'
  573. )
  574. return callback(error)
  575. }
  576. return ProjectHistoryRedisManager.queueResyncDocContent(
  577. project_id,
  578. projectHistoryId,
  579. doc_id,
  580. lines,
  581. version,
  582. pathname,
  583. callback
  584. )
  585. }
  586. )
  587. } else {
  588. logger.debug(
  589. { project_id, doc_id },
  590. 'resyncing doc contents - doc in redis - will queue in redis'
  591. )
  592. return ProjectHistoryRedisManager.queueResyncDocContent(
  593. project_id,
  594. projectHistoryId,
  595. doc_id,
  596. lines,
  597. version,
  598. pathname,
  599. callback
  600. )
  601. }
  602. }
  603. )
  604. },
  605. getDocWithLock(project_id, doc_id, callback) {
  606. if (callback == null) {
  607. callback = function () {}
  608. }
  609. const UpdateManager = require('./UpdateManager')
  610. return UpdateManager.lockUpdatesAndDo(
  611. DocumentManager.getDoc,
  612. project_id,
  613. doc_id,
  614. callback
  615. )
  616. },
  617. getDocAndRecentOpsWithLock(project_id, doc_id, fromVersion, callback) {
  618. if (callback == null) {
  619. callback = function () {}
  620. }
  621. const UpdateManager = require('./UpdateManager')
  622. return UpdateManager.lockUpdatesAndDo(
  623. DocumentManager.getDocAndRecentOps,
  624. project_id,
  625. doc_id,
  626. fromVersion,
  627. callback
  628. )
  629. },
  630. getDocAndFlushIfOldWithLock(project_id, doc_id, callback) {
  631. if (callback == null) {
  632. callback = function () {}
  633. }
  634. const UpdateManager = require('./UpdateManager')
  635. return UpdateManager.lockUpdatesAndDo(
  636. DocumentManager.getDocAndFlushIfOld,
  637. project_id,
  638. doc_id,
  639. callback
  640. )
  641. },
  642. setDocWithLock(
  643. project_id,
  644. doc_id,
  645. lines,
  646. source,
  647. user_id,
  648. undoing,
  649. callback
  650. ) {
  651. if (callback == null) {
  652. callback = function () {}
  653. }
  654. const UpdateManager = require('./UpdateManager')
  655. return UpdateManager.lockUpdatesAndDo(
  656. DocumentManager.setDoc,
  657. project_id,
  658. doc_id,
  659. lines,
  660. source,
  661. user_id,
  662. undoing,
  663. callback
  664. )
  665. },
  666. flushDocIfLoadedWithLock(project_id, doc_id, callback) {
  667. if (callback == null) {
  668. callback = function () {}
  669. }
  670. const UpdateManager = require('./UpdateManager')
  671. return UpdateManager.lockUpdatesAndDo(
  672. DocumentManager.flushDocIfLoaded,
  673. project_id,
  674. doc_id,
  675. callback
  676. )
  677. },
  678. flushAndDeleteDocWithLock(project_id, doc_id, options, callback) {
  679. const UpdateManager = require('./UpdateManager')
  680. return UpdateManager.lockUpdatesAndDo(
  681. DocumentManager.flushAndDeleteDoc,
  682. project_id,
  683. doc_id,
  684. options,
  685. callback
  686. )
  687. },
  688. acceptChangesWithLock(project_id, doc_id, change_ids, callback) {
  689. if (callback == null) {
  690. callback = function () {}
  691. }
  692. const UpdateManager = require('./UpdateManager')
  693. return UpdateManager.lockUpdatesAndDo(
  694. DocumentManager.acceptChanges,
  695. project_id,
  696. doc_id,
  697. change_ids,
  698. callback
  699. )
  700. },
  701. deleteCommentWithLock(project_id, doc_id, thread_id, callback) {
  702. if (callback == null) {
  703. callback = function () {}
  704. }
  705. const UpdateManager = require('./UpdateManager')
  706. return UpdateManager.lockUpdatesAndDo(
  707. DocumentManager.deleteComment,
  708. project_id,
  709. doc_id,
  710. thread_id,
  711. callback
  712. )
  713. },
  714. renameDocWithLock(
  715. project_id,
  716. doc_id,
  717. user_id,
  718. update,
  719. projectHistoryId,
  720. callback
  721. ) {
  722. if (callback == null) {
  723. callback = function () {}
  724. }
  725. const UpdateManager = require('./UpdateManager')
  726. return UpdateManager.lockUpdatesAndDo(
  727. DocumentManager.renameDoc,
  728. project_id,
  729. doc_id,
  730. user_id,
  731. update,
  732. projectHistoryId,
  733. callback
  734. )
  735. },
  736. resyncDocContentsWithLock(project_id, doc_id, callback) {
  737. if (callback == null) {
  738. callback = function () {}
  739. }
  740. const UpdateManager = require('./UpdateManager')
  741. return UpdateManager.lockUpdatesAndDo(
  742. DocumentManager.resyncDocContents,
  743. project_id,
  744. doc_id,
  745. callback
  746. )
  747. },
  748. }