RangeManager.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /* eslint-disable
  2. no-return-assign,
  3. */
  4. // TODO: This file was created by bulk-decaffeinate.
  5. // Fix any style issues and re-enable lint.
  6. /*
  7. * decaffeinate suggestions:
  8. * DS101: Remove unnecessary use of Array.from
  9. * DS102: Remove unnecessary code created because of implicit returns
  10. * DS207: Consider shorter variations of null checks
  11. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  12. */
  13. let RangeManager
  14. const _ = require('lodash')
  15. const { ObjectId } = require('./mongodb')
  16. module.exports = RangeManager = {
  17. shouldUpdateRanges(docRanges, incomingRanges) {
  18. if (incomingRanges == null) {
  19. throw new Error('expected incoming_ranges')
  20. }
  21. // If the ranges are empty, we don't store them in the DB, so set
  22. // doc_ranges to an empty object as default, since this is was the
  23. // incoming_ranges will be for an empty range set.
  24. if (docRanges == null) {
  25. docRanges = {}
  26. }
  27. return !_.isEqual(docRanges, incomingRanges)
  28. },
  29. jsonRangesToMongo(ranges) {
  30. if (ranges == null) {
  31. return null
  32. }
  33. const updateMetadata = function (metadata) {
  34. if ((metadata != null ? metadata.ts : undefined) != null) {
  35. metadata.ts = new Date(metadata.ts)
  36. }
  37. if ((metadata != null ? metadata.user_id : undefined) != null) {
  38. return (metadata.user_id = RangeManager._safeObjectId(metadata.user_id))
  39. }
  40. }
  41. for (const change of Array.from(ranges.changes || [])) {
  42. change.id = RangeManager._safeObjectId(change.id)
  43. updateMetadata(change.metadata)
  44. }
  45. for (const comment of Array.from(ranges.comments || [])) {
  46. // Two bugs resulted in mismatched ids, prefer the thread id from the op: https://github.com/overleaf/internal/issues/23272
  47. comment.id = RangeManager._safeObjectId(comment.op?.t || comment.id)
  48. if (comment.op) comment.op.t = comment.id
  49. // resolved property is added to comments when they are obtained from history, but this state doesn't belong in mongo docs collection
  50. // more info: https://github.com/overleaf/internal/issues/24371#issuecomment-2913095174
  51. delete comment.op?.resolved
  52. updateMetadata(comment.metadata)
  53. }
  54. return ranges
  55. },
  56. fixCommentIds(doc) {
  57. for (const comment of doc?.ranges?.comments || []) {
  58. // Two bugs resulted in mismatched ids, prefer the thread id from the op: https://github.com/overleaf/internal/issues/23272
  59. if (comment.op?.t) comment.id = comment.op.t
  60. }
  61. },
  62. _safeObjectId(data) {
  63. try {
  64. return new ObjectId(data)
  65. } catch (error) {
  66. return data
  67. }
  68. },
  69. }