RangeManager.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. import _ from 'lodash'
  14. import mongodb from './mongodb.js'
  15. const { ObjectId } = mongodb
  16. let RangeManager
  17. export default RangeManager = {
  18. shouldUpdateRanges(docRanges, incomingRanges) {
  19. if (incomingRanges == null) {
  20. throw new Error('expected incoming_ranges')
  21. }
  22. // If the ranges are empty, we don't store them in the DB, so set
  23. // doc_ranges to an empty object as default, since this is was the
  24. // incoming_ranges will be for an empty range set.
  25. if (docRanges == null) {
  26. docRanges = {}
  27. }
  28. return !_.isEqual(docRanges, incomingRanges)
  29. },
  30. jsonRangesToMongo(ranges) {
  31. if (ranges == null) {
  32. return null
  33. }
  34. const updateMetadata = function (metadata) {
  35. if ((metadata != null ? metadata.ts : undefined) != null) {
  36. metadata.ts = new Date(metadata.ts)
  37. }
  38. if ((metadata != null ? metadata.user_id : undefined) != null) {
  39. return (metadata.user_id = RangeManager._safeObjectId(metadata.user_id))
  40. }
  41. }
  42. for (const change of Array.from(ranges.changes || [])) {
  43. change.id = RangeManager._safeObjectId(change.id)
  44. updateMetadata(change.metadata)
  45. }
  46. for (const comment of Array.from(ranges.comments || [])) {
  47. // Two bugs resulted in mismatched ids, prefer the thread id from the op: https://github.com/overleaf/internal/issues/23272
  48. comment.id = RangeManager._safeObjectId(comment.op?.t || comment.id)
  49. if (comment.op) comment.op.t = comment.id
  50. // resolved property is added to comments when they are obtained from history, but this state doesn't belong in mongo docs collection
  51. // more info: https://github.com/overleaf/internal/issues/24371#issuecomment-2913095174
  52. delete comment.op?.resolved
  53. updateMetadata(comment.metadata)
  54. }
  55. return ranges
  56. },
  57. fixCommentIds(doc) {
  58. for (const comment of doc?.ranges?.comments || []) {
  59. // Two bugs resulted in mismatched ids, prefer the thread id from the op: https://github.com/overleaf/internal/issues/23272
  60. if (comment.op?.t) comment.id = comment.op.t
  61. }
  62. },
  63. _safeObjectId(data) {
  64. try {
  65. return new ObjectId(data)
  66. } catch (error) {
  67. return data
  68. }
  69. },
  70. }