RangeManager.js 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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. comment.id = RangeManager._safeObjectId(comment.id)
  47. if ((comment.op != null ? comment.op.t : undefined) != null) {
  48. comment.op.t = RangeManager._safeObjectId(comment.op.t)
  49. }
  50. updateMetadata(comment.metadata)
  51. }
  52. return ranges
  53. },
  54. _safeObjectId(data) {
  55. try {
  56. return ObjectId(data)
  57. } catch (error) {
  58. return data
  59. }
  60. },
  61. }